How do I read data received by the coordinator using a python script?

Hi everyone,

This is mainly a python coding question about the python xbee library as used on a raspberry pi to pull data from the coordinator in a DigiMesh network. My script can get hardware data from the Coordinator but I can’t read the received message coming in from a router.

The application: on a breadboard, I connect a passive infrared sensor (PIR) to an XBee S2C which is configured as a DigiMesh Router. The PIR signal line connects to the Router’s IO4 pin (D4) and the Router checks the digital input on that pin every second. I have a second XBee S2C set up as a DigiMesh Coordinator, mounted on a Parallax XBee adaper which connects to a Raspberry Pi via USB.

When I plug the coordinator into my laptop and open X-CTU, I can see that the DigiMesh network has formed and, in Consoles Working Mode, I can open the Coordinator serial connection and see the data coming in from the Router every second. In the window that translates the incoming message, I see the High/Low data sent by the Router from its IO4 pin.

Now comes the python script. I have the Pi configured to ONLY have Python 3 running, as I hear the xbee-python library works best in that environment. When I connect the Coordinator to the Pi via USB, and SSH into the pi from my laptop, I can run a python script which looks at the Coordinator and reads various property values. But I can’t read the data sent to it by the router, and this is where I need help. I’m hoping that someone is willing to repeat this experiment (with any digital sensor, not just the PIR) and help me decipher where the snag is.

Note: not all PIRs have a pull-down resistor on board, so if it perpetually reads HIGH, you may need to put your own 4.7k ohm pulldown resistor on the PIR signal pin. My PIR has one onboard, so sometimes it’s High and sometimes it’s LOW.

Radio modules updated to these firmware settings:

  • Product family: XB24C
  • Function set: DigiMesh 2.4 TH
  • Firmware version: 9000

After updates complete, I reset all firmware settings on both XBees to their default values (using the Default button). Then I make the following changes, in this order, with all other values being left default:

Coordinator:
CE = Indirect Msg Coordinator [1]
NI = “Coord” — without quotes
AP = API Mode Without Escapes [1]

Router:
CE = Standard Router [1] — default value, but make sure it reads this
NI = “PIR” — without quotes
AP = API Mode Without Escapes [1]
D4 = Digital Input [3]
IR = 3E8

At this point, I unplug the Router from my laptop, plug it into the breadboard with the PIR sensor, and give it power. Back at my laptop, which still has the Coordinator plugged in, I open X-CTU and through the serial connection I see data coming in every second.

Now I unplug the Coordinator and its adapter board from my laptop and plug it into the Pi. Then I open a terminal window, ssh into the Pi, and run the following script:

from digi.xbee.devices import DigiMeshDevice

COM_TEST = “/dev/ttyUSB0”

device = DigiMeshDevice(COM_TEST, 9600)
device.open()

print(“********************************************************************”)
print("Opened connection with ZigBee device at port " + COM_TEST)

Check the 64-bit address of the device

coord_64 = device.get_64bit_addr()
print("Coordinator 64-bit address is " + str(coord_64))

Show the hardware version of the device

hardware_version = device.get_hardware_version()
print("Hardware version is " + str(hardware_version))

Show the protocol

protocol = device.get_protocol()
print("Protocol is " + str(protocol))

Show parameter NI

NI_param = device.get_parameter(“NI”)
print("Node Identification is " + str(NI_param))

Show the XBee network

zbnet = device.get_network()
print("Network is "+ str(zbnet))

Show API mode

api_mode = device.get_api_output_mode()
print("API Output Mode is "+ str(api_mode))

Poll every device on the network for data. Application is blocked until

data from any XBee device of the network is received.

zbee_msg = device.read_data()
print("Data read in = " + str(zbee_msg))

Done - close the connection.

device.close()
print(“Closed connection with ZigBee device.”)
print(“********************************************************************”)

This is the output I get, which proves that my script does connect with the Coordinator:


Opened connection with ZigBee device at port /dev/ttyUSB0
Coordinator 64-bit address is 0013A2004164D656
Hardware version is HardwareVersion.XB24C_TH_DIP
Protocol is XBeeProtocol.DIGI_MESH
Node Identification is bytearray(b’ Coord’)
Network is
API Output Mode is APIOutputMode.NATIVE
Data read in = None
Closed connection with ZigBee device.


The problem is that the device.read_data() returns “None”. I know there’s a binary message being sent across, and I’d like my script to be able to read it, then use some other functions to extract the data and parse out the High/Low.

Any ideas what may be gong wrong here?

Thanks,
Tom

1 Like