Xbee S2C and arduino (mesh network) for transmitting sensor data

hello everyone,

I am now doing a project with Xbee S2C and arduino Uno.
Main Objective of the project is to transmit sound sensor data to coordinator node from 4 sound sensors. Sensor will be distributed around the room and they will send data to coordinator for monitoring.
Coordinator need to receive all data from router. The router has to inform their number like <high,router1> , <low,router2>.

My XBEE configuration

For Coordinator

Firmware - ZIGBEE TH Reg 4060
Pan ID - 1997
DH - 13A200
DL - FFFF
API - enabled

For Router

Firmware - ZIGBEE TH Reg 4060
Pan ID - 1997
DH - 13A200
DL - FFFF
API - enabled

XBee library file that i am using (GitHub - andrewrapp/xbee-arduino: Arduino library for communicating with XBee radios in API mode)

My Transmitter Code

include <SoftwareSerial.h>
#include <Printers.h>
#include <XBee.h>
#include<time.h>
#include<stdlib.h>

#define SOP '<' // start of packet 
#define EOP '>' // end of packet 

int sensorValue = analogRead(A0);
int soundthreshold = 500;
int data[1];


XBee xbee = XBee();
XBeeAddress64 addr64 = XBeeAddress64(0x0013A200,0xFFFFFFFF);
uint8_t input = 0;
SoftwareSerial XBee(2,3); //Rx,Tx


void setup() {
  Serial.begin(9600);
  pinMode(sensorValue,OUTPUT); 
  XBee.begin(9600); 
  Serial.println("starting");
   srand((unsigned)time(NULL));
}

void loop() {

  uint8_t i = 0;
  uint8_t data[1];
  uint8_t receivedChar = '0';
  boolean newData = false;
 int sensorValue = analogRead(sensorValue);

  if(sensorValue > 500)
  {
    Serial.println("high1"); //change in high1 2 3 
  }
  else if(sensorValue < 500)
  {
    Serial.println("low1");
  }
 
  data[0]= sensorValue;
  ZBTxRequest zbTx1 = ZBTxRequest(addr64, data, sizeof(data));
  delay(500);

  Serial.println("starting Tx");
  Serial.print("Data sent: \t");
  Serial.println(data[0]);
  delay(500);
  xbee.send(zbTx1);
  Serial.println("broadcasting");
  delay(500);
}

My Coordianator Code

#include <Printers.h>
#include <XBee.h>
#include <SoftwareSerial.h>


int data[5];
XBee xbee = XBee();
XBeeResponse response = XBeeResponse();
ZBRxResponse rx = ZBRxResponse();
SoftwareSerial XBee(2,3); //Rx,Tx

uint8_t count = 0;
String sample = "";
void setup() 
{
  Serial.begin(9600);
  XBee.begin(9600);
}
void loop()
{
  uint8_t receivedData[4];
  byte* dataPointer;
  xbee.readPacket();
  
  Serial.println("Reading for Packet");
  delay(500);

  if(xbee.getResponse().isAvailable())
  {
    Serial.print("Packet Available: ");
    Serial.println(count);
    delay(500);
    if(xbee.getResponse().getApiId() == ZB_RX_RESPONSE)
    {
      xbee.getResponse().getZBRxResponse(rx);
      delay(300);
      dataPointer = reinterpret_cast<byte *>(receivedData);
      for(int n =0; n < rx.getDataLength(); n++)
      {
        *dataPointer = rx.getData(n);
        dataPointer += 1;
      }
      Serial.print("Packet Received: ");
      for (byte n = 0; n < 4; n++) 
      {
        Serial.println(receivedData[n]);
      }
      Serial.print("count: ");
      count++;
      Serial.println(count);
      delay(1500);
    }
  }
  else if(xbee.getResponse().isError())
  {
    Serial.print("Error Reading Packet. Error Code: ");
    Serial.println(xbee.getResponse().getErrorCode());
    delay(500);
  }
  else
  {
    Serial.println("Packet Not Found");
    delay(500);
  }
}

I am sorry for that there are so many unnecessary stuffs in code because I take this code from other people. I think both the coordinator and router is not working and show not output. I really have no idea what to do , I am new to this XBee stuff and noob at programming.

Coordinator Wiring

Router wiring

Note : I am now trying to read Building Wireless Sensor Networks Using Arduino and trying to use code from that book but it is not working.

I really hope that somebody could help me. If there is more information need , I can provide.

1 Like

Do you KNOW what a value of FFFF for DL means?

PaulS:
Do you KNOW what a value of FFFF for DL means?

It means broadcasting.

The reason why I put DL - FFFF means that I also want to send sensor data to another router. I means that is another part for project.

Firstly, I want to receive sensor data in coordinator.
Sorry,if i make u confused.

Can somebody help me ??

I tried to use XBees in AT mode and data received in XCTU software but nothing on the serial monitor when I connect with arduino. Configuration are same as above but in AT mode.

Coordinator code

//#include <XBee.h>
//#include<SoftwareSerial.h>
#include<time.h>   
#include<stdlib.h>
//SoftwareSerial Xbee(2,3); // RX, TX
int received;

//XBee xbee = XBee();
//XBeeResponse response = XBeeResponse();
//ZBRxResponse rx = ZBRxResponse();

void setup(){
  Serial.begin(9600);
 // Xbee.begin(9600);
}
 
void loop(){
  if(Serial.available()){
    received = Serial.read();
    Serial.println(received);
  }
}

Coordinator code

So, now you are trying to use one serial port to talk to the XBee and the PC. That makes for really confusing communications.

Have you tried the obvious, and NOT put both XBees in broadcast mode? Get the two of them talking, and forget about one XBee/end device trying to talk to multiple coordinators, at least for now.

Coordiantor Config

PAN - 12345
JV - disable
DH - 13A200
DL - address of router
AT mode

Router config

PAN - 12345
JV - enable
DH - 13A200
DL - address of coordinator
AT mode

data receive in XCTU, but nothing receive in arduino serial monitor

Coordinator

API mode - enable

Router

API mode - enable

no packet and nothing receive in coordinator XBee and on serial monitor
Also in XCTU

Note - I used the same code as in first post .

I really confused for coding too.

I also confused about both transmitting and receiving . Does router transmit properly and coordinator receive properly?

updated Output of the coordinator

data receive in serial monitor of coordinator. I configured all the nodes for AT mode. Their destination low address to coordinator and coordinator in broadcasting mode.

Coordinator code

#include<SoftwareSerial.h>
#include<time.h>   
SoftwareSerial XBee(2,3); // RX, TX
int received;


void setup(){
  Serial.begin(115200);
  XBee.begin(9600);
}
 
void loop(){
  if(Serial.available()){
   received = Serial.read();
  return;
  }
}

If I configure in API mode, nothing receive.

My Question is that should I use all the XBee in AT mode and try to make indoor mesh network.

  • 4 sensors need to send data to another router for rotating servo . How can I configure for that one ? Should I change again to broadcasting mode to send data to another router?

-API mode is more appropriate to do all the above stuff ??

Hello PaulS,

Can I ask one question?

Is it ok to move a servo to specific degree in router?
When serial input from router like high1 is received , servo will move to 45 like that.

That will be the connection from sensor router to servo router.

Is it ok to move a servo to specific degree in router?

It's OK by me.

When serial input from router like high1 is received , servo will move to 45 like that.

I would send something like "<S1, 45>" to tell the Arduino that the XBee is talking to to move servo 1 to position 45. That makes more sense to me than having "high1" move the servo to position 45.