Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- // WiFi Settings
- const char* ssid = "Your_WiFi_SSID";
- const char* password = "Your_WiFi_Password";
- // Relay Pins
- const int relay1Pin = 14; // GPIO 14 (D5), Bridge with RY1
- const int relay2Pin = 16; // GPIO 16 (D0), Bridge with RY2
- // Web Server on port 80
- WiFiServer server(80);
- void setup() {
- Serial.begin(115200);
- delay(10);
- // Configure Relay Pins
- pinMode(relay1Pin, OUTPUT);
- pinMode(relay2Pin, OUTPUT);
- digitalWrite(relay1Pin, HIGH); // Relays might be active-low, adjust if needed
- digitalWrite(relay2Pin, HIGH); // Relays might be active-low, adjust if needed
- // Connect to WiFi
- WiFi.begin(ssid, password);
- Serial.println("Connecting to WiFi...");
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- // Print IP Address
- Serial.println("\nWiFi connected. IP address: ");
- Serial.println(WiFi.localIP());
- // Start the server
- server.begin();
- Serial.println("Web server started.");
- }
- void loop() {
- WiFiClient client = server.available();
- if (!client) return;
- // Read incoming request
- String request = client.readStringUntil('\r');
- Serial.println(request);
- client.flush();
- // Handle Relay Control
- if (request.indexOf("/relay1on") != -1) {
- digitalWrite(relay1Pin, LOW);
- client.println("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\nRelay 1 ON");
- } else if (request.indexOf("/relay1off") != -1) {
- digitalWrite(relay1Pin, HIGH);
- client.println("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\nRelay 1 OFF");
- }
- // Add more 'else if' blocks for relay 2 and any future additions
- // Add a simple webpage control interface (optional)
- client.println("HTTP/1.1 200 OK");
- client.println("Content-Type: text/html");
- client.println("");
- client.println("<!DOCTYPE html>");
- // ... Rest of your HTML webpage code ...
- client.stop();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement