UDP vs deepsleep issue on ESP8266

IoTPt1000
Posts: 3
Joined: Thu Dec 07, 2023 10:00 am

UDP vs deepsleep issue on ESP8266

Postby IoTPt1000 » Thu Dec 07, 2023 10:32 am

Hi ESP forum,

I have been stuck on this issue for quite some time, and after googling for many hours trying to find a solution I cannot seem to solve my problem. The problem is that I have an ESP8266 device (ESP-wroom-02D) which should connect to the internet, create an UDP connection, send data, wait for a reply, go to deep sleep and then repeat. If I remove the 'deep-sleep' part of my code, then the UDP connection works flawlessly, however as soon as I try to add the deep sleep function an odd error occurs. The issue is that after the system has been running for a random amount of time (30 minutes to an hour, maybe two?), the ESP device stops transmitting data accross the UDP connection, and getting it to transmit data again is really difficult. During this 'error time' The ESP is connected to WiFi, has a valid IP address, and the ESP thinks the message has been send, but wireshark tells otherwise.
  • I know this problem is caused by the deep-sleep configuration, since the UDP connection works flawlessly without.
    • Which is why I have tried severals types of opening and closing the UDP and wifi connection, and adding delays. But nothing seem to solve the problem.
  • The ESP module is on my own carrier board, and the voltage level is stable, and good during transmitting (keep in mind the board works 100% without the Deepsleep.
  • The ESP thinks it is connected, and thinks it sends the message, but nothing comes out of the ESP device.

Code: Select all

#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
const char* ssid = "Wifiname"; //replace this with your WiFi network name
const char* password = "WifiPassword"; //replace this with your WiFi network password

IPAddress Hostip(192, 168, 0, 52);

#define UDP_PORT 12346 //1112 is the local port of ESP8266

//UDP
WiFiUDP UDP;
char packetBuffer[255]; //buffer to hold incoming packet
bool state = 1; 
unsigned long CurrentTime = 0; 
int Saver = 0; 

void setup() {
  Serial.begin(9600); 
  Serial.print("WiFi Status: ");
  Serial.print(WiFi.status()); //7 = WL_DISCONNECTED if module is not configured in station mode, Wifi is disconnected when going to deep sleep 
  delay(1000); 
  WiFi.begin(ssid, password); // initialize the wifi connection, with ID and password
  Serial.println();
  Serial.println("Connecting");
  while (WiFi.status() != WL_CONNECTED) //Wait for WiFi connection 
    {
      yield();
      delay(750);
      Serial.print(".");
      Serial.print(WiFi.status());
    }
  Serial.println("success!");
  Serial.print("IP Address is: ");
  Serial.println(WiFi.localIP()); //This is the ESP devices IP address 
}

void loop() {
  state = 1; 
  Saver = 0; 
  UDP.begin(UDP_PORT);
  while (state)
  {
    Serial.print("Wifi status: "); 
    Serial.println(WiFi.status()); //3 = WL_CONNECTED after successful connection is established
    Serial.print("Sending data to: ");
    Serial.print(Hostip);
    Serial.print(" "); 
    Serial.print(UDP_PORT);
    Serial.println(); 
    yield(); 
    UDP.beginPacket(Hostip, UDP_PORT); 
    char myString[2] = {'4', '\0'};
    Serial.println("Out of loop"); 
    Serial.print("Data to be send: ");
    for (int i = 0; i < sizeof(myString); i++)
    {
      Serial.print(myString[i]);
    }
    Serial.println();
    yield(); 
    UDP.write(myString);
    while (!UDP.endPacket());
    {
      Serial.println("."); 
    }
    CurrentTime = millis();
    Serial.println(state);
    while( ( (millis() - CurrentTime) <= 2000) && (state == 1) ) //Wait 2 seconds to recieve the message 
    {
      int len1 = UDP.parsePacket();
      Serial.println(len1); 
      if (len1 > 0) 
      {
        int len = UDP.read(packetBuffer, 255);
        packetBuffer[len] = 0;
        Serial.print("Contents: ");
        Serial.println(packetBuffer);
        state = 0; 
      } 
      delay(400); 
    }
    if (Saver >= 4)
    {
      yield(); 
      Serial.println("Came into the saver mode");
      Serial.print(WiFi.status()); 
      Serial.println(); 
      Serial.print(UDP.localPort()); 
      Serial.println(); 
      Serial.println("Over: "); 
      Saver = 0;  
    }
    Saver++; 
    yield();
  }
  UDP.stop(); 
  delay(1); 
  WiFi.disconnect(true);
  Serial.print(WiFi.status()); 
  delay(1);
  ESP.deepSleep(10e6);
}

From the serial monitor:

WiFi Status: 7
Connecting
.7.7.7.7.3success!
IP Address is: 192.168.0.50
Wifi status: 3
Sending data to: 192.168.0.52 12346
Out of loop
Data to be send: 4
.
1
0
0
0
0
0
Wifi status: 3
Sending data to: 192.168.0.52 12346
Out of loop
Data to be send: 4
.
1
0
0
0
0
0
0

And then it continuously repeats the part:

"
Wifi status: 3
Sending data to: 192.168.0.52 12346
Out of loop
Data to be send: 4
.
1
0
0
0
0
0
0
"

Which makes sense, since it is waiting for a reponse which does not come because the ESP does not the send the message. The reciever of the message is a C# application, which then send a response.

What am I missing here? Does the deep-sleep need special attention when using it together with WiFi?

Thanks in advance.
Last edited by IoTPt1000 on Fri Dec 08, 2023 7:09 am, edited 1 time in total.

IoTPt1000
Posts: 3
Joined: Thu Dec 07, 2023 10:00 am

Re: UDP vs deepsleep issue on ESP8266

Postby IoTPt1000 » Sat Dec 09, 2023 6:18 am

Hi @ESP_Minatel,

I Can see that you have made a reply to this topic, but I cannot see it in the thread. Could you maybe post it again?

Sorry for the inconvenience.

AcmeUK
Posts: 23
Joined: Wed Dec 06, 2023 5:14 pm

Re: UDP vs deepsleep issue on ESP8266

Postby AcmeUK » Sat Dec 09, 2023 10:16 am

One user has found that when waking from Deep Sleep the radio has to be put into sleep and immediately woken up.

Code: Select all

WiFi.forceSleepBegin();
delay(1);
WiFi.forceSleepWake();
delay(1);

IoTPt1000
Posts: 3
Joined: Thu Dec 07, 2023 10:00 am

Re: UDP vs deepsleep issue on ESP8266

Postby IoTPt1000 » Sat Dec 09, 2023 10:46 am

Hi AcmeUK,

I will try the code to see if it helps.
I have a question though is this proposed solution necessary to get the WiFi to work at all? Because I am capable of getting the deepsleep and UDP transmission to work as the code is, for a period of time, before it fails. Meaning I don’t have to do this trick. But maybe this is capable of getting the ESP8266 back when it happens?

Who is online

Users browsing this forum: No registered users and 16 guests