/
ttnpub.joinEachTime
310 lines (263 loc) · 8.69 KB
/
ttnpub.joinEachTime
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
# TTNpub.py - sends data to TTN for publishing
# Written by Mark Stanley
#
# Import functions;
try:
import RPi.GPIO as GPIO
except RuntimeError:
print("Error run in sudo mode")
# External module imports
import time, sys, tty, termios, serial, base64
# Check usage
if len(sys.argv) != 2:
print("USAGE: ttnpub.py <\"message\">")
sys.exit(2)
thisMsg=sys.argv[1].encode('hex')
#thisMsg=sys.argv[1].encode('base64')
print("Message is: ")
print(sys.argv[1])
print("Encoded is: ")
print(thisMsg)
########################################################################
# Serial Port Definition from RPi towards RN2483 module
########################################################################
# Baud Rate 57600
port = serial.Serial("/dev/ttyAMA0", baudrate=57600, timeout=5.0)
########################################################################
# Pin Definitions on LoRaWAN PI board
########################################################################
LED1 = 5 # Pin GPIO 5
LED2 = 6 # Pin GPIO 6
Reset = 13 # Reset line of RN2483 module
########################################################################
# Pin Setup
# Define PINs as input or outputs, any default status and if pullup
# are required.
########################################################################
GPIO.setmode(GPIO.BCM) # Broadcom pin numbering
GPIO.setwarnings(False) # Disable warnings for GPIO
GPIO.setup(LED1, GPIO.OUT)
GPIO.setup(LED2, GPIO.OUT)
GPIO.setup(Reset, GPIO.OUT)
# No imput pins are configured, as data is acquired via I2C bus
########################################################################
# Setup initial states (eg: all LEDs off)
########################################################################
GPIO.output(LED1, GPIO.HIGH) # Turn off
GPIO.output(LED2, GPIO.HIGH) # Turn off
########################################################################
# Sub-Routines - Read data from radio
########################################################################
# Used to read-in RN2483 data responses pending a Carriage return;
def readlineCR(port):
rv = ""
while True:
ch = port.read()
rv += ch
if ch=='\r' or ch=='':
return rv
########################################################################
# Start-up Code
########################################################################
# Execute RN2483 system reset;
# Reset RN2483 Module
GPIO.output(LED1, GPIO.LOW) # Turn on
GPIO.output(LED2, GPIO.LOW) # Turn on
GPIO.output(Reset, GPIO.LOW) #RN2483 reset - active low
time.sleep(1)
GPIO.output(Reset, GPIO.HIGH)
time.sleep(5) # 5 seconds stability
GPIO.output(LED1, GPIO.HIGH) # Turn off
GPIO.output(LED2, GPIO.HIGH) # Turn off
########################################################################
# Basic system checks
#port.write("sys factoryRESET\r\n")
port.write("sys reset\r\n")
rcv = readlineCR(port)
rcv = readlineCR(port)
print("Reset - Device Version\r\n" + (rcv))
time.sleep(3)
port.write("mac get deveui\r\n")
rcv = readlineCR(port)
thisDevEUI = rcv[1:]
print("Device EUI " + (thisDevEUI))
time.sleep(1)
port.write("radio get mod\r\n")
rcv = readlineCR(port)
print("System Mode:" + (rcv))
time.sleep(1)
port.write("radio get freq\r\n")
rcv = readlineCR(port)
print("Frequency Band:(in Hertz)" + (rcv))
time.sleep(1)
# Set up the radio
port.write("mac set adr on\r\n")
rcv = readlineCR(port)
print("ADR set "+(rcv))
time.sleep(1)
port.write("mac set pwridx 3\r\n")
rcv = readlineCR(port)
print("PWRIDX set "+(rcv))
time.sleep(1)
#port.write("mac set dr 5\r\n")
port.write("mac set dr 1\r\n")
rcv = readlineCR(port)
print("DR set "+(rcv))
time.sleep(1)
########################################################################
# Setup System (Thingithon Keys)
########################################################################
port.write("mac set appeui 70B3D57ED0000CD9\r\n")
while True:
tdata = readlineCR(port)
if tdata.strip() == "ok":
print("Application EUI is Valid")
break
port.write("mac set appkey 3B49BB48C995D2E2522393CB8DE1AF74\r\n")
while True:
tdata = readlineCR(port)
if tdata.strip() == "ok":
print("Application Key is Valid")
break
print("Joining the network.")
GPIO.output(LED1, GPIO.LOW) # Turn on
port.write("mac join otaa\r\n")
tryCount=0
while tryCount < 6:
tryCount=tryCount+1
time.sleep(2)
tdata = readlineCR(port)
print(tdata.strip())
if tdata.strip() == "ok":
print("Join request is fine")
GPIO.output(LED2, GPIO.LOW) # Turn on
time.sleep(0.25)
GPIO.output(LED2, GPIO.HIGH) # Turn off
if tdata.strip() == "accepted":
print("Join accepted")
GPIO.output(LED2, GPIO.LOW) # Turn on
time.sleep(2)
break
if tdata.strip() == "denied":
print("Error State - join not accepted")
GPIO.output(LED1, GPIO.HIGH) # Turn off
GPIO.output(LED2, GPIO.LOW) # Turn on
time.sleep(0.25)
GPIO.output(LED2, GPIO.HIGH) # Turn off
time.sleep(0.25)
GPIO.output(LED2, GPIO.LOW) # Turn on
time.sleep(0.25)
GPIO.output(LED2, GPIO.HIGH) # Turn off
time.sleep(2)
GPIO.output(LED1, GPIO.LOW) # Turn on
port.write("mac join otaa\r\n")
if tdata.strip() == "busy":
print("Error State - busy")
GPIO.output(LED1, GPIO.HIGH) # Turn off
time.sleep(0.5)
GPIO.output(LED2, GPIO.LOW) # Turn on
time.sleep(1)
GPIO.output(LED2, GPIO.HIGH) # Turn off
time.sleep(8)
GPIO.output(LED1, GPIO.LOW) # Turn on
port.write("mac join otaa\r\n")
if tdata.strip() == "no_free_ch":
print("Error State - no free channel")
GPIO.output(LED1, GPIO.HIGH) # Turn off
time.sleep(1)
GPIO.output(LED2, GPIO.LOW) # Turn on
time.sleep(0.5)
GPIO.output(LED2, GPIO.HIGH) # Turn off
time.sleep(1)
GPIO.output(LED2, GPIO.LOW) # Turn on
time.sleep(0.5)
GPIO.output(LED2, GPIO.HIGH) # Turn off
time.sleep(1)
GPIO.output(LED2, GPIO.LOW) # Turn on
time.sleep(0.5)
GPIO.output(LED2, GPIO.HIGH) # Turn off
time.sleep(20)
GPIO.output(LED1, GPIO.LOW) # Turn on
port.write("mac join otaa\r\n")
GPIO.output(LED1, GPIO.HIGH) # Turn off
GPIO.output(LED2, GPIO.HIGH) # Turn off
# If we successfully joined, lets have a go at sending the message
if tdata.strip()=="accepted":
# Try to send the message
print("SEND MESSAGE")
GPIO.output(LED1, GPIO.LOW) # Turn on
GPIO.output(LED2, GPIO.LOW) # Turn on
port.write("mac tx uncnf 1 " + thisMsg + "\r\n" )
print("mac tx uncnf 1 " + thisMsg + "\r\n" )
tryCount=0
while tryCount < 5:
tryCount=tryCount+1
tdata = readlineCR(port)
print("Getting response..")
print(tdata.strip())
if tdata.strip() == "not_joined":
print("Error - not joined gateway, abort attempt.")
break
if tdata.strip() == "ok":
print("Data Sent to TX buffer")
break
if tdata.strip() == "busy":
print("Please Wait")
time.sleep(10)
print("BUSY - RE-SEND MESSAGE")
port.write("mac tx uncnf 1 " + thisMsg + "\r\n" )
print("mac tx uncnf 1 " + thisMsg + "\r\n" )
if tdata.strip() == "no_free_ch":
print("Please Wait - Duty Cycle exceeed for <1%")
time.sleep(30)
print("NO FREE CH - RE-SEND MESSAGE")
port.write("mac tx uncnf 1 " + thisMsg + "\r\n" )
print("mac tx uncnf 1 " + thisMsg + "\r\n" )
GPIO.output(LED1, GPIO.HIGH) # Turn off
GPIO.output(LED2, GPIO.HIGH) # Turn off
tdata = readlineCR(port)
if tdata.strip() == "mac_tx_ok":
print("Data Sent OTA")
GPIO.output(LED1, GPIO.LOW) # Turn on
GPIO.output(LED2, GPIO.HIGH) # Turn off
time.sleep(0.5)
GPIO.output(LED1, GPIO.HIGH) # Turn off
GPIO.output(LED2, GPIO.LOW) # Turn on
time.sleep(0.5)
GPIO.output(LED1, GPIO.LOW) # Turn on
GPIO.output(LED2, GPIO.HIGH) # Turn off
time.sleep(0.5)
GPIO.output(LED1, GPIO.HIGH) # Turn off
GPIO.output(LED2, GPIO.LOW) # Turn on
time.sleep(0.5)
GPIO.output(LED1, GPIO.LOW) # Turn on
GPIO.output(LED2, GPIO.HIGH) # Turn off
time.sleep(0.5)
GPIO.output(LED2, GPIO.LOW) # Turn on
time.sleep(1)
GPIO.output(LED1, GPIO.HIGH) # Turn off
GPIO.output(LED2, GPIO.HIGH) # Turn off
if tdata.strip() == "mac_err":
print("Please Wait - MAC error - Data will be passed to RN2483 module for final time - else data is ditched this loop")
GPIO.output(LED1, GPIO.LOW) # Turn on
GPIO.output(LED2, GPIO.LOW) # Turn on
time.sleep(0.5)
GPIO.output(LED1, GPIO.HIGH) # Turn off
GPIO.output(LED2, GPIO.HIGH) # Turn off
time.sleep(0.5)
GPIO.output(LED1, GPIO.LOW) # Turn on
GPIO.output(LED2, GPIO.LOW) # Turn on
time.sleep(0.5)
GPIO.output(LED1, GPIO.HIGH) # Turn off
GPIO.output(LED2, GPIO.HIGH) # Turn off
time.sleep(0.5)
GPIO.output(LED1, GPIO.LOW) # Turn on
GPIO.output(LED2, GPIO.LOW) # Turn on
time.sleep(0.5)
GPIO.output(LED1, GPIO.HIGH) # Turn off
GPIO.output(LED2, GPIO.HIGH) # Turn off
time.sleep(5)
print("TRANSMIT ERROR - TRY TO RE-SEND MESSAGE")
port.write("mac tx uncnf 1 " + thisMsg + "\r\n")
# Clean up GPIO ports on program end;
GPIO.cleanup()