This tutorial project is to create a very simple web clock on ESP8266/ESP-12 boards, by using only built-in modules (libraries) of MicroPython.
In short, the project does the following things:
- Connect to a WiFi router;
- HTTP GET and parse web JSON data every minute, then update the internal RTC (real time clock) module; (this is to avoid DDoS-like query to the server. However since the software RTCs are incredibly inaccurate, we still have to update it within a minute or an hour, depends on the board you are using.)
- Display internal RTC date and time on SSD1306 OLED display.
- If the board lose WiFi connection it would reboot itself.
All the functions above can be achieved without importing any third party libraries. Neat!
Change the value of the variable ssid to the name of your local WiFi router, and pw as your Wifi router password. (Do not share your passwords online.)
Then we extract value after "datetime" and get all the info we need.
The SSD1306 OLED display is connected to the board via I2C pins (D1/GPIO5 = SCL, D2/GPIO4 = SDA). Most of the OLED models can be powered by either 5V or 3.3V.
If you don't have or don't want to use OLED, you can replace related codes as print() to output data directly to REPL. Without a connected OLED display the OLED module would time out and halt the code.
In the photo I was using a WeMos D1 mini (ESP-12F), one of the popular ESP-12 boards. The code also works on my NodeMCU V3 (mine version uses ESP-12E). The RTC on my D1 mini works much better; it can be updated only once a hour and maintain decent accuracy. My NodeMCU V3 has to update once a minute.
For other beginner's reference, I used MicroPython for ESP8266 firmware v1.10 and flashed it via NodeMCU-PyFlasher (believe me, this works better than the NodeMCU Flasher!) and written the code in Thonny IDE, which has Python 3.7 built-in and is pretty easy to install/use.