Self-triggering of ESP8266 relay during power surges

Hello,
I am sending this message using google translate. I controlled the door with ESP8266 relay. However, when the power goes out, it automatically clicks and opens the door. How can I prevent this?

We cannot help you without more detailed information. Please read the forum guide to discover what will be required.

Thank you for the quick reply. I control the door lock with esp8266 relay. The control works quite well. However, the only problem and the biggest security vulnerability is that after power outages, that is, when the power is restored, the relay triggers automatically. I don't know how I can explain it in more detail than this. .

Your code is wrong

Without seeing it this is a tad presumptuous.

There are quite a few GPIO pins on the ESP8266 that pulse either from HIGH to LOW or vice versa during boot.

Depending a little on which board you actually use there may be pins that can be used to control the relay.

have a look at this which finally concludes :

Conclusion: GPIOs D1 and D2 are the only safe GPIOs I can use to drive relays if I don’t want them to operate autonomously at boot.

I use an ESP-01 to control a relay, but to make sure that it doesn't switch accidentally i have added an ATtiny13 to prevent that.

If this is the ready made module, also adding an ATtiny13 in between can solve your issue, since then it is using n ESP-01 which doesn't have GPIO 4 & 5 exposed.

Thank you very much for the answer. I also tried D1(5) and D2 (4) GPIOs. the result is still the same. It triggers again when the power comes on. I wonder how you added ATtiny13 to the mix? Is there an example?

Well, unless you post some code and a wiring diagram not much we can do but make guesses

Anything involving security be it cypher locks, door controls or similar should be on a battery power backup system. Names like Alltronix or Alarm-Saf come to mind. A Google of "battery backup" should get you plenty of hits (examples). What you are seeing comes as no surprise.

Ron

Apparently it is a secret system that the OP does not want to post the schematic for! Being secret the odds are the bug will survive and live for a long time.

No, the circuit is definitely not a hidden circuit. A ready esp8266 relay circuit. Let me share the codes too.

#include "ESP8266WiFi.h"

#define RELAY 0
#define LED 2

const char* ssid = ""; // WiFi SSID
const char* password = ""; // WiFi password

unsigned char status_RELAY = 0;

WiFiServer server(80);
String header;
String RELAYState = "off";

IPAddress ip(192, 168, 1, 241 ); // Node static IP
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress dns(192, 168, 1, 1);

unsigned long lastClientDisconnectTime = 0;
const unsigned long disconnectDelay = 1000; // Bağlantıyı kesme gecikme süresi (milisaniye cinsinden)

void setup() {
  Serial.begin(115200);
  pinMode(RELAY, OUTPUT);
  pinMode(LED, OUTPUT);
  digitalWrite(RELAY, HIGH);
  //digitalWrite(RELAY, !status_RELAY);
  digitalWrite(LED, HIGH);
  
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  WiFi.config(ip, gateway, subnet, dns);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  
  Serial.println("");
  Serial.println("WiFi connected");
  server.begin();
  Serial.println("Server started");
  Serial.println(WiFi.localIP());
  digitalWrite(LED, LOW);
}

void loop() {
  WiFiClient client = server.available();   // Listen for incoming clients

  while (client) {                          // While there's a client connected
    Serial.println("New Client.");          // Print a message out in the serial port
    String currentLine = "";                // Make a String to hold incoming data from the client
    while (client.connected()) {            // Loop while the client's connected
      if (client.available()) {             // If there's bytes to read from the client,
        char c = client.read();             // Read a byte, then
        Serial.write(c);                    // Print it out the serial monitor
        header += c;
        if (c == '\n') {                    // If the byte is a newline character
          // If the current line is blank, you got two newline characters in a row.
          // That's the end of the client HTTP request, so send a response:
          if (currentLine.length() == 0) {
            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // And a content-type so the client knows what's coming, then a blank line:
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println("Connection: close");
            client.println();

            // Turns the GPIOs on and off
            if (header.indexOf("GET /on") >= 0) {
              Serial.println("GPIO /on");
              RELAYState = "on";
              digitalWrite(RELAY, LOW);
            } else if (header.indexOf("GET /off") >= 0) {
              Serial.println("GPIO /off");
              RELAYState = "off";
              digitalWrite(RELAY, HIGH);
            }
            
            // Display the HTML web page
            client.println("<!DOCTYPE html><html>");
            client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
            client.println("<link rel=\"icon\" href=\"data:,\">");
            // CSS to style the on/off buttons 
            // Feel free to change the background-color and font-size attributes to fit your preferences
            client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}");
            client.println(".button { background-color: #7ad226; border: none; color: white; padding: 16px 40px; border-radius: 31px;");
            client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");
            client.println(".button2 {background-color: #000000;}</style></head>");
            
            // Web Page Heading
            client.println("<body><h1>ESP8266 Web Server</h1>");
            
            // Display current state, and ON/OFF buttons for GPIO 5  
            client.println("<p>RELAY - State " + RELAYState + "</p>");
            // If the output5State is off, it displays the ON button       
            if (RELAYState=="off") {
              client.println("<p><a href=\"/on\"><button class=\"button\">ON</button></a></p>");
            } else {
              client.println("<p><a href=\"/off\"><button class=\"button button2\">OFF</button></a></p>");
            } 
               
            client.println("</body></html>");
            
            // The HTTP response ends with another blank line
            client.println();
            // Break out of the while loop
            break;
          } else { // If you got a newline, then clear currentLine
            currentLine = "";
          }
        } else if (c != '\r') {  // If you got anything else but a carriage return character,
          currentLine += c;      // Add it to the end of the currentLine
        }
      }
    }
    // Clear the header variable
    header = "";
    // Close the connection
    if (!client.connected()) {
      if (millis() - lastClientDisconnectTime >= disconnectDelay) {
        client.stop();
        Serial.println("Client disconnected.");
        Serial.println("");
      }
    } else {
      lastClientDisconnectTime = millis();
    }
    Serial.println("Client disconnected.");
    Serial.println("");
    
    // Accept the next client
    client = server.available();
  }
}

Yes, definitely secret. OP probably even thinks that a picture of the module is equal to a schematic. Not even so much as a hint as to the part number of the module either.

In void setup() you turn the relay on. Does that open the door?

I bought the module ready. I have nothing to hide. Where is the part number of the module written? Tell me and I will write it too. But this is the module I bought. It works very well. It only clicks automatically when there is a power cut. All I want is a solution to this. There is no problem neither in the codes nor in the circuit. they all work. So I don't think the solution will be with codes. That's why I wrote it here. There seems to be no solution other than using a backup power supply. Thank you for the answers. But I will still continue researching.

Or maybe you turn it off. Hard to tell with no documentation.

I understand. In fact, it triggers without writing any coding, that is, when I directly power the circuit.

Normally when you buy a part, it has some sort of part number that tells the seller what you are ordering. You don't just say "send me a module"

hmm.. thank you. Ok. I will find it and send it. But as I said, the relay is triggered once when I power the circuit.

Does this mean that it opens the door when the power goes off or when the power comes back on?

Yes absulately right.

lol, which is it? Off or back on.