Home/Support/Support Forum/Xbee S2C and Arduino

Xbee S2C and Arduino

0 votes
can anyone provide me with a simple arduino code for an Xbee S2C Router that can send just a simple word or anything ? because i have tried every code out there for the Xbee S2 and nothing works and i opened threads everywhere about the S2C but no one knows how to work with them the only thing out there is how to program them in the XCTu software and i got that covered, so if anyone can provide me with a sample code for the router or both router and coordinator i would be very much appreciated.
asked May 17, 2017 in RF Solutions and XBee by Pgchaer New to the Community (5 points)

Please log in or register to answer this question.

3 Answers

+2 votes
I already tried both at and api mode , i changed them in the xctu software but i got nothing with the codes out there , can you provide me with a sample code just for test that you are 100% positive it works on the xbee s2c ? And which firmware it should be set to if that matters
answered May 17, 2017 by Pgchaer New to the Community (5 points)
I don't have any specific code for that but there are little y hundreds of sites with code that work on the S2B Zigbee hardware and firmware.  Just make sure you have the Zigbee Code installed, one of the two is set to Coordinator (CE1) and both are associated (AI). Then you will find that most will work with API mode 2.
First thank you for your reply its very much appreciated , second what do you mean by make sure you have the zigbee code installed ? If you mean libraries i have the xbee.h labrary installed on the arduino software ,third the S2B are different from the S2C that's why im asking for codes for the S2C since ive tried the tones of codes all over the internet but they dont apply to the xbee S2C
No that is not what I mean. The XBee module has different firmware files for it. Zigbee, 802.15.4 and Digi mesh. Most of the sample codes you find for the Arduino require the use of API mode and Zigbee.

For the S2C, that mean you need to set the CE to 1 one one of the modules to enable the Coordinator and two, you need to set the AP command to either 1 or 2 (Escaped).
0 votes
1. Configure your Xbee S2C modules

Coordinator:(A coordinator starts and maintains the network)
ID PAN ID: 1234
JV : Disabled
CE: Enabled[1]
SM: No Sleep(Router)[0]
AP: Transparent mode[0] (IMPORTANT if you are using Arduino)
Router:
ID PAN ID: 1234
JV: Enabled
CE: Enabled
SM: No Sleep(Router)[0]
AP: Transparent mode[0] (IMPORTANT if you are using Arduino)

Switch consoles working mode and Close the Serial communication with serial modules

Receiver code:

//Constants
const int ledPin = 14; //Led to NodeMCU pin D5 (PWM)

//Variables
bool started= false;//True: Message is started
bool ended = false;//True: Message is finished
char incomingByte ; //Variable to store the incoming byte
char msg[3]; //Message - array from 0 to 2 (3 values - PWM - e.g. 240)
byte bitPosition; //bitPosition of array

void setup() {

//Start the serial communication
Serial.begin(9600); //Baud rate must be the same as is on xBee module
pinMode(ledPin, OUTPUT);
}

void loop() {

while (Serial.available()>0){
//Read the incoming byte
incomingByte = Serial.read();
//Start the message when the '<' symbol is received
if(incomingByte == '<')
{
started = true;
bitPosition = 0;
msg[bitPosition] = '\0'; // Throw away any incomplete packet
}
//End the message when the '>' symbol is received
else if(incomingByte == '>')
{
ended = true;
break; // Done reading - exit from while loop!
}
//Read the message!
else
{
if(bitPosition < 4) // Make sure there is room
{
msg[bitPosition] = incomingByte; // Add char to array
bitPosition++;
msg[bitPosition] = '\0'; // Add NULL to end
}
}
}

if(started && ended)
{
int value = atoi(msg);
Serial.println(msg);
analogWrite(ledPin, value);
//Serial.println(value); //Only for debugging
bitPosition = 0;
msg[bitPosition] = '\0';
started = false;
ended = false;
}

}

Transmitter code:
//Constants:
const int potPin = A0; //Pot at Arduino A0 pin
//Variables:
int value ; //Value from pot

void setup() {

//Start the serial communication
Serial.begin(9600); //Baud rate must be the same as is on xBee module

}

void loop() {

//Read the analog value from pot and store it to "value" variable
value = analogRead(A0);
//Map the analog value to pwm value
value = map (value, 0, 1023, 0, 255);
//Send the message:
Serial.print('<'); //Starting symbol
Serial.print(value);//Value from 0 to 255
Serial.println('>');//Ending symbol


}

References:
https://www.instructables.com/id/How-to-Use-XBee-Modules-As-Transmitter-Receiver-Ar/
answered Jan 23, 2019 by Mohammad Arshad Ali New to the Community (1 point)
–1 vote
Try enabling API mode on your S2C. Most of the examples require the radio to be in API mode which before the S2C was its own firmware version. Now that the S2C has a larger processor, the days of the separate firmware versions for end nodes, coordinator, routers in either AT or API is gone. So if you want to use API mode, which most of the Sample Code want, you simply enable the ATAP command.
answered May 17, 2017 by mvut Veteran of the Digi Community (15,223 points)
...