forked from mayeranalytics/pySX127x
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpi_lora_monitoring.py
executable file
·188 lines (151 loc) · 5.42 KB
/
rpi_lora_monitoring.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
#!/usr/bin/env python2.7
""" A simple continuous receiver class. """
# Copyright 2015 Mayer Analytics Ltd.
#
# This file is part of pySX127x.
#
# pySX127x is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public
# License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# pySX127x is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You can be released from the requirements of the license by obtaining a commercial license. Such a license is
# mandatory as soon as you develop commercial activities involving pySX127x without disclosing the source code of your
# own applications, or shipping pySX127x with a closed source product.
#
# You should have received a copy of the GNU General Public License along with pySX127. If not, see
# <http://www.gnu.org/licenses/>.
from time import sleep
from SX127x.LoRa import *
from SX127x.LoRaArgumentParser import LoRaArgumentParser
from SX127x.board_config import BOARD
import struct
BOARD.setup()
parser = LoRaArgumentParser("Continous LoRa receiver.")
class LoRaRcvCont(LoRa):
def __init__(self, verbose=False):
super(LoRaRcvCont, self).__init__(verbose)
self.set_mode(MODE.SLEEP)
self.set_dio_mapping([0] * 6)
def print_buffer(self, buffer):
text = []
for c in buffer:
text.append(chr(c))
print(text)
def get_rpi_temp(self):
f = open("/sys/class/thermal/thermal_zone0/temp", "r")
line = int(f.read())
temp = int(line) / 1000.0
byte = struct.pack("!b", temp)
return byte
def on_rx_done(self):
BOARD.led_on()
print("\nRxDone")
print(self.get_irq_flags())
#print(map(hex, self.read_payload(nocheck=True)))
_buff = self.read_payload(nocheck=True)
print("received:")
self.print_buffer(_buff)
# send pOnG + 2 bytes
t = ord(self.get_rpi_temp())
_reply = [112, 79, 110, 71, 0, t]
#_buff = [0x70, 73]
print("to be replied:")
self.print_buffer(_reply)
self.set_mode(MODE.SLEEP)
self.set_dio_mapping([1,0,0,0,0,0])
self.set_payload_length(6)
ret = self.write_payload(_reply)
print ("returned by SPI:")
self.print_buffer(ret)
self.set_mode(MODE.TX)
print("reply sent")
#sleep(0.5)
#self.set_mode(MODE.SLEEP)
#self.reset_ptr_rx()
#BOARD.led_off()
#self.set_mode(MODE.RXCONT)
def on_tx_done(self):
print("\nTxDone")
print(self.get_irq_flags())
self.set_mode(MODE.SLEEP)
self.reset_ptr_rx()
BOARD.led_off()
self.set_dio_mapping([0] * 6)
self.set_mode(MODE.RXCONT)
def on_cad_done(self):
print("\non_CadDone")
print(self.get_irq_flags())
self.clear_irq_flags()
print(self.get_irq_flags())
self.set_mode(MODE.SLEEP)
rssi_value = self.get_rssi_value()
status = self.get_modem_status()
sys.stdout.flush()
sys.stdout.write("\r%d %d %d %s" % (rssi_value, status['rx_ongoing'], status['modem_clear'], self))
self.reset_ptr_rx()
BOARD.led_off()
self.set_dio_mapping([0] * 6)
self.set_mode(MODE.RXCONT)
print ("listo, saliendo de cad ===========")
def on_rx_timeout(self):
print("\non_RxTimeout")
print(self.get_irq_flags())
self.set_dio_mapping([0] * 6)
self.set_mode(MODE.RXCONT)
def on_valid_header(self):
print("\non_ValidHeader")
print(self.get_irq_flags())
def on_payload_crc_error(self):
print("\non_PayloadCrcError")
print(self.get_irq_flags())
def on_fhss_change_channel(self):
print("\non_FhssChangeChannel")
print(self.get_irq_flags())
def start(self):
self.reset_ptr_rx()
self.set_mode(MODE.RXCONT)
i = 0
while True:
sleep(.5)
rssi_value = self.get_rssi_value()
status = self.get_modem_status()
sys.stdout.flush()
sys.stdout.write("\r%d %d %d" % (rssi_value, status['rx_ongoing'], status['modem_clear']))
i = i + 1
#if(i % 20 == 0):
# self.set_mode(MODE.SLEEP)
# sys.stdout.flush()
# sys.stdout.write("\r%d %d %d %s" % (rssi_value, status['rx_ongoing'], status['modem_clear'], self))
# self.set_mode(MODE.RXCONT)
lora = LoRaRcvCont(verbose=False)
args = parser.parse_args(lora)
lora.set_mode(MODE.STDBY)
lora.set_pa_config(pa_select=1)
#lora.set_rx_crc(True)
#lora.set_coding_rate(CODING_RATE.CR4_6)
#lora.set_pa_config(max_power=0, output_power=0)
#lora.set_lna_gain(GAIN.G1)
#lora.set_implicit_header_mode(False)
#lora.set_low_data_rate_optim(True)
#lora.set_pa_ramp(PA_RAMP.RAMP_50_us)
#lora.set_agc_auto_on(True)
print(lora)
assert(lora.get_agc_auto_on() == 1)
try: input("Press enter to start...")
except: pass
try:
lora.start()
except KeyboardInterrupt:
sys.stdout.flush()
print("")
sys.stderr.write("KeyboardInterrupt\n")
finally:
sys.stdout.flush()
print("")
lora.set_mode(MODE.SLEEP)
print(lora)
BOARD.teardown()