There are many different ways to access local date and time from the internet, but mostly those interfaces change over time and code using them has to be adopted. I was looking for something that would last for years without interaction form my side. That's why I've decided to use Network Time Protocol. There is only one catch: it's UTC time without date. It was not such a big deal, because there are lot of algorithms out there so I've just adopted one of them.
Now it's possible to obtain local date and time from NodeMCU over serial port and the whole thing is reliable and will last for many years without touching it :)
Here is the source code: https://github.com/maciejmiklas/NodeMCUUtils
The whole implementation is dividend into small building blocks, putting them together will give you desired serial API. This is just short overview:
- Date Format - calculates local date and time based on timestamp
- WiFi Access - simple facade for connecting to WiFi
- NTP Time - obtains UTC timestamp form give NTP server
- NTP Clock - keeps actual UTC timestamp and synchronizes it periodically with NTP server
- Serial API - finally this one provides API for date and time
Provides functionality to get local date and time from timestamp given in seconds since 1970.01.01
For such code:
For such code:
you will get this output:
In order to format date for USA you have to replace require "dateformatEurope" with require "dateformatAmerica" and call setAmericaTime instead of setEuropeTime. Functionality is divided into small scripts in order to save some RAM.
It's simple facade for connecting to WiFi. You have to provide connection credentials and function that will be executed after the connection has been established.
execute(...) connects to WiFi and this can take some time. You can still call this method multiple times. In such case callbacks will be stored in the queue and executed after WiFi connection has been established.
This simple facade connects to given NTP server, request UTC time from it and once response has been received it calls given callback function.
Example below executes following chain: WiFi -> NTP -> Date Format.
So in the fist step we are creating WLAN connection and registering callback function that will be executed after connection has been established. This callback function requests time from NTP server (ntp.requestTime).
On the ntp object we are registering another function that will get called after NTP response has been received: printTime(ts).
and console output:
This script provides functionality to run a clock with precision of one second and to synchronise this clock every few hours with NTP server.
In the code below we first configure WiFi access. Once the WiFi access has been established it will call ntpc.start(). This function will start clock that will get synchronized with given NTP server every minute. Now you can access actual UTC time in seconds over ntpc.current. In order to show that it's working we have registered timer that will call printTime() every second. This function reads current time as ntpc.current and prints it as local time.
so this is the output:
Serial API exposes simple interface that provides access to diagnostic info and date so that it can be accessed outside NodeMCU - for example by Arduino.
Serial API is divided into few Lua scripts. Loading of each script will automatically add new API commands:
- serialAPI.lua - has to be always loaded. It initializes serial interface with few diagnostics commands.
- serialAPIClock.lua - access to clock including date formatter.
Each script above registers set of commands as keys of scmd table - inside of each script you will find further documentation.
Example below provides date access over serial port:
Here are few Serial API commands and their responses:
Executing multiple scripts can lead to out of memory issues on NodeMCU. One possibility to solve it is to build custom firmware containing only minimal set of node-mcu modules: cjson, file, gpio, net, node, tmr, uart, wifi. This blog provides detailed upgrade procedure: http://maciej-miklas.blogspot.de/2016/08/installing-nodemcu-v15-on-eps8266-esp.html