Cannot send/receive using Xbee's in API mode (python)

madFoilist picture madFoilist · Feb 10, 2013 · Viewed 8.4k times · Source

I have two Xbee Pro 900's, each attached to a Raspberry Pi. Both are updated to version 1061 and are set to API Enable with escapes. They also have the same Modem VID of 7FFF. Both Pi's have PySerial and the python-xbee library installed.

Xbee 1(Receiver) has a serial number of 0013A200409A1BB8
Xbee 2(Sender) has a serial number of 0013A200709A1BE9

I've included my code below, which is just sample code I've found online. My issue is that I'm not receiving anything on the appropriate Xbee. I have absolutely no idea what is wrong, I've triple checked the destination address, and both of the Xbee's configuration settings.

Xbee 2 Code(Sender):

#! /usr/bin/python

import time

from xbee import XBee
import serial

PORT = '/dev/ttyUSB0'
BAUD_RATE = 9600

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

# Create API object
xbee = XBee(ser,escaped=True)
import pprint
pprint.pprint(xbee.api_commands)

DEST_ADDR_LONG = "\x00\x13\xA2\x00\x40\x9A\x1B\xB8"

# Continuously read and print packets
while True:
    try:
        print "send data"
        xbee.tx_long_addr(frame='0x1', dest_addr=DEST_ADDR_LONG, data='AB')
        time.sleep(1)
    except KeyboardInterrupt:
        break

ser.close()

Xbee 1 Code(Receiver):

#! /usr/bin/python

from xbee import XBee
import serial

PORT = '/dev/ttyUSB0'
BAUD_RATE = 9600

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

# Create API object
xbee = XBee(ser,escaped=True)

# Continuously read and print packets
while True:
    try:
        print "waiting"
        response = xbee.wait_read_frame()
        print response
    except KeyboardInterrupt:
        break

ser.close()

When both programs are running, the Tx light on the sending Xbee blinks, but I receive nothing on the receiving Xbee. Is there something I'm missing? Thanks for your time!

Answer

Niko Gamulin picture Niko Gamulin · Dec 28, 2013

Are you using XBee or XBeePro? I had the same problem and this post helped me a lot.

Try to modify the Receiver Code the following way:

import config
import serial
import time
from xbee import ZigBee

def toHex(s):
    lst = []
    for ch in s:
        hv = hex(ord(ch)).replace('0x', '')
        if len(hv) == 1:
            hv = '0'+hv
        hv = '0x' + hv
        lst.append(hv)

def decodeReceivedFrame(data):
            source_addr_long = toHex(data['source_addr_long'])
            source_addr = toHex(data['source_addr'])
            id = data['id']
            samples = data['samples']
            options = toHex(data['options'])
            return [source_addr_long, source_addr, id, samples]

PORT = '/dev/ttyUSB0'
BAUD_RATE = 9600

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

zb = ZigBee(ser, escaped = True)

while True:
    try:
        data = zb.wait_read_frame()
        decodedData = decodeReceivedFrame(data)
        print decodedData

    except KeyboardInterrupt:
        break

In my case the code above outputs the following:

[['0x00', '0x13', '0xa2', '0x00', '0x40', '0x9b', '0xaf', '0x4e'], ['0x68', '0x3f'], 'rx_io_data_long_addr', [{'adc-0': 524}]]

Here I shared configuration settings for Controller Node (compatible with X-CTU)