Youtube channel

Check out my youtube channel!

Thursday, 31 December 2015

ESP8266 based NTP Clock

A little over a year ago, the ESP8266 WiFi SoC started getting attention and quickly became a little chip of wonder. On a fully self-contained breakout board with this chip, a complete WiFi enabled controller is well under $5! It's gathered huge interest from the hobbyist world.

Since the device first made its appearance, I've been watching news on the evolution of its development environments. My play time is very limited at the moment, so I was very excited to learn that a standard C++/Arduino environment is available for this - rather than spending time learning a new SDK, I can just use already familiar environments. More time playing with it, less time stuffing around with compilers, etc.

So as you do, the first thing I decided to make of this was a clock. Home made clocks have been in the news a bit lately. Clocks are great as first projects for new MCUs, as they are always useful, and in one project you get to learn a lot about the device:

  • General MCU behaviour and features
  • Display / output interfacing (leading to tight I/O operations)
  • Precision interval/timekeeping/interrupts.
  • Interfacing with the device for time adjustment.
Given that the device is primarily a WiFi / network device, the clock had to interface with the network, and what better than to have it as an NTP client. I also had a spare 4 digit LED display, and the 3-wire interface works perfectly with the 4 GPIO pins broken out on the ESP-1.

An so began my design for this clock. A brief overview of what I envisaged for it:
  • Synchronised over NTP (Network Time Protocol, RFC 5905)
  • Browser based configuration of WiFi credentials, NTP settings, etc.
  • Configuration saved into non volatile memory.
  • Single button to enter AP mode for configuration.
  • IP Address display (again, single button)
  • Indication of NTP sync loss.
  • Absolute minimal component count.
As this was also a learning exercise, the development was not a straight-forward linear process. Each (software) component in the clock involved some tinkering and hacking, but as I found comfortable solutions, I refactored into clean sections.

There are quite a few examples to be found of fetching NTP time with this device, but very few show a standalone day to day device.


The clock hardware is really really simple. I doubt it could be distilled down to anything less than the following components:
  • ESP-1 module.
  • 4-digit 7 segment display (SPI interface) [this Sure Electronics one]
  • LM1117 3.3v regulator.
  • 10uF bypass cap
  • PCB mount pushbutton switch
Diagram of the setup:

The most notable aspect of this is the interfacing between the ESP-1 and the LED display.

Firstly, two of the GPIO pins (GPIO1 and GPIO3) are normally used as TX and RX on UART0. These are used for flashing the ESP as well as the default Serial interface. I want to use them for the clock. Not a problem though, in setup(), I configure them as outputs and this works well.

Secondly, the display is sold as operating at 5v. But I do not need 3.3-5v level converters as the driver chips (74HC164) are 3.3v compatible by virtue of them registering a high level at ~2.1v. That really trims the component count.

Lastly, whilst the interface on the display is SPI-like, and the ESP8266 has a hardware SPI, I could not use this as the ESP has SPI on GPIO12-15 which are not broken out on this board. Not to worry, it only takes a dozen or so lines of code to bit-bang.

As for accurrate timekeeping, there is no point incorporating an RTC here. Early experiments with the millis() call showed that the ESP's main oscillator can keep time with an accuracy of about 1s/day, so I just use this and have NTP re-sync at a configurable interval.


The software is based on the Arduino ESP8266 core. As explained above, I would have like to learn the other environments like NodeMCU, but have a severe lack of time. Instead I stuck with something already familiar.

The software is designed for the clock to be configured entirely over WiFi, with a single helper button for a couple of extra features. This is a clock that anyone can build and use, without having to configure in the code or connect to a PC to set the time.

The code is available at everyone's favourite repository, Github. See

I welcome any patches.


The clock is written to be really simple to setup and use. Here are some details on various features:


After initial powerup, the clock should detect that there is no configuration and automatically enter AP mode. The display should show 'AP'. If not, press the button within 5s of powerup. Once in AP mode, you can connect your phone/etc to the 'ESP-CLOCK' SSID (no security creds), and then point a browser to the clock. The IP address should be, but you can press the button and the address will be displayed.


After entering setup above, and pointing a browser to the address, you will see a configuration page. Most of the settings are self-explanatory. Just remember to tick 'Update Wifi config' to have the SSID/PSK also updated.
The changes take effect as soon as you hit submit. Note that if you update the Wifi settings, the device will (re)associate with the given network.
TODO: Support WPS.

Normal operation

Once configured correctly, at powerup the display will display spinners. The first spinner on the right-most display lasts 5s - pressing the button in this time will cause the device to enter AP mode - useful for if you have to change Wifi credentials. Then the next spinner will appear, this displays whilst the device is attempting to associate with the given Wifi network.

Once associated, the device will fetch the NTP time from the configured server (default is Then the time will be displayed. The decimal between the hours and minutes will cycle each second. If synchronisation fails or is overdue, the rightmost decimal will be lit. This is a useful way to be assured if the clock is accurate.

Once running, you can press the button and the device IP will be displayed. You can point a browser to this IP and view the status and change the configuration.


Building this clock was a great way to get to know this awesome little SoC. The toolchain(s) are become quite mature now and it is an excellent alternative to AVR and PIC for when wireless networking is required. Whilst the hardware lacks some peripheral features I'd like, getting a full Wifi stack/SoC for not much more than a cup of coffee is incredible for so many projects.

Building a clock that "never needs setting" has been really quite fun.

Saturday, 3 August 2013

Native Android VPN to a Cisco Router

Getting IPSec VPN connectivity between two devices is always a painful experience, somewhat akin to a root canal. So I eventually roused up the courage and decided to try and get Android 4.x native VPN  to connect to a Cisco 877 at home. A few four-letter words and some blasphemy later, I finally had success!

The below example should help anyone else having problems getting this working.

The topology is drawn below. Its very simple, consisting of:
  • A Cisco 877 running 12.4(24)T1 (advanced IP services), attached to an ADSL line on the public side, with a LAN in RFC1918 space internally. A simple NAT configuration is configured between public and private.
  • An Android phone (in my case a galaxy S3 running 4.1.2 stock) with a 3G SIM. My carrier runs CG-NAT yet the setup still works.

Thursday, 12 July 2012

3G mobile sim cards in France

Having recently spent some time in France, one of the first things I did was attempt to acquire a 3G data SIM for my phone. Having a local sim makes it really easy to get around using various smartphone apps (eg maps, train timetables and general internet goodness).

After a fair bit of research, I realised that there are no official mobile carrier outlets in the airport. So there is no easy option. Buy you can get a limited access sim from a newsagent..

I got to Charles de Gaulle airport in Paris, and discovered that the SIMs can be purchased at the "Relay" newsagent stores there. I got mine from the outlet in the car rental hall. There is also an outlet near the arrivals gate at 2D, though I don't know if they also have SIM cards.

Friday, 27 April 2012

Bluetooth receiver hack

After installing an AUX input to the radio of my Mini a while back, I got sick of continuously having to plug my phone into the cable I wired up, and needed to go wireless.

Bluetooth A2DP receivers allow a phone to send music-quality audio to a receiver device, and this is the way I wanted to go. But for a while, they were just too expensive - around $50 for a receiver, so I put it off...

Until I discovered this cheap module on DealExtreme. At only $14, it's hard to go wrong!

Wednesday, 25 April 2012

Quick drill bit holder

My drill bits come without any holder, just the usual blister pack. So using some scrap aluminium channel and a block of pine, I made something to hold them in (click for larger):

Beats picking through a mixed pile of bits...

Thursday, 15 December 2011

New Tesla parts

Found a new pair of neon sign transformers off Ebay the other day.

Advertised as a 7.5kv/30mA transformer, details in the auction revealed that there were two 7.5-0-7.5 30mA transformers, for a total of 15kV@60mA.
Manufactured only 6 years ago, and for $100 shipped, this was quite a find!

A doubling of my Tesla coil's power is imminent...

Monday, 24 October 2011

My 40kV Uncle Fester impression

With my SIDAC driver and a light bulb, I thought I'd try my hand at doing an Uncle Fester impression. Remember him from the Addams Family?

I'm putting about 40kV out of the ignition coil, enough to spark around the globe. What might not be so obvious is the little ground wire hanging off the globe's base. I'm not actually conducting the electricity myself!

Needless to say, this was done with a full understanding of the dangers of HV and the particulars of this setup.

Plenty of ozone going around, I have to hold my breath for most of it. At some stage I'll actually get a better set of photos, but the vid will do for now. :)