How can I send strings of data to an XBee with a python library?
Asked Answered
R

4

5

Which library should I be using, and how?

Python XBee seems to be only able to send commands in API mode, and I can't find an example of anyone using it to send a string. Maybe I'm misunderstanding what API mode is, but I can't find a payload in the documentation...

Are Digi's Python Socket extensions baked into Python? I can't seem to get any of the constants they claim to have defined in my Python (2.7.3rc2), nor can I find a mention of how to get these extensions on their site. It seems like that could be a way to pass around strings, but how do I use it?

Ruffina answered 18/11, 2012 at 1:1 Comment(0)
H
8

If the Xbee is connected to the computer as a serial device you can just use a serial library such as pySerial. Here are some code snippets from a project I just finished.

# Connect to Xbee
self.ser = serial.Serial(port, baud, timeout=timeout)

# Send data (a string)
self.ser.write(packet)

# Read data
self.data += self.ser.read()

We were using the Xbees in transparent mode - each byte you write on one end is visible on the other end with a read. There was no need for a special Xbee library.

Hadlock answered 18/11, 2012 at 1:17 Comment(2)
So the XBee on the serial port will repeat that string onto the XBee network, and also write all data it receives from remote XBees to serial? I assume both XBees need to be in transparent mode?Ruffina
I have only ever worked with two Xbees communicating directly, never with a larger network. If both are in transparent mode than any byte you write on one end will appear on the other end, just like normal serial.Hadlock
D
7

I would also recommend using pySerial if you have a very simple setup and only two XBees, but if you have anything more complicated then you are better off with a library.

The python-xbee library is quite simple to use but does lack any sort of comprehensive documentation. To send and receive simple message using it:

from xbee import XBee
from serial import Serial

PORT = '/dev/ttyUSB0'
BAUD = 9600

ser = Serial(PORT, BAUD)

xbee = XBee(ser)
# Send the string 'Hello World' to the module with MY set to 1
xbee.tx(dest_addr='\x00\x01', data='Hello World')

# Wait for and get the response
print(xbee.wait_read_frame())

ser.close()

You can send AT commands by doing:

xbee.at(frame_id='A', command='MY')
reply = xbee.wait_read_frame()
print(reply)

# Getting the integer value out of reply
import struct    
print(struct.unpack('>h', reply['parameter'])[0])

You can set the frame_id to any string, and it is used to identify the correct reply.

Detonate answered 22/11, 2012 at 18:23 Comment(1)
I recently took over the management of python-xbee (the one you pip install) as it hadn't been updated for years. I'm happy to accept pull requests that improve documentation! github.com/nioinnovation/python-xbee/blob/master/docs/source/…Wanton
L
0

The first question is "Are you sure your devices are in API mode?". You are seeing this error because the receiving end is seeing a frame of type 'tx' (type 0x01) coming in. Although this is the frame you have requested to be sent I believe you would expect it to be received as type 'rx' (type 0x81) by the receiving end.

If you look at the code in /xbee/ieee.py you will see two lists: * api_commands = Outgoing: You would never expect one of these frame types incoming. * api_responses = Incoming: You should only see these frame types incoming.

If the library detects one of the api_commands incoming it will throw the error you see: "Incoming frame with id 1 looks like a command frame of type 'tx' (these should not be received). Are you sure your devices are in API mode?"

I am not 100% sure of your situation but it looks like your outgoing 'tx' frame is not being translated to an incoming 'rx' frame at the other end - possibly API mode not enabled on all XBees?

Also see https://github.com/nioinnovation/python-xbee/issues/44

Loeb answered 18/4, 2017 at 16:1 Comment(0)
B
-1
ser = serial.Serial(SERIAL_PORT, 9600)
bee = ZigBee(ser) # <--

Try to use ZigBee instead of XBee, if it fails.

Blanco answered 13/3, 2014 at 13:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.