This repository has been archived by the owner on Dec 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
lora.app.eu.sb
377 lines (340 loc) · 13.1 KB
/
lora.app.eu.sb
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
//******************************************************************************
// Copyright (c) 2016-2017, Laird
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
// IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
//
// SPDX-License-Identifier:ISC
//
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// +++++ ++
// +++++ When UwTerminal downloads the app it will store it as a filename ++
// +++++ which consists of all characters up to the first . and excluding it ++
// +++++ ++
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//
// This application is designed to be ran on the RM186 and will join a LoRa
// network and continually transmit data to it.
//
//******************************************************************************
#include "RM1xx-defs.h"
//Size of i[]
#define NUM_OF_I_PARAMS (8)
Dim rc
Dim data$
Dim x
dim tlen,tkn$
dim i[NUM_OF_I_PARAMS] //Index 0 used for return values
dim urtcmd$ //CMD line from uart
dim urts
dim stRsp$
dim str$
dim reg
dim stringVal$
dim numnosync
dim packettype
dim joined
dim maxsize
dim currentsize
//-------------------------------------------------------------------------
//-------------------------------------------------------------------------
function ExtractIntTokens(u$,stIdx,num)
while num>0
tlen = ExtractIntToken(u$,i[stIdx])
if tlen == 0 then
exitfunc 4
endif
num=num-1
stIdx = stIdx+1
endwhile
endfunc 0
//-------------------------------------------------------------------------
// Lists the currently enabled channels and the maximum datarate of that channel
//-------------------------------------------------------------------------
SUB GetChannelsList()
// Get current list of available channels
print "\n\nEnabled channels and maximum datarate"
rc = LORAMACGetOption(LORAMAC_OPT_CHANNELLIST, stringVal$)
sprint #str$,"\n";stringVal$
print str$
ENDSUB
//-------------------------------------------------------------------------
// Joining event
//-------------------------------------------------------------------------
FUNCTION LoramacJoining() As Integer
print "\nJoining"
endfunc 1
//-------------------------------------------------------------------------
// Joined event
//-------------------------------------------------------------------------
FUNCTION LoramacJoined() As Integer
print "\nSuccessfully Joined network"
joined = 1
//Get current channel list
GetChannelsList()
packettype = 1
endfunc 1
//-------------------------------------------------------------------------
// Tx complete event - marks the end of a transmit/receive cycle
//-------------------------------------------------------------------------
FUNCTION LoramacTxComplete() As Integer
str$ = "\nTxComplete event received - next packet can be loaded"
Print str$
endfunc 1
//-------------------------------------------------------------------------
// RxData event - data has been received from the gateway
//-------------------------------------------------------------------------
FUNCTION HandlerRxData() As Integer
dim datastr$ as string
dim rssi$ as integer
dim port$ as integer
dim snr$ as integer
dim framepending$ as integer
dim packettype$ as integer
rc = LORAMACRxData(datastr$,rssi$,port$,snr$,framepending$,packettype$)
sprint #str$,"\n\22";datastr$;"\22 received from the gateway"
Print str$
sprint #str$,"\nReceived packet rssi: ";rssi$;" snr: ";snr$;" frames pending: ";framepending$;" packet type: ";packettype$
Print str$
endfunc 1
//--------------------------------------------------------------------------------------------------------------------------
// Rx complete event - also marks the end of a transmit/receive cycle when using confirmed packets or the gateway sends data
//--------------------------------------------------------------------------------------------------------------------------
FUNCTION LoramacRxComplete() As Integer
//str$ = "\nRx completed "
//Print str$
endfunc 1
//-------------------------------------------------------------------------
// RxTimeout event
// returned after every missed
//-------------------------------------------------------------------------
FUNCTION LoramacRxTimeout() As Integer
print "\nRx Timeout"
if joined == 0 then
print "\nfailed to join network"
endif
endfunc 1
//-------------------------------------------------------------------------
// TxTimeout event
//-------------------------------------------------------------------------
FUNCTION LoramacTxTimeout() As Integer
print "\nTx Timeout"
endfunc 1
//-------------------------------------------------------------------------
// RxError event - crc error
//-------------------------------------------------------------------------
FUNCTION LoramacRxError() As Integer
print "\nRx Error"
endfunc 1
//-------------------------------------------------------------------------
// LinkResponse event - a response to a LinkCheck has been received
//-------------------------------------------------------------------------
FUNCTION LoramacLinkResponse(Margin, Gateways) As Integer
sprint #str$,"\nMargin: ";Margin
Print str$
endfunc 1
//-------------------------------------------------------------------------
// TxDone event - packet has been transmitted to the gateway
//-------------------------------------------------------------------------
FUNCTION HandlerTxDone() As Integer
numnosync = 0
if packettype == 0 then
str$ = "\nTxDone event received - JoinRequest transmitted to the gateway"
else
sprint #str$,"\nTxDone event received - \22";data$;"\22 sent to gateway"
endif
Print str$
endfunc 1
//------------------------------------------------------------------------------
// NoSync event - an RxWindow has closed after it failed to receive a sync pulse
//------------------------------------------------------------------------------
FUNCTION HandlerNoSync() As Integer
if numnosync == 0 then
str$ = "\nFirst receive window timed out"
numnosync = 1
else
str$ = "\nSecond receive window timed out"
numnosync = 0
endif
Print str$
endfunc 1
//------------------------------------------------------------------------------
// Adr packet has been received so configuration might have changed
//------------------------------------------------------------------------------
FUNCTION HandlerAdr(PacketType, FramePending) As Integer
dim dr$,pow$
sprint #str$,"\nAdr received (Type: ";PacketType;") - "
Print str$
rc = LORAMACGetOption(LORAMAC_OPT_TX_POWER, pow$)
rc = LORAMACGetOption(LORAMAC_OPT_DATA_RATE, dr$)
sprint #str$,"power ";pow$;" datarate ";dr$;"\n"
Print str$
endfunc 1
//------------------------------------------------------------------------------
// Uplnk/downlink sequence has completed - this is an amalgamation of the above events.
// The flag indicates which of the above end of seqnece events triggered this event.
// nexttime is the time to the next EVLORAMACNEXTTX event.
// For AU and US modules this will be 0 and it is safe to send the next packet.
//------------------------------------------------------------------------------
FUNCTION HandlerSequenceComplete(flag, nexttime) As Integer
Print "\nSequence complete ";flag
Print "\nNext time ",nexttime
endfunc 1
//==============================================================================
// there is now duty cycle available to send the next packet
//==============================================================================
function HandlrNextTx()
print "\n---------------------------------------\n"
Print "\nNext Tx"
if joined == 1 then
// Check to see that the packet size is within limits.
rc = LoramacQueryTxPossible(strlen(data$),currentsize,maxsize)
if rc == 0 then
Print "\ncurrent ";currentsize
Print "\nmax ";maxsize
rc = LORAMACTxData(2,data$, 1)
if rc != 0 then
Print "\nFailed to send packet: rc = ";integer.h' rc
endif
else
Print "current ";currentsize
endif
endif
endfunc 1
//-------------------------------------------------------------------------
//#CMD#// lora update param$ #INTvalue#
//#CMD#// lora readparam param$ #INTvalue#
//#CMD#// lora readreg #INTreg# #INTvalue#
//#CMD#// lora pollregs
//#CMD#// lora tx
//#CMD#// lora txpkts1 #INTfrequencyChannel# #INTDataRate# #INTPowerBand # #INThandle#
//#CMD#// lora cancel #INThandle#
//-------------------------------------------------------------------------
function _Lora()
dim prAdr$
dim val
dim res
dim tkn$
tlen = ExtractStrToken(urtcmd$,tkn$)
if tlen == 0 then
exitfunc 5
endif
//Sends a JoinRequest to the gateway
if strcmp(tkn$,"join")==0 then
packettype = 0
rc = ExtractIntToken(urtcmd$,val)
rc = LORAMACJoin(val)
exitfunc 0
endif
//Calls the LoramacGetOption command - outputs some of the configuration options
if strcmp(tkn$,"get")==0 then
rc = ExtractIntToken(urtcmd$,reg)
rc = LORAMACGetOption(reg, stringVal$)
sprint #str$,stringVal$;"\n"
Print str$
exitfunc 0
endif
//Transmits a LinkCheck command
if strcmp(tkn$,"link")==0 then
exitfunc LORAMACLinkCheck()
endif
//Calls the LoramacSetOption command - sets some of the configuration optons
if strcmp(tkn$,"set")==0 then
rc = ExtractIntToken(urtcmd$,reg)
tlen = ExtractStrToken(urtcmd$,tkn$)
if tlen != 0 then
rc = LORAMACSetOption(reg, tkn$)
print "\n";integer.h' rc
if rc == 0 then
sprint #str$,"\n";integer.h' rc;" ";tkn$
else
sprint #str$,"\n";integer.h' rc
endif
Print str$
endif
exitfunc 0
endif
//Enables/disables the debug option
if strcmp(tkn$,"debug")==0 then
rc = ExtractIntTokens(urtcmd$,0,3)
if rc == 0 then
rc = LORAMACSetDebug(i[0],i[1],i[2])
endif
exitfunc rc
endif
endfunc 5
//==============================================================================
//==============================================================================
function OnUartCmd() as integer
rc=1 //assume there is an error
tlen = ExtractStrToken(urtcmd$,tkn$) //get first token
if tlen == 0 then
rc=0
elseif tlen > 0 then
if strcmp(tkn$,"lora")==0 then
rc = _Lora()
if rc != 0 then
sprint #str$,"\nError ";integer.h' rc
Print str$
endif
elseif (strcmp(tkn$,"exit")==0)||(strcmp(tkn$,"stop")==0) then
print "\nType..\nRESUME to continue the application"
print "\n? VARNAME to inspect a variable"
print "\n= VARNAME VALUE to change a variable"
stop
rc=0
endif
endif
endfunc 1
//==============================================================================
//==============================================================================
function HandlerUartRxCmd() as integer
dim nMatch
if urts < 0 then
//UART parser is suspended
exitfunc 1
endif
//Check if CR has been received
nMatch=UartReadMatch(stRsp$,13)
if nMatch!=0 then
//CR exists in the input buffer
urtcmd$ = strsplitleft$(stRsp$,nMatch)
exitfunc OnUartCmd()
endif
endfunc 1
ONEVENT EVLORAMACJOINING CALL LoramacJoining
ONEVENT EVLORAMACJOINED CALL LoramacJoined
ONEVENT EVLORAMACTXCOMPLETE CALL LoramacTxComplete
ONEVENT EVLORAMACRXTIMEOUT CALL LoramacRxTimeout
ONEVENT EVLORAMACTXTIMEOUT CALL LoramacTxTimeout
ONEVENT EVLORAMACRXERROR CALL LoramacRxError
ONEVENT EVLORAMACRXCOMPLETE CALL LoramacRxComplete
ONEVENT EVLORAMACLINKCHECKRESPMSG CALL LoramacLinkResponse
ONEVENT EVUARTRX CALL HandlerUartRxCmd
ONEVENT EVLORAMACTXDONE CALL HandlerTxDone
ONEVENT EVLORAMACNOSYNC CALL HandlerNoSync
ONEVENT EVLORAMACADR CALL HandlerAdr
ONEVENT EVLORAMACRXDATA CALL HandlerRxData
ONEVENT EVLORAMACSEQUENCECOMPLETE CALL HandlerSequenceComplete
ONEVENT EVLORAMACNEXTTX CALL HandlrNextTx
// Initialise variables
data$ = "hello world"
numnosync = 0
packettype = 0
// Get current channel list
GetChannelsList()
joined = 0
rc = LORAMACJoin(1)
if rc != 0 then
print "Error: rc = ";integer.h' rc
endif
WAITEVENT