XBee API mode Why "AllocBuffer" was not declared in this scope please help me

Hello everyone,

I want to send Temperature and Humidity data from DHT22 sensor to XBee receiver in API mode.I use a code from the book "Building Wireless Sensor Networks Using Arduino" By Matthijs Kooijman.

Transmitter code is

#include <XBee.h>
#include <Printers.h>
#include <AltSoftSerial.h>
#include <SoftwareSerial.h>
#include <DHT.h>
#define DHTPIN 6
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
#include "binary.h"
XBeeWithCallbacks xbee;
SoftwareSerial XBee(2,3);
AltSoftSerial SoftSerial;
#define DebugSerial Serial
#define XBeeSerial SoftSerial
void setup() {
//Setup debug serial output
DebugSerial.begin(9600);
dht.begin();
DebugSerial.println(F("Starting..."));

// Setup XBee serial communication
XBee.begin(9600);
xbee.begin(XBee);
delay(1);

// Send a first packet right away
sendPacket();
}

void sendPacket() {

ZBTxRequest txRequest;
txRequest.setAddress64(0x000000000000FFFF);

AllocBuffer <9> packet;
packet.append<uint8_t>(1);
packet.append(dht.readTemperature());
packet.append(dht.readHumidity());
txRequest.setPayload(packet.head,packet.len());
xbee.send(txRequest);
}

unsigned long last_tx_time = 0;

void loop() {
// Check the serial port to see if there is a new packet available
xbee.loop();

// Send a packet every 10 seconds
if (millis() - last_tx_time > 10000) {
last_tx_time = millis();
sendPacket();

}
}

My problem is I can't compile my code. the program show
exit status 1 'AllocBuffer' was not declared in this scope
I'm cannot fixed it. Who know how to fix it please help me.

Thank you

Can we see your code? Pls use code tags...

#include <XBee.h>
#include <Printers.h>
#include <AltSoftSerial.h>
#include <SoftwareSerial.h>
#include <DHT.h>
#define DHTPIN 6     
#define DHTTYPE DHT22 
DHT dht(DHTPIN, DHTTYPE);
#include "binary.h"
XBeeWithCallbacks xbee;
SoftwareSerial XBee(2,3);
AltSoftSerial SoftSerial;
#define DebugSerial Serial
#define XBeeSerial SoftSerial

void setup() {
   //Setup debug serial output
  DebugSerial.begin(152000);
 dht.begin();
  DebugSerial.println(F("Starting..."));

  // Setup XBee serial communication
  XBee.begin(152000);
  xbee.begin(XBee);
  delay(1);

  // Send a first packet right away
  sendPacket();
 
}

void sendPacket() {
    
    ZBTxRequest txRequest;
    txRequest.setAddress64(0x000000000000FFFF);
   
   AllocBuffer <9> packet;
   packet.append<uint8_t>(1);
   packet.append<float>(dht.readTemperature());
   packet.append<float>(dht.readHumidity());
   txRequest.setPayload(packet.head,packet.len());
   xbee.send(txRequest);
  }



unsigned long last_tx_time = 0;

void loop() {
  // Check the serial port to see if there is a new packet available
  xbee.loop();

  // Send a packet every 10 seconds
  if (millis() - last_tx_time > 10000) {
    last_tx_time = millis();
    sendPacket();
   
  }
}

From the book it said you must use library #include "binary.h" to create a AllocBuffer . I used it already but the program cannot compiled.

Thank you

AllocBuffer?

Am I missing something? Why not just use a fixed length null terminated char array as the buffer? Or a fixed length byte array?

AllocBuffer <9> packet; does not look like valid C syntax to me, no matter what AllocBufer is.

Also, while I think the source of the error is clear here - you should always include the full text of any and all compile errors,, which specifies what line and in what file the error came from - and the other errors, if any, may point to a cause of the error other than where the final error occurred. Often, you get a cascade of compile errors, and the final one that it shows is the fallout from one of the earlier errors.

Finally, we don't all own a copy of every book written about Arduino (most of them are also terrible, not good for much other than backup toilet paper or kindling for the fireplace). You should post more context, including links to all these libraries you're using.

Do you have the code bundle that would have come with that book?

If not, you're going to need to rethink this. Given that you know the size of the payload a struct might work well for you. Something along the lines of:

struct PayloadStruct
{
  uint8_t  Index;
  float fTemperature;
  float fHumidity;     
} __attribute__((packed));
  
PayloadStruct
  payload;

void setup()
{
  Serial.begin(9600);
  while( !Serial );
  .
  .
  .
}

  
void SendPacket()
{
  .
  .
  .

  payload.Index = 1;
  payload.fTemperature = dht.readTemperature();
  payload.fHumidity = dht.readHumidity();
  txRequest.setPayload((uint8_t *)&payload, sizeof( struct PayloadStruct ) );
  .
  .
  .
  
}//SendPacket

Thing is, I don't know if "setPayload" is even a valid method so this may not work directly (i.e. you may have to make other changes in the event the Xbee library functions are missing or not what you've been led to believe in the book...)

Thank you very much Mr.Blackfin

I'm already use your code. The program can compile. I think XBee transmitter side cand send temperature and humidity data correctly
XBee transmitter side (router) code

#include <XBee.h>
#include <Printers.h>
#include <AltSoftSerial.h>
#include <SoftwareSerial.h>
#include <DHT.h>
#define DHTPIN 2     
#define DHTTYPE DHT22 
DHT dht(DHTPIN, DHTTYPE);
#include "binary.h"
XBeeWithCallbacks xbee;
SoftwareSerial XBee(2,3);
AltSoftSerial SoftSerial;
#define DebugSerial Serial
#define XBeeSerial SoftSerial

struct PayloadStruct
{
  uint8_t  Index;
  float fTemperature;
  float fHumidity;     
} __attribute__((packed));
  
PayloadStruct
  payload;


void setup() {
   //Setup debug serial output
  DebugSerial.begin(9600);
  dht.begin();
  DebugSerial.println(F("Starting..."));

  // Setup XBee serial communication
  XBee.begin(9600);
  xbee.begin(XBee);
  delay(1);

  // Send a first packet right away
  sendPacket();

}

void sendPacket() {
    // Prepare the Zigbee Transmit Request API packet
    ZBTxRequest txRequest;
    txRequest.setAddress64(0x000000000000FFFF);
    payload.Index = 1;
    payload.fTemperature = dht.readTemperature();
    payload.fHumidity = dht.readHumidity();
    txRequest.setPayload((uint8_t *)&payload, sizeof( struct PayloadStruct ) );
    xbee.send(txRequest);
    DebugSerial.print(F("Temperature"));
    DebugSerial.print(payload.fTemperature);
    DebugSerial.print(F("Humidity"));
    DebugSerial.print(payload.fHumidity);
    DebugSerial.println();

    
}


unsigned long last_tx_time = 0;

void loop() {
  // Check the serial port to see if there is a new packet available
  xbee.loop();

  // Send a packet every 10 seconds
  if (millis() - last_tx_time > 10000) {
    last_tx_time = millis();
    sendPacket();
  }
}

But I cannot write a code in receiver side correctly. The code in receiver side on this book is
XBee receiver side (Coordinator) code from the book

#include <XBee.h>
#include <Printers.h>
#include <AltSoftSerial.h>
#include <SoftwareSerial.h>
#include "binary.h"

XBeeWithCallbacks xbee;
SoftwareSerial XBee(2,3);
AltSoftSerial SoftSerial;
#define DebugSerial Serial
#define XBeeSerial SoftSerial

void setup() {
  DebugSerial.begin(9600);
  DebugSerial.println(F("Starting..."));
  XBee.begin(9600);
  xbee.begin(XBee);
  delay(1);

  // Setup callbacks
  xbee.onZBExplicitRxResponse(processRxPacket);
  xbee.onZBRxResponse(processRxPacket);
}

void processRxPacket(ZBRxResponse& rx, uintptr_t) {
  Buffer b(rx.getData(), rx.getDataLength());
  uint8_t type = b.remove<uint8_t>();
  if (type == 1 && b.len() == 8) {
  DebugSerial.print(F("Received packet from "));
  printHex(DebugSerial, rx.getRemoteAddress64());
  DebugSerial.println();
  DebugSerial.print(F("Temperature: "));
  DebugSerial.println(b.remove<float>());
  DebugSerial.print(F("Humidity: "));
  DebugSerial.println(b.remove<float>());
  return;
}
DebugSerial.println(F("Unknown or invalid packet"));
printResponse(rx, DebugSerial);
}


void loop() {
  // Check the serial port to see if there is a new packet available
  xbee.loop();
}

How can I change this code to receive the temperature and humidity data from XBee transmitter side. Please help me

The configuration in XBee is

  1. XBee XB24-ZB Coordinator API: ID = 123, AP = 2, DH =13A200 , DL = 40A933C5 , BD = 9600
    XBee XBP24BZ7 Router API: ID = 123, AP = 2, DH = 13A200 = , DL = 4052A6F5 , BD 9600

Thank you