From 2f391c95c7374aefe02d7319d8483478a66ebc9b Mon Sep 17 00:00:00 2001 From: Magnus Persson Date: Mon, 10 Jan 2022 20:14:58 +0100 Subject: [PATCH] Updated code to 0.6 test --- src/main.cpp | 14 +++++++------- src/wifi.cpp | 35 ++++++++++++++++++++++++----------- src/wifi.hpp | 2 ++ src_docs/source/index.rst | 3 ++- src_docs/source/releases.rst | 11 +++++++++++ 5 files changed, 46 insertions(+), 19 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 15bd177..afc77c0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -210,11 +210,9 @@ bool loopReadGravity() { float temp = myTempSensor.getTempC(); LOG_PERF_STOP("loop-temp-read"); - float gravity = calculateGravity( - angle, temp); // Takes less than 2ms , so skip this measurment + float gravity = calculateGravity(angle, temp); float corrGravity = gravityTemperatureCorrection( - gravity, temp, myConfig.getTempFormat()); // Takes less than 2ms , so - // skip this measurment + gravity, temp, myConfig.getTempFormat()); #if LOG_LEVEL == 6 && !defined(MAIN_DISABLE_LOGGING) Log.verbose(F("Main: Sensor values gyro angle=%F, temp=%F, gravity=%F, " @@ -261,10 +259,11 @@ void loopGravityOnInterval() { // void goToSleep(int sleepInterval) { float volt = myBatteryVoltage.getVoltage(); + float runtime = (millis() - runtimeMillis); - Log.notice(F("MAIN: Entering deep sleep for %d s, run time %l s, " - "battery=%F V." CR), - sleepInterval, (millis() - runtimeMillis) / 1000, volt); + Log.notice(F("MAIN: Entering deep sleep for %ds, run time %Fs, " + "battery=%FV." CR), + sleepInterval, reduceFloatPrecision(runtime/1000, 2), volt); LittleFS.end(); myGyro.enterSleep(); LOG_PERF_STOP("run-time"); @@ -282,6 +281,7 @@ void loop() { myWebServer.loop(); myWifi.loop(); loopGravityOnInterval(); + myBatteryVoltage.read(); break; case RunMode::gravityMode: diff --git a/src/wifi.cpp b/src/wifi.cpp index 0f987c1..ede0971 100644 --- a/src/wifi.cpp +++ b/src/wifi.cpp @@ -141,13 +141,10 @@ void WifiConnection::startPortal() { void WifiConnection::loop() { myDRD->loop(); } // -// Connect to last known access point or create one if connection is not -// working. +// Connect to last known access point, non blocking mode. // -bool WifiConnection::connect() { - // Connect to wifi - int i = 0; - +void WifiConnection::connectAsync() { + WiFi.persistent(true); WiFi.mode(WIFI_STA); if (strlen(userSSID)) { Log.notice(F("WIFI: Connecting to wifi using hardcoded settings %s." CR), @@ -158,14 +155,22 @@ bool WifiConnection::connect() { myConfig.getWifiSSID()); WiFi.begin(myConfig.getWifiSSID(), myConfig.getWifiPass()); } +} - // WiFi.printDiag(Serial); - +// +// Blocks until wifi connection has been found +// +bool WifiConnection::waitForConnection(int maxTime) { +#if DEBUG_LEVEL == 6 + WiFi.printDiag(Serial); +#endif + int i = 0; while (WiFi.status() != WL_CONNECTED) { - delay(200); - Serial.print("."); + delay(100); - if (i++ > 100) { // Try for 20 seconds. + if ( i % 10 ) Serial.print("."); + + if (i++ > (maxTime*10)) { // Try for maxTime seconds. Since delay is 100ms. Log.error(F("WIFI: Failed to connect to wifi %d, aborting %s." CR), WiFi.status(), getIPAddress().c_str()); WiFi.disconnect(); @@ -180,6 +185,14 @@ bool WifiConnection::connect() { return true; } +// +// Connect to last known access point, blocking mode. +// +bool WifiConnection::connect() { + connectAsync(); + return waitForConnection(20); // 20 seconds. +} + // // This will erase the stored credentials and forcing the WIFI manager to AP // mode. diff --git a/src/wifi.hpp b/src/wifi.hpp index 67b3bc6..4854ffb 100644 --- a/src/wifi.hpp +++ b/src/wifi.hpp @@ -36,6 +36,8 @@ class WifiConnection { bool newFirmware = false; bool parseFirmwareVersionString(int (&num)[3], const char *version); void downloadFile(const char *fname); + void connectAsync(); + bool waitForConnection(int maxTime = 20); public: // WIFI diff --git a/src_docs/source/index.rst b/src_docs/source/index.rst index d3bb0a1..458af55 100644 --- a/src_docs/source/index.rst +++ b/src_docs/source/index.rst @@ -30,6 +30,7 @@ be found here; `GravityMon on Github `_ I dont take responsibility for any errors that can cause problems with the use. I have tested v0.4 on 5+ brews over the last 6 months without any issues. + The main differences: --------------------- @@ -67,7 +68,7 @@ the following libraries and without these this would have been much more difficu Can detect if the reset button is pressed twice, is used to enter WIFI config mode. -* https://github.com/tzapu/WiFiManager +* https://github.com/khoih-prog/ESP_WiFiManager Configure wifi settings. diff --git a/src_docs/source/releases.rst b/src_docs/source/releases.rst index 3095f67..2fc2a08 100644 --- a/src_docs/source/releases.rst +++ b/src_docs/source/releases.rst @@ -3,6 +3,17 @@ Releases ######## +v0.6.0 (work in progress) +------------------------- + +This is features for the next release. + +* Changed the wifi manager and refactored wifi.cpp +* LED is now turned on when Wifi Portal is open +* Refactored main.cpp to make it easier to read +* Tested runtime performance +* Improved documentation + v0.5.0 ------