Introduction: Web Clock Version 2.0 (ESP8266 - Wemos)
- LCD screen of various sizes supported. 2x8 or 2x16 LCD Screen is recommended
- Displays time and date
- Web based time synchronization (No RTC Required)
- Super accurate time keeping
- Hourly chimes
- Time zone friendly
- DST switch for summer time
- 12/24 switch
- WiFi connection to access web
- It can powered via micro USB cable or other 5V source
- WiFi custom message/reminder receiver (Windows software is available to send messages to device)
- Acknowledge/display last message button
This digital clock is based on ESP8266 WIFI micro-controller unit. The flavor used for project design and creation is WEMOS D1 mini which is equipped with a ESP-12F (ESP8266), a 5v to 3.3v regulator, a CH340G USB to UART chip and of course a micro usb connector. This module has plenty of I/O pins to connect the LCD display via the one I2C converter and to use few of them for option and command switches.
Step 1: Some Info About It
How it works
The device uses a rather unusual way to get the time from the web. It connects to an http web server (e.g. google.com) and gets the GMT time from the header file, then it calculates the current time for the time zone selected and it displays it to the LCD screen. To keep it up accurate the web time update is taking place once per 5 minutes which still is very often since the device calculation doesn't seem to have time drift. In any case you can change the update interval by tweaking the value on doloops.lua file.
The whole project is based on NODEMCU firmware which should be flashed to WEMOS D1 mini module quite easy. Running NODEMCU firmware set ESP8266 to run Lua scripts which is super flexible and easy to learn, there are tons of examples and documentation online about it.
Note: Using a lightweight firmware leaves more memory space for operation and the device will be more stable. NODEMCU can be customized by using the online build service (https://nodemcu-build.com/) in order to contain only the project required libraries. Modules required by the code are: bit, file, GPIO, I2C, net, node, PWM, timer, UART and WiFi. You are highly advised to use master branch to build your firmware and select only the modules required. NODEMCU acts like an operating system on the module which means more flexibility because of the code modular structure. The code is separated in different files with different scope, during development we can run just some parts of the code instead the full project and we can check variables and system state during the run-time. In addition future changes or additions to the code will be very simple without messing with the entire system and code.
Step 2: The Hardware
- 1 HD44780 compatible LCD module (preferably 2x8 or 2x16) https://goo.gl/1EJsLO
- 1 Wemos D1 mini WiFi module https://goo.gl/qFMxZX
- 1 IIC/12C/TWI/SPI serial interface module https://goo.gl/U765BN
- 1 5v active buzzer https://goo.gl/Ev8Wjs
- 2 On-On Toggle Switches https://goo.gl/Rhzd3Q
- 1 Push button https://goo.gl/f4crQy
- Some header pins and cables for the connections https://goo.gl/BkCklV
- For configuration purposes: an Arduino board. (Don't buy one just for the configuration! Look read below)
Note: The easiest way to find the serial’s interface address is to use an Arduino board and run a simple detection sketch (http://playground.arduino.cc/Main/I2cScanner). If you don’t have and Arduino board available you can just try some standard addresses in the code and see which one is giving you results. Normally module’s address is set between 0X20 and 0X27. There is descriptive video for I2C addressing available here:
Step 3: Connections
The required connections are very limited.
Start by connecting the I2C module to your LCD module. This can be done by using a straight header pinout to connect all the 16 lines of I2C module one by one to the LCD's 16 line header. The only critical is to check that pin 1 on I2C is connected to pin 1 of LCD.
Second step is to connect the I2C to WEMOS D1. This requires just 4 wires 5V, GND, SDA and SCL connections. Please note that SCL is connected to D5 and SDA is connected to D4.
Next connect buzzer, switches and button as in illustration. The switches illustrated are ON-ON type switches with three pins to connect either to 3v3 level either to GND level. This way pull down resistors can be avoided, but if you want you can use simple two pin switches by connecting 10K pull down resistors to D1 and D7 terminals and just use conenction from 3V3 and digital pin to each switch avoiding the ground connection.
Step 4: The Code (FileTo Download Is Here)
Startup file. The file contains essential info for device setup and some trap routine to display splash screens only on hardware reset. If the device reset caused by software reason or a low memory issue the routine will send it back to operation ASAP. The code here triggers googleTime.lua, udpserver.lua and button.lua (and lcdprint.lua every time we need to display something).
Display to LCD functionality. This file is responsible to display the info to LCD module. It is called several times on the code (in most of the files there is a reference for lcdprint). The only parameters you should set are the pins connected to SDA and SCL signal lines and device address (DEV).
Get the current time from web. It is the system cornerstone functionality. The code here get the header file from google.com and retrieves the GMT date and time. After this the doloops.lua file is kicked in.
Sets device looping routine for 5 minutes. During this routine the time is calculated and after that calculateTime.lua is called. When 5 minutes period is reached the looping routine is stopped to ask for a fresh synchronization from the web using googleTime.lua file again.
Converts GMT time to time zone time and calculates summer time. The code is responsible for checking the switches state, to convert the GMT time to local time and display it to LCD.
Sets UDP server waiting for network messages. The UDP server can set custom messages to be displayed on LCD screen and accept some other diagnostic commands.
Enables the acknowledge button. The button can acknowledge/display last message received by UDP Server.
Step 5: Code Changes
Only two plus one changes are essential to be made on the provided code to set the device up and running:
- Set your WiFi credentials: Open init.lua file and navigate to lines 76 and 77 to set WiFi SSID and password.
- Set your timezone difference: Open calculateTime.lua file and navigate to line 6 and change the value on variable "zonedrift" which represents time zone difference.
- Set your 12C module's address in lcdprint.lua file. The address is located on line 29 of the file. Note:normally the default address for modules sold is 0x27, so no change is needed. Please try the provided file first before change the module's address
Save your files and upload them to your device.
Step 6: How to Flash Firmware - How to Upload the Code
There are lot of examples and tutorials online about firmware flashing on ESP8266. The most complete (and up to date) guide is https://nodemcu.readthedocs.io/en/master/en/flash/
Once the board is flashed with the firmware and it's file system formatted (the format process takes place upon its first boot after flashing), Lua scripts can be uploaded very easily suing one of several tools available online. A guide can be found here: https://nodemcu.readthedocs.io/en/master/en/upload...
I personally have created one tool for Lua file uploading this called Selene.exe which can be downloaded here:
Step 7: Clock Commander Software
Control the device via network
This little utility is created to send commands via LAN to web clock and control the displayed messages. Its interface is straightforward. The only thing you have to configure is web clock's IP address and server port (default port used is 7333). After the network setup you can use the utility to send messages to the device which should immediately display them.
- "Send sound command" causes device to sound an alarm sound when the message is received
- "Send clear command" caused device to clear display before the received text is displayed.
- "Use half display" is a special mode which keep the clock running and only the first line is used to display the custom message
- Use raw command is used to send commands used for diagnostic and control purposes.
The following raw commands are accepted currently by the device:
Note 1: When a message arrives to the clock the clock functionality is suspended. The message will be displayed until user hold down the acknowledge button for 3 seconds and release it.
Note 2: Using the button once again you can display the last message sent by Clock Commander.
Step 8: Improvements - Things to Add - Issues
Improvements - Things to add
- A case to look as a complete device.
- UDP server function can be altered to accept more commands and functions.
- Function needs to be added to calculate date during the period we have difference between DST and local time zone.
- More hourly chimes. More alarm sounds.
- Remote messages with expiration.
- Code cleanup to be more lightweight and run even more stable.
Memory limitations: WEMOS D1 mini is based on ESP-12F (ESP8266) so hardware limitations of ESP-12F apply to WEMOS as well. Adding more code leaves less free memory for runtime operations and this can lead the device to low memory issue. When such event occurs the device will restart automatically. The project code can handle a restart due memory loss by using different startup up routine and set the device up and running within few seconds and set back to normal operation without letting the end user notice about it.
Date calculation: Currently the date is not calculated on the code. The displayed date is what the system received from the web. To avoid wrong date indication a routine takes place during the midnight (+/- time zone difference) to avoid display the date. This will be corrected in next code version.
Participated in the
Microcontroller Contest 2017