ESP8266 WiFi reconnection after sleep

Hello,

I am trying to develop a program where my ESP9266 goes to sleep for 10 seconds and every time it wakes up it re-establishes WiFi connection. The first time the connection is done successfully, but after waking up from sleep it doesn't connect again. Any idea why? This is the code:

#include <ESP8266WiFi.h>

const int wakeUpInterval = 10 * 1000000; // Wake up interval in microseconds (10 seconds)
const char* ssid = "xxx";
const char* password = "xxx";

void setup() {
  Serial.begin(115200);
  while (!Serial);
  Serial.print("Connecting to WiFi...");
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("Connected to WiFi!");

  // Enter deep sleep
  Serial.print("Going to sleep...");
  delay(1000); // Give some time for Serial output to complete
  WiFi.disconnect(); // Disconnect from Wi-Fi before entering deep sleep
  delay(100); // Give some time for disconnection

  // Configure deep sleep
  ESP.deepSleep(wakeUpInterval, WAKE_RF_DISABLED);
}

void loop() {
  // Do nothing in the loop function
}

When it sleeps , it re awakens where it left off - at the end of your setup .
You need something in loop to check and reconnect .
If you lost connection whilst it was running you wouldn’t regain the connection either, this can happen anytime .

There maybe a setting whereby an 8266. Will reboot on wake-up, but it’s not a great route.

  • I’ve no idea google might know ?

setup();

@mribeira

Connect D0-pin with RST-pin (Fig-1) and then try the following tested sketch (your one with some moderation):

(Quote: The RST pin of the ESP8266 is always HIGH while the ESP8266 is running. However, when the RST pin receives a LOW signal, it restarts the microcontroller.

If you set a deep sleep timer with the ESP8266, once the timer ends, GPIO 16/D0 sends a LOW signal. That means that GPIO 16/D0, when connected to the RST pin, can wake up the ESP8266 after a set period of time.)
image
Figure-1:

#include <ESP8266WiFi.h>

const int wakeUpInterval = 5 * 1000000ul; // Wake up interval in microseconds (10 seconds)
const char* ssid = "SiamWifiNew"; //enter ID of your Router
const char* password = "siam1234"; //enter password for your Router

void setup()
{
  Serial.begin(115200);
  while (!Serial);
  Serial.println();
  connectWiFi();
  // Enter deep sleep
  Serial.println("Going to sleep...");
  delay(1000); // Give some time for Serial output to complete
  WiFi.disconnect(); // Disconnect from Wi-Fi before entering deep sleep
  delay(100); // Give some time for disconnection

  // Configure deep sleep
  ESP.deepSleep(wakeUpInterval, WAKE_RF_DISABLED);
  Serial.println();
  Serial.println("ESP8266 has waked up.");
  connectWiFi();
}

void loop()
{

}

void connectWiFi()
{
  Serial.println();
  delay(500);
  Serial.print("Connecting to WiFi...");
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("Connected to WiFi!");
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.