11 February 2019
Espruino devices do not usually have a proper battery-backed RTC available, so the second easiest way to get a time is via (S)NTP. On Espruino on the ESP8266 this is super-simple (192.168.21.1 is my NTP time server and 9 is the timezone offset):
//ESP8266
//Wifi.setSNTP("192.168.21.1", 9);
but that does not work on Espruino on the ESP32 as Wifi.setSNTP() does not exist here (nor anywhere else). But a module for SNTP exists. This is how to use it to set the time:
// ESP32
E.setTimeZone(9);
const sntp=require('sntp');
var options = {
host: '192.168.21.1',
port: 123,
timeout: 1000
};
sntp.time(options, function (err, time) {
if (err) {
console.log('Failed: ' + err.message);
return;
}
setTime(time.T2/1000);
//console.log('Local clock is off by: ' + time.t + ' milliseconds');
//console.log('Full NtpMessage:', time);
});
Way more complicated, but it's more general on Espruino.