-
Notifications
You must be signed in to change notification settings - Fork 4
/
xbee_listen.py
74 lines (52 loc) · 1.91 KB
/
xbee_listen.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import time
import serial
import datetime
import sqlite3
from xbee import XBee
import platform
PORT = ''
if platform.machine() == 'armv6l':
PORT = '/dev/ttyAMA0' #set tty port BEAGLE BONE NOTE: O1
else:
if platform.system() == 'Linux':
PORT = '/dev/ttyUSB0'
if platform.system() == 'Darwin':
PORT = '/dev/tty.usbserial-FTF0FD46' #OSX
BAUD_RATE = 9600 #set baud rate
serial_port = serial.Serial(PORT, BAUD_RATE)
conn = sqlite3.connect('xbee_rssi.db', detect_types=sqlite3.PARSE_DECLTYPES)
with conn:
cur = conn.cursor()
cur.execute("DROP TABLE IF EXISTS signal")
print 'Setting up Database...'
# Create table
cur.execute('''CREATE TABLE signal
(date TIMESTAMP, xbee INTEGER, rssi INTEGER)''')
xbee = XBee(serial_port, escaped=True) #asynchronous calling to
def dump(data): #define callback function
conn = sqlite3.connect('xbee_rssi.db', detect_types=sqlite3.PARSE_DECLTYPES)
cur = conn.cursor()
if 'source_addr' in data:
addr = data['source_addr']
rssi_val = int(data['rssi'].encode('hex'),16)
addr_int = int(addr.encode('hex'),16)
rf_data = "got the message (%s)" % data['rf_data']
fid = data['options']
# now = datetime.datetime.now().strftime('%Y-%m-%d_%H:%M:%S')
now = datetime.datetime.now()
print rssi_val
cur.execute("INSERT INTO signal VALUES(?, ?, ?)", (now,addr_int,-1*rssi_val))
# Save (commit) the changes
conn.commit()
xbee.send('tx', frame_id=fid, dest_addr=addr, data=rf_data)
conn.close()
serial_port = serial.Serial(PORT, BAUD_RATE)
xbee = XBee(serial_port, callback=dump, escaped=True) #asynchronous calling to
# loop forever
while True:
try:
time.sleep(0.5)
except KeyboardInterrupt:
break
xbee.halt()
serial_port.close()