IP Time Clock Part 1

12K1628

Intro: IP Time Clock Part 1

REQUIREMENT: WiFi Access with SSID and password if necessary

So this Lazy Old Geek (LOG) has a couple of clocks in my bedroom. One of them loses time when the power is lost. The other is semi smart and automatically adjusts for Daylight Savings Time. Unfortunately, it does this using the old standard so I have to re-set the time four times a year. Well, I’m LAZY so I decided to make my own Clock. At first I was thinking Atomic clock as I have three in my living room (I’m a GEEK, remember?).

Then I thought, I’ve been looking into these ESP8266s which are microcontrollers with WiFi access. Well, the Internet should know what time it is. Windows computers usually use the Internet to adjust their clocks.

So after surfing the Web, I found NTP(Network Time Protocol) which should work for me.

https://en.wikipedia.org/wiki/Network_Time_Protoco...

and to make it even easier the ESP8266 as Arduino already has an NTP example program

https://github.com/esp8266/arduino

STEP 1: Hardware Design

So I planned to use the ESP8266, specifically, the ESP-03 as the brains of the clock. I decided on using a four digit, seven segment display. To drive the display, I would use a MAX 7219 IC. See picture.

PROBLEM: So the ESP8266 specification for voltage is 3.0 to 3.6Vdc.

The MAX7219 is 4.0 to 5.5Vdc.

Maxim does make a 3.3V IC MAX6950/1 but it is an SMD package and about five times the cost.

SOLUTION: Well, I had some MAX7219s and tried them at 3.3Vdc and they worked.

Here is the way I look at it. When an IC manufacturer writes specifications for a part, they’re essentially guaranteeing that all parts will meet them. Now in order for them to meet them, the design specification has to be much wider than the specification.

WARNING: So for the purists and perfectionist out there, this design is using the part outside of specifications. If I was designing for a commercial product, I wouldn’t do this. But as a hobbyist, I would.

When buying 7 segment displays for the MAX7219, make sure they are common cathode. Some of them come with the two dots like most digital clocks. The ones I have use the 12 pin interface. They come in different sizes and colors.

Major costs

ESP-03 $2.50

MAX7219 $1.00

4 digit 7 segment display $0.50

L4931cz33 voltage regulator $1.50

Prices on ebay/aliexpress.com

I have a lot of 5V wall wart power supplies. I just spliced in a JST2.0 connector.

So the total cost is about $7 USD.

Attached is the schematic.

COMMENTS:

R4 is the RSET resistor for the MAX7219. It helps limit the current for the display. Minimum is about 10K but I wanted a dimmer display for my bedroom so I set it to 27K.

I have the software set for 12 hour display. LED1 is on when it is AM.

Flash and Reset are two pin headers that can be used to set the ESP-03 into program mode to load a different sketch.

JST2.0 connector is the 5Vdc supply connector.

The USB-BUB connector is my standard USB-BUB. It requires a USB serial adapter such as a CP2102 to reload sketch but requires nothing when just running.

I had some problems when I made this PCB. I think the problem is that I just switched to a generic toner for my laser printer and it did not adhere like the original. I couldn’t get the silk screen to transfer at all as you can see in the picture.

But with a lot of work and some extra jumpers, I did get it to work.

NOTE: The display is resting on the MAX7291. I trimmed off the tabs so it fit a little better.

Eagle Cadsoft files attached.

STEP 2: Daylight Savings Time

So this STEP is pretty technical and can be skipped if you’re not interested:

Okay, so there is an Arduino library written to handle Daylight Savings Time changes:

https://github.com/JChristensen/Timezone

However, it wouldn’t compile when using the ESP8266 library. (Something to do with EEPROM.) So I wrote my own code.

Here’s the current US rules for Daylight Savings Time:

https://en.wikipedia.org/wiki/Daylight_saving_time_in_the_United_States

“Currently, daylight saving time starts on the second Sunday in March and ends on the first Sunday in November, with the time changes taking place at 2:00 a.m. local time. With a mnemonic word play referring to seasons, clocks "spring forward and fall back"—that is, in spring (technically late winter) the clocks are moved forward from 2:00 a.m. to 3:00 a.m., and in fall they are moved back from 2:00 am to 1:00 am.”

Well, if you done some programming, this gets pretty complicated. Here’s what I did.

The Arduino time library, like many computers uses something called Unix Time:

https://en.wikipedia.org/wiki/Unix_time

Unix time (also known as POSIX time or Epoch time) is a system for describing instants in time, defined as the number of seconds that have elapsed since 00:00:00 Coordinated Universal Time (UTC), Thursday, 1 January 1970

Now, NTP and GPS use UTC. UTC is not a timezone but it is based on the time in England so you have to convert it to local time and adjust for Daylight Savings time.

So I created two variables called SpringChange and FallChange in Unix time format.

Then using NTP, I found the current year.

From the year, I then calculated the two unix times for the two variables. This is tricky as the second Sunday in March and the first Sunday in November are not the same day from year to year. But one thing the Time library can tell is given a Unix time, it can determine the weekday. So using the month, the day and the weekday, I was able to figure out the Change days for the year.

So when the program gets a new UTC time, if it is greater than or equal to (>=) SpringChange and less than (<) FallChange than it is Daylight Saving Time (DST). Otherwise it’s not DST. Now to get current local time, I adjust for (in my case) Mountain Time and Daylight savings time.

STEP 3: SKETCH

REQUIREMENT: WiFi Access with SSID and password if necessary

NOTE: The sketch adjusts for Daylight Savings according to US rules as of October 2015. Places that do not use Daylight Savings and other countries probably won't work.

So loading the sketch onto the ESP-03 is fairly complicated.

My ESP8266 Instructable may help:

https://www.instructables.com/id/ESP8266-as-Arduino...

The ESP8266 library I use is at:

https://github.com/esp8266/arduino

The library for the MAX7219 is at:

https://github.com/wayoda/LedControl

The latest(I think) Time library is this one:

http://www.pjrc.com/teensy/td_libs_Time.html

First you have to edit the Sketch for your situation.

You need to know the SSID name for your local access point or hotspot and Password if used. Open the sketch for editing and put them into these two lines:

char ssid[] = "yourSSID"; // network SSID (name)

char pass[] = "yourPassword"; // network password

Under // NTP Servers, I listed several. The one I’m using I think is Salt Lake City. You can uncomment any one that you want.

If your timeZone is not Mountain, change the following line:

const int timeZone = -7; // Mountain Standard Time (USA)

You want your local standard time offset

Eastern -5

Central -6

Mountain -7

Pacific -8

Hawaii -10

Under setup(){ there is a line of code:

lc.setIntensity(0,1); // Set the brightness to a low value

The second parameter (1) can be set anywhere from 0 to 15. This (along with the Rset resistor determines the brightness of the display). Since I use mine at night, I have it set pretty low.

NOTE: If you want the display to show 24hr clock instead of 12, under

void digitalClockDisplay(), change the following two lines:

lc.setDigit(0,0,int(hourFormat12()/10),false);

lc.setDigit(0,1,(hourFormat12() % 10),true);

to:

lc.setDigit(0,0,int(hour()/10),false);

lc.setDigit(0,1,(hour() % 10),true);

NOTE: This code is designed to work with a four digit or a six digit display. One little problem is with a four digit display, there is a dot after the minutes. If you want to turn it off, change the following line:

lc.setDigit(0,3,(minute() % 10),true);

to:

lc.setDigit(0,3,(minute() % 10),false);

The following lines:

lc.setDigit(0,4,int(second()/10),false);

lc.setDigit(0,5,(second() % 10),false);

can be deleted as they’re only used by a six digit display.

To load the sketch, put the ESP-03 in programming mode. Start Arduino environment. Select the correct USB port. Under Board, select Generic ESP8266 module. Upload the sketch.

So this Clock seems to work pretty good for me.

8 Comments

I guess you're right about mine being fakes. But I guess they seem to work for my needs.

Thanks for the info.

LOG

Yeah most of the ones on the market are fakes but they all work so far. The AS1107 are just a tad cheaper.

Another project I wanted to do is a NTP server for my network but going to be much harder I think. I wanted to get time from GPS and serve it up. Would also double as a internet status checker.

Where are you finding cheap AS1107s? The cheapest I found was like $9.71 a piece?

I would think it's pretty easy to get time from GPS but I don't know about serving it up. I looked at some of the NTP protocol and don't understand it all.

LOG

Depending on stock mostly Digikey but sometimes directly from AMS. I do usually buy 50-100 but even at 1qty they are a bit cheaper than MAXIM.

http://www.digikey.com/product-detail/en/AS1107WL-...

http://ams.com/eng/Products/Lighting-Management/LE...

http://www.mouser.com/Search/Refine.aspx?Keyword=a...

They are SMD parts but large enough that are easy to solder. AMS does samples too so that is an option.

I haven't taken a look to see what it would take yet. Hmm maybe I can just serve up the time to a Linux VM and let it be the NTP service. Wouldn't be as streamlined but would be easier. Except couldn't be able to get exactly down to the second hence what NTP takes care of.

interesting. I had not considered using the ESP8266 simply for a clock. Where did u get yr 4 digit display? it is unbelievably cheap.
Currently I use a simple piece of code for all my arduino's that keep time from an RTC to set and reset DaylightSavingTime, but I may try yr approach as well some day if I have time (too many projects)

https://arduinodiy.wordpress.com/2015/10/13/the-ar...

I bought my 4 Digit displays from Aliexpress:
http://www.aliexpress.com/item/Free-shipping-50Pcs...

and smaller ones are cheaper:

http://www.aliexpress.com/item/10pcs-lot-0-4-Four-...

I have used some RTCs but they didn't seem to keep time very well after a few months. I guess it might be the cheap crstals I was using?

I never heard of that Sakamoto’s Algorithm. Pretty nice.

One question on your algorithm. Do you need to include d <=31 ? As it seems to me, d can never be greater than 31 anyway.

LOG

aliexpress, I knew it. My favorite store :-)
The DS3231 is fairly accurate. The DS1307 is not that accurate unless you make sure you have a good crystal and keep that close to the chip and ground it very well. Something that can vary per module as self built is hardly worth it considering the price. Though the DS3231 has my preference I just bought a bunch of DS1307's coz they were dirt cheap.

Have to admit that until a few days ago, I had a very complicated way of changing back and forth to DST till I thought that was crazy and took a deeper look. That s also how I came up with the Sakamoto Algorithm. Mind you though it only works for Gregorian calendars :-)
As far as my own algorihm goes, yes you are right. You do not need a check for <=31, but I am an anal retentive controlfreak so I put it in anyway :-) But yes, d will never be greater than 31 on the RTC's that I know