1
votes

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()
    )
  )
1
Can you post the code that sends the ATID command and calls get_xbee_reply()? You might want to try running the XBee module in API mode, and making use of the python-xbee library. - tomlogic
I am using the python-xbee library. The code that sends the ATID command is sending it through serial. the b'OK' is the reply of sending +++, so that is working. Also the first ATID replies. I've added the code to the question - Michaël van D.
Is it possible you're actually sending +++ATID\r and that's why the second ATID fails? If you change the send_get_at() to write ('\r' + command + '\r') does that solve it? 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. - tomlogic
No I'm sorry, but when the +++ is sent it waits until the OK is received before it sends ATID, so it's not possible it sends +++ATID\r. When I try through API mode ( That's XBee.at(command="MY") through the python-xbee module right? ) I don't get a response at all - Michaël van D.
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). - tomlogic

1 Answers

0
votes

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).