Hey. I am trying to establish a network using 3 Xbees. 1 Xbee is programmed as Cordinator API and the other two are Router AT firmware.
One of the router Xbee devices are toggling digital pin high/low every one second
The second router Xbee is taking a temperature reading and sending the data back to the coordinator
I have tested both Xbees seperately and it works:
Digital pin toggling:
//toggles local LED and remote XBee LED on and off
[code=c]
void setup(){
pinMode(led, OUTPUT);
Serial.begin(9600);
}
void loop(){
digitalWrite(led, HIGH);
setRemoteState1(0x5);
//setRemoteState2(0x4);
delay(1000);
digitalWrite(led, LOW);
setRemoteState1(0x4);
//setRemoteState2(0x5);
delay(1000);
}
// first xbee
void setRemoteState1 (char value){
Serial.write(0x7E); //start byte
Serial.write((byte)0x0); //high part of length (always zero)
Serial.write(0x10); //low part of length
Serial.write(0x17); //0x17 is remote AT command
Serial.write((byte)0x0); //frame ID set to zero for no reply
Serial.write(0x00);
Serial.write(0x13);
Serial.write(0xA2);
Serial.write(0x00);
Serial.write(0x40);
Serial.write(0xB8);
Serial.write(0xF4);
Serial.write(0x1F);
//16 bit of recipient of 0xFFFE
Serial.write(0xFF);
Serial.write(0xFE);
Serial.write(0x02);
Serial.write(‘D’);
Serial.write(‘4’);
//command data in as many bytes
Serial.write(value);
//checksum
long sum = 0x17 + 0x13 + 0xA2 + 0x40 + 0xB8 +0xF4+0x1F+0xFF+0xFE + 0x02 + ‘D’ + ‘4’ + value;
Serial.write(0xFF - (sum & 0xFF));
}
Temperature reading:
[code=c]
//SET D2 TO ADC
float temp;
void setup() {
Serial.begin(9600);
// put your setup code here, to run once:
}
void loop() {
if(Serial.available()>=21){
if (Serial.read()==0x7E){
for(int i=1;i<19;i++){
byte discardByte = Serial.read();
}
int analogMSB=Serial.read();
int analogLSB= Serial.read();
int analogReading =analogLSB+(analogMSB*256);
temp=(analogReading/1023.0)*120;
Serial.print(temp);
Serial.println("degrees C");
}
}
}
However, I am struggling to put these two programs together, because I am not sure how to acknowledge which Xbee is doing what.
When I am reading analog sample bytes, it automatically reads from the other Xbee as well which has nothing to do with Analog data therefore i get some garbage data at the output.
combined code:
[code=c]
float temp;
int led = 13;
void setup(){
pinMode(led, OUTPUT);
Serial.begin(9600);
}
void loop(){
if(Serial.available()>=21){
if (Serial.read()==0x7E){
for(int i=1;i<19;i++){
byte discardByte = Serial.read();
}
int analogMSB=Serial.read();
// Serial.print(“MSB:”);
//Serial.println(analogMSB);
int analogLSB= Serial.read();
//Serial.print(“LSB:”);
//Serial.println(analogLSB);
int analogReading =analogLSB+(analogMSB*256);
if (analogReading >0){
temp=(analogReading/1023.0)*120;
Serial.print(temp);
Serial.println(“degrees C”);
}
setRemoteState1(0x5);
delay(1000);
setRemoteState1(0x4);
delay(1000);
}
}
// first xbee
void setRemoteState1 (char value){
Serial.write(0x7E); //start byte
Serial.write((byte)0x0); //high part of length (always zero)
Serial.write(0x10); //low part of length
Serial.write(0x17); //0x17 is remote AT command
Serial.write((byte)0x0); //frame ID set to zero for no reply
Serial.write(0x00);
Serial.write(0x13);
Serial.write(0xA2);
Serial.write(0x00);
Serial.write(0x41);
Serial.write(0x82);
Serial.write(0xD1);
Serial.write(0xB9);
//16 bit of recipient of 0xFFFE
Serial.write(0xFF);
Serial.write(0xFE);
Serial.write(0x02);
Serial.write(‘D’);
Serial.write(‘4’);
//command data in as many bytes
Serial.write(value);
//checksum
long sum = 0x17 + 0x13 + 0xA2 + 0x41 + 0x82 +0xD1+0xB9+0xFF+0xFE + 0x02 + ‘D’ + ‘4’ + value;
Serial.write(0xFF - (sum & 0xFF));
}
It could also be becuase I writing data to one Xbee and reading from the other so it may confuse the serial port, but what I have noticed is that sometimes I even get garbage for like 10 seconds after unplugging the TX and RX wires from my nodemcu and coordinator which..
https://imgur.com/a/moLlCG8