Question

I've been trying to get the XBee S2 working with the Raspberry Pi through Python. Though, trying to get multiple responses from AT commands is not working.

I have a loop to send the ATID command 10 times, and using the following code I am trying to get a response:

def get_xbee_reply(self):
  reply = b''
  while True:
    print ("hi")
    char = self.serial.read()
    if (char == b'\r'):
      print("ended reply")
      break
    reply += char
  print(reply)
  return (reply.decode('utf-8'))

The reply I am getting:

hi
hi
hi
ended reply
b'OK'
hi
hi
hi
hi
ended reply
b'555'
id = 555
hi
hi
hi
ended reply
b'OK'
hi

It hangs after getting the second OK message from +++ (which I send before the ATID to prevent falling out of AT mode. If I hit Ctrl + C it breaks and does this again, receiving OK, 555 and OK again before hanging.

The XBee is working through my PC and through Minicom with the Raspberry Pi, so it's not an XBee problem.

Why does it stop after the second 'OK'? Doesn't it stop after OK or is the command not coming through? Even with a time.sleep it doesn't work.

EDIT: added code for sending the ATID command

def send_get_at(self, command):
  self.serial.write(
    (command + '\r').encode('utf-8')
  )
  print (str(command) + " = {}".format(
      self.get_xbee_reply()
    )
  )
Was it helpful?

Solution

I definitely recommend switching from "AT mode" to "API mode" on the modules, as it's more flexible and you won't have to worry about whether you're in command mode or not.

You would need to configure the XBee module for API mode (ATAP=1) before you could use the API-mode methods/functions in python-xbee.

It might be helpful to monitor the serial connection between the XBee and Raspberry Pi, but that would likely require some hardware hacking (connecting the Tx and Rx pins to the Rx pins of two serial ports, and having software dump the bytes received on both, with timing; or using a logic probe).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top