#include #include #include #if defined(ARDUINO_ARCH_ESP32) #include #include #include #elif defined(ARDUINO_ARCH_ESP8266) #include #include #include #endif #define BOARDFIRMWARE "2.5" #define DEBOUNCEMS 15 #define SERVICE "LEDsActionButton" #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"; unsigned short buttonState = HIGH, lastButtonState = HIGH; unsigned long lastStatusTime = 0; void sendActionMessage(bool state) { if ( DEBUG ) Serial.println("Action button triggered"); String msgURL = actionURL + (state ? "true" : "false"); WiFiClient client; HTTPClient http; if ( BOARD == "ESP32" ) { msgURL = "http://" + hostAddress + ":" + String(hostPort) + msgURL; http.begin(msgURL.c_str()); } else { http.begin(client, hostAddress.c_str(), hostPort, msgURL.c_str()); } http.GET(); http.end(); } unsigned long sendDeviceStatus() { if ( DEBUG ) Serial.println("Sending device status"); WiFiClient client; HTTPClient http; 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); if ( DEBUG && (httpResponseCode != 200) ) { Serial.println("Error sending status: " + String(httpResponseCode)); } http.end(); return millis(); } void setup() { Serial.begin(115200); pinMode(BUTTONPIN, INPUT_PULLUP); if ( DEBUG ) Serial.println("MAC Address: " + WiFi.macAddress()); WiFiManager wifiManager; wifiManager.autoConnect("LEDsActionButton"); lastStatusTime = sendDeviceStatus(); ArduinoOTA.begin(); } unsigned long lastDebounceTime = 0; bool actionState = false; bool debouncing = false; void loop() { unsigned long currentMillis = millis(); if ( (currentMillis - lastStatusTime) >= STATUSINTERVAL ) { lastStatusTime = sendDeviceStatus(); currentMillis = lastStatusTime; } unsigned short buttonCheck = digitalRead(BUTTONPIN); if ( buttonCheck != lastButtonState ) { debouncing = true; lastDebounceTime = currentMillis; } if ( debouncing && (currentMillis >= (lastDebounceTime + DEBOUNCEMS)) ) { debouncing = false; if ( buttonCheck != buttonState ) { buttonState = buttonCheck; if ( buttonState == LOW ) { actionState = !actionState; sendActionMessage(actionState); } } } lastButtonState = buttonCheck; ArduinoOTA.handle(); }