Using an ESP8266 as a time source (Part 2)

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


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".. 
                      "Accept: */*\r\n"..
                      "User-Agent: Mozilla/4.0 (compatible; esp8266 Lua;)"..
conn:on("receive", function(conn, payload)
    print('Time: '..string.sub(payload,string.find(payload,"Date: ")
           +6,string.find(payload,"Date: ")+35))
t =    

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.).


4 thoughts on “Using an ESP8266 as a time source (Part 2)

  1. Improoved version, extracts month, day, year, hour, minute and calculate daytime

    daylight = -7
    m_days = 0 –month days of the previous month

    conn3=net.createConnection(net.TCP, 0)
    conn3:on(“connection”,function(conn3, payload)
    conn3:send(“HEAD / HTTP/1.1rn”..
    “Host: google.comrn”..
    “Accept: */*rn”..
    “User-Agent: Mozilla/4.0 (compatible; esp8266 Lua;)”..
    conn3:on(“receive”, function(conn, payload)
    tmp = string.find(payload,”Date: “)
    t_day = tonumber(string.sub(payload,tmp + 11, tmp + 13))
    t_year = tonumber(string.sub(payload,tmp + 18, tmp + 22))
    t_hour = tonumber(string.sub(payload,tmp + 23, tmp + 24))
    t_min = tonumber(string.sub(payload,tmp + 26, tmp + 27))
    t_month_txt = string.sub(payload,tmp + 14, tmp + 16)
    if t_month_txt == “Jan” then t_month = 1 m_days = 31
    elseif t_month_txt == “Feb” then t_month = 2 m_days = 31
    elseif t_month_txt == “Mar” then t_month = 3
    m_days = 28
    if t_year/2 == math.floor(t_year/2) then
    m_days = 29
    elseif t_month_txt == “Apr” then t_month = 4 m_days = 31
    elseif t_month_txt == “May” then t_month = 5 m_days = 30
    elseif t_month_txt == “Jun” then t_month = 6 m_days = 31
    elseif t_month_txt == “Jul” then t_month = 7 m_days = 30
    elseif t_month_txt == “Aug” then t_month = 8 m_days = 31
    elseif t_month_txt == “Sep” then t_month = 9 m_days = 30
    elseif t_month_txt == “Oct” then t_month = 10 m_days = 30
    elseif t_month_txt == “Nov” then t_month = 11 m_days = 31
    elseif t_month_txt == “Dec” then t_month = 12 m_days = 30

    t_hour = t_hour + daylight
    if t_hour < 0 then
    t_hour = 24 + t_hour
    t_day = t_day -1
    if t_day == 0 then
    t_day = m_days
    t_month = t_month – 1
    if t_month == 0 then
    t_month = 12
    t_year = t_year – 1

    print(t_month.."-"..t_day.."-"..t_year.." "..t_hour..":"..t_min)

Leave a Reply

Your email address will not be published. Required fields are marked *