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/