Advertisement
pleasedontcode

Web based home automation system

Jul 23rd, 2024
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 3.46 KB | Source Code | 0 0
  1. #include <WiFi.h>
  2. #include <WebServer.h>
  3.  
  4. const char* ssid = "your_SSID";
  5. const char* password = "your_PASSWORD";
  6.  
  7. WebServer server(80);
  8.  
  9. const int relay1Pin = 23;
  10. const int relay2Pin = 22;
  11. const int led1Pin = 18;
  12. const int led2Pin = 19;
  13.  
  14. void handleRoot() {
  15.   String html = "<html><body><h1>ESP32 Home Automation</h1>";
  16.   html += "<p><a href=\"/relay1/on\">Turn Relay 1 ON</a></p>";
  17.   html += "<p><a href=\"/relay1/off\">Turn Relay 1 OFF</a></p>";
  18.   html += "<p><a href=\"/relay2/on\">Turn Relay 2 ON</a></p>";
  19.   html += "<p><a href=\"/relay2/off\">Turn Relay 2 OFF</a></p>";
  20.   html += "<p><a href=\"/led1/on\">Turn LED 1 ON</a></p>";
  21.   html += "<p><a href=\"/led1/off\">Turn LED 1 OFF</a></p>";
  22.   html += "<p><a href=\"/led2/on\">Turn LED 2 ON</a></p>";
  23.   html += "<p><a href=\"/led2/off\">Turn LED 2 OFF</a></p>";
  24.   html += "</body></html>";
  25.   server.send(200, "text/html", html);
  26. }
  27.  
  28. void handleRelay1On() {
  29.   digitalWrite(relay1Pin, LOW); // Assuming LOW turns the relay ON
  30.   server.send(200, "text/html", "<html><body><h1>Relay 1 ON</h1><p><a href=\"/\">Back</a></p></body></html>");
  31. }
  32.  
  33. void handleRelay1Off() {
  34.   digitalWrite(relay1Pin, HIGH); // Assuming HIGH turns the relay OFF
  35.   server.send(200, "text/html", "<html><body><h1>Relay 1 OFF</h1><p><a href=\"/\">Back</a></p></body></html>");
  36. }
  37.  
  38. void handleRelay2On() {
  39.   digitalWrite(relay2Pin, LOW); // Assuming LOW turns the relay ON
  40.   server.send(200, "text/html", "<html><body><h1>Relay 2 ON</h1><p><a href=\"/\">Back</a></p></body></html>");
  41. }
  42.  
  43. void handleRelay2Off() {
  44.   digitalWrite(relay2Pin, HIGH); // Assuming HIGH turns the relay OFF
  45.   server.send(200, "text/html", "<html><body><h1>Relay 2 OFF</h1><p><a href=\"/\">Back</a></p></body></html>");
  46. }
  47.  
  48. void handleLED1On() {
  49.   digitalWrite(led1Pin, HIGH);
  50.   server.send(200, "text/html", "<html><body><h1>LED 1 ON</h1><p><a href=\"/\">Back</a></p></body></html>");
  51. }
  52.  
  53. void handleLED1Off() {
  54.   digitalWrite(led1Pin, LOW);
  55.   server.send(200, "text/html", "<html><body><h1>LED 1 OFF</h1><p><a href=\"/\">Back</a></p></body></html>");
  56. }
  57.  
  58. void handleLED2On() {
  59.   digitalWrite(led2Pin, HIGH);
  60.   server.send(200, "text/html", "<html><body><h1>LED 2 ON</h1><p><a href=\"/\">Back</a></p></body></html>");
  61. }
  62.  
  63. void handleLED2Off() {
  64.   digitalWrite(led2Pin, LOW);
  65.   server.send(200, "text/html", "<html><body><h1>LED 2 OFF</h1><p><a href=\"/\">Back</a></p></body></html>");
  66. }
  67.  
  68. void setup() {
  69.   Serial.begin(115200);
  70.   WiFi.begin(ssid, password);
  71.  
  72.   while (WiFi.status() != WL_CONNECTED) {
  73.     delay(1000);
  74.     Serial.println("Connecting to WiFi...");
  75.   }
  76.   Serial.println("Connected to WiFi");
  77.  
  78.   pinMode(relay1Pin, OUTPUT);
  79.   pinMode(relay2Pin, OUTPUT);
  80.   pinMode(led1Pin, OUTPUT);
  81.   pinMode(led2Pin, OUTPUT);
  82.  
  83.   digitalWrite(relay1Pin, HIGH); // Initialize relay to OFF state
  84.   digitalWrite(relay2Pin, HIGH); // Initialize relay to OFF state
  85.   digitalWrite(led1Pin, LOW); // Initialize LED to OFF state
  86.   digitalWrite(led2Pin, LOW); // Initialize LED to OFF state
  87.  
  88.   server.on("/", handleRoot);
  89.   server.on("/relay1/on", handleRelay1On);
  90.   server.on("/relay1/off", handleRelay1Off);
  91.   server.on("/relay2/on", handleRelay2On);
  92.   server.on("/relay2/off", handleRelay2Off);
  93.   server.on("/led1/on", handleLED1On);
  94.   server.on("/led1/off", handleLED1Off);
  95.   server.on("/led2/on", handleLED2On);
  96.   server.on("/led2/off", handleLED2Off);
  97.  
  98.   server.begin();
  99.   Serial.println("HTTP server started");
  100. }
  101.  
  102. void loop() {
  103.   server.handleClient();
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement