So you're a Noob? Post your questions here until you graduate! Don't be shy.

User avatar
By Bonzo
#75899 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.

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()
{
}
User avatar
By Bonzo
#75938 Seems to be working now:
Arduino timing is mili seconds and the ESP8266 is micro. So at some points I was trying micro and others milli which means the timing was totally out somewhere!
Also I needed a delay before going into deep sleep as the wireless was shutting down before the data was sent.
User avatar
By Bonzo
#75964 Tried some different code without success but suddenly realised the Arduino is using mili seconds and the ESP8266 is in micro seconds. This means some times the time of the Arduino code was very long or the Nodemcu code was exceptionally short.

It still did not write the data to the Pi but the code was actually doing what I expected. Adding a delay before the Deep sleep fixed that; I assume it was disconnecting the wireless before the data was sent.