Header Ads Widget

Responsive Advertisement

ESP 8266 Remote Control Switch

Download apk And code

Sbr IP Remote

Download 👇🏻👇🏻👇🏻



#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

// Set GPIO pins for the switches (use GPIO numbers)
int switchPins[10] = {5, 4, 0, 2, 14, 12, 13, 15, 3, 1}; // Adjust based on available pins

ESP8266WebServer server(80); // Create a web server on port 80

void setup() {
  Serial.begin(115200); // Initialize serial communication for debugging

  // Initialize GPIO pins as OUTPUT and set them LOW (off)
  for (int i = 0; i < 10; i++) {
    pinMode(switchPins[i], OUTPUT);
    digitalWrite(switchPins[i], LOW); // Start with all switches 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", []() { toggleSwitch(0); });
  server.on("/command2", []() { toggleSwitch(1); });
  server.on("/command3", []() { toggleSwitch(2); });
  server.on("/command4", []() { toggleSwitch(3); });
  server.on("/command5", []() { toggleSwitch(4); });
  server.on("/command6", []() { toggleSwitch(5); });
  server.on("/command7", []() { toggleSwitch(6); });
  server.on("/command8", []() { toggleSwitch(7); });
  server.on("/command9", []() { toggleSwitch(8); });
  server.on("/command10", []() { toggleSwitch(9); });

  // Start the server
  server.begin();
  Serial.println("HTTP server started");
}

void loop() {
  // Handle incoming client requests
  server.handleClient();
}

// Function to toggle a switch on or off
void toggleSwitch(int switchIndex) {
  int currentState = digitalRead(switchPins[switchIndex]);
  
  // Toggle the state
  if (currentState == LOW) {
    digitalWrite(switchPins[switchIndex], HIGH); // Turn on the switch
    server.send(200, "text/plain", "Switch ON");
  } else {
    digitalWrite(switchPins[switchIndex], LOW);  // Turn off the switch
    server.send(200, "text/plain", "Switch OFF");
  }

  Serial.print("Toggled switch ");
  Serial.println(switchIndex + 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");
}

Circuit diagram



Esp8266 Remote control switch with Relay module circuit diagram



Post a Comment

0 Comments