
Home automation 5 switch Codeππ»ππ»ππ»
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
// Replace with your network credentials
const char* ssid = "your_SSID"; // Enter your WiFi SSID
const char* password = "your_PASSWORD"; // Enter your WiFi Password
// Define GPIO pins for buttons and relays
int buttonPins[5] = {5, 4, 0, 2, 14}; // D1, D2, D3, D4, D5 (button input pins)
int relayPins[5] = {12, 13, 15, 3, 1}; // D6, D7, D8, D9, D10 (relay output pins)
ESP8266WebServer server(80); // Create a web server on port 80
// Array to keep track of relay states
bool relayState[5] = {false, false, false, false, false}; // false = OFF, true = ON
void setup() {
Serial.begin(115200); // Initialize serial communication for debugging
// Initialize button GPIO pins as INPUT with pull-up resistors
for (int i = 0; i < 5; i++) {
pinMode(buttonPins[i], INPUT_PULLUP); // Button pins as input with internal pull-up
}
// Initialize relay GPIO pins as OUTPUT and set them LOW (off)
for (int i = 0; i < 5; i++) {
pinMode(relayPins[i], OUTPUT);
digitalWrite(relayPins[i], LOW); // Start with all relays OFF
}
// Attempt to connect to WiFi
connectToWiFi();
// Print the IP address
Serial.print("ESP8266 IP Address: ");
Serial.println(WiFi.localIP());
// Set up URL handlers for each command
server.on("/command1", []() { toggleRelay(0); });
server.on("/command2", []() { toggleRelay(1); });
server.on("/command3", []() { toggleRelay(2); });
server.on("/command4", []() { toggleRelay(3); });
server.on("/command5", []() { toggleRelay(4); });
// Start the server
server.begin();
Serial.println("HTTP server started");
}
void loop() {
// Handle incoming client requests
server.handleClient();
// Check button states and toggle relay if a button is pressed
for (int i = 0; i < 5; i++) {
if (digitalRead(buttonPins[i]) == LOW) { // Button is pressed (LOW due to pull-up)
delay(50); // Debounce delay
if (digitalRead(buttonPins[i]) == LOW) { // Confirm button press
toggleRelay(i); // Toggle the corresponding relay
}
while (digitalRead(buttonPins[i]) == LOW); // Wait until button is released
}
}
}
// Function to toggle a relay on or off
void toggleRelay(int relayIndex) {
relayState[relayIndex] = !relayState[relayIndex]; // Toggle relay state
digitalWrite(relayPins[relayIndex], relayState[relayIndex] ? HIGH : LOW); // Update relay
// Send feedback over HTTP
server.send(200, "text/plain", relayState[relayIndex] ? "Relay ON" : "Relay OFF");
Serial.print("Toggled relay ");
Serial.println(relayIndex + 1);
}
// Function to connect to Wi-Fi with retry mechanism
void connectToWiFi() {
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi");
int retries = 0; // Track number of connection attempts
while (WiFi.status() != WL_CONNECTED) {
retries++;
delay(1000);
Serial.print(".");
if (retries >= 20) { // Retry for 20 seconds
Serial.println();
Serial.println("Failed to connect to WiFi. Restarting...");
ESP.restart(); // Restart ESP if WiFi doesn't connect
}
}
Serial.println();
Serial.println("Connected to WiFi");
}




0 Comments