-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoordinator.py
More file actions
245 lines (189 loc) · 7.4 KB
/
Copy pathcoordinator.py
File metadata and controls
245 lines (189 loc) · 7.4 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#!/usr/bin/python3
"""
2018-04-10 18:36
"""
import os
import time
import json
import logging
import struct
from digi.xbee.devices import XBeeDevice, RemoteXBeeDevice, XBee64BitAddress
from digi.xbee.exception import TimeoutException
PORT = '/dev/ttyUSB0'
PRIORITY_FILE = 'priority.txt'
DATA_FORMAT = {
'sequence': ('temperature', 'humidity', 'soil_moisture', 'light'),
'temperature': 4,
'humidity': 4,
'soil_moisture': 4,
'light': 4
}
THRESHOLDS = {
'0013A200410809DD': {
'soil_moisture': 800.0,
'light_up': 300.0,
'light_down': 50.0
},
'0013A200410809E3': {
'soil_moisture': 800.0,
'light_up': 300.0,
'light_down': 50.0
},
'0013A200410809D8': {
'soil_moisture': 800.0,
'light_up': 300.0,
'light_down': 50.0
}
}
PLANTS = {
'1': '0013A200410809DD',
'0013A200410809DD': '1',
'2': '0013A200410809E3',
'0013A200410809E3': '2',
'3': '0013A200410809D8',
'0013A200410809D8': '3'
}
PUMP_STATUS = {'0013A200410809DD': False, '0013A200410809E3': False, '0013A200410809D8': False}
LIGHT_STATUS = {'0013A200410809DD': False, '0013A200410809E3': False, '0013A200410809D8': False}
IRRIGATION_ADDRESS = '0013A200415D76BF'
LIGHT_ADDRESS = '0013A200415B8CBA'
def setup_logger(level=logging.DEBUG):
"""
Setup logger.
`return`: logger
"""
logger = logging.getLogger(__name__)
logger.setLevel(level)
handler = logging.StreamHandler()
formatter = logging.Formatter('%(asctime)s %(name)s %(levelname)s: %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
return logger
def write_to_json(address, data):
"""
Write data to json file.
------------------------
`address`: The remote devidebugce address.
`data`: dict
"""
logger = logging.getLogger(__name__)
logger.info("Data to be writen: %s", data)
json_data = None
with open('data.json', mode='r') as json_file:
try:
json_data = json.load(json_file)
except json.JSONDecodeError:
logger.warning("Cannot load json data.")
json_data = {}
with open('data.json', mode='w')as json_file:
if address in json_data:
json_data[address].append(data)
else:
json_data[address] = []
json_data[address].append(data)
json.dump(json_data, json_file)
def main():
"""
The procedure of detecting packet and process the message.
----------------------------------------------------------
"""
setup_logger(level=logging.INFO)
device = XBeeDevice(port=PORT, baud_rate=9600)
device.open()
logger = logging.getLogger(__name__)
light_on = False
light_system = RemoteXBeeDevice(device, XBee64BitAddress.from_hex_string(LIGHT_ADDRESS))
irrigation_system = RemoteXBeeDevice(device, XBee64BitAddress.from_hex_string(IRRIGATION_ADDRESS))
while True:
try:
message = device.read_data(timeout=60)
except TimeoutException:
logger.info("It seems the end device have not send any message for 1 minute.")
else:
message = message.to_dict()
address = message['Sender: ']
data = message['Data: ']
logger.info("Address: %s, Plant: %s", address, PLANTS[address])
logger.debug("Data: %s", data)
sensor_data = {}
for part in DATA_FORMAT['sequence']:
sensor_data[part] = round(struct.unpack('f', data[:DATA_FORMAT[part]])[0], 2)
# delete the value data
data = data[DATA_FORMAT[part]:]
sensor_data['timestamp'] = time.strftime('%Y-%m-%d %H:%M:%S')
write_to_json(address, sensor_data)
# ligth
light_message = bytearray.fromhex(address)
if sensor_data['light'] < THRESHOLDS[address]['light_down'] and not LIGHT_STATUS[address]:
light_message.append(1)
LIGHT_STATUS[address] = True
logger.info('Light will turn on.')
device.send_data_async(light_system, light_message)
if sensor_data['light'] > THRESHOLDS[address]['light_up'] and LIGHT_STATUS[address]:
light_message.append(0)
LIGHT_STATUS[address] = False
logger.info('Light will turn off.')
device.send_data_async(light_system, light_message)
# if True in LIGHT_STATUS.values() and not light_on:
# light_message.append(1)
# light_on = True
# logger.debug('Light will turn on.')
# device.send_data_async(light_system, light_message)
# if True not in LIGHT_STATUS.values() and light_on:
# light_message.append(0)
# light_on = False
# logger.debug('Light will turn off.')
# device.send_data_async(light_system, light_message)
# pump
irrigation_message = bytearray.fromhex(address)
if sensor_data['soil_moisture'] > THRESHOLDS[address]['soil_moisture'] and not PUMP_STATUS[address]:
irrigation_message.append(1)
PUMP_STATUS[address] = True
logger.info('Pump will turn on.')
device.send_data_async(irrigation_system, irrigation_message)
if sensor_data['soil_moisture'] <= THRESHOLDS[address]['soil_moisture'] and PUMP_STATUS[address]:
irrigation_message.append(0)
PUMP_STATUS[address] = False
logger.info('Pump will turn off.')
device.send_data_async(irrigation_system, irrigation_message)
#
# App message
#
try:
with open(PRIORITY_FILE, mode='r+') as f:
m = list(f.read())
if m and len(m) == 3:
f.truncate(0)
logger.info("App: %s", m)
address = PLANTS[m[0]]
priority_message = bytearray.fromhex(address)
priority_message.append(int(m[2]))
priority_message.append(1)
if m[1] == '0':
# irrigation
device.send_data_async(irrigation_system, priority_message)
logger.debug("App pump message: %s", priority_message)
if m[2] == '0':
PUMP_STATUS[address] = False
logger.info("App pump off.")
elif m[2] == '1':
PUMP_STATUS[address] = True
logger.info("App pump on.")
elif m[1] == '1':
# light
device.send_data_async(light_system, priority_message)
logger.debug("App light message: %s", priority_message)
if m[2] == '0':
LIGHT_STATUS[address] = False
logger.info("App light off.")
elif m[2] == '1':
LIGHT_STATUS[address] = True
logger.info("App light on.")
else:
f.truncate(0)
logger.warning("App message warning! (%s)", m)
except FileNotFoundError:
pass
device.close()
if __name__ == '__main__':
main()