Chat freely about anything...

User avatar
By julezrules
#92756 Good evening,

I'm struggling with one or more ESP8266 projects.
In this project the ESP8266 monitors the voltage of a 3,3V power supply to tell, whether there's power in the sockets or not. Then it sends a post to a php file on my server.
Everything works fine, however after a while it doesnt work anymore. It just doesnt do anything.
I don't know if it loses connection or goes to a sleep mode? or if I'm just making any major mistakes in my code for years now on webservers and client scripts..

Here's my code, please tell me if that looks ok for you: (BTW, I put this 100ms delay in the main loop cz I just read it somewhere to put it there so the esp doesnt hang up, idk if thats true but it doesn't bother me so I put it in there.)

I've also had this issue on a different system with a NodeMCU board. I used to blame it on my router but I'm starting to doubt on my coding skills rather.
This current project now though uses a Wemos D1 mini clone.

Thanks in advance and have a good evening :)


Code: Select all#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266HTTPClient.h>

const char *ssid = "myssid";
const char *password = "mypassword";
int powerstate = 0;
int upd = 0;

void setup() {
  pinMode(D2, INPUT);
  delay(1000);
  Serial.begin(115200);
  WiFi.mode(WIFI_OFF);
  delay(1000);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  Serial.println("");

  Serial.print("connecting");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.print("connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
}

WiFiClient client;

void loop() {
  int senspin = digitalRead(D2);
  if (senspin == HIGH) {
    if (powerstate == 0) {
      powerstate = 1;
      upd = 1;
    }
  }
  else if (senspin == LOW) {
    if (powerstate == 1) {
      powerstate = 0;
      upd = 1;
    }
  }
  if (upd == 1) {
    HTTPClient http;
    http.begin(client, "http://www.myserver.com/myphpfile.php");
    http.addHeader("Content-Type", "application/x-www-form-urlencoded");
    Serial.print("powerstate: ");
    Serial.println(powerstate);
    Serial.print("upd: ");
    Serial.println(upd);
    if (powerstate == 0) {
      int httpCode = http.POST("status=0");
      String payload = http.getString();
      Serial.print("httpCode: ");
      Serial.println(httpCode);
      Serial.print("payload: ");
      Serial.println(payload);
    }
    else if (powerstate == 1) {
      int httpCode = http.POST("status=1");
      String payload = http.getString();
      Serial.print("httpCode: ");
      Serial.println(httpCode);
      Serial.print("payload: ");
      Serial.println(payload);
    }
    http.end();
    upd = 0;
  }
  delay(100);
}
User avatar
By robersmithenton
#92926 Reconnect to Wi-Fi Network After Lost Connection
The ESP8266 has the ability to automatically reconnect to your router in case of a Wi-Fi outage. For example, if the ESP8266 is connected to your router and you suddenly turn it off, when it goes back on, the ESP8266 can reconnect automatically.