Search code examples
arduinoesp8266arduino-idearduino-esp8266relay

esp8266 relay read relay status in real-time


I made a simple script to control a relay via local web server. It works, but I just want to read the "real" relay status, if it is on or off.

#include <ESP8266WiFi.h>

const char* ssid = "myssid";
const char* password = "mypwd";

#define RELAY 0

WiFiServer server(80);

void setup() {
    Serial.begin(115200);
    pinMode(RELAY,OUTPUT);
    digitalWrite(RELAY, LOW);
    server.begin();
}

void loop() {
    WiFiClient client = server.available();
    if (!client) return;
    while(!client.available()) delay(1);
    String request = client.readStringUntil('\r');
    int value = LOW;
    if (request.indexOf("/RELAY=ON") != -1){
      Serial.println("RELAY=ON");
      digitalWrite(RELAY,LOW);
      value = LOW;
    }
    if (request.indexOf("/RELAY=OFF") != -1){
      Serial.println("RELAY=OFF");
      digitalWrite(RELAY,HIGH);
      value = HIGH;
    }
    client.println("HTTP/1.1 200 OK");
    client.println("Content-Type: text/html");
    client.println("");
    client.println("<!DOCTYPE HTML>");
    client.println("<html>");
    client.println("<head><title>Test RELAY</title></head>");
    client.print("Relay is now: ");
    if(value == HIGH)
      client.print("OFF");
    else
      client.print("ON");
    client.println("<br><br>");
    client.println("Turn <a href=\"/RELAY=OFF\">OFF</a> RELAY<br>");
    client.println("Turn <a href=\"/RELAY=ON\">ON</a> RELAY<br>");
    client.println("</html>");
    delay(1);
}

With this code, when I start the esp, the relay status is off by default. Then, I reach the web page with my desktop pc at the local ip address of the esp, and what I see is:

Relay is now: OFF

Turn OFF RELAY
Turn ON RELAY

This is correct, then I click on "Turn ON RELAY", and I see:

Relay is now: ON

Turn OFF RELAY
Turn ON RELAY

At this point the relay is really ON, but I close my browser and I reach the web page from my phone. It say:

Relay is now: OFF

Turn OFF RELAY
Turn ON RELAY

This is incorrect, because the relay is ON... How can I read the real value? I also tried with digitalRead(RELAY), but it always return 0.


Solution

  • It's a silly bug it took me a while to spot it. The reason is int value = LOW; in your loop method. The best way to fix that is to put it as a global var on top. That way it will not get pre-set to LOW on every request.

    #include <ESP8266WiFi.h>
    
    const char* ssid = "myssid";
    const char* password = "mypwd";
    int value = LOW;
    ... // Rest of the code
    

    and remove the declaration of int value from your loop method. As a side note, I would suggest you to use server.on(route, httpMethod, arduinoMethod);

    to handle to request rather than putting it in your loop method. That way it has better performance and the code is 10 times more clear.