Tim
Published © CERN-OHL

ESP8266 Desktop Clock (WiFi Synchronised!)

A simple clock based solely on the ESP8266. Time is always accurate as it is synchronized via WiFi!

IntermediateFull instructions provided2 hours5,256
ESP8266 Desktop Clock (WiFi Synchronised!)

Things used in this project

Hardware components

ESP8266 ESP-01
Espressif ESP8266 ESP-01
×1

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Custom parts and enclosures

Clock board eagle file

Eagle clock schematic

Code

ESP8266_Clock_12thMay.ino

Arduino
Upload this to the ESP
/************************************************************** /
  Shiftout code intended for esp8266-01
  Test code

  Now with 4 digit multiplexing using 2 registers
  Function for printing digits written successfully
****************************************************************/
#include <TimeLib.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>

//WiFi variables............................................................
const char ssid[] = "abc";        //your network SSID (name)
const char pass[] = "progsnob";       // your network password

// NTP Servers:
//static const char ntpServerName[] = "us.pool.ntp.org";
static const char ntpServerName[] = "time-b.timefreq.bldrdoc.gov";//static const char ntpServerName[] = "time-a.timefreq.bldrdoc.gov";
float timeZone = 5.5;  //+5.30 GMT

WiFiUDP Udp;
unsigned int localPort = 8888;  // local port to listen for UDP packets

time_t getNtpTime();
void sendNTPpacket(IPAddress &address);

//OTHER variables................................................................
//Pin connected to ST_CP of 74HC595
int latchPin = 1; //1 is TX of esp
//Pin connected to SH_CP of 74HC595
int clockPin = 2;
////Pin connected to DS of 74HC595
int dataPin = 0;

byte dotByte;

const byte numbers[11] = {
  B00111111,  //0
  B00000110,  //1
  B01011011,  //2
  B01001111,  //3
  B01100110,  //4
  B01101101,  //5
  B01111101,  //6
  B00000111,  //7
  B01111111,  //8
  B01101111,   //9
  B00000000   //blank
};

/*-------- NTP code ----------*/

const int NTP_PACKET_SIZE = 48; // NTP time is in the first 48 bytes of message
byte packetBuffer[NTP_PACKET_SIZE]; //buffer to hold incoming & outgoing packets

time_t getNtpTime() {
  IPAddress ntpServerIP; // NTP server's ip address

  while (Udp.parsePacket() > 0) ; // discard any previously received packets
  // get a random server from the pool
  WiFi.hostByName(ntpServerName, ntpServerIP);
  sendNTPpacket(ntpServerIP);
  uint32_t beginWait = millis();
  while (millis() - beginWait < 1600) {
    int size = Udp.parsePacket();
    if (size >= NTP_PACKET_SIZE) {
      Udp.read(packetBuffer, NTP_PACKET_SIZE);  // read packet into the buffer
      unsigned long secsSince1900;
      // convert four bytes starting at location 40 to a long integer
      secsSince1900 =  (unsigned long)packetBuffer[40] << 24;
      secsSince1900 |= (unsigned long)packetBuffer[41] << 16;
      secsSince1900 |= (unsigned long)packetBuffer[42] << 8;
      secsSince1900 |= (unsigned long)packetBuffer[43];
      return secsSince1900 - 2208988800UL + timeZone * SECS_PER_HOUR;
    }
  }
  return 0; // return 0 if unable to get the time
}

// send an NTP request to the time server at the given address
void sendNTPpacket(IPAddress &address) {
  // set all bytes in the buffer to 0
  memset(packetBuffer, 0, NTP_PACKET_SIZE);
  // Initialize values needed to form NTP request
  // (see URL above for details on the packets)
  packetBuffer[0] = 0b11100011;   // LI, Version, Mode
  packetBuffer[1] = 0;     // Stratum, or type of clock
  packetBuffer[2] = 6;     // Polling Interval
  packetBuffer[3] = 0xEC;  // Peer Clock Precision
  // 8 bytes of zero for Root Delay & Root Dispersion
  packetBuffer[12] = 49;
  packetBuffer[13] = 0x4E;
  packetBuffer[14] = 49;
  packetBuffer[15] = 52;
  // all NTP fields have been given values, now
  // you can send a packet requesting a timestamp:
  Udp.beginPacket(address, 123); //NTP requests are to port 123
  Udp.write(packetBuffer, NTP_PACKET_SIZE);
  Udp.endPacket();
}

void printDigits(int value) {

  if (second() % 2 == 0) {
    dotByte = B00010000;
  }
  else {
    dotByte = 0;
  }
  int th = value / 1000;
  int hun = value / 100;
  int tens = value / 10;
  byte thByte;

  if (th == 0) {
    thByte = 10;
  }
  else {
    thByte = th % 10;
  }

  digitalWrite(latchPin, LOW);  //THOUSANDS
  shiftOut(dataPin, clockPin, MSBFIRST, numbers[thByte]);
  shiftOut(dataPin, clockPin, MSBFIRST, B00000001 | dotByte); //to 2nd disp from left
  digitalWrite(latchPin, HIGH);
  delay(1);

  digitalWrite(latchPin, LOW);  //HUNDREDS
  shiftOut(dataPin, clockPin, MSBFIRST, numbers[hun % 10]);
  shiftOut(dataPin, clockPin, MSBFIRST, B00000010 | dotByte);
  digitalWrite(latchPin, HIGH);
  delay(1);

  digitalWrite(latchPin, LOW);  //TENS
  shiftOut(dataPin, clockPin, MSBFIRST, numbers[tens % 10]);
  shiftOut(dataPin, clockPin, MSBFIRST, B00000100 | dotByte);
  digitalWrite(latchPin, HIGH);
  delay(1);

  digitalWrite(latchPin, LOW);  //ONES
  shiftOut(dataPin, clockPin, MSBFIRST, numbers[(value % 10)]);
  shiftOut(dataPin, clockPin, MSBFIRST, B00001000 | dotByte);
  digitalWrite(latchPin, HIGH);
  delay(1);
}

void setup() {
  //set pins to output so you can control the shift register
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);

  Udp.begin(localPort);
  printDigits(1234);
  while (WiFi.status() != WL_CONNECTED) {
    WiFi.begin(ssid, pass);
    if (millis() > 20000) {
      break;
    }
    delay(10000);
  }
  setSyncProvider(getNtpTime);
  setSyncInterval(360);
  if (hour() == 0 && second() < 3) {
    setSyncProvider(getNtpTime);
  }

}

void loop() {
  printDigits(int(hour() * 100 + minute()));
}

Credits

Tim

Tim

3 projects • 7 followers
I like electronics, Arduino in particular. I suck at programming (but i still try my luck at it). Other interests include music, particularly prog.

Comments