-
Notifications
You must be signed in to change notification settings - Fork 5
/
RUI3-RAK12500-RAK1904-GNSS.ino
435 lines (377 loc) · 10.3 KB
/
RUI3-RAK12500-RAK1904-GNSS.ino
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/**
* @file RUI3-Modular.ino
* @author Bernd Giesecke ([email protected])
* @brief RUI3 based code for low power practice
* @version 0.1
* @date 2023-03-29
*
* @copyright Copyright (c) 2023
*
*/
#include "app.h"
/** Packet is confirmed/unconfirmed (Set with AT commands) */
bool g_confirmed_mode = false;
/** If confirmed packet, number or retries (Set with AT commands) */
uint8_t g_confirmed_retry = 0;
/** Data rate (Set with AT commands) */
uint8_t g_data_rate = 3;
/** Flag if transmit is active, used by some sensors */
volatile bool tx_active = false;
/** fPort to send packages */
uint8_t set_fPort = 2;
/** LoRaWAN packet */
WisCayenne g_solution_data(255);
/** Counter for GNSS readings */
uint16_t check_gnss_counter = 0;
/** Max number of GNSS readings before giving up */
uint16_t check_gnss_max_try = 0;
/** Flag for GNSS readings active */
bool gnss_active = false;
/** Flag if RAK1904 is present */
bool has_rak1904 = false;
/**
* @brief Callback after join request cycle
*
* @param status Join result
*/
void joinCallback(int32_t status)
{
// MYLOG("JOIN-CB", "Join result %d", status);
if (status != 0)
{
// MYLOG("JOIN-CB", "LoRaWan OTAA - join fail! \r\n");
// To be checked if this makes sense
// api.lorawan.join();
}
else
{
// MYLOG("JOIN-CB", "LoRaWan OTAA - joined! \r\n");
digitalWrite(LED_BLUE, LOW);
}
}
/**
* @brief LoRaWAN callback after packet was received
*
* @param data pointer to structure with the received data
*/
void receiveCallback(SERVICE_LORA_RECEIVE_T *data)
{
MYLOG("RX-CB", "RX, port %d, DR %d, RSSI %d, SNR %d", data->Port, data->RxDatarate, data->Rssi, data->Snr);
// for (int i = 0; i < data->BufferSize; i++)
// {
// Serial.printf("%02X", data->Buffer[i]);
// }
// Serial.print("\r\n");
tx_active = false;
}
/**
* @brief Callback for LinkCheck result
*
* @param data pointer to structure with the linkcheck result
*/
void linkcheckCallback(SERVICE_LORA_LINKCHECK_T *data)
{
MYLOG("LC_CB", "%s Margin %d GW# %d RSSI%d SNR %d", data->State == 0 ? "Success" : "Failed",
data->DemodMargin, data->NbGateways,
data->Rssi, data->Snr);
}
/**
* @brief LoRaWAN callback after TX is finished
*
* @param status TX status
*/
void sendCallback(int32_t status)
{
MYLOG("TX-CB", "TX status %d", status);
digitalWrite(LED_BLUE, LOW);
tx_active = false;
// If it was motion triggered, reset the ACC interrupts
if (has_rak1904)
{
clear_int_rak1904();
}
}
/**
* @brief LoRa P2P callback if a packet was received
*
* @param data pointer to the data with the received data
*/
void recv_cb(rui_lora_p2p_recv_t data)
{
MYLOG("PRX-CB", "P2P RX, RSSI %d, SNR %d", data.Rssi, data.Snr);
for (int i = 0; i < data.BufferSize; i++)
{
Serial.printf("%02X", data.Buffer[i]);
}
Serial.print("\r\n");
tx_active = false;
}
/**
* @brief LoRa P2P callback if a packet was sent
*
*/
void send_cb(void)
{
MYLOG("PTX-CB", "P2P TX finished");
digitalWrite(LED_BLUE, LOW);
tx_active = false;
// If it was motion triggered, reset the ACC interrupts
if (has_rak1904)
{
clear_int_rak1904();
}
}
/**
* @brief LoRa P2P callback for CAD result
*
* @param result true if activity was detected, false if no activity was detected
*/
void cad_cb(bool result)
{
MYLOG("CAD-CB", "P2P CAD reports %s", result ? "activity" : "no activity");
}
/**
* @brief Arduino setup, called once after reboot/power-up
*
*/
void setup()
{
// Setup for LoRaWAN
if (api.lorawan.nwm.get() == 1)
{
g_confirmed_mode = api.lorawan.cfm.get();
g_confirmed_retry = api.lorawan.rety.get();
g_data_rate = api.lorawan.dr.get();
// Setup the callbacks for joined and send finished
api.lorawan.registerRecvCallback(receiveCallback);
api.lorawan.registerSendCallback(sendCallback);
api.lorawan.registerJoinCallback(joinCallback);
api.lorawan.registerLinkCheckCallback(linkcheckCallback);
}
else // Setup for LoRa P2P
{
api.lora.registerPRecvCallback(recv_cb);
api.lora.registerPSendCallback(send_cb);
api.lora.registerPSendCADCallback(cad_cb);
}
pinMode(LED_GREEN, OUTPUT);
digitalWrite(LED_GREEN, HIGH);
pinMode(LED_BLUE, OUTPUT);
digitalWrite(LED_BLUE, HIGH);
pinMode(WB_IO2, OUTPUT);
digitalWrite(WB_IO2, LOW);
// Start Serial
Serial.begin(115200);
// Delay for 5 seconds to give the chance for AT+BOOT
delay(5000);
api.system.firmwareVersion.set("RUI3-Location-Tracker-V1.0.0");
Serial.println("RAKwireless RUI3 Location Tracker");
Serial.println("------------------------------------------------------");
Serial.println("Setup the device with WisToolBox or AT commands before using it");
Serial.printf("Version %s\n", api.system.firmwareVersion.get().c_str());
Serial.println("------------------------------------------------------");
// Initialize module
Wire.begin();
// Register the custom AT command to get device status
if (!init_status_at())
{
MYLOG("SETUP", "Add custom AT command STATUS fail");
}
// Register the custom AT command to set the send interval
if (!init_interval_at())
{
MYLOG("SETUP", "Add custom AT command Send Interval fail");
}
// Register the custom AT command to set the minimal interval
if (!init_min_interval_at())
{
MYLOG("SETUP", "Add custom AT command Minimal Interval fail");
}
// Get saved sending interval from flash
get_at_setting();
digitalWrite(LED_GREEN, LOW);
// Create a timer.
api.system.timer.create(RAK_TIMER_0, sensor_handler, RAK_TIMER_PERIODIC);
if (custom_parameters.send_interval != 0)
{
// Start a timer.
api.system.timer.start(RAK_TIMER_0, custom_parameters.send_interval, NULL);
}
MYLOG("SETUP", "Create timer for GNSS polling");
// Create a timer.
api.system.timer.create(RAK_TIMER_1, gnss_handler, RAK_TIMER_PERIODIC);
// Check min send interval, if too small, set to send interval - 10 seconds
if (custom_parameters.min_interval < 10000)
{
if (custom_parameters.send_interval != 0)
{
custom_parameters.min_interval = custom_parameters.send_interval - 1000;
}
else
{
custom_parameters.min_interval = 10000;
}
// Save custom settings
save_at_setting();
}
MYLOG("SETUP", "Create timer for interrupt handler");
// Create a timer
api.system.timer.create(RAK_TIMER_2, sensor_handler, RAK_TIMER_ONESHOT);
// Check if it is LoRa P2P
if (api.lorawan.nwm.get() == 0)
{
digitalWrite(LED_BLUE, LOW);
sensor_handler(NULL);
}
if (api.lorawan.nwm.get() == 1)
{
if (g_confirmed_mode)
{
MYLOG("SETUP", "Confirmed enabled");
}
else
{
MYLOG("SETUP", "Confirmed disabled");
}
MYLOG("SETUP", "Retry = %d", g_confirmed_retry);
MYLOG("SETUP", "DR = %d", g_data_rate);
}
// Enable low power mode
api.system.lpm.set(1);
#if defined(_VARIANT_RAK3172_) || defined(_VARIANT_RAK3172_SIP_)
// No BLE
#else
Serial6.begin(115200, RAK_AT_MODE);
api.ble.advertise.start(30);
#endif
// Initialize RAK1904
has_rak1904 = init_rak1904();
// Set last trigger time to boot time
last_trigger = millis();
digitalWrite(WB_IO2, LOW);
}
/**
* @brief sensor_handler is a timer function called every
* custom_parameters.send_interval milliseconds. Can be
* changed with ATC+SENDINT command
*
*/
void sensor_handler(void *)
{
if (api.lorawan.nwm.get() == 1)
{
// Check if the node has joined the network
if (!api.lorawan.njs.get())
{
MYLOG("UPLINK", "Not joined, skip sending");
// If it was motion triggered, reset the ACC interrupts
if (has_rak1904)
{
clear_int_rak1904();
}
last_trigger = millis();
return;
}
}
digitalWrite(LED_BLUE, HIGH);
// Just for debug, show if the call is because of a motion detection
if (motion_detected)
{
MYLOG("UPLINK", "ACC triggered IRQ");
motion_detected = false;
}
// If GNSS is not yet active, start the timer to aquire the location
if (!gnss_active)
{
// Clear payload
g_solution_data.reset();
// Add battery voltage
g_solution_data.addVoltage(LPP_CHANNEL_BATT, api.system.bat.get());
// Set flag for GNSS active to avoid retrigger */
gnss_active = true;
// Startup GNSS module
init_gnss();
check_gnss_counter = 0;
check_gnss_max_try = custom_parameters.min_interval / 2500; // / 2 / 2500;
gnss_start = millis();
// Start the timer
api.system.timer.start(RAK_TIMER_1, 2500, NULL);
}
else
{
MYLOG("UPLINK", "GNSS still active");
}
digitalWrite(LED_BLUE, LOW);
}
/**
* @brief This example is complete timer
* driven. The loop() does nothing than
* sleep.
*
*/
void loop()
{
api.system.sleep.all();
// api.system.scheduler.task.destroy();
}
/**
* @brief Send the data packet that was prepared in
* Cayenne LPP format by the different sensor and location
* acquisition functions
*
*/
void send_packet(void)
{
// Set flag for GNSS inactive */
gnss_active = false;
// Reset delay timer value
last_trigger = millis();
// Add acquisition time in seconds
g_solution_data.addUnixTime(LPP_CHANNEL_GPS, (gnss_finished - gnss_start) / 1000);
// Check if it is LoRaWAN
if (api.lorawan.nwm.get() == 1)
{
// Check DR
uint8_t new_dr = get_min_dr(api.lorawan.band.get(), g_solution_data.getSize());
if (new_dr > api.lorawan.dr.get())
{
api.lorawan.dr.set(new_dr);
MYLOG("UPLINK", "Datarate changed to %d", api.lorawan.dr.get());
}
// Send the packet
if (api.lorawan.send(g_solution_data.getSize(), g_solution_data.getBuffer(), set_fPort, g_confirmed_mode, g_confirmed_retry))
{
MYLOG("UPLINK", "Packet enqueued, size %d", g_solution_data.getSize());
tx_active = true;
}
else
{
MYLOG("UPLINK", "Send failed");
tx_active = false;
// If it was motion triggered, reset the ACC interrupts
if (has_rak1904)
{
clear_int_rak1904();
}
}
}
// It is P2P
else
{
MYLOG("UPLINK", "Send packet with size 4 over P2P");
digitalWrite(LED_BLUE, LOW);
if (api.lora.psend(g_solution_data.getSize(), g_solution_data.getBuffer(), true))
{
MYLOG("UPLINK", "Packet enqueued");
}
else
{
MYLOG("UPLINK", "Send failed");
// If it was motion triggered, reset the ACC interrupts
if (has_rak1904)
{
clear_int_rak1904();
}
}
}
}