Howdy!
I am very new to Arduino and XBee, so please bear with me if I get a few things wrong! I have done quite a bit of research to try to implement what I am about to describe, but have hit a road bump that I can’t seem to overcome.
I have two Arduinos (one Nano and one Uno) each with their own XBee. I am attempting to send data (just a 1 value for now) from the Uno to the Nano, and display that data on an LCD (based on this tutorial).
Currently, the tx (dout) led of the Uno’s XBee is blinking as expected, but the rx (din) led of the Nano’s XBee never blinks, and it doesn’t seem to be picking up any data at all. Any help with this would be greatly appreciated! (I have tried switching rx and tx connections between XBee’s and Arduino’s, too.) All specifications below.
Both XBee’s are connected 5V → 5V, GND → GND, DIN → RX, DOUT → TX for both Arduino’s.
I have configured the XBee’s in the following way:
Nano’s XBee: CH - C, PAN ID - 1001, API Enabled, DL - 88, MY - 77, Coordinator
Uno’s XBee: CH - C, PAN ID - 1001, API Enabled, DL - 77, MY - 88, End Device
The code for each Arduino is as follows:
Nano:
/* ~ Simple Arduino - xBee Receiver sketch ~
Read an PWM value from Arduino Transmitter to fade an LED
The receiving message starts with '<' and closes with '>' symbol.
Dev: Michalis Vasilakis // Date:2/3/2016 // Info: www.ardumotive.com // Licence: CC BY-NC-SA */
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
//Constants
LiquidCrystal_I2C lcd(0x24, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
//Variables
bool started= false;//True: Message is strated
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 index; //Index of array
void setup() {
//Start the serial communication
Serial.begin(9600); //Baud rate must be the same as is on xBee module
lcd.begin(16, 2);
lcd.backlight();
lcd.clear();
}
void loop() {
lcd.clear();
lcd.print("Trying to receive");
while (Serial.available()>0){
//Read the incoming byte
incomingByte = Serial.read();
//Start the message when the '<' symbol is received
if(incomingByte == '<')
{
started = true;
index = 0;
msg[index] = '\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(index < 4) // Make sure there is room
{
msg[index] = incomingByte; // Add char to array
index++;
msg[index] = '\0'; // Add NULL to end
}
}
}
delay(1000);
lcd.clear();
lcd.print(started);
lcd.print(" ");
lcd.print(ended);
delay(2000);
if(started && ended)
{
int value = atoi(msg);
lcd.print(value);
//Serial.println(value); //Only for debugging
delay(2000);
index = 0;
msg[index] = '\0';
started = false;
ended = false;
}
}
Uno:
/* ~ Simple Arduino - xBee Transmitter sketch ~
Read an analog value from potentiometer, then convert it to PWM and finally send it through serial port to xBee.
The xBee serial module will send it to another xBee (resiver) and an Arduino will turn on (fade) an LED.
The sending message starts with '<' and closes with '>' symbol.
Dev: Michalis Vasilakis // Date:2/3/2016 // Info: www.ardumotive.com // Licence: CC BY-NC-SA */
int value = 1;
void setup() {
//Start the serial communication
Serial.begin(9600); //Baud rate must be the same as is on xBee module
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
//Send the message:
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
Serial.print('<'); //Starting symbol
Serial.print(value);//Value from 0 to 255
Serial.println('>');//Ending symbol
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
}