From b1b307b4c3532be24c46f116cebcd30a1fabfd49 Mon Sep 17 00:00:00 2001 From: Junior Date: Thu, 18 Dec 2025 15:24:16 -0500 Subject: [PATCH] Customize platform and src so the code can be used on both ESP32 and ESP8266 --- include/esp32.h | 2 ++ include/esp8266.h | 2 ++ platformio.ini | 28 ++++++++++++++++-- src/main.cpp | 87 ++++++++++++++++++++----------------------------------- 4 files changed, 61 insertions(+), 58 deletions(-) create mode 100644 include/esp32.h create mode 100644 include/esp8266.h diff --git a/include/esp32.h b/include/esp32.h new file mode 100644 index 0000000..2aa932b --- /dev/null +++ b/include/esp32.h @@ -0,0 +1,2 @@ +#define BUTTONPIN 22 // 22/GPIO22 +#define BOARD "ESP32" \ No newline at end of file diff --git a/include/esp8266.h b/include/esp8266.h new file mode 100644 index 0000000..9d2ecc1 --- /dev/null +++ b/include/esp8266.h @@ -0,0 +1,2 @@ +#define BUTTONPIN 14 // D5/GPIO14 +#define BOARD "ESP8266" \ No newline at end of file diff --git a/platformio.ini b/platformio.ini index 8e76d76..7a56534 100644 --- a/platformio.ini +++ b/platformio.ini @@ -8,14 +8,38 @@ ; Please visit documentation for the other options and examples ; https://docs.platformio.org/page/projectconf.html +[platformio] +default_envs = esp32doit-devkit-v1 +;default_envs = nodemcuv2 + +[common] +build_flags = + -D DEBUG=false + [env:esp32doit-devkit-v1] platform = espressif32 board = esp32doit-devkit-v1 framework = arduino monitor_speed = 115200 upload_protocol = espota -upload_port = 192.168.2.232 +upload_port = 192.168.2.221 +;upload_port = COM6 +build_flags = + ${common.build_flags} lib_compat_mode = strict lib_deps = tzapu/WiFiManager@^2.0.17 - lostincompilation/PrettyOTA@^1.1.2 + +[env:nodemcuv2] +platform = https://github.com/platformio/platform-espressif8266.git +board = nodemcuv2 +framework = arduino +upload_protocol = espota +upload_port = 192.168.2.238 +;upload_port = COM6 +upload_speed = 115200 +monitor_speed = 115200 +build_flags = + ${common.build_flags} +lib_deps = + tzapu/WiFiManager@^2.0.17 diff --git a/src/main.cpp b/src/main.cpp index 694d893..fff7411 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,41 +1,50 @@ #include #include -#include -#include -#include +#include +#if defined(ARDUINO_ARCH_ESP32) + #include + #include + #include +#elif defined(ARDUINO_ARCH_ESP8266) + #include + #include + #include +#endif -#define BOARDFIRMWARE "2.2" +#define BOARDFIRMWARE "2.5" -#define BUTTONPIN 22 #define DEBOUNCEMS 15 -#define BOARD "ESP32" #define SERVICE "LEDsActionButton" -#define STATUSINTERVAL 600000 +#define STATUSINTERVAL 600000 // 10 minutes + +String hostAddress = "moon.basement.lan"; +unsigned int hostPort = 80; +String actionURL = "/l/actionbutton.php?state="; +String statusURL = "/l/boardstatus.php"; -String actionURL = "http://moon.basement.lan/l/actionbutton.php?state="; -String statusURL = "http://moon.basement.lan/l/boardstatus.php"; unsigned short buttonState = HIGH, lastButtonState = HIGH; unsigned long lastStatusTime = 0; -AsyncWebServer server(80); -PrettyOTA OTAUpdates; - void sendActionMessage(bool state) { - Serial.println("Action button triggered"); - char webMsg[255] = ""; - strcat(webMsg, actionURL.c_str()); - strcat(webMsg, (state) ? "true" : "false"); + if ( DEBUG ) Serial.println("Action button triggered"); + String msgURL = actionURL + (state ? "true" : "false"); HTTPClient http; - http.begin(webMsg); + if ( BOARD == "ESP32" ) { + msgURL = "http://" + hostAddress + ":" + String(hostPort) + msgURL; + http.begin(msgURL.c_str()); + } else { + WiFiClient client; + http.begin(client, hostAddress.c_str(), hostPort, msgURL.c_str()); + } http.GET(); http.end(); } unsigned long sendDeviceStatus() { - Serial.println("Sending device status"); + if ( DEBUG ) Serial.println("Sending device status"); WiFiClient client; HTTPClient http; - http.begin(client, statusURL.c_str()); + http.begin(client, ("http://" + hostAddress + ":" + String(hostPort) + statusURL).c_str()); http.addHeader("Content-Type", "application/x-www-form-urlencoded"); String httpData = "mac=" + WiFi.macAddress() + "&board=" + BOARD + "&firmware=" + BOARDFIRMWARE + "&service=" + SERVICE; int httpResponseCode = http.POST(httpData); @@ -43,46 +52,14 @@ unsigned long sendDeviceStatus() { return millis(); } -// Gets called when update starts -// updateMode can be FILESYSTEM or FIRMWARE -void OnOTAStart(NSPrettyOTA::UPDATE_MODE updateMode) { - Serial.println("OTA update started"); - if(updateMode == NSPrettyOTA::UPDATE_MODE::FIRMWARE) - Serial.println("Mode: Firmware"); - else if(updateMode == NSPrettyOTA::UPDATE_MODE::FILESYSTEM) - Serial.println("Mode: Filesystem"); -} - -// Gets called while update is running -// currentSize: Number of bytes already processed -// totalSize: Total size of new firmware in bytes -void OnOTAProgress(uint32_t currentSize, uint32_t totalSize) { - Serial.printf("OTA Progress Current: %u bytes, Total: %u bytes\n", currentSize, totalSize); -} - -// Gets called when update finishes -void OnOTAEnd(bool successful) { - if (successful) - Serial.println("OTA update finished successfully"); - else - Serial.println("OTA update failed"); -} - void setup() { Serial.begin(115200); pinMode(BUTTONPIN, INPUT_PULLUP); - Serial.println("MAC Address: " + WiFi.macAddress()); + if ( DEBUG ) Serial.println("MAC Address: " + WiFi.macAddress()); WiFiManager wifiManager; wifiManager.autoConnect("LEDsActionButton"); lastStatusTime = sendDeviceStatus(); - OTAUpdates.Begin(&server); - OTAUpdates.SetHardwareID(SERVICE); - OTAUpdates.OverwriteAppVersion(BOARDFIRMWARE); - PRETTY_OTA_SET_CURRENT_BUILD_TIME_AND_DATE(); - OTAUpdates.OnStart(OnOTAStart); - OTAUpdates.OnProgress(OnOTAProgress); - OTAUpdates.OnEnd(OnOTAEnd); - server.begin(); + ArduinoOTA.begin(); } unsigned long lastDebounceTime = 0; @@ -92,9 +69,6 @@ void loop() { if ( millis() > (lastStatusTime + STATUSINTERVAL) ) { lastStatusTime = sendDeviceStatus(); } - if ( millis() > 86400000 ) { - ESP.restart(); - } unsigned short buttonCheck = digitalRead(BUTTONPIN); if ( buttonCheck != lastButtonState ) { debouncing = true; @@ -111,4 +85,5 @@ void loop() { } } lastButtonState = buttonCheck; + ArduinoOTA.handle(); }