-
Notifications
You must be signed in to change notification settings - Fork 1
/
xbee_listen.py
75 lines (54 loc) · 2.07 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
#! /usr/bin/python
"""
xbee_listen.py
By Mike Furlotti, Damien Clark, Noah Zaitlen, and Benajmin Zaitlen
This example reads the Serial Port (UART 1) on BeagleBone connected to an XBee. The RF Packet is the processed
using the Python-XBee API found at http://code.google.com/p/python-xbee/
"""
import os
import serial
import time
from xbee import ZigBee
PORT = '/dev/ttyO1' #set tty port NOTE: ON BEAGLE BONE O1 is the Letter O
BAUD_RATE = 9600 #set baud rate
uart1_pin_mux = [
('uart1_rxd', ( 0 | (1<<5) )), # Bit 5 sets the receiver to enabled for RX Pin
('uart1_txd', ( 0 )), #no bits to be set for TX Pin
]
for (fname, mode) in uart1_pin_mux:
with open(os.path.join('/sys/kernel/debug/omap_mux', \
fname), 'wb') as f:
f.write("%X" % mode)
#callbacks and threads
def dump(data):
print data
def dump_back(data): #define callback function
if 'source_addr' in data:
#get packet data
rf_data = "got the message (%s)" % data['rf_data']
print rf_data, " from ", str(data['source_addr_long'].encode("hex"))
#collect packet info for return message
addr = data['source_addr']
addr_long = data['source_addr_long']
fid = data['options']
tx_data = rf_data #send same message back
xbee.send('tx', frame_id=fid, dest_addr=addr, dest_addr_long=addr_long, data=tx_data)
else:
print data #print raw_data
serial_port = serial.Serial(PORT, BAUD_RATE)
xbee = ZigBee(serial_port, callback=dump_back, escaped=True) #asynchronous calling to process XBee Data
#Send AT commands
xbee.send("at", frame_id='a', command='DH') #Destination High
xbee.send("at", frame_id='a', command='DL') #Destination Low
xbee.send("at", frame_id='a', command='NI') #Node Identifier
xbee.send("at", frame_id='a', command='MY') #Source 16-Bit Address
print "------------------------------------------------------------------"
xbee.send("at", frame_id='a', command='ND') #Node Discovery
# loop forever
while True:
try:
time.sleep(0.01)
except KeyboardInterrupt:
break
xbee.halt()
serial_port.close()