Xbee S2C and Arduino

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.

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.

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

2 Likes

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).

  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/

1 Like