The web clock part is similar to my previous project Very Simple MicroPython ESP8266/ESP-12 Web Clock. The main difference is that it queries time via NTP (Network Time Protocol) instead of using a third-party API.
The ntptime is a built-in library of MicroPython, which can be easily used to update system time (local time) of ESP8266/ESP32 boards by simply calling ntptime.settime(). (Be noted that your NTP queries may not always be successful depending on your WiFi connection quality.)
The time you get from NTP is UTC+0; in the code the variable timezone_hour can be used to set the timezone adjustment. For example, set timezone_hour = 8 if your timezone is UTC+8.
This project also uses a non-blocking web server to provide a alarm clock setting interface (it would not block the loop process to wait for new client connection, hence the clock time can still be displayed normally). And I wrote a piece of code that can parse any number of parameters by any name into a list para_array from the URL. This may be useful to people who want to build similar projects.
REPL output sample:
Connecting to WiFi...
Connected.
Web server is now online on xxx, IP: 192.168.100.155
NTP server query successful.
System time updated: (2019, 12, 4, 7, 56, 25, 2, 338)
New client connected, IP: ('192.168.100.164', 54551)
Alarm has been set to 7:30
Alarm enabled
Sending web page...
Client connection ended.
An active piezo buzzer serves as the alarm and a tilt switch (I used a SW-520D) can be used to turn the alarm off by shaking it. A 0.96" OLED display shows the clock time and alarm time, the alarm status ("o" is enabled and "x" is not) as well as the server IP (on your local WiFI network).
The alarm would turn itself off after a minute if no one shake the tilt switch.
The code is tested on MicroPython firmware v1.11 with WeMos D1 mini.
Comments