mysql - Storing xbee source addr into database in Python -
i have raspberry pi connected xbees , motion sensor, receiving data motion sensor connected 1 of xbee. send data raspberry pi. there way manipulate or split output such status being true/false , address =\x00\x13\xa2\x00@\xbbj wanted store address database if status=="true".
so if
if status[0]['dio-0'] == true : print "yes" cur = con.cursor() cur.execute("insert ignore sensor(sensor_id, status) values(%s,true)",(add[0])) con.commit()
but address stored database weird characters instead of \x00\x13\xa2\x00@\xbbj. or should other ways?
this codes.
from xbee import xbee import serial port = '/dev/ttyusbxbee' baud_rate = 9600 # open serial port ser = serial.serial(port, baud_rate) # create api object xbee = xbee(ser) def decodereceivedframe(response): add = str(response['source_addr_long']) status = response['samples'] return [add, status] # continuously read , print packets while true: try: response = xbee.wait_read_frame() decodeddata = decodereceivedframe(response) status = decodereceivedframe(response)[1] print status print decodeddata add= decodeddata[0] except keyboardinterrupt: break ser.close()
and output.
[{'dio-0': true}] ['\x00\x13\xa2\x00@\xbbj}', [{'dio-0': true}]] [{'dio-0': false}] ['\x00\x13\xa2\x00@\xbbj}', [{'dio-0': false}]]
in database
+------------+--------+ | sensor_id | status | +------------+--------+ | ¢ @»j} | 1 | +------------+--------+
the variable sensor_id
array of bytes, , sounds want store in human-readable format.
one way convert formatted string before storing in database.
sensor_id = ':'.join("%02x" % ord(b) b in add)
that statement loops through bytes in address (for b in add
), formats each two-character hex string ("%02x" % ord(b)
), , joins each of strings colon in between (':'.join()
).
Comments
Post a Comment