-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.py
278 lines (255 loc) · 9.46 KB
/
code.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
import binascii
import time
import adafruit_rfm9x
import board
import busio
import digitalio
import usb_cdc
from digitalio import DigitalInOut
from microcontroller import watchdog as w
from watchdog import WatchDogMode
# configure watchdog
w.timeout = 5
w.mode = WatchDogMode.RESET
w.feed()
# setup usb serial
serial = usb_cdc.data
# leds
sInLED = digitalio.DigitalInOut(board.GP17)
sInLED.direction = digitalio.Direction.OUTPUT
sInLED.value = True
sOutLED = digitalio.DigitalInOut(board.GP25)
sOutLED.direction = digitalio.Direction.OUTPUT
sOutLED.value = True
lOutLED = digitalio.DigitalInOut(board.GP24)
lOutLED.direction = digitalio.Direction.OUTPUT
lOutLED.value = True
lInLED = digitalio.DigitalInOut(board.GP13)
lInLED.direction = digitalio.Direction.OUTPUT
lInLED.value = True
def yellow(data):
return "\x1b[38;5;220m" + data + "\x1b[0m"
def red(data):
return "\x1b[1;5;31m" + data + "\x1b[0m"
# our version
print("RF.Guru\nLoRa868Stick 0.1")
def recvSerial():
if serial is not None:
if serial.connected:
if serial.in_waiting > 0:
sInLED.value = False
letter = serial.read(serial.in_waiting)
time.sleep(0.01)
sInLED.value = True
return letter
def sendSerial(data):
sOutLED.value = False
if serial is not None:
if serial.connected:
serial.write(data)
time.sleep(0.01)
sOutLED.value = True
# Lora Stuff
RADIO_FREQ_MHZ = 868.000
CS = DigitalInOut(board.GP21)
RESET = DigitalInOut(board.GP20)
spi = busio.SPI(board.GP18, MOSI=board.GP19, MISO=board.GP16)
rfm9x = adafruit_rfm9x.RFM9x(
spi, CS, RESET, RADIO_FREQ_MHZ, baudrate=1000000, agc=False, crc=True
)
rfm9x.tx_power = 23
sendSerial(b"RF.Guru\r\nLoRa868Stick 0.1\r\n")
string = ""
lastdata = ""
mode = False
arrow = False
modetype = "CLI"
buf = ""
while True:
w.feed()
input = recvSerial()
if input is not None:
buf = buf + input.decode("utf-8")
if input == b"\r":
string = buf
buf = ""
sendSerial(b"\r\n")
if mode is True:
sendSerial(modetype.encode("utf8"))
sendSerial(b">")
elif arrow is True and input == b"A":
sendSerial(b"\33[2K\r")
if mode is True:
sendSerial(modetype.encode("utf8"))
sendSerial(b">")
sendSerial(lastdata.encode("utf8"))
buf = lastdata
arrow = False
elif input == b"[":
arrow = True
else:
if input == b"\x7f":
sendSerial(b"\33[2K\r")
buf = buf[:-2]
sendSerial(b"\r")
if mode is True:
sendSerial(modetype.encode("utf8"))
sendSerial(b">")
sendSerial(buf.encode("utf8"))
if input == b"\x1d":
buf = ""
sendSerial(b"\33[2K\r")
sendSerial(modetype.encode("utf8"))
sendSerial(b">")
mode = True
else:
sendSerial(input)
if string != "":
data = string.strip()
if mode is True:
if modetype == "CLI":
if data == "rx":
modetype = "RX"
sendSerial(b"\33[2K\r")
sendSerial(modetype.encode("utf8"))
sendSerial(b">")
data = ""
elif data == "tx":
modetype = "TX"
sendSerial(b"\33[2K\r")
sendSerial(modetype.encode("utf8"))
sendSerial(b">")
data = ""
elif data == "q":
mode = False
modetype = "CLI"
sendSerial(b"\33[2K\r")
data = ""
else:
sendSerial(b"\33[2K\r")
msg = yellow("tx -> TRANSMIT MODE")
sendSerial(msg.encode("ascii"))
sendSerial(b"\r\n")
msg = yellow("rx -> LISTEN 4 ALL LORA PACKETS")
sendSerial(msg.encode("ascii"))
sendSerial(b"\r\n")
msg = yellow("q -> QUIT TO NORMAL MODE")
sendSerial(msg.encode("ascii"))
sendSerial(b"\r\n")
sendSerial(modetype.encode("utf8"))
sendSerial(b">")
data = ""
elif modetype == "RX":
if data == "q":
modetype = "CLI"
sendSerial(b"\33[2K\r")
sendSerial(modetype.encode("utf8"))
sendSerial(b">")
data = ""
elif data[:2] == "rx":
listentime = int(data[2:])
sendSerial(b"\33[2K\r")
msg = yellow(
"RX:receiving>Waiting for LoRa packets for "
+ str(listentime)
+ " seconds ..."
)
sendSerial(msg.encode("ascii"))
sendSerial(b"\r\n")
lInLED.value = False
packet = "ok"
while packet is not None:
packet = rfm9x.receive(w, with_header=True, timeout=listentime)
if packet is not None:
print("RAW Packet:")
print(packet)
sendSerial(packet[3:])
sendSerial(b"\r\n")
lInLED.value = True
msg = yellow("nothing ;(")
sendSerial(msg.encode("ascii"))
sendSerial(b"\r\n")
modetype = "RX"
sendSerial(modetype.encode("utf8"))
sendSerial(b">")
else:
sendSerial(b"\33[2K\r")
msg = yellow("rx?? -> Listen for lora packets for ?? seconds")
sendSerial(msg.encode("ascii"))
sendSerial(b"\r\n")
msg = yellow("q -> QUIT TO NORMAL MODE")
sendSerial(msg.encode("ascii"))
sendSerial(b"\r\n")
sendSerial(modetype.encode("utf8"))
sendSerial(b">")
data = ""
elif modetype == "TX":
if data != "" and data != "?":
if data == "q":
modetype = "CLI"
sendSerial(b"\33[2K\r")
sendSerial(modetype.encode("utf8"))
sendSerial(b">")
data = ""
else:
msg = yellow("TX:transmit>" + data)
sendSerial(b"\r")
sendSerial(msg.encode("ascii"))
sendSerial(b"\r\n")
sendSerial(modetype.encode("utf8"))
sendSerial(b">")
lOutLED.value = False
rfm9x.send(
w,
bytes("{}".format("<"), "UTF-8")
+ binascii.unhexlify("AA")
+ binascii.unhexlify("01")
+ bytes("{}".format(data), "UTF-8"),
)
lOutLED.value = True
else:
sendSerial(b"\33[2K\r")
msg = yellow("example: sw0/1 -> SWITCHES sw0 port1")
sendSerial(msg.encode("ascii"))
sendSerial(b"\r\n")
msg = yellow("q -> QUIT TO NORMAL MODE")
sendSerial(msg.encode("ascii"))
sendSerial(b"\r\n")
sendSerial(modetype.encode("utf8"))
sendSerial(b">")
else:
if data[:1] == "#":
data = data[1:]
if data[:3] == "tx#":
data = data[3:]
lOutLED.value = False
msg = yellow("TX:transmit>" + data)
print(msg)
rfm9x.send(
w,
bytes("{}".format("<"), "UTF-8")
+ binascii.unhexlify("AA")
+ binascii.unhexlify("01")
+ bytes("{}".format(data), "UTF-8"),
)
lOutLED.value = True
sendSerial(b"#tx#done\r\n")
if data[:3] == "rx#":
data = data[3:]
packet = "ok"
lInLED.value = False
while packet is not None:
packet = rfm9x.receive(w, with_header=True, timeout=int(data))
if packet is not None:
print("RAW Packet:")
print(packet)
sendSerial(packet[3:])
sendSerial(b"\r\n")
sendSerial(b"#rx#done\r\n")
lInLED.value = True
else:
sendSerial(b"Unknown message !! CTRL + ] Gives CLI promt.\r\n")
sendSerial(b"#tx#<msg> for direct msg to switches.\r\n")
sendSerial(b"#rx#<time> receive lora msgs.\r\n")
lastdata = data
string = ""