APC Australia

Make a Wi-Fi internet clock – Part 2

Darren Yates continues his introducti­on into Internet of Things hardware, building an internet clock with built-in Wi-Fi and OLED panel.

-

Last month, we introduced the NodeMCU Developmen­t Kit, a $5 maker board with 802.11b/g/n Wi-Fi, built around the popular ESP8266 Wi-Fi microcontr­oller. The fact you can code it via the standard Arduino integrated developmen­t environmen­t (IDE, www.arduino.cc/downloads) also makes it ideal for learning to make your own Internet of Things (IoT) gadgets. We’ll continue this month with making an automated Wi-Fi internet clock with OLED display.

NODEMCU AND BREADBOARD­S

There are a few NodeMCU-style boards on the market, the ‘LoLin’ being one of the cheapest. But this particular board has an issue — the width requires the full width of a standard 400-point breadboard. That means the only way you can plug wires into it is to use special insulated breadboard bridging jumpers that fit underneath the NodeMCU board. The good thing about the LoLin board, though, is it has no components on the underside, so insulated jumper wires fit comfortabl­y. We recommend you buy a ‘140-piece breadboard jumper wire kit’ featuring different lengths of insulated solidcore jumper wire — eBay has these for $4 or so and they’re perfect. You want the jumper wires sitting as flat to the breadboard as possible. Follow the wiring diagram above, the NodeMCU and OLED panel then just slot into place (we added some double-sided tape to the back of the OLED panel to ensure isolation).

128 X 64 OLED DISPLAY

One of the tricky things with the ESP8266 is that it runs on 3.3VDC voltage, not the usual 5VDC used by standard Arduino boards such as the Uno or Nano (the NodeMCU board has a voltage regulator to convert the USB’s 5VDC down to 3.3VDC). The lower voltage can be a problem for many Arduino-ready displays designed to work on 5VDC and, in fact, try them here and many won’t work, simply because 3.3VDC is too low.

One display panel we like is the 128 x 64-pixel I2C OLED display available on eBay. For around $5, it delivers text and graphics while only requiring two data pins (the inter-integrated circuit or I2C bus). But the other great thing is that it works on 3.3 and 5VDC, so it’s right at home plugged into the NodeMCU.

CLOCK CODE

The secret sauce for our clock is the wificlock.ino source code you’ll find on the APC website ( www.apcmag.com/ magstuff). As we mentioned last time, Network Time Protocol (NTP) is a global network standard for timesynchr­onising computers together and the ntp.org project provides freeaccess public servers for IoT devices to keep time. To make our Wi-Fi internet clock, we’ve taken the ‘NTPClient’ code example from the NodeMCU library and modded it, switching from the US to the Australian time server pool and including the OLED library.

However, we see two issues with the NTPClient code. First, it hits the time servers every 10 seconds with a ‘send time’ request. This needs a substantia­l rewrite to address more permanentl­y, but a crude-yet-simple delay() function can push out the request frequency — this we’ve done, backing it off from every 10 seconds to every 30 seconds. It means your clock will be up to 30 seconds out from internet time, but close enough for a simple time piece. The second issue is that, with this code, the ESP8266 chip runs continuous­ly at full power. That won’t matter much while it’s plugged into your mainspower­ed PC, but if you’re running off a battery, the 80mA power consumptio­n will kill a 2,000mAh battery in a day.

Just like the CPU inside your PC, almost all microcontr­ollers have lowpower modes to reduce consumptio­n for when they’re not needed to run full-tilt. The main power draw on the ESP8266 is the Wi-Fi transceive­r, the radio section sending and receiving data. What we need is a low-power mode that switches off the Wi-Fi but keeps the CPU section running. If you look through the wificlock.ino code, the command for this is: WiFi.forceSleep­Begin(); And to wake up the Wi-Fi engine, we use: WiFi.forceSleep­Wake();

However — each time you shut down the Wi-Fi engine, you lose your network connection. Re-establishi­ng the link on ‘wake’ happens automatica­lly, but this takes a few seconds to complete, so it’s not an ‘instant-on’ thing. Doing this means power consumptio­n drops from around 80mA to nearer 30mA (and that’s mostly the OLED display). The ESP8266 does have a couple of sleep modes — the mode we’re using keeps the CPU core running at full speed. Then ‘deep sleep’ suspends most of the core until a ‘wake’ interrupt triggers, so on balance, our chosen mode achieves maximum power drop whilst maintainin­g full core performanc­e.

SETTING UP NODEMCU LIBRARY

You need to import the NodeMCU library into the Arduino IDE before you can write code to the board. To do so, launch the IDE and, from the menu, select File, Preference­s and in the ‘Additional Boards Manager URLs’, add the text: http://arduino.esp8266. com/stable/package_ esp8266com_index.json Press OK. Next, from the menu, choose ‘Tools’, select ‘Board’, ‘Boards Manager’. Type “ESP8266” in the search box and look for ‘esp2866 by ESP8266 Community’. Move your cursor over the entry and press the Install button. Next, click ‘Close’, then shut down all open instances of the Arduino IDE.

Most Arduino projects require extra libraries, so we’ve included these in the source code pack, so copy the contents of the ‘libraries’ subfolder to the same folder in the Arduino IDE location. Once that’s done, you can restart the IDE.

FLASHING CODE

To flash the clock code to the board, plug the board into your PC via USB and in the IDE, select ‘Tools’, ‘Boards’, scroll down and choose ‘NodeMCU 1.0 (ESP-12E module)’. After that, you again choose from the menu ‘Tools’, ‘Port’ and select the ‘COMx’ option that isn’t COM1 (that’ll be your mouse). Load up the wificlock.ino code, enter into the source code the SSID name and password for your wireless network, plus the appropriat­e UTC time offset for your location, hit the ‘Upload’ button and watch it upload. After about 10 seconds or so, the current local time should appear on the OLED panel.

HAVE A GO

There are any number of projects you can build using the NodeMCU board — so get hold of one and start making your own!

 ??  ?? You’ll find this ‘breadboard jumper wire kit’ on eBay for $3.
You’ll find this ‘breadboard jumper wire kit’ on eBay for $3.
 ??  ?? Use insulated jumper wires on the 400-tiepoint breadboard for this project.
Use insulated jumper wires on the 400-tiepoint breadboard for this project.
 ??  ?? Use this overlay diagram to build your own Wi-Fi internet clock. Make sure to select the correct COM port before trying to flash your code.
Use this overlay diagram to build your own Wi-Fi internet clock. Make sure to select the correct COM port before trying to flash your code.
 ??  ?? Our Wi-Fi internet clock hooks into your network and sets its own time.
Our Wi-Fi internet clock hooks into your network and sets its own time.
 ??  ?? Choose File, Preference­s and add the link to import the NodeMCU library.
Choose File, Preference­s and add the link to import the NodeMCU library.
 ??  ??

Newspapers in English

Newspapers from Australia