-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlora.py
557 lines (456 loc) · 20.2 KB
/
lora.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
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
import logging
import threading
import lora_constants
import sys
class LoRa:
"""LoRa class.
Manages the LoRa operations communicating with LoRa modules.
Attributes:
lock (threading.Lock): Semaphore to use LoRa object.
loraModule (str): LoRa module type.
serialPort (serial.Serial): Serial object.
base_band (str): LoRa base band.
lora_class (str): LoRa class.
tx_power (int): LoRa transmission power.
"""
def __init__(self, _loraModule, _serialPortObj):
"""LoRa class constructor.
Parameters:
_loraModule (str): LoRa module type.
_serialPortObj (serial.Serial): Serial object.
Returns:
None
"""
## Semaphore to use LoRa object
self.lock = threading.Lock()
## LoRa module type
self.loraModule = _loraModule
## Serial object
self.serialPort = _serialPortObj
# # Get and check LoRa base band
# self.base_band = _loraCfg.get("base_band")
# if self.base_band not in lora_constants.lora_baseband_ref:
# logging.error("Base band parameter in LoRa configuration file is not valid")
# sys.exit("\tERROR: Base band parameter in LoRa configuration file is not valid!!!")
# # Get and check LoRa class
# self.lora_class = _loraCfg.get("lora_class")
# if self.lora_class not in lora_constants.lora_class_ref:
# logging.error("Class parameter in LoRa configuration file is not valid")
# sys.exit("\tERROR: Class parameter in LoRa configuration file is not valid!!!")
# # Get and check LoRa transmission power
# self.tx_power = _loraCfg.get("tx_power")
# if self.base_band == "EU434" or self.base_band == "EU868":
# if self.tx_power not in lora_constants.lora_tx_power_868_ref:
# logging.error("Transmission power parameter in LoRa configuration file is not valid")
# sys.exit("\tERROR: Transmission power parameter in LoRa configuration file is not valid!!!")
# elif self.base_band == "US915" or self.base_band == "AU920":
# if self.tx_power not in lora_constants.lora_tx_power_915_ref:
# logging.error("Transmission power parameter in LoRa configuration file is not valid")
# sys.exit("\tERROR: Transmission power parameter in LoRa configuration file is not valid!!!")
# # Get and check ADR (Automatic Data Rate)
# self.adr = _loraCfg.get("adr")
# if self.adr not in lora_constants.lora_adr_ref:
# logging.error("ADR parameter in LoRa configuration file is not valid")
# sys.exit("\tERROR: ADR parameter in LoRa configuration file is not valid!!!")
# # Get and check Authentication Mode
# self.auth_mode = _loraCfg.get("auth_mode")
# if self.auth_mode not in lora_constants.lora_auth_mode_ref:
# logging.error("Authentication mode parameter in LoRa configuration file is not valid")
# sys.exit("\tERROR: Authentication mode parameter in LoRa configuration file is not valid!!!")
# self.repeat = _loraCfg.get("repeat")
# self.retry = _loraCfg.get("retry")
def checkLoRa(self):
"""Check if LoRa module is connected and ready to use.
Parameters:
None
Returns:
bool: True - if successfuly / False - if failed
"""
self.serialPort.write(b"AT\n")
response = self.serialPort.readline().decode("UTF-8")
if "OK" in response:
self.serialPort.write(b"AT+VER\n")
response = self.serialPort.readline().decode("UTF-8")
response = response.split(": ")
response = response[1]
response = response[0:len(response)-2]
logging.info("Connected to %s LoRa module with firmware version %s", self.loraModule, response)
return True
else:
logging.error("LoRa module is not connected")
return False
def getLoRaMaxPayload(self, loraBaseBand, loraUPDatarate):
"""Get the maximum payload size based on base band and uplink data rate.
Parameters:
loraBaseBand (str): LoRa base band.
loraUPDatarate (str): Uplink data rate.
Returns:
int: Maximum payload size.
"""
max_payload = 0
if loraBaseBand == "EU434" or loraBaseBand == "EU868":
if loraUPDatarate not in lora_constants.lora_dr_868_ref:
logging.error("Uplink data rate parameter in LoRa configuration file is not valid")
sys.exit("\tERROR: Uplink data rate parameter in LoRa configuration file is not valid!!!")
if (loraUPDatarate == "DR0" or loraUPDatarate == "DR1" or loraUPDatarate == "DR2"):
max_payload = 51
elif (loraUPDatarate == "DR3"):
max_payload = 115
if (loraUPDatarate == "DR4" or loraUPDatarate == "DR5" or loraUPDatarate == "DR6" or loraUPDatarate == "DR7"):
max_payload = 242
if loraBaseBand == "US915" or loraBaseBand == "AU915" or loraBaseBand == "AU920":
if loraUPDatarate not in lora_constants.lora_dr_915_ref:
logging.error("Uplink data rate parameter in LoRa configuration file is not valid")
sys.exit("\tERROR: Uplink data rate parameter in LoRa configuration file is not valid!!!")
if (loraUPDatarate == "DR0"):
max_payload = 11
elif (loraUPDatarate == "DR1"):
max_payload = 53
elif (loraUPDatarate == "DR2"):
max_payload = 126
elif (loraUPDatarate == "DR3" or loraUPDatarate == "DR4"):
max_payload = 242
return max_payload
# # Setup LoRa channel 0
# logging.debug("Setting LoRa channel 0 to %s and %s", self.chan0_freq, self.chan0_dr)
# cmd = "AT+CH=0,"+str(self.chan0_freq)+","+self.chan0_dr+"\n"
# self.serialPort.write(cmd.encode("UTF-8"))
# response = self.serialPort.readline().decode("UTF-8")
# if self.chan0_dr in response:
# pass
# else:
# logging.warning("Channel 0 at LoRa module not configured")
# # Setup LoRa channel 1
# logging.debug("Setting LoRa channel 1 to %s and %s", self.chan1_freq, self.chan1_dr)
# cmd = "AT+CH=1,"+str(self.chan1_freq)+","+self.chan1_dr+"\n"
# self.serialPort.write(cmd.encode("UTF-8"))
# response = self.serialPort.readline().decode("UTF-8")
# if self.chan1_dr in response:
# pass
# else:
# logging.warning("Channel 1 at LoRa module not configured")
def setLoRaRX1Win(self):
"""Enable RX window 1."""
logging.debug("Enabling LoRa receive window 1")
cmd = "AT+RXWIN1=ON\n"
self.serialPort.write(cmd.encode("UTF-8"))
response = self.serialPort.readline().decode("UTF-8")
if "RXWIN1: ON" in response:
pass
else:
logging.warning("RX window 1 at LoRa module not enabled")
def setLoRaRX2Win(self, loraRX2Frequency, loraRX2Datarate):
"""Sets the RX window 2 parameters.
Parameters:
loraRX2Frequency (int): RX window 2 frequency.
loraRX2Datarate (str): RX window 2 data rate.
Returns:
None
"""
logging.debug("Setting LoRa receive window 2 to %s and %s", loraRX2Frequency, loraRX2Datarate)
cmd = "AT+RXWIN2="+str(loraRX2Frequency)+","+loraRX2Datarate+"\n"
self.serialPort.write(cmd.encode("UTF-8"))
response = self.serialPort.readline().decode("UTF-8")
if loraRX2Datarate in response:
pass
else:
logging.warning("Response window 2 at LoRa module not configured")
def setLoRaAuthMode(self, loraAuthMode):
"""Sets the authentication mode.
Parameters:
loraAuthMode (str): authentication mode
Returns:
None
"""
logging.debug("Setting LoRa authentication mode to %s", loraAuthMode)
cmd = "AT+MODE="+loraAuthMode+"\n"
self.serialPort.write(cmd.encode("UTF-8"))
response = self.serialPort.readline().decode("UTF-8")
if loraAuthMode in response:
pass
else:
logging.warning("Authentication mode at LoRa module not configured")
def setLoRaADR(self, loraADR):
"""Enable or disable ADR (Automatic Data Rate).
Parameters:
loraADR (str): enable (ON) or disable (OFF)
Returns:
None
"""
logging.debug("Setting LoRa ADR to %s", loraADR)
cmd = "AT+ADR="+loraADR+"\n"
self.serialPort.write(cmd.encode("UTF-8"))
response = self.serialPort.readline().decode("UTF-8")
if loraADR in response:
pass
else:
logging.warning("ADR at LoRa module not configured")
def setLoRaUPDatarate(self, loraUPDatarate):
"""Sets the uplink data rate.
Parameters:
loraUPDatarate (str): uplink data rate
Returns:
None
"""
logging.debug("Setting LoRa uplink data rate to %s", loraUPDatarate)
cmd = "AT+DR="+loraUPDatarate+"\n"
self.serialPort.write(cmd.encode("UTF-8"))
response = self.serialPort.readline().decode("UTF-8")
if loraUPDatarate in response:
pass
else:
logging.warning("Uplink data rate at LoRa module not configured")
def setLoRaTXPower(self, loraTXPower):
"""Sets the transmission power (in dBm).
Parameters:
loraTXPower (int): transmission power (in dBm)
Returns:
None
"""
logging.debug("Setting LoRa transmission power to %s dBm", loraTXPower)
cmd = "AT+POWER="+str(loraTXPower)+"\n"
self.serialPort.write(cmd.encode("UTF-8"))
response = self.serialPort.readline().decode("UTF-8")
if str(loraTXPower) in response:
pass
else:
logging.warning("Transmission power at LoRa module not configured")
def setLoRaClass(self, loraClass):
"""Sets the LoRa class.
Parameters:
loraClass (str): LoRa class
Returns:
None
"""
logging.debug("Setting LoRa class to %s", loraClass)
cmd = "AT+CLASS="+loraClass+"\n"
self.serialPort.write(cmd.encode("UTF-8"))
response = self.serialPort.readline().decode("UTF-8")
if loraClass in response:
pass
else:
logging.warning("Class at LoRa module not configured")
def setLoRaSubBand(self, loraSubBand):
"""Sets the sub-band. This will disable all channels not belonging to the specified sub-band.
Parameters:
loraSubBand (int): sub-band
Returns:
None
"""
if (self.loraModule == "RHF76-052"):
if loraSubBand == 1:
for ch in range(8, 72):
logging.debug("Disabling LoRa channel %s", ch)
cmd = "AT+CH="+str(ch)+", 0\n"
self.serialPort.write(cmd.encode("UTF-8"))
response = self.serialPort.readline().decode("UTF-8")
if str(ch) in response:
pass
else:
logging.warning("Failed disabling LoRa channel %s", ch)
elif loraSubBand == 2:
for ch in range(0, 8):
logging.debug("Disabling LoRa channel %s", ch)
cmd = "AT+CH="+str(ch)+", 0\n"
self.serialPort.write(cmd.encode("UTF-8"))
response = self.serialPort.readline().decode("UTF-8")
if str(ch) in response:
pass
else:
logging.warning("Failed disabling LoRa channel %s", ch)
for ch in range(16, 65):
logging.debug("Disabling LoRa channel %s", ch)
cmd = "AT+CH="+str(ch)+", 0\n"
self.serialPort.write(cmd.encode("UTF-8"))
response = self.serialPort.readline().decode("UTF-8")
if str(ch) in response:
pass
else:
logging.warning("Failed disabling LoRa channel %s", ch)
for ch in range(66, 72):
logging.debug("Disabling LoRa channel %s", ch)
cmd = "AT+CH="+str(ch)+", 0\n"
self.serialPort.write(cmd.encode("UTF-8"))
response = self.serialPort.readline().decode("UTF-8")
if str(ch) in response:
pass
else:
logging.warning("Failed disabling LoRa channel %s", ch)
# elif (self.loraModule == "RHF03M003"):
def setLoRaBaseBand(self, loraBaseBand):
"""Sets the base-band.
Parameters:
loraBaseBand (str): base-band
Returns:
None
"""
logging.debug("Setting LoRa base band to %s", loraBaseBand)
cmd = "AT+DR="+loraBaseBand+"\n"
self.serialPort.write(cmd.encode("UTF-8"))
response = self.serialPort.readline().decode("UTF-8")
if loraBaseBand in response:
pass
else:
logging.warning("Base band at LoRa module not configured")
def setDevEUI(self, devEUI):
"""Sets the device EUI.
Parameters:
devEUI (str): device EUI
Returns:
None
"""
logging.debug("Setting TTN device EUI to %s", devEUI)
cmd = "AT+ID=DevEui,"+devEUI+"\n"
self.serialPort.write(cmd.encode("UTF-8"))
response = self.serialPort.readline().decode("UTF-8")
if "DevEui," in response:
pass
else:
logging.warning("Device EUI at LoRa module not configured")
def setAppEUI(self, appEUI):
"""Sets the application EUI.
Parameters:
appEUI (str): application EUI
Returns:
None
"""
logging.debug("Setting TTN application EUI to %s", appEUI)
cmd = "AT+ID=AppEui,"+appEUI+"\n"
self.serialPort.write(cmd.encode("UTF-8"))
response = self.serialPort.readline().decode("UTF-8")
if "AppEui," in response:
pass
else:
logging.warning("Application EUI at LoRa module not configured")
def setDevAddr(self, devAddr):
"""Sets the device address.
Parameters:
devAddr (str): device address
Returns:
None
"""
logging.debug("Setting TTN device address to %s", devAddr)
cmd = "AT+ID=DevAddr,"+devAddr+"\n"
self.serialPort.write(cmd.encode("UTF-8"))
response = self.serialPort.readline().decode("UTF-8")
if "DevAddr," in response:
pass
else:
logging.warning("Device address at LoRa module not configured")
def setNwkSKey(self, nwkSKey):
"""Sets the network session key.
Parameters:
nwkSKey network session
Returns:
None
"""
logging.debug("Setting TTN network session key to %s", nwkSKey)
cmd = "AT+KEY=NwkSKey,"+nwkSKey+"\n"
self.serialPort.write(cmd.encode("UTF-8"))
response = self.serialPort.readline().decode("UTF-8")
if "NWKSKEY" in response:
pass
else:
logging.warning("Network session key at LoRa module not configured")
def setAppSKey(self, appSKey):
"""Sets the application session key.
Parameters:
appSKey application session key.
Returns:
None
"""
logging.debug("Setting TTN application session key to %s", appSKey)
cmd = "AT+KEY=AppSKey,"+appSKey+"\n"
self.serialPort.write(cmd.encode("UTF-8"))
response = self.serialPort.readline().decode("UTF-8")
if "APPSKEY" in response:
pass
else:
logging.warning("Application session key at LoRa module not configured")
def setAppKey(self, appKey):
"""Sets the application key.
Parameters:
appKey application key.
Returns:
None
"""
logging.debug("Setting TTN application key to %s", appKey)
cmd = "AT+KEY=AppKey,"+appKey+"\n"
self.serialPort.write(cmd.encode("UTF-8"))
response = self.serialPort.readline().decode("UTF-8")
if "AppKey," in response:
pass
else:
logging.warning("Application key at LoRa module not configured")
def sendJoinRequest(self):
"""Sends a join request.
Parameters:
None
Returns:
bool: True - if successful / False - if failed
"""
logging.debug("Sending Lora JOIN request")
cmd = "AT+Join\n"
self.serialPort.write(cmd.encode("UTF-8"))
response = self.serialPort.readline().decode("UTF-8")
print(response)
while True:
response = self.serialPort.readline().decode("UTF-8")
print(response)
if "failed" in response:
logging.warning("Join failed")
return False
elif "DevAddr" in response:
logging.info("Network joined")
return True
elif "Joined" in response:
logging.info("Joined already")
return True
def sendNoAckMsgHex(self, _loraPort, _loraMsg):
"""Sends unconfirmed hexadecimal messages.
Parameters:
_loraPort port
_loraMsg hexadecimal message
Returns:
None
"""
logging.debug("Setting Lora port to %s", _loraPort)
cmd = "AT+PORT="+str(_loraPort)+"\n"
self.serialPort.write(cmd.encode("UTF-8"))
response = self.serialPort.readline().decode("UTF-8")
if "PORT" in response:
pass
else:
logging.warning("LoRa port at LoRa module not configured")
logging.info("Sending no ack hexadecimal message: %s", _loraMsg)
cmd = "AT+MSGHEX=\""+_loraMsg+"\"\n"
self.serialPort.write(cmd.encode("UTF-8"))
while True:
response = self.serialPort.readline().decode("UTF-8")
# print(response)
if "Done" in response:
break
def sendAckMsgHex(self, _loraPort, _loraMsg):
"""Sends confirmed hexadecimal messages.
Parameters:
_loraPort port
_loraMsg hexadecimal message
Returns:
None
"""
logging.debug("Setting Lora port to %s", _loraPort)
cmd = "AT+PORT="+str(_loraPort)+"\n"
self.serialPort.write(cmd.encode("UTF-8"))
response = self.serialPort.readline().decode("UTF-8")
if "PORT" in response:
pass
else:
logging.warning("LoRa port at LoRa module not configured")
logging.info("Sending ack hexadecimal message: %s", _loraMsg)
cmd = "AT+CMSGHEX=\""+_loraMsg+"\"\n"
self.serialPort.write(cmd.encode("UTF-8"))
while True:
response = self.serialPort.readline().decode("UTF-8")
# print(response)
if "Done" in response:
break