-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmain.ino
122 lines (97 loc) · 4.06 KB
/
main.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
/* ======================================================================
This file is part of disk91_tracker.
disk91_tracker is free software: 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
(at your option) any later version.
Foobar 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 Foobar. If not, see <http://www.gnu.org/licenses/>.
=======================================================================
*/
/* ======================================================================
* ESP8266 Main module
* ----------------------------------------------------------------------
* (c) Disk91.com - 2018
* Author : Paul Pinault aka disk91.com
* ----------------------------------------------------------------------
*/
#include <ESP8266WiFi.h>
#include "config.h"
#include "debug.h"
#include "logger.h"
#include "low_power.h"
#include "tracker.h"
int bootTime,bootCycle;
bool debugMode = false;
bool debugModeLoop = false;
bool inCommandMode = false;
void setup() {
bootTime = millis();
// Enable WatchDog
ESP.wdtEnable(32000); // 32s watchdog
Serial.begin(LOGGER_SERIAL_DEFAULT_SPEED); // needed for config load & lowpower trace on Serial
// Disable Wifi
WiFi.disconnect();
WiFi.mode(WIFI_OFF);
WiFi.forceSleepBegin();
delay(1);
// On start, check the STOP PIN, in zero => stop execution
// This is protecting against programmation issue when running deep sleep continuously
pinMode(D5,INPUT);
if ( digitalRead(D5) == LOW ) {
debugMode = true;
}
TTRACE1(("BootTime %d \r\n", bootTime));
}
void manageCommand() {
char c;
if ( Serial.available() ) {
char c = Serial.read();
if ( c == '!' ) {
inCommandMode = true;
} else if ( inCommandMode ) {
if ( c == 'd' ) { TTRACE(("switch to debug Mode\r\n")); debugMode = true; }
trackrService.processCommands(c);
inCommandMode = false;
}
}
}
void loop() {
// ----
// For real this loop will be executed only one time after every deep sleep wake up
uint32_t elapsed = ( debugModeLoop )? 0 : millis();
if ( debugModeLoop || lowPowerService.wakeUp((uint8_t*)&trackrService.state, &trackrService.state.crc32, sizeof(trackrService.state)) ) {
// This is a standard loop from a device wake up signal or after an internal wait loop
// We execute all what we have to do on regular basis
trackrService.execute(SCHEDULER_PERIOD_MS+elapsed); // Load the context from RTC memory & execute actions
} else {
// This is the first loop after powering ON the device
// We can call boot method to execute all the first time settings
trackrService.boot(elapsed+5000); // What have to be done just one time on startup from a reset
// wait for potential debug mode switch for 5 seconds
uint32_t start = millis();
while ( (millis() - start) < 5000 ) {
manageCommand();
}
}
if ( ! debugMode ) {
// In the normal mod the ESP8266 is going deep sleep
// going deep sleep...
lowPowerService.deepSleep( SCHEDULER_PERIOD_MS,(uint8_t*)&trackrService.state, &trackrService.state.crc32, sizeof(trackrService.state) );
} else {
// debug mode, no sleep, always run so we can listen for command on the
// serial line
uint32_t start = millis();
while ( (millis() - start) < SCHEDULER_PERIOD_MS ) {
manageCommand();
delay(1);
yield();
ESP.wdtFeed();
}
}
if ( debugMode ) debugModeLoop = true;
}