My ESP8266 countdown timer for my trip to India
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include "HCMAX7219.h"
#include "SPI.h"

#define SECS_PER_MIN  (60UL)
#define SECS_PER_HOUR (3600UL)
#define SECS_PER_DAY  (SECS_PER_HOUR * 24L)
#define numberOfSeconds(_time_) (_time_ % SECS_PER_MIN)
#define numberOfMinutes(_time_) ((_time_ / SECS_PER_MIN) % SECS_PER_MIN)
#define numberOfHours(_time_) (( _time_% SECS_PER_DAY) / SECS_PER_HOUR)
#define elapsedDays(_time_) ( _time_ / SECS_PER_DAY)
#define NUMBEROFDRIVERS 1
//We always have to include the library
#include "LedControl.h"

LedControl lc = LedControl(12, 14, 15, 1); // gpio pin numbers, 1 driver

/* we always wait a bit between updates of the display */
unsigned long delaytime = 250;
char ssid[] = "**NETWORK NAME**"; // your network SSID (name) 
char pass[] = "**NETWORK PASSWORD**"; // your network password 
int targetTime = 1541005200;

unsigned long epoch = 0; int count = 10; unsigned long secsSince1900 = 0; unsigned int localPort = 2390; // local port to listen for UDP packets const char* ntpServerName = "time.nist.gov"; const unsigned long seventyYears = 2208988800UL; const int NTP_PACKET_SIZE = 48; // NTP time stamp is in the first 48 bytes of the message byte packetBuffer[ NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets String data; bool got = false; IPAddress timeServerIP; // time.nist.gov NTP server address // A UDP instance to let us send and receive packets over UDP WiFiUDP udp; void setup() { /* The MAX72XX is in power-saving mode on startup, we have to do a wakeup call */ lc.shutdown(0, false); /* Set the brightness to a medium values */ lc.setIntensity(0, 8); /* and clear the display */ lc.clearDisplay(0); lc.clearDisplay(0); String data = " -- "; lc.setChar(0, 0, (byte)data[7], false); lc.setChar(0, 1, (byte)data[6], false); lc.setChar(0, 2, (byte)data[5], false); lc.setChar(0, 3, (byte)data[4], false); lc.setChar(0, 4, (byte)data[3], false); lc.setChar(0, 5, (byte)data[2], false); lc.setChar(0, 6, (byte)data[1], false); lc.setChar(0, 7, (byte)data[0], false); Serial.begin(115200); Serial.println(); Serial.println(); // We start by connecting to a WiFi network Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, pass); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); Serial.println("Starting UDP"); udp.begin(localPort); Serial.print("Local port: "); Serial.println(udp.localPort()); } void loop() { if (!got) { WiFi.hostByName(ntpServerName, timeServerIP); sendNTPpacket(timeServerIP); // send an NTP packet to a time server delay(1000); int cb = udp.parsePacket(); if (!cb) { data = "- - - - "; updateScreen(); delay(1000); data = " - - - -"; updateScreen(); delay(1000); data = "- - - - "; updateScreen(); delay(1000); data = " - - - -"; updateScreen(); delay(1000); } else { // We've received a packet, read the data from it udp.read(packetBuffer, NTP_PACKET_SIZE); // read the packet into the buffer //the timestamp starts at byte 40 of the received packet and is four bytes, // or two words, long. First, esxtract the two words: unsigned long highWord = word(packetBuffer[40], packetBuffer[41]); unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]); // combine the four bytes (two words) into a long integer // this is NTP time (seconds since Jan 1 1900): secsSince1900 = highWord << 16 | lowWord; got=true; } } else if (secsSince1900 > 0) { updateScreen(); delay(1000); secsSince1900 = secsSince1900 + 1; } } void updateScreen() { epoch = secsSince1900 - seventyYears; Serial.println(epoch); //String data = time(1497754800 - epoch); data = String(targetTime - epoch); while (data.length() < 8) { data = "0" + data; } Serial.println(data); lc.clearDisplay(0); lc.setChar(0, 0, (byte)data[7], false); lc.setChar(0, 1, (byte)data[6], false); lc.setChar(0, 2, (byte)data[5], false); lc.setChar(0, 3, (byte)data[4], false); lc.setChar(0, 4, (byte)data[3], false); lc.setChar(0, 5, (byte)data[2], false); lc.setChar(0, 6, (byte)data[1], false); lc.setChar(0, 7, (byte)data[0], false); } String time(long val) { if (val < 0) { val = 0; } int days = elapsedDays(val); int hours = numberOfHours(val); int minutes = numberOfMinutes(val); int seconds = numberOfSeconds(val); String retval; // digital clock display of current time retval = printDigits(days) + printDigits(hours) + printDigits(minutes) + printDigits(seconds); return retval; } String printDigits(byte digits) { String ret = ""; if (digits < 10) ret = '0'; ret = ret + String(digits); return ret; } // send an NTP request to the time server at the given address unsigned long sendNTPpacket(IPAddress& address) { Serial.println("sending NTP packet..."); // 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(); }
Show CommentsClose Comments

Leave a comment