-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlora.py
executable file
·159 lines (134 loc) · 5.67 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
#!/usr/bin/env python3
from SX127x.LoRa import *
import time
import config
from SX127x.board_config import BOARD
import leds
import mqtt_i
import json
BOARD.setup()
BOARD.reset()
class mylora(LoRa):
def publish_lora_sensors(self, pl, a='lora', n='device'):
if a in config.config:
s = config.config[a]
try:
plj = json.loads(pl.lower())
except ValueError as error:
# print("invalid json: %s" % error, '\r')
return False
for ss in range(0, len(s)):
full_dev_name = config.device_name
if n in s[ss]:
full_dev_name += '_' + a + '_' + s[ss][n]
else:
full_dev_name += '_' + mqtt_i.mqtt["built_in"]
if 'sensors' in s[ss]:
mqtt_i.mqtt_connect()
sensors = s[ss]["sensors"]
for sss in range(0, len(sensors)):
sensor = sensors[sss]["sensor"]
r = ' Reading: '
if s[ss][n]:
r += s[ss][n] + ', '
r += sensor + ' '
print(r.center(50, '*'), '\r')
mc = sensors[sss]["monitored_conditions"]
if mc:
leds.greenLed()
for c in mc:
if plj[n] == s[ss][n] and sensor.lower(
) in plj:
print(c.ljust(15, '.'), end=': ')
pl = str(plj[sensor.lower()][c])
print((pl + config.general[c]
["unit_of_measurement"]).rjust(10),
end='')
state_topic = full_dev_name.replace(
" ", "_")
state_topic += '/' + mqtt_i.mqtt[
"sensor"] + '/'
state_topic += sensor + "_" + c
state_topic += '/' + mqtt_i.mqtt[
"state"]
state_topic = state_topic.lower()
config.printAllowed = False
print(', Publishing', end='')
print(' to state_topic:',
state_topic,
end='')
try:
mqtt_i.mqttc.publish(state_topic, pl)
print(', done!', end='')
except Exception as e:
print(
', error {0} publishing!'.format(
e), '\r')
time.sleep(
mqtt_i.mqtt["mqtt_delay"])
print('\r')
else:
leds.redLed()
print('no data!', '\r')
else:
leds.redLed()
print('No value from sensor!', '\r')
else:
leds.redLed()
print('No sensors!', '\r')
mqtt_i.mqtt_disconnect()
config.printAllowed = True
def __init__(self, verbose=False):
super(mylora, self).__init__(verbose)
self.set_mode(MODE.SLEEP)
self.set_dio_mapping([0] * 6)
def on_rx_done(self):
self.clear_irq_flags(RxDone=1)
payload = self.read_payload(nocheck=True)
pl = bytes(payload).decode("utf-8", 'ignore')
print("Received:", pl, '\r') # Receive DATA
time.sleep(1) # Wait for the client be ready
self.publish_lora_sensors(pl)
pl = [255, 255, 0, 0, 65, 67, 75, 0]
print("Sending: ACK\r")
self.write_payload(pl) # Send ACK
self.set_mode(MODE.TX)
self.reset_ptr_rx()
self.set_mode(MODE.RXCONT) # Receiver mode
def on_tx_done(self):
print("TxDone", self.get_irq_flags())
def on_cad_done(self):
print("on_CadDone, ", self.get_irq_flags())
def on_rx_timeout(self):
print("on_RxTimeout, ", self.get_irq_flags())
def on_valid_header(self):
print("on_ValidHeader, ", self.get_irq_flags())
def on_payload_crc_error(self):
print("on_PayloadCrcError, ", self.get_irq_flags())
def on_fhss_change_channel(self):
print("on_FhssChangeChannel, ", self.get_irq_flags())
def send(self, pl):
b_pl = []
b_pl.extend(map(ord, pl))
print("Sending payload:", pl)
self.write_payload(b_pl) # Send INF
self.set_mode(MODE.TX)
time.sleep(0.5)
self.reset_ptr_rx()
self.set_mode(MODE.RXCONT) # Receiver mode
def start(self):
self.reset_ptr_rx()
self.set_mode(MODE.RXCONT) # Receiver mode
lora = mylora(verbose=False)
lora.set_pa_config(pa_select=1, max_power=21, output_power=15)
lora.set_bw(BW.BW125)
lora.set_coding_rate(CODING_RATE.CR4_7)
lora.set_spreading_factor(9)
lora.set_rx_crc(True)
assert (lora.get_agc_auto_on() == 1)
lora.start()
if __name__ == "__main__":
leds.config_leds()
lora.publish_lora_sensors(
'{"Count":13,"device":"node_mcu_25794f","DHT22":{"Temperature":15.150,"Humidity":66.66}}'
)