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!

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

Give feedback

Comments

Similar projects you might like

ioTank
Easy
  • 302
  • 2

Full instructions

An indoor environment monitor for the internet of things which senses temperature, humidity, ultraviolet, and clear light intensity

ioTank

Team Object Syndicate LLC

IoT Made Simple: Servo Control With NodeMCU and Blynk
Easy
  • 242
  • 3

Full instructions

In this simple tutorial, we will explore how to control a servo motor over the internet, using a smartphone.

ESP-01 Dev Kit
Easy
  • 214
  • 4

Full instructions

A complete hardware setup for the ESP-01 to start exploring IoT.

Control Stuff From The Internet: Create a WiFi Switch
Easy
  • 655
  • 7

Full instructions

Learn about relays and WiFi and how to create an internet switch using the Wemos D1 Mini and the relay shield.

Wherzutat-In-a-Box - IoAT
Easy
  • 118
  • 0

Full instructions

IoAT - The Internet of Annoying Things used to make a hidden buzzer that will drive people crazy.

Programming ESP8266 ESP-01 with Arduino
Easy
  • 64,926
  • 45

Protip

ESP8266 is an impressive, low cost WiFi module suitable for adding WiFi functionality project.

ProjectsCommunitiesTopicsContestsLiveAppsBetaFree StoreBlogAdd projectSign up / Login
Feedback