diff --git a/platformio.ini b/platformio.ini index 99f3702..65688ed 100644 --- a/platformio.ini +++ b/platformio.ini @@ -17,17 +17,18 @@ lib_deps = https://github.com/tzapu/WiFiManager.git https://github.com/me-no-dev/ESPAsyncWebServer.git + https://github.com/LennartHennigs/Button2.git miq19/eModbus @ ^1.7.0 monitor_speed = 115200 [env:esp32release] - board = esp32dev - build_flags = + board = esp32dev + build_flags = -DWIFI_RST_BUTTON_PIN=0 [env:esp32debug] board = esp32dev - build_flags = -DLOG_LEVEL=LOG_LEVEL_DEBUG + build_flags = -DLOG_LEVEL=LOG_LEVEL_DEBUG -DWIFI_RST_BUTTON_PIN=0 [env:olimex-poe] board = esp32-poe diff --git a/src/main.cpp b/src/main.cpp index 35e67da..90e8850 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,6 +8,9 @@ #include #include "config.h" #include "pages.h" +#include "Button2.h" + + AsyncWebServer webServer(80); Config config; @@ -15,12 +18,46 @@ Preferences prefs; ModbusClientRTU *MBclient; ModbusBridgeWiFi MBbridge; WiFiManager wm; +#if defined(WIFI_RST_BUTTON_PIN) +Button2 wifiButton; +#endif + +// select which pin will trigger the configuration portal when set to LOW + + const int timeout = 120; // seconds to run for +#ifndef LONG_PRESS_TIME + #define LONG_PRESS_TIME 3000 // 4000 milliseconds +#endif + + +void onLongClick(Button2 &btn){ + + // set configportal timeout + wm.setConfigPortalTimeout(timeout); + + if (!wm.startConfigPortal()) { + dbgln("failed to connect and hit timeout"); + delay(1000); + //reset and try again, or maybe put it to deep sleep + ESP.restart(); + } + + //if you get here you have connected to the WiFi + dbgln("connected to wifi..."); +} + + void setup() { debugSerial.begin(115200); dbgln(); dbgln("[config] load") prefs.begin("modbusRtuGw"); + #if defined(WIFI_RST_BUTTON_PIN) + wifiButton.begin(WIFI_RST_BUTTON_PIN); + wifiButton.setLongClickDetectedHandler( &onLongClick); + wifiButton.setLongClickTime(LONG_PRESS_TIME); + #endif config.begin(&prefs); debugSerial.end(); debugSerial.begin(config.getSerialBaudRate(), config.getSerialConfig()); @@ -55,6 +92,11 @@ void setup() { dbgln("[setup] finished"); } + + + void loop() { - // put your main code here, to run repeatedly: +#if defined(WIFI_RST_BUTTON_PIN) + wifiButton.loop(); +#endif } \ No newline at end of file