Nodemcu deep sleep prevents UDP from working
Posted: Sat May 12, 2018 2:00 pm
I am building a wireless sensor using a Nodemcu connecting to Python on a Pi. Last night it worked for about 9 hours although for some reason it looked like the Nodemcu was going to sleep for some reason.
Anyway today I have been tying to get deep sleep to work. The code I have found works OK in the normal Arduino with a setup and loop functions. But when I modify to run in the setup function it fails on the Udp.parsePacket(); - it has a 0 size.
This is the code I currently have and what I want is for the Nodemcu to wake up after a set time, Python sends a packet at a set time about one min after the Nodemcu wakes up. The Nodemcu then sends the data and goes to sleep - repeat.
Currently the nodemcu starts and is waiting for the packet and as the packet is 0 the program just sits there.
I am new to all this and would appreciate some help to get this running.
Anyway today I have been tying to get deep sleep to work. The code I have found works OK in the normal Arduino with a setup and loop functions. But when I modify to run in the setup function it fails on the Udp.parsePacket(); - it has a 0 size.
This is the code I currently have and what I want is for the Nodemcu to wake up after a set time, Python sends a packet at a set time about one min after the Nodemcu wakes up. The Nodemcu then sends the data and goes to sleep - repeat.
Currently the nodemcu starts and is waiting for the packet and as the packet is 0 the program just sits there.
I am new to all this and would appreciate some help to get this running.
Code: Select all
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
const char* ssid = "**********";
const char* password = "**********";
WiFiUDP Udp;
unsigned int localUdpPort = 8888; // local port to listen on was 4210
char incomingPacket[255]; // buffer for incoming packets
char replyPacket[] = "Hi there! Got the message :-)"; // a reply string to send back
const int sleepTime = 5000000;
void setup()
{
Serial.begin(115200);
Serial.println();
Serial.printf("Connecting to %s ", ssid);
//WiFi.forceSleepWake();
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println(" connected");
Udp.begin(localUdpPort);
Serial.printf("Now listening at IP %s, UDP port %d\n", WiFi.localIP().toString().c_str(), localUdpPort);
int packetSize = Udp.parsePacket();
if (packetSize)
{
// receive incoming UDP packets
Serial.printf("Received %d bytes from %s, port %d\n", packetSize, Udp.remoteIP().toString().c_str(), Udp.remotePort());
int len = Udp.read(incomingPacket, 255);
if (len > 0)
{
incomingPacket[len] = 0;
}
Serial.printf("UDP packet contents: %s\n", incomingPacket);
// send back a reply, to the IP address and port we got the packet from
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
Udp.write(replyPacket);
Udp.endPacket();
Serial.print("deep sleep now");
ESP.deepSleep(50000000, WAKE_RF_DEFAULT);
delay(1000);
}
}
void loop()
{
}