Sending Sensor Data Via Xbee Series 2

good day folks,
I'm using the following sketch from the arduino xbee series2_Tx example as well as the DHT 22 examlpe to read humidity and temperature, then transmits it using a ZigBee RF module. Could anyone offer any advice on how to modify the current xbee series2_Rx example to receive the packets from the xbee series2_Tx sketch I have below and display the humidity and temperature values on the serial window?

#include "XBee.h"
#include "DHT.h"
 
#define DHTPIN 2     // data pin of the DHT sensor
 
// Uncomment whatever type you're using!
//#define DHTTYPE DHT11   // DHT 11 
#define DHTTYPE DHT22   // DHT 22  (AM2302)
//#define DHTTYPE DHT21   // DHT 21 (AM2301)
 
// Connect pin 1 (on the left) of the sensor to +5V
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor
 
DHT dht(DHTPIN, DHTTYPE);
 
/*
This example is for Series 2 XBee
 Sends a ZB TX request with the value of analogRead(pin5) and checks the status response for success
*/
 
// create the XBee object
XBee xbee = XBee();
 
// we are going to send two floats of 4 bytes each
uint8_t payload[8] = { 0, 0, 0, 0, 0, 0, 0, 0};
 
// union to convery float to byte string
union u_tag {
    uint8_t b[4];
    float fval;
} u;
 
 
// SH + SL Address of receiving XBee
XBeeAddress64 addr64 = XBeeAddress64(0x0013a200, 0x406F4973);
ZBTxRequest zbTx = ZBTxRequest(addr64, payload, sizeof(payload));
ZBTxStatusResponse txStatus = ZBTxStatusResponse();
 
int statusLed = 13;
int errorLed = 13;
 
void flashLed(int pin, int times, int wait) {
 
  for (int i = 0; i < times; i++) {
    digitalWrite(pin, HIGH);
    delay(wait);
    digitalWrite(pin, LOW);
 
    if (i + 1 < times) {
      delay(wait);
    }
  }
}
 
void setup() {
  pinMode(statusLed, OUTPUT);
  pinMode(errorLed, OUTPUT);
  dht.begin();
  xbee.begin(9600);
}
 
void loop() {
  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  float t = dht.readTemperature();
 
  // check if returns are valid, if they are NaN (not a number) then something went wrong!
  if (!isnan(t) && !isnan(h)) {
     
    // convert humidity into a byte array and copy it into the payload array
    u.fval = h;
    for (int i=0;i<4;i++){
      payload[i]=u.b[i];
    }
     
    // same for the temperature
    u.fval = t;
    for (int i=0;i<4;i++){
      payload[i+4]=u.b[i];
    }
     
    xbee.send(zbTx);
 
    // flash TX indicator
    flashLed(statusLed, 1, 100);
 
    // after sending a tx request, we expect a status response
    // wait up to half second for the status response
    if (xbee.readPacket(500)) {
      // got a response!
 
      // should be a znet tx status             
      if (xbee.getResponse().getApiId() == ZB_TX_STATUS_RESPONSE) {
        xbee.getResponse().getZBTxStatusResponse(txStatus);
 
        // get the delivery status, the fifth byte
        if (txStatus.getDeliveryStatus() == SUCCESS) {
          // success.  time to celebrate
          flashLed(statusLed, 5, 50);
        } else {
          // the remote XBee did not receive our packet. is it powered on?
          flashLed(errorLed, 3, 500);
        }
      }
    } else if (xbee.getResponse().isError()) {
      //nss.print("Error reading packet.  Error code: ");  
      //nss.println(xbee.getResponse().getErrorCode());
    } else {
      // local XBee did not provide a timely TX Status Response -- should not happen
      flashLed(errorLed, 2, 50);
    }
  }
  delay(1000);
}

Thank you in advance!!
Dave

That looks pretty straight-forward. What does your receiver code look like? What isn't working?

Hi Paul,
If it was so straight forward, I wouldn't have asked for any advice. That's the reason Im asking for help as I'm not sure if, or how the Rx code could be written. It may seem straight forward to you as you have more experience than I have, hence the idea behind this forum.....to teach and to learn. Please if you have no intention of being a mentor, I would ask you not to reply.

Thank You
Dave

What I meant was that the code for sending two values seems well structured. You got that code from somewhere. Either it was able to send two values when you got it, or you modified it to send two values instead of one.

If you were able to find code to send one (or more) values, surely you were able to find matching code to receive one (or more) values.

If the code you found is structured to receive one value, we can help you modify it to receive two values, and, in the process, explain how to modify the sender and receiver to send and receive any number of values (withing the payload size limits.

Could anyone offer any advice on how to modify the current xbee series2_Rx example to receive the packets from the xbee series2_Tx sketch I have below

Perhaps my comments that hinted that you should post this code were too subtle. So, forget hints. Post this code.

Hey I am a beginner to Xbee and Arduino coding .. can anyone please help me with the code at reciever end.
I found this code online for transmitter side.

#include "XBee.h"
#include "DHT.h"
 
#define DHTPIN 2     // data pin of the DHT sensor
 
// Uncomment whatever type you're using!
//#define DHTTYPE DHT11   // DHT 11 
#define DHTTYPE DHT22   // DHT 22  (AM2302)
//#define DHTTYPE DHT21   // DHT 21 (AM2301)
 
// Connect pin 1 (on the left) of the sensor to +5V
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor
 
DHT dht(DHTPIN, DHTTYPE);
 
/*
This example is for Series 2 XBee
 Sends a ZB TX request with the value of analogRead(pin5) and checks the status response for success
*/
 
// create the XBee object
XBee xbee = XBee();
 
// we are going to send two floats of 4 bytes each
uint8_t payload[8] = { 0, 0, 0, 0, 0, 0, 0, 0};
 
// union to convery float to byte string
union u_tag {
    uint8_t b[4];
    float fval;
} u;
 
 
// SH + SL Address of receiving XBee
XBeeAddress64 addr64 = XBeeAddress64(0x0013a200, 0x406F4973);
ZBTxRequest zbTx = ZBTxRequest(addr64, payload, sizeof(payload));
ZBTxStatusResponse txStatus = ZBTxStatusResponse();
 
int statusLed = 13;
int errorLed = 13;
 
void flashLed(int pin, int times, int wait) {
 
  for (int i = 0; i < times; i++) {
    digitalWrite(pin, HIGH);
    delay(wait);
    digitalWrite(pin, LOW);
 
    if (i + 1 < times) {
      delay(wait);
    }
  }
}
 
void setup() {
  pinMode(statusLed, OUTPUT);
  pinMode(errorLed, OUTPUT);
  dht.begin();
  xbee.begin(9600);
}
 
void loop() {
  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  float t = dht.readTemperature();
 
  // check if returns are valid, if they are NaN (not a number) then something went wrong!
  if (!isnan(t) && !isnan(h)) {
     
    // convert humidity into a byte array and copy it into the payload array
    u.fval = h;
    for (int i=0;i<4;i++){
      payload[i]=u.b[i];
    }
     
    // same for the temperature
    u.fval = t;
    for (int i=0;i<4;i++){
      payload[i+4]=u.b[i];
    }
     
    xbee.send(zbTx);
 
    // flash TX indicator
    flashLed(statusLed, 1, 100);
 
    // after sending a tx request, we expect a status response
    // wait up to half second for the status response
    if (xbee.readPacket(500)) {
      // got a response!
 
      // should be a znet tx status             
      if (xbee.getResponse().getApiId() == ZB_TX_STATUS_RESPONSE) {
        xbee.getResponse().getZBTxStatusResponse(txStatus);
 
        // get the delivery status, the fifth byte
        if (txStatus.getDeliveryStatus() == SUCCESS) {
          // success.  time to celebrate
          flashLed(statusLed, 5, 50);
        } else {
          // the remote XBee did not receive our packet. is it powered on?
          flashLed(errorLed, 3, 500);
        }
      }
    } else if (xbee.getResponse().isError()) {
      //nss.print("Error reading packet.  Error code: ");  
      //nss.println(xbee.getResponse().getErrorCode());
    } else {
      // local XBee did not provide a timely TX Status Response -- should not happen
      flashLed(errorLed, 2, 50);
    }
  }
  delay(1000);
}

I realize this posting is two years old, just in case someone has recently Googled this site looking for an answer to the above transmit sketch, here is a receiving sketch that will work.

#include <XBee.h>
#include <string.h>

uint8_t payload[8] = {0,0,0,0,0,0,0,0};//initialises the data array with 0

char temp;
char hum;
XBee xbee = XBee();

ZBRxResponse zbRx = ZBRxResponse();

char Tb[4];
char Hb[4];

typedef struct {
float temp = 0.0f;//temperature
float hum = 0.0f; //RHumidity
// float measuredvbat = 0.0f;// battery

} load;
load theData;

void setup()
{
// delay(1000);
Serial.begin(9600);
xbee.begin(Serial);
Serial.println("Ready");
}

// continuously reads packets, looking for ZB Receive or Modem Status
void loop()
{
xbee.readPacket();
if (xbee.getResponse().isAvailable()){
if (xbee.getResponse().getApiId() == ZB_RX_RESPONSE){
xbee.getResponse().getZBRxResponse(zbRx);

for(int i = 0; i < zbRx.getDataLength(); i++){
payload = zbRx.getData(i);
}

  • // temp = zbRx.getData(0);// get the T for temperature to ID the value*

  • Tb[0]= zbRx.getData(0);*

  • Tb[1]= zbRx.getData(1);*

  • Tb[2]= zbRx.getData(2);*

  • Tb[3]= zbRx.getData(3);*

  • memcpy(&theData.temp, &Tb, 4);*

  • Hb[0]= zbRx.getData(4);*

  • Hb[1]= zbRx.getData(5);*

  • Hb[2]= zbRx.getData(6);*

  • Hb[3]= zbRx.getData(7);*

  • memcpy(&theData.hum, &Hb, 4);*

  • }*

  • }*
    Serial.print(" Humidity: ");

  • Serial.print(theData.hum);*

  • Serial.print(" ");*

  • Serial.print("Temperature: ");*

  • Serial.print(theData.temp);*
    Serial.println('\n' );

  • delay(500);*
    }// END OF LOOP

here is a receiving sketch that will work.

Not the way you posted it, it won't. There is reason that we expect you to use code tags.

I have not a glue what a code tag is, but as long as you line up the bytes in the payload on the transmitting end with the incoming bytes in the payload on the receiving end it works. It works for me.

I have not a glue what a code tag is

So, you couldn't be bothered to read the stickies at the top of the forum - the ones that you were supposed to read BEFORE posting. I see how valuable a contributor you are going to be.

Does your code REALLY look like that?

sorry master.....please give me transmit sketch n receive sketch
i'm very very need it for my last exam...
thanks all

please give me transmit sketch n receive sketch

BEFORE you sit down to write code, you need to know what the code is supposed to do. You've tacked a request onto a thread that was resurrected by an idiot, with not a clue as to what you want the sending program to send, or what the receiving program is supposed to do with the data.

i'm very very need it for my last exam...

Best get busy, then.

sorry for my english,,i'm from indonesia
i have 2 xbee s2 with shield,2 arduino uno and sensor dht11,,
1 xbee as cordinator and 1 again as end device..i want to send data from cordinator to end device but the data always failed sent,,
here the sketch transmite:

#include "XBee.h"
#include "DHT.h"

#define DHTPIN A5 // data pin of the DHT sensor

// Uncomment whatever type you're using!
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22 // DHT 22 (AM2302)
//#define DHTTYPE DHT21 // DHT 21 (AM2301)

// Connect pin 1 (on the left) of the sensor to +5V
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor

DHT dht(DHTPIN, DHTTYPE);

/*
This example is for Series 2 XBee
Sends a ZB TX request with the value of analogRead(pin5) and checks the status response for success
*/

// create the XBee object
XBee xbee = XBee();

// we are going to send two floats of 4 bytes each
uint8_t payload[8] = { 0, 0, 0, 0, 0, 0, 0, 0};

// union to convery float to byte string
union u_tag {
uint8_t b[4];
float fval;
} u;

// SH + SL Address of receiving XBee
XBeeAddress64 addr64 = XBeeAddress64(0x0013A200, 0x40B7A017);
ZBTxRequest zbTx = ZBTxRequest(addr64, payload, sizeof(payload));
ZBTxStatusResponse txStatus = ZBTxStatusResponse();

int statusLed = 13;
int errorLed = 13;

void flashLed(int pin, int times, int wait) {

for (int i = 0; i < times; i++) {
digitalWrite(pin, HIGH);
delay(wait);
digitalWrite(pin, LOW);

if (i + 1 < times) {
delay(wait);
}
}
}

void setup() {
pinMode(statusLed, OUTPUT);
pinMode(errorLed, OUTPUT);
dht.begin();
Serial.begin(9600);
xbee.setSerial(Serial);
}

void loop() {
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
float t = dht.readTemperature();

// check if returns are valid, if they are NaN (not a number) then something went wrong!
if (!isnan(t) && !isnan(h)) {

// convert humidity into a byte array and copy it into the payload array
u.fval = h;
for (int i=0;i<4;i++){
payload_=u.b*;_
_
}*_

* // same for the temperature*
* u.fval = t;*
* for (int i=0;i<4;i++){*
_ payload[i+4]=u.b*;
}*_

* xbee.send(zbTx);*
* Serial.println("Data Dikirim");*

* // flash TX indicator*
* flashLed(statusLed, 1, 100);*

* // after sending a tx request, we expect a status response*
* // wait up to half second for the status response*
* if (xbee.readPacket(500)) {*
* // got a response!*

* // should be a znet tx status *
* if (xbee.getResponse().getApiId() == ZB_TX_STATUS_RESPONSE) {
_ xbee.getResponse().getZBTxStatusResponse(txStatus);*_

* // get the delivery status, the fifth byte*
* if (txStatus.getDeliveryStatus() == SUCCESS) {*
* // success. time to celebrate*
* Serial.println("Berhasil");*
* flashLed(statusLed, 5, 50);*
* } else {*
* // the remote XBee did not receive our packet. is it powered on?*
* flashLed(errorLed, 3, 500);*
* Serial.println ("Gagal");*
* }*
* }*
* }else {*
* Serial.println ("Data Tidak Diterima");*
* }*
* } else if (xbee.getResponse().isError()) {*
* //nss.print("Error reading packet. Error code: "); *
* //nss.println(xbee.getResponse().getErrorCode());*
* } else {*
* // local XBee did not provide a timely TX Status Response -- should not happen*
* flashLed(errorLed, 2, 50);*
* }*
* delay(2000);*
* }*
>> the sketch receive:
#include <XBee.h>
#include <string.h>
uint8_t payload[8] = {0,0,0,0,0,0,0,0};//initialises the data array with 0
char temp;
char hum;
XBee xbee = XBee();
ZBRxResponse zbRx = ZBRxResponse();
char Tb[4];
char Hb[4];
typedef struct {
float temp = 0.0f;//temperature
float hum = 0.0f; //RHumidity
// float measuredvbat = 0.0f;// battery

} load;
load theData;
void setup()
{
// delay(1000);
Serial.begin(9600);
xbee.begin(Serial);
Serial.println("Ready");
}
// continuously reads packets, looking for ZB Receive or Modem Status
void loop()
{
xbee.readPacket();
if (xbee.getResponse().isAvailable()){
* if (xbee.getResponse().getApiId() == ZB_RX_RESPONSE){
_ xbee.getResponse().getZBRxResponse(zbRx);*_

for(int i = 0; i < zbRx.getDataLength(); i++){
_ payload = zbRx.getData(i);
}
* // temp = zbRx.getData(0);// get the T for temperature to ID the value*_

* Tb[0]= zbRx.getData(0);*
* Tb[1]= zbRx.getData(1);*
* Tb[2]= zbRx.getData(2);*
* Tb[3]= zbRx.getData(3);*
* memcpy(&theData.temp, &Tb, 4);*

* Hb[0]= zbRx.getData(4);*
* Hb[1]= zbRx.getData(5);*
* Hb[2]= zbRx.getData(6);*
* Hb[3]= zbRx.getData(7);*

* memcpy(&theData.hum, &Hb, 4);*
* }*
}
Serial.print(" Humidity: ");
Serial.print(theData.hum);
Serial.print(" ");
Serial.print("Temperature: ");
Serial.print(theData.temp);
Serial.println('\n' );
delay(1000);
}// END OF LOOP
plese give me solution,,,sorry i'm newbie

plese give me solution

Put the XBees in AT mode, not API mode.

Configure the coordinator to talk to the end device only (DH, DL match SH, SL of the end device).

Configure the end device to listen to the coordinator only (DH, DL match SH, SL of the coordinator).

Dump the XBee library (for now).

Send the data as text, not packets.

coordinator AT and end device / router AT??

so, what must change in configuration ?

sams99:
coordinator AT and end device / router AT??

Yes.

so, what must change in configuration ?

The mode.

the sketch not work,,,give me solution please,,,

Using Pro S2C radios, coordinator (receiver) and router (transmitter), both radios are set to API-2 with escaping, JV channel set to enable on the router

used two XBee, Pro S2C radios, Zigbee TH Pro firmware, both set to API-2 w/escaping, configured - router (transmitting), coordinator ( receiving), JV channel enabled on the router and they work

i'm only have 2 xbee s2,,,i have xbee pro s2b but the only one

the sketch not work

The one that you didn't post? Too bad.

If you did not change the code you last posted (incorrectly), go read reply #12 again.