-
Notifications
You must be signed in to change notification settings - Fork 1
/
xbeeControl.py
79 lines (64 loc) · 2.72 KB
/
xbeeControl.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
75
76
77
78
79
#!/usr/bin/env python
# XBee Control for Raspberry Pi with Series S2C (XB24CZ7WIT-004)
# https://www.adafruit.com/product/968
#-------------------------------------------------------------------------------
#### Imports ####
from xbee import ZigBee
from serial import Serial
#### Constants ####
#-------------------------------------------------------------------------------
PORT = '/dev/ttyUSB0'
BAUD_RATE = 9600
# Open serial port
ser = Serial(PORT, BAUD_RATE, timeout = 1)
# Create API object
xbee = ZigBee(ser, escaped=True)
#### Objects ####
#-------------------------------------------------------------------------------
class XbeeControl(object):
# Make sure there is only one instance of XbeeControl
_instances=[]
# Initialize the object
def __init__(self):
if ( len(self._instances)>1 ):
print("ERROR: One instance of XbeeControl is running already.")
exit(1)
self._instances.append(self)
#-------------------------------------------------------------------------------
# Close serial port
def cleanup(self):
ser.close()
#-------------------------------------------------------------------------------
# Send packets
def send(self, packet_data, mode):
'''
Parameter Description Default
--------------------------------------------------------------
id Frame Type 0x10
frame_id Frame ID 0x01
dest_addr 16 bit destination addr FF FE
dest_addr_long 64 bit destination addr None
data RF data in packet None
'''
# Router --> Coordinator (A Router node is running this program)
if mode == 'R':
xbee.send('tx', dest_addr_long = '\x00\x00\x00\x00\x00\x00\x00\x00', data = packet_data)
# Coordinator --> Router (Coordinator is running this program)
elif mode == 'C':
xbee.send('tx', data = packet_data)
else:
print("ERROR: Invalid XBee configuration mode. Set 'C' for Coordinator or 'R' for Router XBee.")
exit(1)
# Receive packets
def receive(self):
'''
Parameter Description
--------------------------------------------------------------
id Frame Type
source_addr 16 bit source addr
source_addr_long 64 bit source addr
rf_data RF data in packet
'''
frame = xbee.wait_read_frame()
clean_data = frame['rf_data'].decode("utf-8")
return clean_data