#include #include #include #include #include #define FIRMWARE "2.0" #define BUTTONPIN 23 #define DEBOUNCEMS 15 #define BOARD "ESP32" #define SERVICE "LEDsActionButton" #define STATUSINTERVAL 600000 String toggleURL = "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; void sendToggleMessage(bool state) { Serial.println("Action button triggered"); char webMsg[255] = ""; strcat(webMsg, toggleURL.c_str()); strcat(webMsg, (state) ? "true" : "false"); HTTPClient http; http.begin(webMsg); http.GET(); http.end(); } unsigned long sendDeviceStatus() { Serial.println("Sending device status"); WiFiClient client; HTTPClient http; http.begin(client, statusURL.c_str()); http.addHeader("Content-Type", "application/x-www-form-urlencoded"); String httpData = "mac=" + WiFi.macAddress() + "&board=" + BOARD + "&firmware=" + FIRMWARE + "&service=" + SERVICE; int httpResponseCode = http.POST(httpData); http.end(); return millis(); } void setup() { Serial.begin(115200); pinMode(BUTTONPIN, INPUT_PULLUP); Serial.println("MAC Address: " + WiFi.macAddress()); WiFiManager wifiManager; wifiManager.autoConnect("LEDsActionButton"); lastStatusTime = sendDeviceStatus(); ArduinoOTA.onStart([]() { String type; if ( ArduinoOTA.getCommand() == U_FLASH ) type = "sketch"; else // U_SPIFFS type = "filesystem"; Serial.println("Start OTA updating " + type); }); ArduinoOTA.onEnd([]() { Serial.println("\nOTA Update Completed"); }); ArduinoOTA.onError([](ota_error_t error) { Serial.printf("OTA Update Error[%u]: ", error); if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed"); else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed"); else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed"); else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed"); else if (error == OTA_END_ERROR) Serial.println("End Failed"); }); ArduinoOTA.begin(); } unsigned long lastDebounceTime = 0; bool toggleState = false; bool debouncing = false; void loop() { if ( millis() > (lastStatusTime + STATUSINTERVAL) ) { lastStatusTime = sendDeviceStatus(); } if ( millis() > 86400000 ) { ESP.restart(); } unsigned short buttonCheck = digitalRead(BUTTONPIN); if ( buttonCheck != lastButtonState ) { debouncing = true; lastDebounceTime = millis(); } if ( debouncing && (millis() >= (lastDebounceTime + DEBOUNCEMS)) ) { debouncing = false; if ( buttonCheck != buttonState ) { buttonState = buttonCheck; if ( buttonState == LOW ) { toggleState = !toggleState; sendToggleMessage(toggleState); } } } lastButtonState = buttonCheck; ArduinoOTA.handle(); }