Advertisement
microrobotics

ESP32 Webserver Relay 2 Channel Module Opto Isolated 3.3V Logic

Apr 28th, 2023 (edited)
2,288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. This complete code sets up a web server on the ESP32 to control a 2 Channel Relay Module using a simple HTML interface with buttons to toggle the relays. The HTML is embedded in the Arduino code as a raw string literal, and the ESP32 serves the HTML file when requested.
  3.  
  4. Make sure to replace "your_SSID" and "your_PASSWORD" with your Wi-Fi network credentials.
  5.  
  6. After uploading the code, open the Serial Monitor, and you should see the ESP32 connecting to your Wi-Fi network. Once connected, it will display the assigned IP address. Use this IP address to control the relays from a web browser.
  7. */
  8.  
  9. #include <WiFi.h>
  10. #include <ESPAsyncWebServer.h>
  11.  
  12. // Replace with your network credentials
  13. const char* ssid = "your_SSID";
  14. const char* password = "your_PASSWORD";
  15.  
  16. // Define the ESP32 pins connected to the Relay Module
  17. const int relayPin1 = 26; // Connect the first relay channel to GPIO 26
  18. const int relayPin2 = 27; // Connect the second relay channel to GPIO 27
  19.  
  20. // Create an instance of the server
  21. AsyncWebServer server(80);
  22.  
  23. void setup() {
  24.   // Serial port for debugging purposes
  25.   Serial.begin(115200);
  26.  
  27.   // Set the relay pins as output
  28.   pinMode(relayPin1, OUTPUT);
  29.   pinMode(relayPin2, OUTPUT);
  30.  
  31.   // Initialize the relays to be off
  32.   digitalWrite(relayPin1, LOW);
  33.   digitalWrite(relayPin2, LOW);
  34.  
  35.  
  36.  // Connect to Wi-Fi
  37.   WiFi.begin(ssid, password);
  38.   while (WiFi.status() != WL_CONNECTED) {
  39.     delay(1000);
  40.     Serial.println("Connecting to WiFi...");
  41.   }
  42.   Serial.println("Connected to WiFi");
  43.   Serial.print("IP Address: ");
  44.   Serial.println(WiFi.localIP());
  45.  
  46.   // Serve the HTML file
  47.   server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
  48.     request->send_P(200, "text/html", R"rawliteral(
  49. <!DOCTYPE html>
  50. <html lang="en">
  51. <head>
  52.  <meta charset="UTF-8">
  53.  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  54.  <title>Relay Control</title>
  55.  <script>
  56.    // Function to send a request to the ESP32 to toggle the specified relay
  57.    async function toggleRelay(relay) {
  58.      const response = await fetch(`/relay${relay}`);
  59.      const text = await response.text();
  60.      document.getElementById(`status${relay}`).innerText = text;
  61.    }
  62.  
  63.    async function getInitialStatus() {
  64.      for (let i = 1; i <= 2; i++) {
  65.        const response = await fetch(`/relay${i}/status`);
  66.        const text = await response.text();
  67.        document.getElementById(`status${i}`).innerText = text;
  68.      }
  69.    }
  70.  </script>
  71. </head>
  72. <body onload="getInitialStatus()">
  73.  <h1>Relay Control</h1>
  74.  
  75.  <button onclick="toggleRelay(1)">Toggle Relay 1</button>
  76.  <p id="status1">Relay 1 status: unknown</p>
  77.  
  78.  <button onclick="toggleRelay(2)">Toggle Relay 2</button>
  79.  <p id="status2">Relay 2 status: unknown</p>
  80. </body>
  81. </html>
  82.    )rawliteral");
  83.   });
  84.  
  85.   // Route for handling the toggle request of relay 1
  86.   server.on("/relay1", HTTP_GET, [](AsyncWebServerRequest *request){
  87.     digitalWrite(relayPin1, !digitalRead(relayPin1)); // Toggle relay 1
  88.     request->send(200, "text/plain", digitalRead(relayPin1) ? "Relay 1 ON" : "Relay 1 OFF");
  89.   });
  90.  
  91.   // Route for handling the toggle request of relay 2
  92.   server.on("/relay2", HTTP_GET, [](AsyncWebServerRequest *request){
  93.     digitalWrite(relayPin2, !digitalRead(relayPin2)); // Toggle relay 2
  94.     request->send(200, "text/plain", digitalRead(relayPin2) ? "Relay 2 ON" : "Relay 2 OFF");
  95.   });
  96.  
  97.     // Route for handling the status request of relay 1
  98.   server.on("/relay1/status", HTTP_GET, [](AsyncWebServerRequest *request){
  99.     request->send(200, "text/plain", digitalRead(relayPin1) ? "Relay 1 ON" : "Relay 1 OFF");
  100.   });
  101.  
  102.   // Route for handling the status request of relay 2
  103.   server.on("/relay2/status", HTTP_GET, [](AsyncWebServerRequest *request){
  104.     request->send(200, "text/plain", digitalRead(relayPin2) ? "Relay 2 ON" : "Relay 2 OFF");
  105.   });
  106.  
  107.   // Start the server
  108.   server.begin();
  109. }
  110.  
  111. void loop() {
  112.   // Nothing to do here, the web server is handling everything
  113. }
  114.  
  115.  
  116.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement