1

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.

1 Answer 1

2

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.

2
  • With my script, using ESP8255WiFi.h Library I'm able to control 4 relay using the pins: GPIO 0, TX, GPIO 2 and RX. I tried to use your suggestion "server.on()" to handle the requests, so I had to use the ESP8266WebServer.h Library. But I cannot use the TX and RX pins... Is there another way? Im using ESP8266-01.
    – fmineo
    Commented Dec 30, 2018 at 14:59
  • @FrancescoMineo hmm why you are unable to use the pins? What kind of board are you using? is it D1? Commented Dec 30, 2018 at 15:17

Not the answer you're looking for? Browse other questions tagged or ask your own question.