diff --git a/platformio.ini b/platformio.ini index 763c81f..06ad259 100644 --- a/platformio.ini +++ b/platformio.ini @@ -30,18 +30,17 @@ build_flags = #-D DEBUG_ESP_WIFI #-D DEBUG_ESP_CORE #-D SKIP_SLEEPMODE - #-D DOUBLERESETDETECTOR_DEBUG true -D CFG_DISABLE_LOGGING # Turn off verbose logging for some of the parts (too much will cause a crash) but also add space -D GYRO_DISABLE_LOGGING -D PUSH_DISABLE_LOGGING -D TSEN_DISABLE_LOGGING -D WEB_DISABLE_LOGGING - #-D MAIN_DISABLE_LOGGING + -D MAIN_DISABLE_LOGGING -D USE_LITTLEFS=true -D EMBED_HTML # If this is not used the html files needs to be on the file system (can be uploaded) -D USER_SSID=\""\"" # =\""myssid\"" -D USER_SSID_PWD=\""\"" # =\""mypwd\"" - -D CFG_APPVER="\"0.5.0\"" + -D CFG_APPVER="\"0.5.1\"" lib_deps = # Switched to forks for better version control. # Using local copy of this library #https://github.com/jrowberg/i2cdevlib.git# @@ -66,17 +65,19 @@ extra_scripts = script/create_versionjson.py build_unflags = ${common_env_data.build_unflags} + -D MAIN_DISABLE_LOGGING build_flags = - -D PIO_FRAMEWORK_ARDUINO_ENABLE_EXCEPTIONS - #-D SKIP_SLEEPMODE ${common_env_data.build_flags} + #-D PIO_FRAMEWORK_ARDUINO_ENABLE_EXCEPTIONS + #-D SKIP_SLEEPMODE + -D DOUBLERESETDETECTOR_DEBUG=true -D COLLECT_PERFDATA # This option will collect runtime data for a few defined methods to measure time, dumped to serial and/or influxdb -D LOG_LEVEL=6 # Maximum log level for the debug build. lib_deps = ${common_env_data.lib_deps} -#board = ${common_env_data.board} -board = nodemcuv2 -build_type = debug +board = ${common_env_data.board} +#build_type = debug # Using debug type crashes my devkit... +build_type = release board_build.filesystem = littlefs monitor_filters = esp8266_exception_decoder diff --git a/src/main.cpp b/src/main.cpp index 5c76816..15bd177 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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; } } diff --git a/src/wifi.cpp b/src/wifi.cpp index ae2345d..0f987c1 100644 --- a/src/wifi.cpp +++ b/src/wifi.cpp @@ -21,73 +21,54 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include -#include #include #include -//#include #include #include +#include #include #include #include #include #include - -/* -// Configuration settings for WifiManager_Lite -#define ESP_WM_LITE_DEBUG_OUTPUT Serial -#define _ESP_WM_LITE_LOGLEVEL_ 3 -#define MULTIRESETDETECTOR_DEBUG true -#define USE_DYNAMIC_PARAMETERS false -#define CONFIG_TIMEOUT 120*1000 -#define USING_MRD false // We use DRD instead -#define REQUIRE_ONE_SET_SSID_PW true -#include -bool LOAD_DEFAULT_CONFIG_DATA = false; -ESP_WiFiManager_Lite* wifiManager = 0; -ESP_WM_LITE_Configuration defaultConfig; -*/ +#include // Settings for DRD -#define ESP_DRD_USE_LITTLEFS true -#define ESP_DRD_USE_SPIFFS false -#define ESP_DRD_USE_EEPROM false -#define DOUBLERESETDETECTOR_DEBUG true -#include +#define ESP_DRD_USE_LITTLEFS true +#define ESP_DRD_USE_SPIFFS false +#define ESP_DRD_USE_EEPROM false +#include #define DRD_TIMEOUT 3 #define DRD_ADDRESS 0 // Settings for WIFI Manager -/*#define USING_AFRICA false -#define USING_AMERICA true -#define USING_ANTARCTICA false -#define USING_ASIA false -#define USING_ATLANTIC false -#define USING_AUSTRALIA false -#define USING_EUROPE true -#define USING_INDIAN false -#define USING_PACIFIC false -#define USING_ETC_GMT false -#define USE_CLOUDFLARE_NTP false -#define CONFIG_FILENAME F("/wifi_cred.dat")*/ #define USE_ESP_WIFIMANAGER_NTP false -#define USE_CLOUDFLARE_NTP false -#define USING_CORS_FEATURE false -#define NUM_WIFI_CREDENTIALS 1 +#define USE_CLOUDFLARE_NTP false +#define USING_CORS_FEATURE false +#define NUM_WIFI_CREDENTIALS 1 #define USE_STATIC_IP_CONFIG_IN_CP false #include -const char WM_HTTP_FORM_START[] PROGMEM = "
"; +// Override the look and feel of the standard ui (hide secondary forms) +const char WM_HTTP_FORM_START[] PROGMEM = + "
"; #include -ESP_WiFiManager* myWifiManager; -DoubleResetDetector* myDRD; +ESP_WiFiManager *myWifiManager; +DoubleResetDetector *myDRD; WifiConnection myWifi; const char *userSSID = USER_SSID; -const char *userPWD = USER_SSID_PWD; +const char *userPWD = USER_SSID_PWD; -const int PIN_LED = 2; +const int PIN_LED = 2; // // Constructor @@ -100,42 +81,30 @@ WifiConnection::WifiConnection() { // Check if we have a valid wifi configuration // bool WifiConnection::hasConfig() { - if (strlen(myConfig.getWifiSSID()) ) return true; - if (strlen(userSSID) ) return true; + if (strlen(myConfig.getWifiSSID())) return true; + if (strlen(userSSID)) return true; return false; } // // Check if the wifi is connected // -bool WifiConnection::isConnected() { - return WiFi.status() == WL_CONNECTED; -} +bool WifiConnection::isConnected() { return WiFi.status() == WL_CONNECTED; } // // Get the IP adress // -String WifiConnection::getIPAddress() { - return WiFi.localIP().toString(); -} +String WifiConnection::getIPAddress() { return WiFi.localIP().toString(); } // -// Additional method to detect double reset. +// Additional method to detect double reset. // -bool WifiConnection::isDoubleResetDetected() { - if (myDRD->detectDoubleReset()) { - Log.notice(F("WIFI: Double reset has been detected." CR)); - return true; - } - return false; -} +bool WifiConnection::isDoubleResetDetected() { return myDRD->detectDoubleReset(); } // // Stop double reset detection // -void WifiConnection::stopDoubleReset() { - myDRD->stop(); -} +void WifiConnection::stopDoubleReset() { myDRD->stop(); } // // Start the wifi manager @@ -144,21 +113,21 @@ void WifiConnection::startPortal() { Log.notice(F("WIFI: Starting Wifi config portal." CR)); pinMode(PIN_LED, OUTPUT); - digitalWrite(PIN_LED, LOW); + digitalWrite(PIN_LED, LOW); myWifiManager = new ESP_WiFiManager(WIFI_MDNS); myWifiManager->setMinimumSignalQuality(-1); myWifiManager->setConfigPortalChannel(0); myWifiManager->setConfigPortalTimeout(120); - if (myWifiManager->startConfigPortal(WIFI_DEFAULT_SSID, WIFI_DEFAULT_PWD)) { - Log.notice(F("WIFI: Exited portal, connected to wifi." CR)); + if (myWifiManager->startConfigPortal(WIFI_DEFAULT_SSID, WIFI_DEFAULT_PWD)) { + Log.notice(F("WIFI: Exited portal, connected to wifi. Rebooting..." CR)); myConfig.setWifiSSID(myWifiManager->getSSID()); myConfig.setWifiPass(myWifiManager->getPW()); myConfig.saveFile(); - } - else { - Log.notice(F("WIFI: Exited portal, no connection to wifi." CR)); Serial.println(F("Not connected to WiFi")); + } else { + Log.notice( + F("WIFI: Exited portal, no connection to wifi. Rebooting..." CR)); } stopDoubleReset(); @@ -169,9 +138,7 @@ void WifiConnection::startPortal() { // // Call the wifi manager in loop // -void WifiConnection::loop() { - myDRD->loop(); -} +void WifiConnection::loop() { myDRD->loop(); } // // Connect to last known access point or create one if connection is not @@ -372,7 +339,8 @@ bool WifiConnection::checkFirmwareVersion() { // // Parse a version string in the format M.m.p (eg. 1.2.10) // -bool WifiConnection::parseFirmwareVersionString(int (&num)[3], const char *version) { +bool WifiConnection::parseFirmwareVersionString(int (&num)[3], + const char *version) { #if LOG_LEVEL == 6 Log.verbose(F("WIFI: Parsing version number string %s." CR), version); #endif diff --git a/src/wifi.hpp b/src/wifi.hpp index 0334b6d..67b3bc6 100644 --- a/src/wifi.hpp +++ b/src/wifi.hpp @@ -25,7 +25,6 @@ SOFTWARE. #define SRC_WIFI_HPP_ // Include -//#include #include // classes @@ -41,15 +40,16 @@ class WifiConnection { public: // WIFI WifiConnection(); - bool connect(); - bool disconnect(); - bool isConnected(); - bool isDoubleResetDetected(); - void stopDoubleReset(); - bool hasConfig(); + + bool connect(); + bool disconnect(); + bool isConnected(); + bool isDoubleResetDetected(); + void stopDoubleReset(); + bool hasConfig(); String getIPAddress(); - void startPortal(); - void loop(); + void startPortal(); + void loop(); // OTA bool updateFirmware();