-- John Longworth December 2017 -- This program displays the time from a SNTP Server on a 128x 64 LCD OLED -- Uses i2c, rtctime, sntptime, u8g & u8g.font_6x10 modules days = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"} months = {"January","February","March","April","May","June","July", "August","September","October","November","December"} function init_i2c_display() id, sda, scl, device = 0, 3, 4, 0x3C i2c.setup(id, sda, scl, i2c.SLOW) disp = u8g.ssd1306_128x64_i2c(device) disp:setFont(u8g.font_6x10) end function getTime() sntp.sync("uk.pool.ntp.org", function(sec, usec, server, info) --print('Time Received ', sec, usec, server) tm = rtctime.epoch2cal(rtctime.get()) tf = (string.format("%02d:%02d:%02d",tm["hour"], tm["min"], tm["sec"])) print("\n The time is ",tf) print(" The day is ", days[tm["wday"]]) print(" The date is ", tm["day"].." ".. months[tm["mon"]].." ".. tm["year"]) disp:firstPage() -- Write to OLED display repeat updateOLED() until disp:nextPage() == false end, function() print("Failed to Update!") end) end function updateOLED() line1 = "Today is "..days[tm["wday"]] line2 = tm["day"].." ".. months[tm["mon"]].." ".. tm["year"] line3 = "Time is "..tf disp:drawFrame(0,0,128,64) --Centre text on the display disp:drawStr((128 - #line1 * 6) / 2, 16, line1) disp:drawStr((128 - #line2 * 6) / 2, 36, line2) disp:drawStr((128 - #line3 * 6) / 2, 56, line3) end init_i2c_display() -- NOTE: The program needs the time delay to allow it to -- connect and get the information fron the SNTP server tmr.alarm(1,10000,1,function() getTime() -- Update display every 10 seconds end)