Go Down

Topic: Get Date/Time with NodeMCU (Read 1 time) previous topic - next topic

mattbaum

Although relatively new to this module, I have successfully measured temperature and output it to an LCD.
Now I am trying to get the time and date.
Started with the "example" and can get "seconds since . . ." OK.
However, what I want to get is date and time.
A program that I found somewhere is at:
      https://randomnerdtutorials.com/esp32-ntp-client-date-time-arduino-ide/
This seems to have what I need, but it will not compile.
The compiler complains that " 'class NTPClient' has no member named 'getFormattedDate'
     formattedDate = timeClient.getFormattedDate();
I think that the three "included" files are present.
(Somewhere I found that there exists a file <ESP8266WiFi.h>  -  but that this is obsolete now.)

My questions are:
 - What am I doing wrong, that the referenced example won't work?
 - Is there a better or easier way to get the time and date?

Any help appreciated!
    -   Matt

Nick_Pyner

I think the below came from the library examples
Code: [Select]
#include <ESP8266WiFi.h>
#include <time.h>

const char* ssid = "snookypoo";
const char* password = "xxxxxxxxxx";

int timezone = 10;
int dst = 0;

void setup() {
  Serial.begin(115200);
  Serial.setDebugOutput(true);

  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  Serial.println("\nConnecting to WiFi");
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(1000);
  }

  configTime(3 * 3600, 0, "pool.ntp.org", "time.nist.gov");
  Serial.println("\nWaiting for time");
  while (!time(nullptr)) {
    Serial.print(".");
    delay(1000);
  }
  Serial.println("");
}

void loop() {
  time_t now = time(nullptr);
  Serial.println(ctime(&now));
  delay(1000);
}

Juraj

https://github.com/esp8266/Arduino/blob/master/libraries/esp8266/examples/NTP-TZ-DST/NTP-TZ-DST.ino

mattbaum

Update:
I tried the approach of Nick_Pyner.
I updated the file "ESP8266" from 2.5.2 to 2.6.1.
Now the program seems to compile, but gets stuck with the error messages below.
HOWEVER, I have no idea how to interpret the errors.
It merely says "Error compiling for board Generic ESP8266 Module.."

Any suggestions?
===========================================
Arduino: 1.8.9 (Windows Store 1.8.21.0) (Windows 10), Board: "Generic ESP8266 Module, 80 MHz, Flash, Legacy (new can return nullptr), All SSL ciphers (most compatible), dtr (aka nodemcu), 26 MHz, 40MHz, DOUT (compatible), 1MB (FS:64KB OTA:~470KB), 2, nonos-sdk 2.2.1+111 (191024), v2 Lower Memory, Disabled, None, Only Sketch, 115200"

In file included from C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.21.0_x86__mdqgnx93n4wtt\libraries\WiFi\src\WiFiUdp.cpp:29:0:

C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.21.0_x86__mdqgnx93n4wtt\libraries\WiFi\src\WiFiUdp.h:27:7: error: redefinition of 'class WiFiUDP'

 class WiFiUDP : public UDP {

       ^

In file included from C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.21.0_x86__mdqgnx93n4wtt\libraries\WiFi\src\utility/wifi_drv.h:26:0,

                 from C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.21.0_x86__mdqgnx93n4wtt\libraries\WiFi\src\WiFiUdp.cpp:26:

C:\Users\Matt\OneDrive\Documents\ArduinoData\packages\esp8266\hardware\esp8266\2.6.1\libraries\ESP8266WiFi\src/WiFiUdp.h:32:7: error: previous definition of 'class WiFiUDP'

 class WiFiUDP : public UDP, public SList<WiFiUDP> {

       ^

C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.21.0_x86__mdqgnx93n4wtt\libraries\WiFi\src\WiFiUdp.cpp: In constructor 'WiFiUDP::WiFiUDP()':

C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.21.0_x86__mdqgnx93n4wtt\libraries\WiFi\src\WiFiUdp.cpp:35:22: error: class 'WiFiUDP' does not have any field named '_sock'

 WiFiUDP::WiFiUDP() : _sock(NO_SOCKET_AVAIL) {}

                      ^

C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.21.0_x86__mdqgnx93n4wtt\libraries\WiFi\src\WiFiUdp.cpp: In member function 'virtual uint8_t WiFiUDP::begin(uint16_t)':

C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.21.0_x86__mdqgnx93n4wtt\libraries\WiFi\src\WiFiUdp.cpp:45:9: error: '_sock' was not declared in this scope

         _sock = sock;

         ^

C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.21.0_x86__mdqgnx93n4wtt\libraries\WiFi\src\WiFiUdp.cpp:46:9: error: '_port' was not declared in this scope

         _port = port;

         ^

C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.21.0_x86__mdqgnx93n4wtt\libraries\WiFi\src\WiFiUdp.cpp: In member function 'virtual int WiFiUDP::available()':

C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.21.0_x86__mdqgnx93n4wtt\libraries\WiFi\src\WiFiUdp.cpp:56:7: error: '_sock' was not declared in this scope

   if (_sock != NO_SOCKET_AVAIL)

       ^

C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.21.0_x86__mdqgnx93n4wtt\libraries\WiFi\src\WiFiUdp.cpp: In member function 'virtual void WiFiUDP::stop()':

C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.21.0_x86__mdqgnx93n4wtt\libraries\WiFi\src\WiFiUdp.cpp:66:8: error: '_sock' was not declared in this scope

    if (_sock == NO_SOCKET_AVAIL)

        ^

C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.21.0_x86__mdqgnx93n4wtt\libraries\WiFi\src\WiFiUdp.cpp:69:26: error: '_sock' was not declared in this scope

    ServerDrv::stopClient(_sock);

                          ^

C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.21.0_x86__mdqgnx93n4wtt\libraries\WiFi\src\WiFiUdp.cpp: In member function 'virtual int WiFiUDP::beginPacket(IPAddress, uint16_t)':

C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.21.0_x86__mdqgnx93n4wtt\libraries\WiFi\src\WiFiUdp.cpp:88:7: error: '_sock' was not declared in this scope

   if (_sock == NO_SOCKET_AVAIL)

       ^

C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.21.0_x86__mdqgnx93n4wtt\libraries\WiFi\src\WiFiUdp.cpp:90:7: error: '_sock' was not declared in this scope

   if (_sock != NO_SOCKET_AVAIL)

       ^

C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.21.0_x86__mdqgnx93n4wtt\libraries\WiFi\src\WiFiUdp.cpp: In member function 'virtual int WiFiUDP::endPacket()':

C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.21.0_x86__mdqgnx93n4wtt\libraries\WiFi\src\WiFiUdp.cpp:101:32: error: '_sock' was not declared in this scope

  return ServerDrv::sendUdpData(_sock);

                                ^

C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.21.0_x86__mdqgnx93n4wtt\libraries\WiFi\src\WiFiUdp.cpp: In member function 'virtual size_t WiFiUDP::write(const uint8_t*, size_t)':

C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.21.0_x86__mdqgnx93n4wtt\libraries\WiFi\src\WiFiUdp.cpp:111:27: error: '_sock' was not declared in this scope

  ServerDrv::insertDataBuf(_sock, buffer, size);

                           ^

C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.21.0_x86__mdqgnx93n4wtt\libraries\WiFi\src\WiFiUdp.cpp: In member function 'virtual int WiFiUDP::read()':

C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.21.0_x86__mdqgnx93n4wtt\libraries\WiFi\src\WiFiUdp.cpp:125:23: error: '_sock' was not declared in this scope

    ServerDrv::getData(_sock, &b);

                       ^

C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.21.0_x86__mdqgnx93n4wtt\libraries\WiFi\src\WiFiUdp.cpp: In member function 'virtual int WiFiUDP::read(unsigned char*, size_t)':

C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.21.0_x86__mdqgnx93n4wtt\libraries\WiFi\src\WiFiUdp.cpp:137:31: error: '_sock' was not declared in this scope

    if (!ServerDrv::getDataBuf(_sock, buffer, &size))

                               ^

C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.21.0_x86__mdqgnx93n4wtt\libraries\WiFi\src\WiFiUdp.cpp: In member function 'virtual int WiFiUDP::peek()':

C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.21.0_x86__mdqgnx93n4wtt\libraries\WiFi\src\WiFiUdp.cpp:152:22: error: '_sock' was not declared in this scope

   ServerDrv::getData(_sock, &b, 1);

                      ^

C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.21.0_x86__mdqgnx93n4wtt\libraries\WiFi\src\WiFiUdp.cpp: In member function 'virtual IPAddress WiFiUDP::remoteIP()':

C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.21.0_x86__mdqgnx93n4wtt\libraries\WiFi\src\WiFiUdp.cpp:166:25: error: '_sock' was not declared in this scope

  WiFiDrv::getRemoteData(_sock, _remoteIp, _remotePort);

                         ^

C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.21.0_x86__mdqgnx93n4wtt\libraries\WiFi\src\WiFiUdp.cpp: In member function 'virtual uint16_t WiFiUDP::remotePort()':

C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.21.0_x86__mdqgnx93n4wtt\libraries\WiFi\src\WiFiUdp.cpp:176:25: error: '_sock' was not declared in this scope

  WiFiDrv::getRemoteData(_sock, _remoteIp, _remotePort);

                         ^

exit status 1
Error compiling for board Generic ESP8266 Module.

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.s

Nick_Pyner

That all looks terrifying and I'm afraid I cannot comment much, as I have not used my NodeMCU for a couple of years and the above is all I have done with it anyway BUT
I'm betting your problem is all about board installation - because that is what it so often is. I don't think a NodeMCU qualifies as a "generic ESP8266". You might get some help from the eminence gris here


Juraj

this compilation error happens if WiFiUdp.h is included outside of ESP8266WiFi.h. The builder then includes WiFiUdp.h of the Arduino WiFi library bundled with IDE.

mattbaum

Thanks for the suggestions so far.

I've now looked into things some more, in light of the comments made, and I think I am back where I started from  -  I have the wrong board selected/installed.

I've since tried all the "8266" boards listed in the library that are available for selection.
Similar response for each.  Cannot compile for . . .

The module I'm actually using is a NodeMCU version 0.9  -   also tried the only NodeMCU 0.9 listed - also NG.

For all the other work I've done with this module, I've used the "Generic 8266" board with no problems.
That's why I thought it was a software problem.
Now I'm totally confused!

Any help??


Juraj

this compilation error happens if WiFiUdp.h is included outside of ESP8266WiFi.h. The builder then includes WiFiUdp.h of the Arduino WiFi library bundled with IDE.
remove
#include <WiFi.h>
#include <WiFiUdp.h>

Nick_Pyner

and note that, in the code I posted, only two libraries are used, of which only one is relevant to the NodeMCU

mattbaum

Problem solved!

Thanks to both of you guys.

I removed the unused/unneeded libraries and that seemed to do the trick.
This after wasting a whole heap of time - initially with software, and then when the message complained about the board, with hardware as well.

I should have asked the Forum right away  -  but it seemed like (and eventually was) a simple problem.

Thanks again!
                     -  Matt

Go Up