Since my last attempt at using an ESP8266 to get the date and time from the internet I’ve tried out another module, this time with the NodeMCU firmware. This is an open source firmware that allows you to load code onto the processor on the board and run Lua code directly from the microprocessor, rather than needing another MCU such as an Arduino to read the data.
The onboard processor is 32 bit with SDIO, SPI, UART and 16 GPIO pins so it has a lot more power than an Arduino. From the NodeMCU wiki:
- It’s running at 80MHz
- 32KBytes of instruction RAM
- 96KBytes of data RAM
- 64KBytes boot ROM
- It has a Winbond W25Q40BVNIG SPI flash
- It’s a RISC architecture
- The core is a 106micro Diamond Standard core (LX3) made by Tensilica
So as you can see, pretty powerful stuff for an MCU.
To flash the module with the NodeMCU firmware use the NodeMCU flasher tool and the firmware is either built into the tool or you can download the latest binary. All can be found at the NodeMCU GitHub repository:
Don’t forget to pull the GPIO1 pin to GROUND when flashing the firmware.
Once you’ve flashed the firmware you can connect to the unit via any serial terminal or using ESPlorer. I recommend ESPlorer.
The module once reset looks for a Lua file called init.Lua, this must have your code that will run when the unit is powered up or reset. You can also upload other files and run those. To obtain time I’ve uploaded two Lua files, one to initialise the module and another, that is ran by an interrupt timer from init.Lua every 60 seconds and outputs the time to the serial port. This arrangement is far easy to use than my previous attempt using an Arduino.
The init.Lua file is
wifi.setmode(wifi.STATION) wifi.sta.config("SSID","PASSWORD") tmr.delay(5000) uart.setup( 0, 115200, 8, 0, 1, 0, 1 ) print("NodeMCU Welcome") tmr.alarm(0, 60000, 1, function() dofile('googleTime.lua') end )
and the file that fetches the time is
-- retrieve the current time from Google -- tested on NodeMCU 0.9.5 build 20150213 conn=net.createConnection(net.TCP, 0) conn:on("connection",function(conn, payload) conn:send("HEAD / HTTP/1.1\r\n".. "Host: google.com\r\n".. "Accept: */*\r\n".. "User-Agent: Mozilla/4.0 (compatible; esp8266 Lua;)".. "\r\n\r\n") end) conn:on("receive", function(conn, payload) print('Time: '..string.sub(payload,string.find(payload,"Date: ") +6,string.find(payload,"Date: ")+35)) conn:close() end) t = tmr.now() conn:connect(80,'google.com')
As you can see, the script is really simple and this offloads any processing from the Arduino to the ESP8266 to obtain time. Adding this to my clock project will simply require parsing the date/time string and syncing the RTC chip accordingly. Alternatively, i could use the ESP8266 on it’s own to control the display and RTC and get rid of the Arduino altogether.
I’m going to keep playing with a NodeMCU loaded module and learn a bit more Lua to see what else I can do. My idea is to use one to obtain all kinds of useful information from the internet and then display it on an information display in the house (time, date, weather, upcoming Google calendar entries, birthdays, weather, etc.).