Integrated changed for new wifimgr

This commit is contained in:
Magnus Persson
2022-01-10 10:27:24 +01:00
parent 4ff114642e
commit 93d9effcef
4 changed files with 120 additions and 143 deletions

View File

@ -34,21 +34,18 @@ SOFTWARE.
// Define constats for this program
#ifdef DEACTIVATE_SLEEPMODE
const int interval = 1000; // ms, time to wait between changes to output
const int interval = 1000; // ms, time to wait between changes to output
#else
int interval = 200; // ms, time to wait between changes to output
bool sleepModeAlwaysSkip = false; // Web interface can override normal behaviour
int interval = 200; // ms, time to wait between changes to output
#endif
uint32_t loopMillis = 0;// Used for main loop to run the code every _interval_
uint32_t runtimeMillis; // Used to calculate the total time since start/wakeup
uint32_t stableGyroMillis; // Used to calculate the total time since last
// stable gyro reading
bool sleepModeAlwaysSkip =
false; // Flag set in web interface to override normal behaviour
uint32_t loopMillis = 0; // Used for main loop to run the code every _interval_
uint32_t runtimeMillis; // Used to calculate the total time since start/wakeup
uint32_t stableGyroMillis; // Used to calculate the total time since last
// stable gyro reading
enum RunMode {
gravityMode = 0,
configurationMode = 1,
wifiSetupMode = 2
};
enum RunMode { gravityMode = 0, configurationMode = 1, wifiSetupMode = 2 };
RunMode runMode = RunMode::gravityMode;
@ -64,15 +61,18 @@ void checkSleepMode(float angle, float volt) {
const RawGyroData &g = myConfig.getGyroCalibration();
if (g.ax == 0 && g.ay == 0 && g.az == 0 && g.gx == 0 && g.gy == 0 && g.gz == 0) {
if (!g.ax && !g.ay && !g.az && !g.gx && !g.gy && !g.gz) {
// Will not enter sleep mode if: no calibration data
Log.notice(F("MAIN: Missing calibration data, so forcing webserver to be active." CR));
Log.notice(
F("MAIN: Missing calibration data, so forcing webserver to be "
"active." CR));
runMode = RunMode::configurationMode;
} else if (sleepModeAlwaysSkip) {
// Check if the flag from the UI has been set, the we force configuration mode.
// Check if the flag from the UI has been set, the we force configuration
// mode.
Log.notice(F("MAIN: Sleep mode disabled from web interface." CR));
runMode = RunMode::configurationMode;
} else if ( (volt < 4.15 && (angle > 85 && angle < 95)) || (volt > 4.15) ) {
} else if ((volt < 4.15 && (angle > 85 && angle < 95)) || (volt > 4.15)) {
runMode = RunMode::configurationMode;
} else {
runMode = RunMode::gravityMode;
@ -80,13 +80,15 @@ void checkSleepMode(float angle, float volt) {
switch (runMode) {
case RunMode::configurationMode:
Log.notice(F("MAIN: run mode CONFIG (angle=%F volt=%F)." CR), angle, volt);
break;
Log.notice(F("MAIN: run mode CONFIG (angle=%F volt=%F)." CR), angle,
volt);
break;
case RunMode::wifiSetupMode:
break;
break;
case RunMode::gravityMode:
Log.notice(F("MAIN: run mode GRAVITY (angle=%F volt=%F)." CR), angle, volt);
break;
Log.notice(F("MAIN: run mode GRAVITY (angle=%F volt=%F)." CR), angle,
volt);
break;
}
}
@ -99,8 +101,8 @@ void setup() {
runtimeMillis = millis();
#if LOG_LEVEL == 6 && !defined(MAIN_DISABLE_LOGGING)
delay(3000); // Wait a few seconds when using debug version so that serial is
// started.
// Add a delay so that serial is started.
// delay(3000);
Log.verbose(F("Main: Reset reason %s." CR), ESP.getResetInfo().c_str());
#endif
// Main startup
@ -115,16 +117,17 @@ void setup() {
// Setup watchdog
ESP.wdtDisable();
ESP.wdtEnable(5000); // 5 seconds
ESP.wdtEnable(5000); // 5 seconds
// No stored config, move to portal
if( !myWifi.hasConfig() ) {
Log.notice(F("Main: No wifi configuration detected, entering wifi setup." CR));
if (!myWifi.hasConfig()) {
Log.notice(
F("Main: No wifi configuration detected, entering wifi setup." CR));
runMode = RunMode::wifiSetupMode;
}
// Double reset, go to portal.
if( myWifi.isDoubleResetDetected() ) {
if (myWifi.isDoubleResetDetected()) {
Log.notice(F("Main: Double reset detected, entering wifi setup." CR));
runMode = RunMode::wifiSetupMode;
}
@ -133,7 +136,7 @@ void setup() {
switch (runMode) {
case RunMode::wifiSetupMode:
myWifi.startPortal();
break;
break;
default:
LOG_PERF_START("main-wifi-connect");
@ -152,9 +155,9 @@ void setup() {
LOG_PERF_STOP("main-gyro-read");
}
myBatteryVoltage.read();
myBatteryVoltage.read();
checkSleepMode(myGyro.getAngle(), myBatteryVoltage.getVoltage());
break;
break;
}
// Do this setup for configuration mode
@ -163,18 +166,18 @@ void setup() {
if (myWifi.isConnected()) {
#if defined(ACTIVATE_OTA)
LOG_PERF_START("main-wifi-ota");
if ( myWifi.checkFirmwareVersion())
myWifi.updateFirmware();
if (myWifi.checkFirmwareVersion()) myWifi.updateFirmware();
LOG_PERF_STOP("main-wifi-ota");
#endif
myWebServer.setupWebServer(); // Takes less than 4ms, so skip this measurement
myWebServer
.setupWebServer(); // Takes less than 4ms, so skip this measurement
}
interval = 1000; // Change interval from 200ms to 1s
break;
break;
default:
break;
break;
}
LOG_PERF_STOP("main-setup");
@ -200,7 +203,7 @@ bool loopReadGravity() {
// interval.
//
if (myGyro.hasValue()) {
angle = myGyro.getAngle(); // Gyro angle
angle = myGyro.getAngle(); // Gyro angle
stableGyroMillis = millis(); // Reset timer
LOG_PERF_START("loop-temp-read");
@ -211,17 +214,19 @@ bool loopReadGravity() {
angle, temp); // Takes less than 2ms , so skip this measurment
float corrGravity = gravityTemperatureCorrection(
gravity, temp, myConfig.getTempFormat()); // Takes less than 2ms , so
// skip this measurment
// skip this measurment
#if LOG_LEVEL == 6 && !defined(MAIN_DISABLE_LOGGING)
Log.verbose(F("Main: Sensor values gyro angle=%F, temp=%F, gravity=%F, "
"corr=%F." CR),
angle, temp, gravity, corrGravity);
angle, temp, gravity, corrGravity);
#endif
LOG_PERF_START("loop-push");
// Force the transmission if we are going to sleep
myPushTarget.send(angle, gravity, corrGravity, temp, (millis() - runtimeMillis) / 1000, runMode == RunMode::gravityMode ? true : false );
myPushTarget.send(angle, gravity, corrGravity, temp,
(millis() - runtimeMillis) / 1000,
runMode == RunMode::gravityMode ? true : false);
LOG_PERF_STOP("loop-push");
return true;
} else {
@ -231,7 +236,8 @@ bool loopReadGravity() {
}
//
// Wrapper for loopGravity that only calls every 200ms so that we dont overload this.
// Wrapper for loopGravity that only calls every 200ms so that we dont overload
// this.
//
void loopGravityOnInterval() {
if (abs((int32_t)(millis() - loopMillis)) > interval) {
@ -257,8 +263,8 @@ void goToSleep(int sleepInterval) {
float volt = myBatteryVoltage.getVoltage();
Log.notice(F("MAIN: Entering deep sleep for %d s, run time %l s, "
"battery=%F V." CR),
sleepInterval, (millis() - runtimeMillis) / 1000, volt);
"battery=%F V." CR),
sleepInterval, (millis() - runtimeMillis) / 1000, volt);
LittleFS.end();
myGyro.enterSleep();
LOG_PERF_STOP("run-time");
@ -271,33 +277,35 @@ void goToSleep(int sleepInterval) {
// Main loops
//
void loop() {
// myDRD->loop();
switch (runMode) {
case RunMode::configurationMode:
myWebServer.loop();
myWifi.loop();
loopGravityOnInterval();
break;
break;
case RunMode::gravityMode:
// If we didnt get a wifi connection, we enter sleep for a short time to
// conserve battery.
if (!myWifi.isConnected()) { // no connection to wifi
Log.notice(F("MAIN: No connection to wifi established, sleeping for 60s." CR));
Log.notice(
F("MAIN: No connection to wifi established, sleeping for 60s." CR));
myWifi.stopDoubleReset();
goToSleep(60);
}
if( loopReadGravity() ) {
if (loopReadGravity()) {
myWifi.stopDoubleReset();
goToSleep(myConfig.getSleepInterval());
}
// If the sensor is moving and we are not getting a clear reading, we enter
// sleep for a short time to conserve battery.
if (((millis() - stableGyroMillis) > 10000L)) { // 10s since last stable gyro reading
Log.notice(F("MAIN: Unable to get a stable reading for 10s, sleeping for 60s." CR));
// If the sensor is moving and we are not getting a clear reading, we
// enter sleep for a short time to conserve battery.
if (((millis() - stableGyroMillis) >
10000L)) { // 10s since last stable gyro reading
Log.notice(
F("MAIN: Unable to get a stable reading for 10s, sleeping for "
"60s." CR));
myWifi.stopDoubleReset();
goToSleep(60);
}
@ -306,11 +314,11 @@ void loop() {
myGyro.read();
LOG_PERF_STOP("loop-gyro-read");
myWifi.loop();
break;
break;
case RunMode::wifiSetupMode:
myWifi.loop();
break;
break;
}
}