Example sketches for the new Arduino IDE for ESP8266

Moderator: igrr

User avatar
By JurajA
#87009
RIN67630 wrote:
JurajA wrote:why #include <WiFiUdp.h>?


You are right. The ESP lib works without that include.
Does it include it implicitly?
Or ist is querying NTP with all the ballast of HTTP? :-(


this functions are in the SDK. the Arduino layer is written on top of the SDK. so this time functions don't use Arduino library to acceess network.

@schufti what is the established way? I know there are problems with the SDK functions. I don't know if they are resolved or not in recent versions. But I really don't know: you say use them or not use them?
User avatar
By schufti
#87010 I can't tell for the pure sdk functions as I never used sdk itself.
But what I see from the sdk functions in the earlier core versions regarding ntp and "time" it is unusable as it depends on the user setting the exact timezone difference in seconds and manually taking care of summertime. Additionally there are none of the standard time functions therefor the need for 3rd party time lib, confusing newbies as they find lots of incompatible timelibs on google (giving trouble in compiling and running).
in recent cores ntp-client and timelib functionality is included (partly in lwIP, newlib).
Example how to use:
https://github.com/esp8266/Arduino/blob ... TZ-DST.ino
it is an example demonstrating all of the features, so it might be a little confusing.
Code: Select all#include <coredecls.h>                  // settimeofday_cb()
#include <Schedule.h>
#include <PolledTimeout.h>
#include <sntp.h>                       // sntp_servermode_dhcp()
extern "C" int clock_gettime(clockid_t unused, struct timespec *tp);
are only necessary for demonstration of the "features"

Code: Select all#include <time.h>
is not for including a "new time lib" but to get the variable type defines

For the basic "get time" it is only few lines to initialize and use.
Code: Select all#define NTP0 "0.pool.ntp.org"
#define NTP1 "1.pool.ntp.org"
#define TZ "CET-1CEST,M3.5.0,M10.5.0/3"

void setup() {
  time_t tnow;
  struct tm *ti;
  setenv("TZ", TZ, 0);
  tzset();
  configTime(0, 0, NTP0, NTP1);
//start WiFi here
wifi.begin()
// wait until we get ntp time
  while (t < 300000) {
    t = time(nullptr);
    delay(10);
  }
  tnow = time(nullptr);
  Serial.println(ctime(&tnow));
  ti = localtime(&tnow);
  Serial.println(asctime(ti));
}

void loop {
}
User avatar
By schufti
#87021 maybe I misinterpreted some parts?
I saw code including some ntpclient, statements I interpreted as appraising espressif code and finally most likely wrote my answer on friday w/o reloading the thread; missing (and not rereading) some relevant answers.
But still, the working solution behind configTime() is not coming from espressif, it was implemented by the good souls over at the core github; overriding the espressif crap.
I wouldn't feel to sure automatic daylightsaving is working on esp32, wait for the next switchover.

So I apologize for and withdraw my comments and stand corrected.