Reputation: 18271
I would like to use Digi XStick2 ZB
(connected via USB and visible as serial interface on /dev/ttyUSB0
) as a ZigBee controller for a bunch of ZigBee sensors and actuators (temperature and humidity, contact sensor, water leakage, power plug,...). In other words, I am looking for the way to configure mesh network and to communicate with sensors/actuators.
I found python xbee and wrote a simple python script to communicate with the USB stick:
#!/usr/bin/python
import serial
import time
from xbee import ZigBee
def print_data(data):
print "Data received:", data
serial_port=serial.Serial('/dev/ttyUSB0', 9600)
zbee = ZigBee(serial_port, callback=print_data)
print "Sending some command"
zbee.send("at", frame='A', command='MY', parameter=None)
print "Waiting..."
while True:
try:
time.sleep(0.001)
except KeyboardInterrupt:
break
zbee.halt()
serial_port.close()
It works, and I get some response to my command:
$ ./test.py
Sending some command
Waiting...
Data received: {'status': '\x00', 'frame_id': '\x01', 'parameter': '\x00\x00', 'command': 'MY', 'id': 'at_response'}
But I can't find anything about the protocol used (namely, content of the messages I can send) - is there some command reference or protocol definition I can use?
Alternatively, is there an easier way (higher level library?) to control sensors via USB stick?
Upvotes: 0
Views: 1009
Reputation: 18271
As usual, I found the answer shortly after asking... I looks like XBee ZB supports two modes:
To quote:
This example demonstrates XBee operation in AT mode. AT mode is synonymous with "Transparent" mode. In AT mode, any data sent to the XBee module is immediately sent to the remote module identified by the Destination Address in memory. When the module is in AT mode, it can be configured by the user or a host microcontroller by first placing the module in Command mode and then sending predefined AT commands through the UART port. This mode is useful when you don't need to change destination addresses very often, or you have a very simple network, or simple point to point communication. For larger networks that involve nodes talking to multiple targets, API mode is more useful. In API mode, rather than sending AT commands serially, data packets are assembled that include the Destination Address. API mode allows you to change destination address much more quickly because Command Mode doesn't need to be entered. API mode is also useful if the user needs to change the configuration of a remote module. This project focuses on AT mode operation.
Articles contain the link to documentation for both modes: https://eewiki.net/download/attachments/24313921/XBee_ZB_User_Guide.pdf?version=1&modificationDate=1380318639117&api=v2
A short working example is now:
#!/usr/bin/python
import serial
import time
from xbee import ZigBee
def print_data(data):
print "Data received:", data
serial_port=serial.Serial('/dev/ttyUSB0', 9600)
xbee = ZigBee(serial_port, callback=print_data)
xbee.send("at", frame='A', command='SH', parameter=None)
xbee.send("at", frame='A', command='SL', parameter=None)
while True:
try:
time.sleep(0.001)
except KeyboardInterrupt:
break
xbee.halt()
serial_port.close()
Output:
Data received: {'status': '\x00', 'frame_id': '\x01', 'parameter': '\x00\x01\x02\x03', 'command': 'SH', 'id': 'at_response'}
Data received: {'status': '\x00', 'frame_id': '\x01', 'parameter': '\x04\x05\x07\x07', 'command': 'SL', 'id': 'at_response'}
This retrieves serial number of the controller (01020304050607
in this case).
Hope it helps someone.
Upvotes: 1