Customize platform and src so the code can be used on both ESP32 and ESP8266

This commit is contained in:
Junior 2025-12-18 15:24:16 -05:00
parent 8310fd0a17
commit b1b307b4c3
4 changed files with 61 additions and 58 deletions

2
include/esp32.h Normal file
View File

@ -0,0 +1,2 @@
#define BUTTONPIN 22 // 22/GPIO22
#define BOARD "ESP32"

2
include/esp8266.h Normal file
View File

@ -0,0 +1,2 @@
#define BUTTONPIN 14 // D5/GPIO14
#define BOARD "ESP8266"

View File

@ -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

View File

@ -1,41 +1,50 @@
#include <Arduino.h>
#include <WiFiManager.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <PrettyOTA.h>
#include <ArduinoOTA.h>
#if defined(ARDUINO_ARCH_ESP32)
#include <esp32.h>
#include <WiFi.h>
#include <HTTPClient.h>
#elif defined(ARDUINO_ARCH_ESP8266)
#include <esp8266.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#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();
}