Hi, im from indonesia. sorry if my english is bad
I have a problem to confirm that the packet on the receiver side is acknowledged and sender always get deliverystatus = success, even though packet not receive
Sender Code
#include <XBee.h>
#include <SoftwareSerial.h>
// create the XBee object
XBee xbee = XBee();
SoftwareSerial xb(2, 3); // RX, TX
// we are going to send two floats of 4 bytes each
uint8_t payload[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
union u_tag {
float f1,f2;
int adc,adc1,s,m,h;
uint8_t b[4];
} value;
// SH + SL Address of receiving XBee
XBeeAddress64 addr64 = XBeeAddress64(0x0013A200, 0x41630C48);
ZBTxRequest zbTx = ZBTxRequest(addr64, payload, sizeof(payload));
ZBTxStatusResponse txStatus = ZBTxStatusResponse();
float voltage,volt;
float ntu;
int statusLed = 13;
int errorLed = 13;
void setup() {
Serial.begin(9600); //Baud rate: 9600
pinMode(statusLed, OUTPUT);
//xbee.begin(Serial);
//xbee.begin(xb);
xbee.setSerial(Serial);
}
void loop() {
int sensorpH = analogRead(A0);// read the input on analog pin 0:
int sensorTurbidity = analogRead(A1);// read the input on analog pin 0:
voltage = sensorTurbidity * (5.0 / 1024); // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
volt = sensorpH * (5.0 / 1024);
ntu = 100 - (voltage/4.16)*100.00; //4.16
float Po = 7 + ((2.5 - volt) / 0.18); //0.357
Serial.print("ADC ");
Serial.print(sensorTurbidity);
Serial.print(" ADC pH ");
Serial.print(sensorpH); // print out the value you read:
Serial.print(" Turbidity ");
Serial.print(ntu);
Serial.print("%");
Serial.print(" pH ");
Serial.println(Po,3);
if (!isnan(sensorTurbidity) && !isnan(sensorpH)) {
// convert value sensor float into a byte array and copy it into the payload array
value.adc = sensorpH;
for (int i=0;i<4;i++){
payload[i]=value.b[i];
}
// sama juga dengan diatas
value.adc1 = sensorTurbidity;
for (int i=0;i<4;i++){
payload[i+4]=value.b[i];
}
int s=1;
int m=30;
int h=12;
/*
value.f1 = ntu;
for (int i=0;i<4;i++){
payload[i+8]=value.b[i];
}
value.f2 = Po;
for (int i=0;i<4;i++){
payload[i+12]=value.b[i];
}
*/
payload[8]=h >> 8 & 0xff;
payload[9]=h & 0xff;
payload[10]=m >> 8 & 0xff;
payload[11]=m & 0xff;
payload[12]=s >> 8 & 0xff;
payload[13]=s & 0xff;
xbee.send(zbTx);
flashLed(statusLed, 1, 100);
// after sending a tx request, we expect a status response
// wait up to half second for the status response
xbee.readPacket(5000);
if (xbee.getResponse().isAvailable()) {
// got a response!
// trx packet from xbee to variable
if (xbee.getResponse().getApiId() == ZB_TX_STATUS_RESPONSE) {
xbee.getResponse().getZBTxStatusResponse(txStatus);
if (txStatus.getDeliveryStatus() == SUCCESS) {
// success. time to celebrate
Serial.println("Packet send has been success");
flashLed(statusLed, 1, 500);
}
else {
// the remote XBee did not receive our packet. is it powered on?
flashLed(errorLed, 3, 500);
Serial.println ("Packet not sending");
}
}
else if (xbee.getResponse().isError()) {
Serial.println("Error reading packet. Error code: ");
Serial.println(xbee.getResponse().getErrorCode());
}
}
}
delay(2000);
}
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);
}
}
}
Receiver Code
#include <Printers.h>
#include <XBee.h>
#include <string.h>
#include <SoftwareSerial.h>
//initialises the data array with 0
uint8_t payload[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
//
XBee xbee = XBee();
// create reusable response objects for responses we expect to handle
ZBRxResponse zbRx = ZBRxResponse();
ModemStatusResponse msr = ModemStatusResponse();
//XBeeAddress64 addr64 = XBeeAddress64(0x0013A200, 0x41554A03);
unsigned long start, finished, elapsed;
char ValAdc[4];
char ValAdc1[4];
char n[4];
char no[4];
char noo[4];
int index,rx,loss;
typedef struct {
float volt = 0.0f;//voltage
float ntu = 0.0f; //turbidity
int adc,adc1;
int h,m,s; //integer var
} load;
load theData;
void setup()
{
Serial.begin(9600);
xbee.begin(Serial);
xbee.setSerial(Serial);
}
// continuously reads packets, looking for ZB Receive or Modem Status
void loop()
{
// 1. This will read any data that is available:
xbee.readPacket();
// 2. Now, to check if a packet was received:
index++;
Serial.print("index ");
Serial.println(index);
if (xbee.getResponse().isAvailable())
{
rx++;
Serial.print("packet receive ");
Serial.println(rx);
// got packet from xbee
if (xbee.getResponse().getApiId() == ZB_RX_RESPONSE)
{
// trx packet from xbee to variable
xbee.getResponse().getZBRxResponse(zbRx);
if (zbRx.getOption() == ZB_PACKET_ACKNOWLEDGED) {
// the sender got an ACK
//flashLed(statusLed, 10, 100);
Serial.println("packet acknowledged");
}
else {
// we got it (obviously) but sender didn't get an ACK
//flashLed(errorLed, 2, 100);
Serial.println("packet not acknowledged");
}
for(int i = 0; i < 14; i++){
payload[i] = zbRx.getData(i);
}
ValAdc[0]= zbRx.getData(0);
ValAdc[1]= zbRx.getData(1);
ValAdc[2]= zbRx.getData(2);
ValAdc[3]= zbRx.getData(3);
memcpy(&theData.adc, &ValAdc, 4);
ValAdc1[0]= zbRx.getData(4);
ValAdc1[1]= zbRx.getData(5);
ValAdc1[2]= zbRx.getData(6);
ValAdc1[3]= zbRx.getData(7);
memcpy(&theData.adc1, &ValAdc1, 4);
//n[0] = zbRx.getData(8);
//n[1] = zbRx.getData(9);
byte un1,un2,un3,un4,un5,un6;
un1 = zbRx.getData(8);
un2 = zbRx.getData(9);
un3 = zbRx.getData(10);
un4 = zbRx.getData(11);
un5 = zbRx.getData(12);
un6 = zbRx.getData(13);
//memcpy(&theData.h, &n, 2);
//no[0] = zbRx.getData(10);
//no[1] = zbRx.getData(11);
//memcpy(&theData.m, &no, 2);
//n2[0] = zbRx.getData[10];
//n2[1] = zbRx.getData[11];
//byte unsecond5 = zbRx.getData[12];
//byte unsecond6 = zbRx.getData[13];
Serial.print("Turbidity : ");
Serial.print(theData.adc1);
Serial.print(" pH: ");
Serial.print(theData.adc);
Serial.print(" waktu ");
Serial.print(word(un1,un2));
Serial.print(":");
Serial.print(word(un3,un4));
Serial.print(":");
Serial.print(word(un5,un6));
Serial.print(" Size ");
Serial.println(sizeof(payload));
} else {
Serial.print("No data getting out of the packet");
}
/*
else if (xbee.getResponse().getApiId() == MODEM_STATUS_RESPONSE) {
xbee.getResponse().getModemStatusResponse(msr);
// the local XBee sends this response on certain events, like association/dissociation
if (msr.getStatus() == ASSOCIATED) {
// status xbee terhubung flashLed(statusLed, 10, 10);
Serial.println("xbee associated");
} else if (msr.getStatus() == DISASSOCIATED) {
// status xbee tidak terhubung flashLed(errorLed, 10, 10);
Serial.println("xbee disassociated");
} else {
// another status flashLed(statusLed, 5, 10);
Serial.println("Unknown trouble");
}
}*/
}
else if (xbee.getResponse().isError()){
Serial.print("oh no!!! error code:");
Serial.println(xbee.getResponse().getErrorCode());
}
else
{
loss++;
Serial.print("packet loss ");
Serial.println(loss);
}
if (index==100) {
Serial.print("PLR=");
int total=index-rx;
Serial.println(total);
}
delay(1500);
}// END OF LOOP
