forked from disk91/WioLoRaWANFieldTester
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WioLoRaWANFieldTester.ino
367 lines (342 loc) · 11.5 KB
/
WioLoRaWANFieldTester.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
/**
* This file is part of Wio LoRaWan Field Tester.
*
* Wio LoRaWan Field Tester is free software created by Paul Pinault aka disk91.
* You can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* Wio LoRaWan Field Tester 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Wio LoRaWan Field Tester. If not, see <https://www.gnu.org/licenses/>.
*
* Author : Paul Pinault (disk91.com)
*
*/
#include "Arduino.h"
#include <SPI.h>
#include "LoRaCom.h"
#include "config.h"
#include "ui.h"
#include "testeur.h"
#include "gps.h"
#if HWTARGET == LORAE5 && defined WITH_LIPO
#include <SparkFunBQ27441.h>
const unsigned int BATTERY_CAPACITY = 600;
#endif
// No more than 10 bytes to suppor DR0 in US915
static uint8_t myFrame[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // GPS position 0 if invalid
0x00, 0x00, // Altitude
0x00, // HDOP x 10
0x00, // Sats
};
static uint8_t emptyFrame[] = {
0x00
};
// Try to get battery percent depending on voltage
// Ok that's empiric mapping but better than nothing :-)
// Used 0.5C curve
// https://forum.pycom.io/topic/6585/read_battery_voltage-voltage-value-to-battery-level/3
uint8_t batteryPercent(uint16_t volt) {
uint8_t percent = 0;
if (volt >= 4180 ) {
percent = 100;
} else if ( volt >= 4100 ) { percent = 95;
} else if ( volt >= 4000 ) { percent = 90;
} else if ( volt >= 3950 ) { percent = 85;
} else if ( volt >= 3900 ) { percent = 80;
} else if ( volt >= 3850 ) { percent = 70;
} else if ( volt >= 3800 ) { percent = 60;
} else if ( volt >= 3750 ) { percent = 50;
} else if ( volt >= 3725 ) { percent = 40;
} else if ( volt >= 3700 ) { percent = 30;
} else if ( volt >= 3650 ) { percent = 20;
} else if ( volt >= 3600 ) { percent = 10;
} else if ( volt >= 3400 ) { percent = 5;
} else if ( volt >= 3200 ) { percent = 2;
} else {
percent = 1;
}
return percent;
}
void setup() {
#if defined SERIALCONFIG || defined DEBUG
Serial.begin(9600);
uint32_t s = millis();
while ( !Serial && (millis() - s) < 3000);
#endif
#if HWTARGET == LORAE5 && defined WITH_LIPO
if ( lipo.begin() ) {
state.batOk = true;
lipo.setCapacity(BATTERY_CAPACITY);
} else {
state.batOk = false;
}
#endif
initState();
initScreen();
// With some LoRa-E5 we have a problem with the GPS default serial speed
// So ate first we need to fix that
#if HWTARGET == LORAE5 && defined(WITH_GPS)
processLoRaE5GpsFix();
#endif
displayTitle();
// Specific build to clear the LoRa E5 memory storing the LoRa configuration
// that allows firwmare update.
#if defined JUSTCLEAN && HWTARGET == LORAE5
loraQuickSetup();
clearBackup();
while(1);
#endif
// Make sure the LoraWan configuration has been made if not wait for the configuration
boolean zero = true;
for ( int i = 0 ; i < 8 ; i++ ) {
if ( loraConf.deveui[i] != 0 ) zero = false;
}
bool first = true;
bool hasChange = false;
if ( zero ) {
// try to restore backup
if ( readConfigFromBackup() ) {
storeConfig();
NVIC_SystemReset();
}
while (true) {
if ( manageConfigScreen(true, first || hasChange, false ) ){
NVIC_SystemReset();
}
first = false;
hasChange = processLoRaConfig();
}
} else if ( digitalRead(WIO_5S_PRESS) == LOW ) {
while ( digitalRead(WIO_5S_PRESS) == LOW );
manageConfigScreen(false,true, false);
NVIC_SystemReset();
}
#if HWTARGET == LORAE5
if ( loraConf.zone == ZONE_LATER ) {
loraConf.zone = ZONE_UNDEFINED;
manageConfigScreen(false,true,true);
NVIC_SystemReset();
}
#endif
gpsSetup();
displaySplash();
loraSetup();
// at this point configuration is ready
if ( ! state.cnfBack ) {
// backup config
if ( storeConfigToBackup() ) {
storeConfig(); // update backup status
}
}
clearScreen();
screenSetup();
analogReference(AR_INTERNAL2V23);
}
void loop(void) {
static unsigned long cTime = 0;
static unsigned long batUpdateTime = 0;
static unsigned long noMovementCounter = 0;
unsigned long sTime;
bool fireMessage;
sTime = millis();
fireMessage = false;
#ifdef WITH_LIPO
if ( batUpdateTime > 1000 ) {
#if HWTARGET == RFM95
uint32_t v = analogRead(LIPO_ADC);
v = 2*( 3300 * v ) / 1024; // should be 2230 ...
#ifdef LIPO_OFFSET_MV
v += LIPO_OFFSET_MV;
#endif
state.batVoltage = v;
state.batPercent = batteryPercent(state.batVoltage);
#elif HWTARGET == LORAE5
if ( state.batOk ) {
unsigned int soc = lipo.soc(); // Read state-of-charge (%)
unsigned int volts = lipo.voltage(); // Read battery voltage (mV)
//unsigned int fullCapacity = lipo.capacity(FULL); // Read full capacity (mAh)
//unsigned int capacity = lipo.capacity(REMAIN); // Read remaining capacity (mAh)
//Serial.printf("charge %d / volt : %d / full : %d / remain : %d\r\n",soc,volts,fullCapacity,capacity);
state.batVoltage = volts;
state.batPercent = soc;
}
#endif
state.batUpdated = true;
batUpdateTime = 0;
}
#endif
refresUI();
switch ( ui.selected_mode ) {
default:
case MODE_MANUAL:
if ( ui.hasClick && canLoRaSend() ) {
fireMessage = true;
ui.hasClick = false;
}
break;
case MODE_AUTO_1H:
if ( cTime >= ( 60 * 60 * 1000 ) && canLoRaSend() ) fireMessage = true;
break;
case MODE_AUTO_15MIN:
if ( cTime >= ( 15 * 60 * 1000 ) && canLoRaSend() ) fireMessage = true;
break;
case MODE_AUTO_5MIN:
if ( cTime >= ( 5 * 60 * 1000 ) && canLoRaSend() ) fireMessage = true;
#ifdef WITH_GPS
if ( noMovementCounter >= MAXNONMOVEMENT_DURATION_MS ) {
ui.selected_mode = MODE_AUTO_15MIN;
refreshMode();
}
#endif
break;
case MODE_AUTO_1MIN:
if ( cTime >= ( 1 * 60 * 1000 ) && canLoRaSend() ) fireMessage = true;
#ifdef WITH_GPS
if ( noMovementCounter >= MAXNONMOVEMENT_DURATION_MS ) {
ui.selected_mode = MODE_AUTO_15MIN;
refreshMode();
}
#endif
break;
case MODE_MAX_RATE:
#ifdef WITH_GPS
// join on start
if ( ( state.cState == NOT_JOINED || state.cState == JOIN_FAILED ) && canLoRaSend() ) fireMessage = true;
// in case no GPS, we switch to 60s minimum
// for indoor test, manual mode can be use
if ( ! gps.isReady || ! gpsQualityIsGoodEnough() ) {
if ( cTime >= ( 1 * 60 * 1000 ) && canLoRaSend() ) fireMessage = true;
} else {
// check distance with the previous sent position
// under 50 meters we send messages on every minutes.
#ifndef NODISTANCECHECK
if ( gpsEstimateDistance() > 50 && canLoRaSend() ) fireMessage = true;
else if ( cTime >= ( 1 * 60 * 1000 ) && canLoRaSend() ) fireMessage = true;
#else
// to facilitate debugging
if ( canLoRaSend() ) fireMessage = true;
#endif
}
if ( noMovementCounter >= MAXNONMOVEMENT_DURATION_MS ) {
ui.selected_mode = MODE_AUTO_15MIN;
refreshMode();
}
#else
if ( canLoRaSend() ) fireMessage = true;
#endif
break;
}
if ( ui.selected_display == DISPLAY_DISCO ) {
// send a SF10, max power message on every 30 seconds for 5 minutes
switch ( state.discoveryState ) {
case DISCO_READY:
// wait for start
//nothing to do
break;
case DISCO_WAIT:
// wait for the end of the duty cycle
if ( state.cState >= JOINED && canLoRaSend()
#ifdef WITH_GPS
&& gps.isReady && gpsQualityIsGoodEnough()
#endif
) {
state.startingPosition = gpsEncodePosition48b();
state.discoveryState = DISCO_TX;
state.lastSendMs = 0xFFFFFFFF;
state.totalSent = 0;
refreshDisco();
}
break;
case DISCO_TX:
// do transmit
if ( millis() < state.lastSendMs || ( millis() - state.lastSendMs ) > DISCO_TIME_MS ) {
state.lastSendMs = millis();
// we send a message
myFrame[0] = (state.startingPosition >> 40) & 0xFF;
myFrame[1] = (state.startingPosition >> 32) & 0xFF;
myFrame[2] = (state.startingPosition >> 24) & 0xFF;
myFrame[3] = (state.startingPosition >> 16) & 0xFF;
myFrame[4] = (state.startingPosition >> 8) & 0xFF;
myFrame[5] = (state.startingPosition ) & 0xFF;
do_send(3, myFrame, 6, 10, state.cPwr, false, 0); // send frame SF10 (works everywhere, no ack, no retry on port 3)
state.totalSent++;
if ( state.totalSent >= DISCO_FRAMES ) {
// end
state.discoveryState = DISCO_END;
}
refreshDisco();
}
break;
case DISCO_PAUSE:
// pause the discovery execution
// nothing to be done
break;
case DISCO_END:
// print QR
// do nothing, wait for the end
break;
}
} else {
if ( state.cState == EMPTY_DWNLINK && canLoRaSend() ) {
// clean the downlink queue
// send messages on port2 : the backend will not proceed port 2.
do_send(2, emptyFrame, sizeof(emptyFrame),getCurrentSf(), state.cPwr,true, state.cRetry);
state.eptyLoops++;
if ( state.eptyLoops > MAXDOWNLINKRETRY ) {
state.cState = JOINED;
state.eptyLoops = 0;
}
} else if ( fireMessage ) {
// send a new test message on port 1, backend will create a downlink with information about network side reception
cTime = 0;
// Fill the frame
#ifdef WITH_GPS
if ( gps.isReady && gpsQualityIsGoodEnough() ) {
gpsBackupPosition();
uint64_t pos = gpsEncodePosition48b();
myFrame[0] = (pos >> 40) & 0xFF;
myFrame[1] = (pos >> 32) & 0xFF;
myFrame[2] = (pos >> 24) & 0xFF;
myFrame[3] = (pos >> 16) & 0xFF;
myFrame[4] = (pos >> 8) & 0xFF;
myFrame[5] = (pos ) & 0xFF;
myFrame[6] = ((gps.altitude+1000) >> 8) & 0xFF;
myFrame[7] = ((gps.altitude+1000) ) & 0xFF;
myFrame[8] = (uint8_t)gps.hdop / 10;
myFrame[9] = gps.sats;
} else {
bzero(myFrame, sizeof(myFrame));
}
#else
bzero(myFrame, sizeof(myFrame));
#endif
do_send(1, myFrame, sizeof(myFrame),getCurrentSf(), state.cPwr,true, state.cRetry);
}
}
loraLoop();
gpsLoop();
// Wait for the next loop and update time with overflow consideration
delay(10);
long duration = millis() - sTime;
if ( duration < 0 ) duration = 10;
cTime += duration;
batUpdateTime += duration;
// Time duration without movement
#ifdef WITH_GPS
if ( ! gps.isReady || ! gpsQualityIsGoodEnough() || gpsEstimateDistance() < 50 ) {
if ( noMovementCounter < MAXNONMOVEMENT_DURATION_MS ) {
noMovementCounter += duration;
}
} else {
noMovementCounter = 0;
}
#endif
}