Home/Support/Support Forum/Xbee pythong library problems
Welcome to Digi Forum, where you can ask questions and receive answers from other members of the community.

Xbee pythong library problems

0 votes
Hello to you all, i hope your well and this reaches you well.

I have been racking my brains with this, and would be gratful for any assistance please.

The poblem i am experiancing is when polling for data with a temprature sensor.

Background:
On line 22 of the polling sample, there is a statment which says xbee_message.data.decode(). Froim my understanding this method is responsable for decoding frames receved from an end device and works with normal text fine. However when i try to receve DHT22 sensor data from an XBee end device, i am thrwon an error UnicodeDecodeError: 'utf-8' codec can't decode byte 0x9a in position 1: invalid start byte.

i have been trying to figure out how to correct this, as i understand that the decodert is waiting for unicode data (test) but how can i get it to receve other data?

code i am using in python:
from digi.xbee.devices import XBeeDevice

#port = "/dev/ttyUSB0"
port = "com7"
rate = 9600


def main():
print("Starting XBee.....")
print("opening port....")

comms = XBeeDevice(port, rate)

try:
comms.open()
comms.flush_queues()
print("Waiting for data...\n")

while True:
xbee_message=comms.read_data()
if xbee_message is not None:
print("From %s >> %s" % (xbee_message.remote_device.get_64bit_addr(), xbee_message.data.decode()))

finally:
if comms is not None and comms.is_open():
comms.close()


if __name__ == '__main__':
main()

code i am using in arduino:
#include <XBee.h>
#include <Printers.h>
#include <DHT.h>
#include "binary.h"

XBeeWithCallbacks xbee;

#define DebugSerial Serial
#define XBeeSerial Serial3

// Sensor type is DHT22, connected to pin D4.
DHT dht(4, DHT22);

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

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

// Setup callbacks
xbee.onPacketError(printErrorCb, (uintptr_t)(Print*)&DebugSerial);
xbee.onResponse(printErrorCb, (uintptr_t)(Print*)&DebugSerial);

// Setup DHT sensor
dht.begin();

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

void sendPacket() {
// Prepare the Zigbee Transmit Request API packet
ZBTxRequest txRequest;
txRequest.setAddress64(0x0000000000000000);

// Allocate 9 payload bytes: 1 type byte plus two floats of 4 bytes each
AllocBuffer<9> packet;

// Packet type, temperature, humidity
packet.append<uint8_t>(1);
packet.append<float>(dht.readTemperature());
packet.append<float>(dht.readHumidity());
txRequest.setPayload(packet.head, packet.len());

// And send it
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) {
sendPacket();
last_tx_time = millis();
}
}

binary.h file used to wrap serial data for transmission:
#ifndef __BINARY_H
#define __BINARY_H

#include <stdint.h>
#include <string.h>

struct Buffer {
/* Wrap a buffer, containing the given number of
* data bytes and a given remaining capacity (so total capacity is len
* + capacity). */
Buffer(uint8_t *buf, size_t len, size_t capacity = 0) : head(buf), tail(buf + len), end(tail + capacity) { }

uint8_t *head;
uint8_t *tail;
uint8_t *end;

/**
* Appends the given value to the end of this buffer and returns true.
*
* If this would write past the end of the available memory (end),
* nothing happens and false is returned.
*/
template <typename T>
bool append(const T &v) {
if (tail + sizeof(T) <= end) {
memcpy(tail, (const uint8_t*)&v, sizeof(T));
tail += sizeof(T);
return true;
}
return false;
}

/**
* Gets a copy of the value of the given type, on the given offset
* into the data in this buffer.
*
* If this would read past the end of the data in this buffer (tail),
* an all-zeroes value is returned instead.
*/
template <typename T>
T get(size_t offset) {
T res;
if (head + offset + sizeof(T) <= tail)
memcpy((uint8_t *)&res, head + offset, sizeof(T));
else
memset((uint8_t *)&res, 0, sizeof(T));
return res;
}

/*
* Gets a pointer into this buffer, starting from the given offset
* within the data.
*
* If this would read past the end of the data in this buffer (tail),
* NULL is returned instead.
*/
uint8_t *get(size_t offset) {
if (head + offset < tail)
return head + offset;
return NULL;
}

/*
* Remove one value of the given type from the data and return it.
*
* If this would read past the end of the data in this buffer (tail),
* an all-zeroes value is returned instead (any partial data that
* could have been read is removed, but not returned).
*/
template <typename T>
T remove() {
T res = get<T>(0);
head += min(len(), sizeof(T));
return res;
}

/*
* Remove the given number of bytes from the data and return a pointer
* to them.
*
* If this would read past the end of the data in this buffer (tail),
* NULL returned instead (any partial data that could have been read
* is removed, but not returned).
*/
uint8_t *remove(size_t bytes) {
uint8_t *res = get(0);
head += min(len(), bytes);
return res;
}

size_t len() {
return tail - head;
}
};

template <size_t size>
struct AllocBuffer : Buffer {
AllocBuffer() : Buffer(buf, 0, size) { };
uint8_t buf[size];
};

#endif // __BINARY_H
asked Mar 4, 2019 in 3rd Party Python and API development in other Languages by K0ng New to the Community (0 points)

Please log in or register to answer this question.

...