I've been trying to figure this put for myself.
Coordinator has MY:1234
End Device has MY: 1235-1236-1237.
What I'm trying to do is make Coordinator send a message to each of the End Devices and get a response before going to the next one.
It seems that every End Device gets the message even with the address for sending set to one of the End Devices.
The code posted works but it seems to halt for more than the delay time at times and some End Device packets gets delivered more than others.
The data will be updated every second and the Coordinator needs to collect from up to 6 End Devices if possible.
I would thinks there is a easier way of doing this, I'm just having a hard time finding it.
My Coordinator code:
#include <XBee.h>
#include <SoftwareSerial.h>
/*
This example is for Series 1 XBee
Sends a TX16 or TX64 request with the value of analogRead(pin5) and checks the status response for success
Note: In my testing it took about 15 seconds for the XBee to start reporting success, so I've added a startup delay
*/
SoftwareSerial softserial(2, 3); // RX, TX
XBee xbee = XBee();
//rx
XBeeResponse response = XBeeResponse();
// create reusable response objects for responses we expect to handle
Rx16Response rx16 = Rx16Response();
//
int value1;
int value2;
int value3;
int count1;
int count2;
int count3;
unsigned long start = millis();
// allocate two bytes for to hold a 10-bit analog reading
uint8_t payload1[] = { 1, 0 };
uint8_t payload2[] = { 2, 0 };
uint8_t payload3[] = { 3, 0 };
uint8_t node11[] = { 0, 0, 0 };
uint8_t node22[] = { 0, 0, 0 };
uint8_t node33[] = { 0, 0, 0 };
// with Series 1 you can use either 16-bit or 64-bit addressing
// 16-bit addressing: Enter address of remote XBee, typically the coordinator
Tx16Request node1 = Tx16Request(0x1235, payload1, sizeof(payload1));
Tx16Request node2 = Tx16Request(0x1236, payload2, sizeof(payload1));
Tx16Request node3 = Tx16Request(0x1237, payload3, sizeof(payload3));
void setup() {
Serial.begin(9600);
softserial.begin(9600);
xbee.setSerial(softserial);
xbee.begin(softserial);
}
void loop() {
data_in_1 ();
softserial.flush();
data_in_2 ();
softserial.flush();
data_in_3 ();
softserial.flush();
}
void data_in_1 (){
xbee.send(node1);
delay (5);
xbee.readPacket();
if (xbee.getResponse().isAvailable()) {
// got something
if (xbee.getResponse().getApiId() == RX_16_RESPONSE) {
xbee.getResponse().getRx16Response(rx16);
for (int i = 0; i < rx16.getDataLength(); i++)
node11[i] = rx16.getData(i);
}
}
if (node11[0] == 1){
uint8_t analogHigh1 = node11[1];
uint8_t analogLow1 = node11[2];
value1 = analogLow1 + (analogHigh1 * 256);
count1 = count1 + 1;
Serial.print(value1);
Serial.print(" ");
Serial.println(count1);
}
delay (50);
}
void data_in_2 (){
xbee.send(node1);
delay (5);
xbee.readPacket();
if (xbee.getResponse().isAvailable()) {
// got something
if (xbee.getResponse().getApiId() == RX_16_RESPONSE) {
xbee.getResponse().getRx16Response(rx16);
for (int i = 0; i < rx16.getDataLength(); i++)
node22[i] = rx16.getData(i);
}
}
if (node22[0] == 2){
uint8_t analogHigh2 = node22[1];
uint8_t analogLow2 = node22[2];
value2 = analogLow2 + (analogHigh2 * 256);
count2 = count2 + 1;
Serial.print(value2);
Serial.print(" ");
Serial.println(count2);
}
delay (50);
}
void data_in_3 (){
xbee.send(node1);
delay (5);
xbee.readPacket();
if (xbee.getResponse().isAvailable()) {
// got something
if (xbee.getResponse().getApiId() == RX_16_RESPONSE) {
xbee.getResponse().getRx16Response(rx16);
for (int i = 0; i < rx16.getDataLength(); i++)
node33[i] = rx16.getData(i);
}
}
if (node33[0] == 3){
uint8_t analogHigh3 = node33[1];
uint8_t analogLow3 = node33[2];
value3 = analogLow3 + (analogHigh3 * 256);
count3 = count3 + 1;
Serial.print(value3);
Serial.print(" ");
Serial.println(count3);
Serial.println();
}
delay (50);
}
One of the three End Device codes.
#include <XBee.h>
#include <SoftwareSerial.h>
/*
This example is for Series 1 XBee (802.15.4)
Receives either a RX16 or RX64 packet and sets a PWM value based on packet data.
Error led is flashed if an unexpected packet is received
*/
SoftwareSerial softserial(2, 3); // RX, TX
XBee xbee = XBee();
XBeeResponse response = XBeeResponse();
// create reusable response objects for responses we expect to handle
Rx16Response rx16 = Rx16Response();
uint8_t payload[] = { 0, 0 };
uint8_t txx[] = { 1, 0, 0 };
//tx
Tx16Request tx = Tx16Request(0x1234, txx, sizeof(txx));
//
void setup() {
// start serial
Serial.begin(9600);
xbee.setSerial(Serial);
xbee.begin(Serial);
}
// continuously reads packets, looking for RX16 or RX64
void loop() {
xbee.readPacket();
if (xbee.getResponse().isAvailable()) {
// got something
if (xbee.getResponse().getApiId() == RX_16_RESPONSE) {
// got a rx packet
if (xbee.getResponse().getApiId() == RX_16_RESPONSE) {
xbee.getResponse().getRx16Response(rx16);
for (int i = 0; i < rx16.getDataLength(); i++)
payload [i] = rx16.getData(i);
}
}
}
if(payload[0] == 1){
Serial.println(payload[0]);
int pin5 = 5;
txx[1] = pin5 >> 8 & 0xff;
txx[2] = pin5 & 0xff;
xbee.send(tx);
}else {
Serial.println("not correct id");
}
}