Reputation: 105
I have raspberry pi connected with xbees and a motion sensor, receiving data from the motion sensor connected to one of the xbee. It would then send the data to my raspberry pi. Is there any way I could manipulate or split the output such as the status being only True/False and the address =\x00\x13\xa2\x00@\xbbJ as I wanted to store the address into database if status=="True".
So if I do
if status[0]['dio-0'] == True :
print "Yes"
cur = con.cursor()
cur.execute("INSERT ignore into sensor(sensor_id, status) VALUES(%s,True)",(add[0]))
con.commit()
But the address stored into the database is weird characters instead of \x00\x13\xa2\x00@\xbbJ. or should I do any other ways?
This is the 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 and 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 this is the output.
[{'dio-0': True}]
['\x00\x13\xa2\x00@\xbbJ}', [{'dio-0': True}]]
[{'dio-0': False}]
['\x00\x13\xa2\x00@\xbbJ}', [{'dio-0': False}]]
In the database
+------------+--------+
| sensor_id | status |
+------------+--------+
| ¢ @»J} | 1 |
+------------+--------+
Upvotes: 1
Views: 506
Reputation: 11694
The variable sensor_id
is an array of bytes, and it sounds like you want to store it in a human-readable format.
One way is to convert it to a formatted string before storing it in the database.
sensor_id = ':'.join("%02X" % ord(b) for b in add)
That statement loops through the bytes in the address (for b in add
), formats each as a two-character hex string ("%02X" % ord(b)
), and then joins each of those strings together with a colon in between (':'.join()
).
Upvotes: 1