Advertisement
PlowmanPlow

HouseLEDs Toggle

Mar 6th, 2025
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.26 KB | None | 0 0
  1. #include <Arduino.h>
  2. #include <WiFiManager.h>
  3. #include <HTTPClient.h>
  4.  
  5. #define BUTTONPIN 23
  6. #define DEBOUNCEMS 15
  7.  
  8. String toggleURL = "http://led.web.lan/toggleleds.php?state=";
  9. unsigned short buttonState = HIGH, lastButtonState = HIGH;
  10.  
  11. void sendToggleMessage(bool state) {
  12.   char webMsg[255] = "";
  13.   strcat(webMsg, toggleURL.c_str());
  14.   strcat(webMsg, (state) ? "true" : "false");
  15.   HTTPClient http;
  16.   http.begin(webMsg);
  17.   http.GET();
  18.   http.end();
  19. }
  20.  
  21. void setup() {
  22.   Serial.begin(115200);
  23.   pinMode(BUTTONPIN, INPUT_PULLUP);
  24.   Serial.println("MAC Address: " + WiFi.macAddress());
  25.   WiFiManager wifiManager;
  26.   wifiManager.autoConnect("ToggleLEDs");
  27. }
  28.  
  29. unsigned long lastDebounceTime = 0;
  30. bool toggleState = false;
  31. bool debouncing = false;
  32. void loop() {
  33.   unsigned short buttonCheck = digitalRead(BUTTONPIN);
  34.   if ( buttonCheck != lastButtonState ) {
  35.     debouncing = true;
  36.     lastDebounceTime = millis();
  37.   }
  38.   if ( debouncing && (millis() >= (lastDebounceTime + DEBOUNCEMS)) ) {
  39.     debouncing = false;
  40.     if ( buttonCheck != buttonState ) {
  41.       buttonState = buttonCheck;
  42.       if ( buttonState == LOW ) {
  43.         toggleState = !toggleState;
  44.         sendToggleMessage(toggleState);
  45.       }
  46.     }
  47.   }
  48.   lastButtonState = buttonCheck;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement