Things used in this project

Hardware components:
Everything ESP ESP-12E ESP8266 Dev Board
An ESP-01 would do as well, but you would need different wiring and a USB to TTL converter.
×1
SSD1306 0.96" I2C OLED Display for Arduino
×1
LiFePo4wered/USB 3.3V power system USB chargable
×1
12002 04
Breadboard (generic)
×1
11026 02
Jumper wires (generic)
×1

Schematics

Hookups
Connect the following ESP pins to the OLED pins.

ESP -> OLED
---------------
GND -> GND
3V3 -> VCC
D1 -> SDA
D2 -> SCL
Img 20160317 162004896

Code

clock1.luaLua
This is the clock program. You need to change the wifi settings to match your wireless network
-- Clock 1 - OLED Display clock
-- March, 2016 
-- @kayakpete | pete@hoffswell.com
-- Hardware: 
--   ESP-12E Devkit
--   4 pin I2C OLED 128x64 Display Module
-- Connections:
--   ESP  --  OLED
--   3v3  --  VCC
--   GND  --  GND
--   D1   --  SDA
--   D2   --  SCL

-- This program gets time from the Internet, and displays it on the OLED.

-- Variables 
time=nil
sda = 1 -- SDA Pin
scl = 2 -- SCL Pin
timezone = "EST"  
str1="    Hello World!!"
str2="     @kayakpete"
ssid="yourssid"
ssid_password="ssidpassword"
s=0
m=0
h=0

function gettime() -- Get Time by connecting to timeapi.org
   time=nil
   conn=net.createConnection(net.TCP, 0) 
   conn:on("connection",function(conn, payload)
   conn:send("GET /"..timezone.."/now "..
       "HTTP/1.1\r\n".. 
       "Host: www.timeapi.org\r\n"..
       "Accept: */*\r\n"..
       "User-Agent: Mozilla/4.0 (compatible; esp8266 Lua;)"..
       "\r\n\r\n") 
   end)
            
   conn:on("receive", function(conn, payload)
       for line in string.gmatch(payload.."\n", "([^\n]*)\n") do -- Just get last line of html
          datetime = line
       end;
       --print('DateTime: '..datetime)
       time = string.sub(datetime,string.find(datetime,"T")+1,string.find(datetime,"T")+8)
       conn:close()
   end) 

   conn:connect(80,'www.timeapi.org')
   return(time)
end

function wait_for_time()
  if time == nil then 
     print("Waiting for time...")
  else
    tmr.stop(2)
    print("Time Set! "..time)
    h = tonumber(string.sub(time,1,2))
    m = tonumber(string.sub(time,4,5))
    s = tonumber(string.sub(time,7,8))
  end
end
         
function init_OLED(sda,scl) --Set up the u8glib lib
     sla = 0x3C
     i2c.setup(0, sda, scl, i2c.SLOW)
     disp = u8g.ssd1306_128x64_i2c(sla)
     disp:setFont(u8g.font_6x10)
     disp:setFontRefHeightExtendedText()
     disp:setDefaultForegroundColor()
     disp:setFontPosTop()
     --disp:setRot180()           -- Rotate Display if needed
end

function write_OLED() -- Write Display
   disp:firstPage()
   repeat
     --disp:drawFrame(2,2,126,62)
     disp:drawStr(5, 10, str1)
     disp:drawStr(35, 30,  string.format("%02d:%02d:%02d",hd,m,s)..meridies)
     disp:drawStr(5, 50, str2)
     --disp:drawCircle(18, 47, 14)
   until disp:nextPage() == false
   
end



-- Main Program 

wifi.setmode(wifi.STATION)
wifi.sta.config(ssid,ssid_password)
wifi.sta.connect()
tmr.delay(1000000)   -- wait 1,000,000 us = 1 second
print(wifi.sta.status())
print(wifi.sta.getip())
  
init_OLED(sda,scl)

time=gettime() -- Set clock to time server
tmr.alarm(2, 1000, 1, wait_for_time)

tmr.alarm(0, 1000, 1, function() -- Every second increment clock and display
   s = s+1
   if s==60 then
     s=0
     m=m + 1
   end
   if m==60 then
     m=0
     h=h + 1
     time=gettime() -- sync time every hour
     tmr.alarm(2, 1000, 1, wait_for_time)
   end
   if h==24 then
     h=12
   end
   hd = h
   meridies="AM"
   if hd > 12 then
     hd = hd - 12
     meridies="PM"
   end
   
   --print(string.format("%02d:%02d:%02d",h,m,s))
   write_OLED()
end)
display1.luaLua
Let's just test the display. This is handy if you don't have wifi, or just want to troubleshoot/play with the display
-- OLED Display demo
-- March, 2016 
-- @kayakpete | pete@hoffswell.com
-- Hardware: 
--   ESP-12E Devkit
--   4 pin I2C OLED 128x64 Display Module
-- Connections:
--   ESP  --  OLED
--   3v3  --  VCC
--   GND  --  GND
--   D1   --  SDA
--   D2   --  SCL

-- Variables 
sda = 1 -- SDA Pin
scl = 2 -- SCL Pin

function init_OLED(sda,scl) --Set up the u8glib lib
     sla = 0x3C
     i2c.setup(0, sda, scl, i2c.SLOW)
     disp = u8g.ssd1306_128x64_i2c(sla)
     disp:setFont(u8g.font_6x10)
     disp:setFontRefHeightExtendedText()
     disp:setDefaultForegroundColor()
     disp:setFontPosTop()
     --disp:setRot180()           -- Rotate Display if needed
end

function print_OLED()
   disp:firstPage()
   repeat
     disp:drawFrame(2,2,126,62)
     disp:drawStr(5, 10, str1)
     disp:drawStr(5, 20, str2)
     disp:drawCircle(18, 47, 14)
   until disp:nextPage() == false
   
end

-- Main Program 
str1="    Hello World!!"
str2="     @kayakpete"
init_OLED(sda,scl)
print_OLED() 

Credits

Pete2014
Pete Hoffswell

Network Engineer for All the People. Internet advocate. Microcontroller Fan. Maker.

Contact

Replications

Did you replicate this project? Share it!

I made one

Love this project? Think it could be improved? Tell us what you think!

Give feedback

Comments

Similar projects you might like

Send e-mail - NodeMCU with Mikroelektronika Clicks
Easy
  • 126
  • 2

Full instructions

We read the data with the ambient click and in case the values become small, an e-mail message is sent.

Automation Using Amazon Echo Alexa and NodeMCU Without IFTTT
Easy
  • 324
  • 3

Protip

Control relays attached to NodeMCUs using Amazon Echo Alexa without IFTTT.

Arduino-Controlled Wall-E Head
Easy
  • 597
  • 8

Full instructions

In this project I'll show you how to control a servo remotely from a regular web page without needing a web server in your Arduino board.

NodeMCU Webserver with Class-Based 60-Second Temp Average
Easy
  • 300
  • 2

Protip

Averaging temperatures over 60 seconds smooths some of the sensor limitations, allowing for a smoother graphic.

BLYNKING webpage data using ThingSpeak and NodeMcu
Easy
  • 281
  • 3

Protip

A simple project to display a particular webpage data in blynk app via bluetooth module and using thingspeak platform.

Add WiFi to Arduino UNO
Easy
  • 993
  • 3

Protip

Connect your mincrocontroller to the internet by adding ESP8266 WiFi Module!

ProjectsCommunitiesTopicsContestsLiveAppsBetaFree StoreBlogAdd projectSign up / Login