I have one 3-way switch in my garage with the traveler wires going to a 3v relay which is controlled by an ESP8266.
It works but I don't know how to detect if someone turns on or off the garage lights with the manual 3-way wall switch.
How do I detect if the AC 120v wall switch has been turned on or off, so I can update the light status in my web server, and UPDATE my On or Off-web server button text to the proper text that the lights are ON or the lights are OFF, when the manual switch is turned on or off
Thanks for any help
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
// Network configuration
const char* ssid = "MY_SSID";
const char* password = "XXXXXXXX";
IPAddress staticIP(10, 0, XX, XX); // Static IP address
IPAddress gateway(10, 0, XX, X); // Gateway IP address
IPAddress subnet(255, 255, 255, 0); // Subnet mask
IPAddress primaryDNS(8, 8, 8, 8); // Primary DNS
IPAddress secondaryDNS(8, 8, 4, 4); // Secondary DNS
ESP8266WebServer server(80);
// GPIO pin assignments
#define RELAY_PIN 5 // GPIO pin for relay control
#define SWITCH_PIN 4 // GPIO pin for SPDT switch input
// State machine variables
enum LightState { LIGHT_OFF, LIGHT_ON };
LightState lightState = LIGHT_OFF;
bool lastSwitchState = LOW;
// Timing variables
unsigned long previousMillis = 0;
const long interval = 50; // Debounce interval in milliseconds
void setup() {
// Initialize serial communication
Serial.begin(9600);
// Set GPIO pin modes
pinMode(RELAY_PIN, OUTPUT);
pinMode(SWITCH_PIN, INPUT);
// Start with the light off
digitalWrite(RELAY_PIN, LOW);
// Connect to Wi-Fi
WiFi.config(staticIP, gateway, subnet, primaryDNS, secondaryDNS);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected.");
Serial.println(WiFi.localIP());
// Set up web server routes
server.on("/", handleRoot);
server.on("/toggle", handleToggle); // Route to handle button click
server.onNotFound(handleNotFound);
// Start the server
server.begin();
Serial.println("HTTP server started.");
}
void loop() {
server.handleClient();
checkSwitchState();
}
void checkSwitchState() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
bool switchState = digitalRead(SWITCH_PIN);
if (switchState != lastSwitchState) {
lastSwitchState = switchState;
if (switchState == HIGH) {
lightState = (lightState == LIGHT_OFF) ? LIGHT_ON : LIGHT_OFF;
digitalWrite(RELAY_PIN, (lightState == LIGHT_ON) ? HIGH : LOW);
}
}
}
}
void handleRoot() {
String page = "<html><body>";
page += "<h1 style='text-align:center;'>John's LED Garage Lights Controller</h1>";
if (lightState == LIGHT_ON) {
//page += "<p style='text-align:center;'><a href=\"/toggle\"><button style='background-color:green;color:white;font-size:20px;width:200px;height:50px;'>The Garage Lights Are ON</button></a></p>";
page += "<p style='text-align:center;'><a href=\"/toggle\"><button style='background-color:green;color:white;font-size:20px;width:275px;height:50px;'>The Garage Lights Are ON</button></a></p>";
} else {
//page += "<p style='text-align:center;'><a href=\"/toggle\"><button style='background-color:red;color:white;font-size:20px;width:200px;height:50px;'>The Garage Lights Are OFF</button></a></p>";
page += "<p style='text-align:center;'><a href=\"/toggle\"><button style='background-color:red;color:white;font-size:20px;width:275px;height:50px;'>The Garage Lights Are OFF</button></a></p>";
}
page += "</body></html>";
server.send(200, "text/html", page);
}
void handleToggle() {
// Toggle the light state
lightState = (lightState == LIGHT_OFF) ? LIGHT_ON : LIGHT_OFF;
digitalWrite(RELAY_PIN, (lightState == LIGHT_ON) ? HIGH : LOW);
// Redirect back to the main page
server.sendHeader("Location", "/");
server.send(303);
}
void handleNotFound() {
server.send(404, "text/plain", "404: Not found");
}