The use of the ESP8266 in the world of IoT

User avatar
By gabry1609
#76147 Hey guys!

I'm experiencing many problems with my last project: I've made a simple relay that shortly connects for a while the cables of the electric lock of my gate, in order to make it open, acting like a pushbutton.

The circuit is really easy: I've recycled a mobile phone 5V charger so that it can power up the esp and also the relay (which is a solid state one from Omron, so without mechanical parts). The relay is connected to the GPIO pin 2 and to make it open the gate I send this kind of string: "http://static_ip_of_the_esp/gpio/0" it works, but only few times. I mean, after few minutes (like 2 or 5 sometimes) it stops working...

I've read this problem has been reported several times but no solutions have been found.

Talking about the code, I am using the stock version of the wifiwebserver sketch, which I have sent to the ESP using an FTDI adapter and Arduino IDE. The only differences in my code are the network and pass name (obviously) and the fact that I've added a line in order to make the relay switch on for a while and then switch off (so: switch on, wait 10ms and then switch off).

I'm using this system in order to let my Google Assistant (via Google Home and also my smartphone) control it by opening that url via an applet which is triggered by IFTTT everytime I ask Assistant to open the gate.

Right now the module is seating in the electrical derivation in which run all cables of my home. Could this be an interference problem? Also the module is closed in a 3D printed PLA housing...

I've also tried changing my WiFi network channel and also the router, but the problem occurs anyway.

I don't know what to think anymore :(
User avatar
By gabry1609
#76274 Hey Guys!

Quick update: I've discovered that after several hours, actually it totally disconnects from my network. I've read on many posts that this problem could be related to the ARP algorithm, which is preventing the ESP from stay connected to the network.

I have tried to modify the sketch in order to make my ESP automatically disconnect and reconnect everytime I make a request (so when I open the commanding url): it seems to work, apart from the fact that it takes a while to reconnect and also when I stop sending commands, after few minutes the problem comes back again.

Here is the sketch I'm using:

Code: Select all#include <ESP8266WiFi.h>

const char* ssid = "mynetworkname";
const char* password = "mypassword";

// Create an instance of the server
// specify the port to listen on as an argument
WiFiServer server(80);

void setup() {
 
  // prepare GPIO2
  pinMode(0, OUTPUT);
  digitalWrite(0, 0);
 
  // Connect to WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
 
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
 
  // Start the server
  server.begin();
  Serial.println("Server started");

  // Print the IP address
  Serial.println(WiFi.localIP());
}

void loop() {
  // Check if a client has connected
  WiFiClient client = server.available();
  if (!client) {
    return;
  }
 
  // Wait until the client sends some data
  Serial.println("new client");
  while(!client.available()){
    delay(1);
  }
 
  // Read the first line of the request
  String req = client.readStringUntil('\r');
  Serial.println(req);
  client.flush();
 
  // Match the request
  int val;
  if (req.indexOf("/gpio/0") != -1)
    val = 0;
  else if (req.indexOf("/gpio/1") != -1)
    val = 1;
  else {
    Serial.println("invalid request");
    client.stop();
    return;
  }

  // Set GPIO2 according to the request
  digitalWrite(0, val);
  delay(100);
  digitalWrite(0, 0);
 
  client.flush();

  // Prepare the response
  String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\nGPIO is now ";
  s += (val)?"high":"low";
  s += "</html>\n";

  // Send the response to the client
  client.print(s);
  delay(1);
  Serial.println("Client disonnected");

    WiFi.mode(WIFI_OFF);
    delay(1000);
    ESP.restart();

  // The client will actually be disconnected
  // when the function returns and 'client' object is detroyed
}


The last 3 lines of the code (WiFi.mode(WIFI_OFF); delay(1000); ESP.restart(); have been added subsequently, they were not present in the original code.

Help please :(