This commit is contained in:
Thorsten von Eicken 2015-12-24 12:49:55 -08:00
parent f210ca368f
commit 9aa07bc92d
5 changed files with 78 additions and 15 deletions

View File

@ -19,7 +19,7 @@ info = {
'espruino_page_link' : 'EspruinoESP8266',
'default_console' : "EV_SERIAL1",
'default_console_baudrate' : "115200",
'variables' : 1400,
'variables' : 1300,
'binary_name' : 'espruino_%v_esp8266',
};
chip = {

View File

@ -40,6 +40,7 @@
#include <osapi.h>
#include <ping.h>
#include <espconn.h>
#include <sntp.h>
#include <espmissingincludes.h>
#include <uart.h>
@ -1527,6 +1528,63 @@ void stopMDNS() {
mdns_started = false;
}
//===== SNTP
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-sysTime != 0) {
DBG("NTP time: %ld delta=%ld %s\n", ntpTime, ntpTime-sysTime, sntp_get_real_time(ntpTime));
}
jshSetSystemTime((int64_t)ntpTime * 1000000);
os_timer_disarm(&sntpTimer);
os_timer_arm(&sntpTimer, 30*1000, 0);
}
/*JSON{
"type" : "staticmethod",
"class" : "Wifi",
"name" : "setSNTP",
"generate" : "jswrap_ESP8266_wifi_setSNTP",
"params" : [
["server", "JsVar", "The NTP server to query, for example, `us.pool.ntp.org`"],
["tz_offset", "JsVar", "Local time zone offset in the range -11..13."]
]
}
Starts the SNTP (Simple Network Time Protocol) service to keep the clock synchronized with the specified server. Note that the time zone is really just an offset to UTC and doesn't handle daylight savings time.
The interval determines how often the time server is queried and Espruino's time is synchronized. The initial synchronization occurs asynchronously after setSNTP returns.
*/
void jswrap_ESP8266_wifi_setSNTP(JsVar *jsZone, JsVar *jsServer) {
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);
}
//===== Reset wifi
// When the Espruino environment is reset (e.g. the reset() function), this callback function

View File

@ -37,5 +37,6 @@ JsVar *jswrap_ESP8266_wifi_getAPIP(JsVar *jsCallback);
JsVar *jswrap_ESP8266_wifi_getHostname(JsVar *jsCallback);
void jswrap_ESP8266_wifi_setHostname(JsVar *jsHostname);
void jswrap_ESP8266_wifi_getHostByName(JsVar *jsHostname, JsVar *jsCallback);
void jswrap_ESP8266_wifi_setSNTP(JsVar *zone, JsVar *server);
#endif /* LIBS_NETWORK_ESP8266_JSWRAP_ESP8266_NETWORK_H_ */

View File

@ -877,31 +877,31 @@ error:
*
* It seems pretty clear from the API and the calibration concepts that the RTC runs off an
* internal RC oscillator or something similar and the SDK provides functions to calibrate
* it WRT the crystal oscillator, i.e., to get the current clock ratio.
*
* The RTC timer is preserved when the chip goes into sleep mode, including deep sleep, as
* well when it is reset (but not if reset using the ch_pd pin).
* it WRT the crystal oscillator, i.e., to get the current clock ratio. The only benefit of
* RTC timer is that it keeps running when in light sleep mode. (It also keeps running in
* deep sleep mode since it can be used to exit deep sleep but some brilliant engineer at
* espressif decided to reset the RTC timer when coming out of deep sleep so the time is
* actually lost!)
*
* It seems that the best course of action is to use the system timer for jshGetSystemTime()
* and related functions and to use the rtc timer only at start-up to initialize the system
* timer to the best guess available for the current date-time.
* and related functions and to use the rtc timer only to preserve time during light sleep.
*/
/**
* Given a time in milliseconds as float, get us the value in microsecond
*/
JsSysTime jshGetTimeFromMilliseconds(JsVarFloat ms) {
// os_printf("jshGetTimeFromMilliseconds %d, %f\n", (JsSysTime)(ms * 1000.0), ms);
// os_printf("jshGetTimeFromMilliseconds %d, %f\n", (JsSysTime)(ms * 1000.0), ms);
return (JsSysTime) (ms * 1000.0 + 0.5);
} // End of jshGetTimeFromMilliseconds
}
/**
* Given a time in microseconds, get us the value in milliseconds (float)
*/
JsVarFloat jshGetMillisecondsFromTime(JsSysTime time) {
// os_printf("jshGetMillisecondsFromTime %d, %f\n", time, (JsVarFloat)time / 1000.0);
// os_printf("jshGetMillisecondsFromTime %d, %f\n", time, (JsVarFloat)time / 1000.0);
return (JsVarFloat) time / 1000.0;
} // End of jshGetMillisecondsFromTime
}
// Structure to hold a timestamp in us since the epoch, plus the system timer value at that time
// stamp. The crc field is used when saving this to RTC RAM
@ -938,7 +938,7 @@ static void saveTime() {
*/
JsSysTime CALLED_FROM_INTERRUPT jshGetSystemTime() { // in us -- can be called at interrupt time
return sysTimeStamp.timeStamp + (JsSysTime)(system_get_time() - sysTimeStamp.hwTimeStamp);
} // End of jshGetSystemTime
}
/**
@ -954,7 +954,7 @@ void jshSetSystemTime(JsSysTime newTime) {
rtcTimeStamp.timeStamp = newTime;
rtcTimeStamp.hwTimeStamp = rtcTime;
saveTime(&rtcTimeStamp);
} // End of jshSetSystemTime
}
/**
* Periodic system timer to update the time structure and save it to RTC RAM so we don't loose

View File

@ -16,6 +16,7 @@
#include <user_interface.h>
#include <osapi.h>
#include <sntp.h>
#include <uart.h>
#include <espmissingincludes.h>
@ -219,8 +220,11 @@ static void mainLoop() {
#ifdef EPS8266_BOARD_HEARTBEAT
if (system_get_time() - lastTime > 1000 * 1000 * 60) {
lastTime = system_get_time();
os_printf("tick: %us, heap: %u\n",
(uint32)(jshGetSystemTime())/1000000, system_get_free_heap_size());
// system_get_free_heap_size adds a newline !?
char *t = sntp_get_real_time((uint32)(jshGetSystemTime()/1000000));
int l = strlen(t);
if (l > 2) t[l-1] = 0;
os_printf("%s, heap: %u\n", t, system_get_free_heap_size());
}
#endif