Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ESP32: setSNTP implementation and setInterval #1612

Closed
wilberforce opened this issue Feb 2, 2019 · 3 comments
Closed

ESP32: setSNTP implementation and setInterval #1612

wilberforce opened this issue Feb 2, 2019 · 3 comments
Labels
ESP32 This is only a problem on ESP32-based devices

Comments

@wilberforce
Copy link
Member

Discussed in forum here:

http://forum.espruino.com/conversations/330214/#comment14602067

The esp-idf updates the system time - however jswrap_interactive_setTime needs to be called to update the last jsiLastIdleTime.

unfortunately the api does not provide a callback - in the ESP8266 implementation a system timeout is added to poll every 100ms:

static os_timer_t sntpTimer;
static void sntpSync(void *arg) {
uint32_t sysTime = (uint32_t)((jshGetSystemTime() + 500000) / 1000000);
uint32_t ntpTime = sntp_get_current_timestamp();
if (!ntpTime) {
DBG("NTP time: null\n");
} else {
if (ntpTime-sysTime != 0) {
DBG("NTP time: %ld delta=%ld %s\n", (long unsigned int) ntpTime, (long unsigned int)ntpTime-sysTime, sntp_get_real_time(ntpTime));
}
jswrap_interactive_setTime((JsVarFloat)ntpTime);
}
os_timer_disarm(&sntpTimer);
os_timer_arm(&sntpTimer, 30*1000, 0);
}
void jswrap_wifi_setSNTP(JsVar *jsServer, JsVar *jsZone) {
if (!jsvIsNumeric(jsZone)) {
jsExceptionHere(JSET_ERROR, "Zone is not a number");
return;
}
int zone = jsvGetInteger(jsZone);
if (zone < -11 || zone > 13) {
jsExceptionHere(JSET_ERROR, "Zone must be in range -11..13");
return;
}
if (!jsvIsString(jsServer)) {
jsExceptionHere(JSET_ERROR, "Server is not a string");
return;
}
char server[64];
jsvGetString(jsServer, server, 64);
sntp_stop();
if (sntp_set_timezone(zone)) {
sntp_setservername(0, server);
sntp_init();
os_timer_disarm(&sntpTimer);
os_timer_setfn(&sntpTimer, sntpSync, 0);
os_timer_arm(&sntpTimer, 100, 0); // 100ms
}
DBG("SNTP: %s %s%d\n", server, zone>=0?"+":"", zone);
}

@wilberforce wilberforce added the ESP32 This is only a problem on ESP32-based devices label Feb 2, 2019
@MaBecker
Copy link
Contributor

@wilberforce how should this be implemented?

@jumjum123
Copy link
Contributor

jumjum123 commented Mar 17, 2020 via email

@MaBecker
Copy link
Contributor

Hmm @jumjum123 it is working on my site, running on 2v06.xx

Date()
//=Date: Thu Jan 1 1970 00:00:07 GMT+0000
E.setTimeZone(2)
//=undefined
Date()
// =Date: Thu Jan 1 1970 02:00:23 GMT+0200
require('Wifi').setSNTP('0.de.pool.ntp.org','2');
//WARNING: SNTP: 0.de.pool.ntp.org 2
//undefined
Date()
// =Date: Sun Aug 30 2020 13:32:19 GMT+0200

Should this be closed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ESP32 This is only a problem on ESP32-based devices
Projects
None yet
Development

No branches or pull requests

3 participants