NTPClient library is working without any code to establish a network connection!

Currently, I have an older ESP8266 plugged into a handy USB to ESP-01 adapter.

I uploaded a simple sketch (below) to retrieve Time using NTPClient:

#include <NTPClient.h>
#include <WiFiUdp.h>

WiFiUDP ntpUDP;
NTPClient ntpClient(ntpUDP, "pool.ntp.org", -18000, 60000);

void setup() {

  Serial.begin(115200);

  Serial.println();
  Serial.println();
  Serial.println();

  for (uint8_t t = 5; t > 0; t--) {
    Serial.printf("[SETUP] WAIT %d...\n", t);
    Serial.flush();
    delay(1000);
  }
 
  ntpClient.begin();
}

void loop() {

  ntpClient.update();
  Serial.print("NTP time: ");
  Serial.print(ntpClient.getFormattedTime());
  Serial.print(" ");
  Serial.print(ntpClient.getEpochTime());
  Serial.println();

  Serial.println("Wait 5s before next round...");
  delay(5000);
}

Notice that in no way do I establish any kind of IP connection in the code, yet when I upload the sketch, unplug the adapter and plug it back in to run the sketch, I get working results in the Serial monitor:

I would expect my code to fail at this point, but I can only surmise that the USB to ESP-01 adapter provides a working connection to the 8266 that is plugged into it. If that case, how can I determine (from within the sketch) what the IP address might be?

You can You can query the IP address using WiFi.localIP() from within your sketch. But to do this, you need to #include <ESP8266WiFi.h>. using WiFi.localIP() from within your sketch. But to do this, you need to #include <ESP8266WiFi.h>.

Thanks. I tried that, but I get back "(IP unset)". I also tried it with Ethernet.h, but get the same result.

you don't need an external library for NTP on an ESP8266.
The ESP Plattform package comes with everything you need for NTP on an ESP8266.

see:

1 Like

Thank you for that information, it is helpful.

But, I'm trying to figure out how NTP is working on my 8266 when I have not established any kind of network connection within the sketch itself. Somehow, the board has enough access to the Internet that NTP is reporting proper time.

This is done by the ESP in the background - may be due to a previous sketch you had on your ESP before this outdated NTP sketch.

You can try to delete the stored wifi data:

grafik

1 Like

It remembers an old connection.

If you Call WiFi.disconnect(true); in the setup() that will clear saved credentials.

2 Likes