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()
)
)
get_xbee_reply()? You might want to try running the XBee module in API mode, and making use of the python-xbee library. - tomlogicb'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.+++ATID\rand that's why the secondATIDfails? If you change thesend_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+++is sent it waits until theOKis received before it sendsATID, so it's not possible it sends+++ATID\r. When I try through API mode ( That'sXBee.at(command="MY")through the python-xbee module right? ) I don't get a response at all - Michaël van D.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