-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWioHeliumMonitor.ino
133 lines (118 loc) · 3.41 KB
/
WioHeliumMonitor.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
/**
* This file is part of Wio Helium Monitor.
*
* Wio Helium Monitor 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 Helium Monitor 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)
*
* Dependencies
* - seeed Arduino rpcUnified
* - seeed Arduino rpcWifi
* - seeed Arduino mbdetps
*/
#include "Arduino.h"
#include "config.h"
#include "ui.h"
#include "wifi.h"
#include "state.h"
#include "keys.h"
#include "sound.h"
#include "watchdog.h"
#include "LoraCom.h"
#include "memory.h"
state_t state;
void setup() {
memoryTraceInit();
#if defined SERIALCONFIG || defined DEBUG
Serial.begin(9600);
while (!Serial && millis() < 5000);
LOGLN((""));
LOGLN(("####################################################"));
#endif
initScreen();
displayTitle();
delay(1000);
if ( digitalRead(WIO_5S_PRESS) == LOW ) {
while ( digitalRead(WIO_5S_PRESS) == LOW );
wifiConfig();
NVIC_SystemReset();
}
setupSound();
initState();
displaySplash();
setupWifi();
connectingLoRa();
loraSetup();
clearScreen();
initWatchdog();
enableWatchdog(20000);
}
#define PING_PERIOD_MS (30*1000)
#define LORAPING_PERIOD_MS (20*60*1000)
#define REPORT_PERIOD_MS (15*60*1000)
#define SOUND_PERIOD_MS (1*60*1000)
void loop() {
static uint32_t cTime = 0;
static uint32_t pTime = PING_PERIOD_MS - 1000; // last ping
//static uint32_t lTime = 0; // last LoRa ping
static uint32_t lTime = LORAPING_PERIOD_MS - 5000; // for test
static uint32_t rTime = REPORT_PERIOD_MS - 1000; // report time
static uint32_t sTime = 0; // last sound played
static uint32_t tTime = 0; // Total time
uint32_t stTime;
iAmAlive();
stTime = millis();
if ( pTime >= PING_PERIOD_MS ) {
#ifdef DEBUG
usedHeap();
#endif
runMonitor();
pTime = 0;
if ( rTime >= REPORT_PERIOD_MS ) {
// only report data when the external ping works
// over network, otherwise make non sens and will crash the Wio
if ( !reportData() ) {
lTime = 0; // reported with LoRaWan
}
rTime = 0;
}
}
if ( lTime >= LORAPING_PERIOD_MS ) {
runLoRaPing();
lTime = 0;
}
refreshUI();
// Play sound notification when a problem is detected
if ( state.withSound && sTime > SOUND_PERIOD_MS) {
if ( state.intState == 0 || state.extState == 0 || ( state.hsState > 0 && state.hsState != 99 ) ) {
playSound();
sTime = 0;
}
}
loraLoop();
delay(10);
long duration = millis() - stTime;
if ( duration < 0 ) duration = 10;
cTime += duration;
pTime += duration;
rTime += duration;
sTime += duration;
lTime += duration;
tTime += duration;
if ( tTime > 1000 ) {
state.uptime += tTime / 1000;
tTime = (tTime % 1000);
}
}