Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <WiFi.h>
- #include <WebServer.h>
- const char* ssid = "your_SSID";
- const char* password = "your_PASSWORD";
- WebServer server(80);
- const int relay1Pin = 23;
- const int relay2Pin = 22;
- const int led1Pin = 18;
- const int led2Pin = 19;
- void handleRoot() {
- String html = "<html><body><h1>ESP32 Home Automation</h1>";
- html += "<p><a href=\"/relay1/on\">Turn Relay 1 ON</a></p>";
- html += "<p><a href=\"/relay1/off\">Turn Relay 1 OFF</a></p>";
- html += "<p><a href=\"/relay2/on\">Turn Relay 2 ON</a></p>";
- html += "<p><a href=\"/relay2/off\">Turn Relay 2 OFF</a></p>";
- html += "<p><a href=\"/led1/on\">Turn LED 1 ON</a></p>";
- html += "<p><a href=\"/led1/off\">Turn LED 1 OFF</a></p>";
- html += "<p><a href=\"/led2/on\">Turn LED 2 ON</a></p>";
- html += "<p><a href=\"/led2/off\">Turn LED 2 OFF</a></p>";
- html += "</body></html>";
- server.send(200, "text/html", html);
- }
- void handleRelay1On() {
- digitalWrite(relay1Pin, LOW); // Assuming LOW turns the relay ON
- server.send(200, "text/html", "<html><body><h1>Relay 1 ON</h1><p><a href=\"/\">Back</a></p></body></html>");
- }
- void handleRelay1Off() {
- digitalWrite(relay1Pin, HIGH); // Assuming HIGH turns the relay OFF
- server.send(200, "text/html", "<html><body><h1>Relay 1 OFF</h1><p><a href=\"/\">Back</a></p></body></html>");
- }
- void handleRelay2On() {
- digitalWrite(relay2Pin, LOW); // Assuming LOW turns the relay ON
- server.send(200, "text/html", "<html><body><h1>Relay 2 ON</h1><p><a href=\"/\">Back</a></p></body></html>");
- }
- void handleRelay2Off() {
- digitalWrite(relay2Pin, HIGH); // Assuming HIGH turns the relay OFF
- server.send(200, "text/html", "<html><body><h1>Relay 2 OFF</h1><p><a href=\"/\">Back</a></p></body></html>");
- }
- void handleLED1On() {
- digitalWrite(led1Pin, HIGH);
- server.send(200, "text/html", "<html><body><h1>LED 1 ON</h1><p><a href=\"/\">Back</a></p></body></html>");
- }
- void handleLED1Off() {
- digitalWrite(led1Pin, LOW);
- server.send(200, "text/html", "<html><body><h1>LED 1 OFF</h1><p><a href=\"/\">Back</a></p></body></html>");
- }
- void handleLED2On() {
- digitalWrite(led2Pin, HIGH);
- server.send(200, "text/html", "<html><body><h1>LED 2 ON</h1><p><a href=\"/\">Back</a></p></body></html>");
- }
- void handleLED2Off() {
- digitalWrite(led2Pin, LOW);
- server.send(200, "text/html", "<html><body><h1>LED 2 OFF</h1><p><a href=\"/\">Back</a></p></body></html>");
- }
- void setup() {
- Serial.begin(115200);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(1000);
- Serial.println("Connecting to WiFi...");
- }
- Serial.println("Connected to WiFi");
- pinMode(relay1Pin, OUTPUT);
- pinMode(relay2Pin, OUTPUT);
- pinMode(led1Pin, OUTPUT);
- pinMode(led2Pin, OUTPUT);
- digitalWrite(relay1Pin, HIGH); // Initialize relay to OFF state
- digitalWrite(relay2Pin, HIGH); // Initialize relay to OFF state
- digitalWrite(led1Pin, LOW); // Initialize LED to OFF state
- digitalWrite(led2Pin, LOW); // Initialize LED to OFF state
- server.on("/", handleRoot);
- server.on("/relay1/on", handleRelay1On);
- server.on("/relay1/off", handleRelay1Off);
- server.on("/relay2/on", handleRelay2On);
- server.on("/relay2/off", handleRelay2Off);
- server.on("/led1/on", handleLED1On);
- server.on("/led1/off", handleLED1Off);
- server.on("/led2/on", handleLED2On);
- server.on("/led2/off", handleLED2Off);
- server.begin();
- Serial.println("HTTP server started");
- }
- void loop() {
- server.handleClient();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement