Minh_Duc
Posts: 8
Joined: Thu Jun 10, 2021 10:11 am

Send data from xbee arduino (router) to xbee raspberry (coordinator)

Fri Apr 29, 2022 10:22 am

Hi everyone,
I'm having a project aboout module XBee S2C. I have some problem when trying to send data from xbee arduino DHT11 to xbee raspberry pi.
When I upload xbee arduino dht11 code, I receive some strange characters in serial monitor, so data sent to raspberry pi is not true.
Can you help me solve this problem?
Thanks.

This is electronic components I use:
- Xbee S2C
- Arduino UNO
- Xbee Shield Arduino
- DHT11 sensor
- Xbee USB Adapter Ft232rl
Configure Xbee in XCTU:
Coordinator:
Function: ZIGBEE TH Reg
ID PAN ID: 1234
SC Scan Channels: 7FFF
JV Channel Verification: Disabled
CE Coordinator Enable: Enabled
AP API Enable: API enabled [1]
Router:
Function: ZIGBEE TH Reg
ID PAN ID: 1234
SC Scan Channels: 7FFF
JV Channel Verification: Enabled
CE Coordinator Enable: Disabled
AP API Enable: API enabled with Escaping [2]
Code Arduino:
#include <DHT.h>
#include <DHT_U.h>
#include <XBee.h>
#include "DHT.h"
#define DHTPIN 2
#define DHTTYPE DHT11

//Initialize XBee ZB client
XBee xbee = XBee();
//Payload byte array
uint8_t payload[8];
//Send to coordinator
XBeeAddress64 addr64 = XBeeAddress64(0, 0);
//Create request for XBee ZB
ZBTxRequest zbTx = ZBTxRequest(addr64, payload, sizeof(payload));

//Initialize DHT sensor module
DHT dht(DHTPIN, DHTTYPE);

void set_float_to_payload(float value, int index) {
uint8_t *value_array;
value_array = reinterpret_cast<uint8_t*>(&value);
for(int i=0; i<sizeof(value); i++){
payload[i+index] = value_array;
}
}
void setup() {
Serial.begin(9600);
xbee.setSerial(Serial);

dht.begin();
}
void loop() {
delay(3000);

float temperature = dht.readTemperature();
float humidity = dht.readHumidity();

set_float_to_payload(temperature, 0);
set_float_to_payload(humidity, 4);
xbee.send(zbTx);

}
Code python receive data on raspberry:
#!/usr/bin/python

"""
Application to receive data from XBee ZB using XBee Python Library.

"""
from xbee import ZigBee
import serial
import struct

# TODO Replace with the serial port where your receiver module is connected.
PORT = '/dev/ttyUSB0'
# TODO Replace with the baud rate of you receiver module.
BAUD_RATE = 9600

# Open serial port
myDevice = serial.Serial(PORT, BAUD_RATE)

# Create API object
xbee = ZigBee(myDevice)

def prettyHexString(str):

"split string by 2 length"

return ' '.join([str[i:i+2] for i in range(0, len(str), 2)])

# Continuously read and print packets
print(">> Waiting for data...")
while True:

try:

response = xbee.wait_read_frame()
source_addr = response['source_addr_long'].hex().upper()
payload = prettyHexString(response['rf_data'].hex().upper())
data = struct.unpack('ff', response['rf_data'])
print('From %s >> [%s] | { temperature: %.1f degrees, humidity: %.1f%% }' % (source_addr, payload, data[0], data[1]))

except KeyboardInterrupt:
break
myDevice.close()

MiscBits
Posts: 1748
Joined: Wed Jan 27, 2021 12:48 pm

Re: Send data from xbee arduino (router) to xbee raspberry (coordinator)

Fri Apr 29, 2022 6:46 pm

Serial ports have more than a baud rate that can be configured.

It could be worth setting up both sides the same with start / stop bits, parity and data size.

Also check the data is using the same character set e.g. utf8 or ascii if you send text vs binary.

Minh_Duc
Posts: 8
Joined: Thu Jun 10, 2021 10:11 am

Re: Send data from xbee arduino (router) to xbee raspberry (coordinator)

Mon May 02, 2022 3:28 am

MiscBits wrote:
Fri Apr 29, 2022 6:46 pm
Serial ports have more than a baud rate that can be configured.

It could be worth setting up both sides the same with start / stop bits, parity and data size.

Also check the data is using the same character set e.g. utf8 or ascii if you send text vs binary.
I don't know how to check properly. Can you help me with specific instructions?

Return to “General discussion”