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

@ -30,18 +30,17 @@ build_flags =
#-D DEBUG_ESP_WIFI #-D DEBUG_ESP_WIFI
#-D DEBUG_ESP_CORE #-D DEBUG_ESP_CORE
#-D SKIP_SLEEPMODE #-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 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 GYRO_DISABLE_LOGGING
-D PUSH_DISABLE_LOGGING -D PUSH_DISABLE_LOGGING
-D TSEN_DISABLE_LOGGING -D TSEN_DISABLE_LOGGING
-D WEB_DISABLE_LOGGING -D WEB_DISABLE_LOGGING
#-D MAIN_DISABLE_LOGGING -D MAIN_DISABLE_LOGGING
-D USE_LITTLEFS=true -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 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=\""\"" # =\""myssid\""
-D USER_SSID_PWD=\""\"" # =\""mypwd\"" -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. lib_deps = # Switched to forks for better version control.
# Using local copy of this library # Using local copy of this library
#https://github.com/jrowberg/i2cdevlib.git#<document> #https://github.com/jrowberg/i2cdevlib.git#<document>
@ -66,17 +65,19 @@ extra_scripts =
script/create_versionjson.py script/create_versionjson.py
build_unflags = build_unflags =
${common_env_data.build_unflags} ${common_env_data.build_unflags}
-D MAIN_DISABLE_LOGGING
build_flags = build_flags =
-D PIO_FRAMEWORK_ARDUINO_ENABLE_EXCEPTIONS
#-D SKIP_SLEEPMODE
${common_env_data.build_flags} ${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 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. -D LOG_LEVEL=6 # Maximum log level for the debug build.
lib_deps = lib_deps =
${common_env_data.lib_deps} ${common_env_data.lib_deps}
#board = ${common_env_data.board} board = ${common_env_data.board}
board = nodemcuv2 #build_type = debug # Using debug type crashes my devkit...
build_type = debug build_type = release
board_build.filesystem = littlefs board_build.filesystem = littlefs
monitor_filters = esp8266_exception_decoder monitor_filters = esp8266_exception_decoder

View File

@ -37,18 +37,15 @@ SOFTWARE.
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 #else
int interval = 200; // ms, time to wait between changes to output int interval = 200; // ms, time to wait between changes to output
bool sleepModeAlwaysSkip = false; // Web interface can override normal behaviour
#endif #endif
uint32_t loopMillis = 0;// Used for main loop to run the code every _interval_ 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 runtimeMillis; // Used to calculate the total time since start/wakeup
uint32_t stableGyroMillis; // Used to calculate the total time since last uint32_t stableGyroMillis; // Used to calculate the total time since last
// stable gyro reading // stable gyro reading
enum RunMode { enum RunMode { gravityMode = 0, configurationMode = 1, wifiSetupMode = 2 };
gravityMode = 0,
configurationMode = 1,
wifiSetupMode = 2
};
RunMode runMode = RunMode::gravityMode; RunMode runMode = RunMode::gravityMode;
@ -64,15 +61,18 @@ void checkSleepMode(float angle, float volt) {
const RawGyroData &g = myConfig.getGyroCalibration(); 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 // 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; runMode = RunMode::configurationMode;
} else if (sleepModeAlwaysSkip) { } 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)); Log.notice(F("MAIN: Sleep mode disabled from web interface." CR));
runMode = RunMode::configurationMode; 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; runMode = RunMode::configurationMode;
} else { } else {
runMode = RunMode::gravityMode; runMode = RunMode::gravityMode;
@ -80,12 +80,14 @@ void checkSleepMode(float angle, float volt) {
switch (runMode) { switch (runMode) {
case RunMode::configurationMode: case RunMode::configurationMode:
Log.notice(F("MAIN: run mode CONFIG (angle=%F volt=%F)." CR), angle, volt); Log.notice(F("MAIN: run mode CONFIG (angle=%F volt=%F)." CR), angle,
volt);
break; break;
case RunMode::wifiSetupMode: case RunMode::wifiSetupMode:
break; break;
case RunMode::gravityMode: case RunMode::gravityMode:
Log.notice(F("MAIN: run mode GRAVITY (angle=%F volt=%F)." CR), angle, volt); Log.notice(F("MAIN: run mode GRAVITY (angle=%F volt=%F)." CR), angle,
volt);
break; break;
} }
} }
@ -99,8 +101,8 @@ void setup() {
runtimeMillis = millis(); runtimeMillis = millis();
#if LOG_LEVEL == 6 && !defined(MAIN_DISABLE_LOGGING) #if LOG_LEVEL == 6 && !defined(MAIN_DISABLE_LOGGING)
delay(3000); // Wait a few seconds when using debug version so that serial is // Add a delay so that serial is started.
// started. // delay(3000);
Log.verbose(F("Main: Reset reason %s." CR), ESP.getResetInfo().c_str()); Log.verbose(F("Main: Reset reason %s." CR), ESP.getResetInfo().c_str());
#endif #endif
// Main startup // Main startup
@ -118,13 +120,14 @@ void setup() {
ESP.wdtEnable(5000); // 5 seconds ESP.wdtEnable(5000); // 5 seconds
// No stored config, move to portal // No stored config, move to portal
if( !myWifi.hasConfig() ) { if (!myWifi.hasConfig()) {
Log.notice(F("Main: No wifi configuration detected, entering wifi setup." CR)); Log.notice(
F("Main: No wifi configuration detected, entering wifi setup." CR));
runMode = RunMode::wifiSetupMode; runMode = RunMode::wifiSetupMode;
} }
// Double reset, go to portal. // Double reset, go to portal.
if( myWifi.isDoubleResetDetected() ) { if (myWifi.isDoubleResetDetected()) {
Log.notice(F("Main: Double reset detected, entering wifi setup." CR)); Log.notice(F("Main: Double reset detected, entering wifi setup." CR));
runMode = RunMode::wifiSetupMode; runMode = RunMode::wifiSetupMode;
} }
@ -163,11 +166,11 @@ void setup() {
if (myWifi.isConnected()) { if (myWifi.isConnected()) {
#if defined(ACTIVATE_OTA) #if defined(ACTIVATE_OTA)
LOG_PERF_START("main-wifi-ota"); LOG_PERF_START("main-wifi-ota");
if ( myWifi.checkFirmwareVersion()) if (myWifi.checkFirmwareVersion()) myWifi.updateFirmware();
myWifi.updateFirmware();
LOG_PERF_STOP("main-wifi-ota"); LOG_PERF_STOP("main-wifi-ota");
#endif #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 interval = 1000; // Change interval from 200ms to 1s
@ -221,7 +224,9 @@ bool loopReadGravity() {
LOG_PERF_START("loop-push"); LOG_PERF_START("loop-push");
// Force the transmission if we are going to sleep // 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"); LOG_PERF_STOP("loop-push");
return true; return true;
} else { } 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() { void loopGravityOnInterval() {
if (abs((int32_t)(millis() - loopMillis)) > interval) { if (abs((int32_t)(millis() - loopMillis)) > interval) {
@ -271,8 +277,6 @@ void goToSleep(int sleepInterval) {
// Main loops // Main loops
// //
void loop() { void loop() {
// myDRD->loop();
switch (runMode) { switch (runMode) {
case RunMode::configurationMode: case RunMode::configurationMode:
myWebServer.loop(); myWebServer.loop();
@ -284,20 +288,24 @@ void loop() {
// If we didnt get a wifi connection, we enter sleep for a short time to // If we didnt get a wifi connection, we enter sleep for a short time to
// conserve battery. // conserve battery.
if (!myWifi.isConnected()) { // no connection to wifi 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(); myWifi.stopDoubleReset();
goToSleep(60); goToSleep(60);
} }
if( loopReadGravity() ) { if (loopReadGravity()) {
myWifi.stopDoubleReset(); myWifi.stopDoubleReset();
goToSleep(myConfig.getSleepInterval()); goToSleep(myConfig.getSleepInterval());
} }
// If the sensor is moving and we are not getting a clear reading, we enter // If the sensor is moving and we are not getting a clear reading, we
// sleep for a short time to conserve battery. // enter sleep for a short time to conserve battery.
if (((millis() - stableGyroMillis) > 10000L)) { // 10s since last stable gyro reading if (((millis() - stableGyroMillis) >
Log.notice(F("MAIN: Unable to get a stable reading for 10s, sleeping for 60s." CR)); 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(); myWifi.stopDoubleReset();
goToSleep(60); goToSleep(60);
} }

View File

@ -21,67 +21,48 @@ 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 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. SOFTWARE.
*/ */
#include <wifi.hpp>
#include <ArduinoJson.hpp>
#include <ESP8266HTTPClient.h> #include <ESP8266HTTPClient.h>
#include <ESP8266httpUpdate.h> #include <ESP8266httpUpdate.h>
//#include <ESP8266mDNS.h>
#include <LittleFS.h> #include <LittleFS.h>
#include <incbin.h> #include <incbin.h>
#include <ArduinoJson.hpp>
#include <calc.hpp> #include <calc.hpp>
#include <config.hpp> #include <config.hpp>
#include <gyro.hpp> #include <gyro.hpp>
#include <helper.hpp> #include <helper.hpp>
#include <tempsensor.hpp> #include <tempsensor.hpp>
#include <wifi.hpp>
/*
// 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 <ESP_WiFiManager_Lite.h>
bool LOAD_DEFAULT_CONFIG_DATA = false;
ESP_WiFiManager_Lite* wifiManager = 0;
ESP_WM_LITE_Configuration defaultConfig;
*/
// Settings for DRD // Settings for DRD
#define ESP_DRD_USE_LITTLEFS true #define ESP_DRD_USE_LITTLEFS true
#define ESP_DRD_USE_SPIFFS false #define ESP_DRD_USE_SPIFFS false
#define ESP_DRD_USE_EEPROM false #define ESP_DRD_USE_EEPROM false
#define DOUBLERESETDETECTOR_DEBUG true
#include <ESP_DoubleResetDetector.h> #include <ESP_DoubleResetDetector.h>
#define DRD_TIMEOUT 3 #define DRD_TIMEOUT 3
#define DRD_ADDRESS 0 #define DRD_ADDRESS 0
// Settings for WIFI Manager // 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_ESP_WIFIMANAGER_NTP false
#define USE_CLOUDFLARE_NTP false #define USE_CLOUDFLARE_NTP false
#define USING_CORS_FEATURE false #define USING_CORS_FEATURE false
#define NUM_WIFI_CREDENTIALS 1 #define NUM_WIFI_CREDENTIALS 1
#define USE_STATIC_IP_CONFIG_IN_CP false #define USE_STATIC_IP_CONFIG_IN_CP false
#include <ESP_WiFiManager.h> #include <ESP_WiFiManager.h>
const char WM_HTTP_FORM_START[] PROGMEM = "<form method='get' action='wifisave'><fieldset><div><label>SSID</label><input id='s' name='s' length=32 placeholder='SSID'><div></div></div><div><label>Password</label><input id='p' name='p' length=64 placeholder='password'><div></div></div><div hidden><label>SSID1</label><input id='s1' name='s1' length=32 placeholder='SSID1'><div></div></div><div hidden><label>Password</label><input id='p1' name='p1' length=64 placeholder='password1'><div></div></div></fieldset>"; // Override the look and feel of the standard ui (hide secondary forms)
const char WM_HTTP_FORM_START[] PROGMEM =
"<form method='get' "
"action='wifisave'><fieldset><div><label>SSID</label><input id='s' "
"name='s' length=32 "
"placeholder='SSID'><div></div></div><div><label>Password</label><input "
"id='p' name='p' length=64 placeholder='password'><div></div></div><div "
"hidden><label>SSID1</label><input id='s1' name='s1' length=32 "
"placeholder='SSID1'><div></div></div><div "
"hidden><label>Password</label><input id='p1' name='p1' length=64 "
"placeholder='password1'><div></div></div></fieldset>";
#include <ESP_WiFiManager-Impl.h> #include <ESP_WiFiManager-Impl.h>
ESP_WiFiManager* myWifiManager; ESP_WiFiManager *myWifiManager;
DoubleResetDetector* myDRD; DoubleResetDetector *myDRD;
WifiConnection myWifi; WifiConnection myWifi;
const char *userSSID = USER_SSID; const char *userSSID = USER_SSID;
@ -100,42 +81,30 @@ WifiConnection::WifiConnection() {
// Check if we have a valid wifi configuration // Check if we have a valid wifi configuration
// //
bool WifiConnection::hasConfig() { bool WifiConnection::hasConfig() {
if (strlen(myConfig.getWifiSSID()) ) return true; if (strlen(myConfig.getWifiSSID())) return true;
if (strlen(userSSID) ) return true; if (strlen(userSSID)) return true;
return false; return false;
} }
// //
// Check if the wifi is connected // Check if the wifi is connected
// //
bool WifiConnection::isConnected() { bool WifiConnection::isConnected() { return WiFi.status() == WL_CONNECTED; }
return WiFi.status() == WL_CONNECTED;
}
// //
// Get the IP adress // Get the IP adress
// //
String WifiConnection::getIPAddress() { String WifiConnection::getIPAddress() { return WiFi.localIP().toString(); }
return WiFi.localIP().toString();
}
// //
// Additional method to detect double reset. // Additional method to detect double reset.
// //
bool WifiConnection::isDoubleResetDetected() { bool WifiConnection::isDoubleResetDetected() { return myDRD->detectDoubleReset(); }
if (myDRD->detectDoubleReset()) {
Log.notice(F("WIFI: Double reset has been detected." CR));
return true;
}
return false;
}
// //
// Stop double reset detection // Stop double reset detection
// //
void WifiConnection::stopDoubleReset() { void WifiConnection::stopDoubleReset() { myDRD->stop(); }
myDRD->stop();
}
// //
// Start the wifi manager // Start the wifi manager
@ -152,13 +121,13 @@ void WifiConnection::startPortal() {
myWifiManager->setConfigPortalTimeout(120); myWifiManager->setConfigPortalTimeout(120);
if (myWifiManager->startConfigPortal(WIFI_DEFAULT_SSID, WIFI_DEFAULT_PWD)) { if (myWifiManager->startConfigPortal(WIFI_DEFAULT_SSID, WIFI_DEFAULT_PWD)) {
Log.notice(F("WIFI: Exited portal, connected to wifi." CR)); Log.notice(F("WIFI: Exited portal, connected to wifi. Rebooting..." CR));
myConfig.setWifiSSID(myWifiManager->getSSID()); myConfig.setWifiSSID(myWifiManager->getSSID());
myConfig.setWifiPass(myWifiManager->getPW()); myConfig.setWifiPass(myWifiManager->getPW());
myConfig.saveFile(); myConfig.saveFile();
} } else {
else { Log.notice(
Log.notice(F("WIFI: Exited portal, no connection to wifi." CR)); Serial.println(F("Not connected to WiFi")); F("WIFI: Exited portal, no connection to wifi. Rebooting..." CR));
} }
stopDoubleReset(); stopDoubleReset();
@ -169,9 +138,7 @@ void WifiConnection::startPortal() {
// //
// Call the wifi manager in loop // Call the wifi manager in loop
// //
void WifiConnection::loop() { void WifiConnection::loop() { myDRD->loop(); }
myDRD->loop();
}
// //
// Connect to last known access point or create one if connection is not // 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) // 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 #if LOG_LEVEL == 6
Log.verbose(F("WIFI: Parsing version number string %s." CR), version); Log.verbose(F("WIFI: Parsing version number string %s." CR), version);
#endif #endif

View File

@ -25,7 +25,6 @@ SOFTWARE.
#define SRC_WIFI_HPP_ #define SRC_WIFI_HPP_
// Include // Include
//#include <ESP8266WiFi.h>
#include <Arduino.h> #include <Arduino.h>
// classes // classes
@ -41,6 +40,7 @@ class WifiConnection {
public: public:
// WIFI // WIFI
WifiConnection(); WifiConnection();
bool connect(); bool connect();
bool disconnect(); bool disconnect();
bool isConnected(); bool isConnected();