YMMV

* your mileage may vary

Sunday, 13 November 2016

ESP8266 with Sainsmart 7-Segment Serial Display

Combined ESP8266 with Sainsmart 7-Segment Serial Display.
https://learn.sparkfun.com/tutorials/using-the-serial-7-segment-display


Cut and past of some code - it works ...but could be cleaned a little.  Added code for DST (Europe).

Change SSID & Password

/*
 * Time_NTP.pde
 * Example showing time sync to NTP time source
 *
 * This sketch uses the ESP8266WiFi library
 */

#include <TimeLib.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <SoftwareSerial.h>
time_t getNtpTime();
void digitalClockDisplay();
void printDigits(int digits);
void sendNTPpacket(IPAddress &address);
void clearDisplay();
void fetchNtpTime();
void setDecimals(byte decimals);
void setBrightness(byte value);

const char ssid[] = "BLANK";  //  your network SSID (name)
const char pass[] = "BLANK";       // your network password
const int softwareTx = 1;
const int softwareRx = 3;
 int currentHour = -1;

// NTP Servers:
IPAddress timeServer(132, 163, 4, 101); // time-a.timefreq.bldrdoc.gov
// IPAddress timeServer(132, 163, 4, 102); // time-b.timefreq.bldrdoc.gov
// IPAddress timeServer(132, 163, 4, 103); // time-c.timefreq.bldrdoc.gov


const int timeZone = 1;     // Central European Time
//const int timeZone = -5;  // Eastern Standard Time (USA)
//const int timeZone = -4;  // Eastern Daylight Time (USA)
//const int timeZone = -8;  // Pacific Standard Time (USA)
//const int timeZone = -7;  // Pacific Daylight Time (USA)


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

SoftwareSerial s7s(softwareRx, softwareTx);

void setup()
{

 s7s.begin(9600);
   setBrightness(127);  // Medium brightness
    clearDisplay();
  WiFi.begin(ssid, pass);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    s7s.print("linc");
  }
  clearDisplay();

  Udp.begin(localPort);

}

time_t prevDisplay = 0; // when the digital clock was displayed

void loop()
{
if (currentHour != hour() )
{ fetchNtpTime();
currentHour = hour();
}

  delay(1000);
 setDecimals(0b00000);  // Turn on all decimals, colon, apo
 delay(1000);
setDecimals(0b10000);  // Turn on all decimals, colon, apo

  if (timeStatus() != timeNotSet) {
    if (now() != prevDisplay) { //update the display only if time has changed
      prevDisplay = now();
      digitalClockDisplay();
    }
  }
}

void digitalClockDisplay(){
// int timeAdjust=0;

if (hour() < 10)
  s7s.print(" ");
s7s.print(hour());
if (minute() < 10)
  s7s.print(0);
s7s.print(minute());
}



void printDigits(int digits){
  // utility for digital clock display: prints preceding colon and leading 0
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}

/*-------- 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()
{

  while (Udp.parsePacket() > 0) ; // discard any previously received packets
  Serial.println("Transmit NTP Request");
  sendNTPpacket(timeServer);
  uint32_t beginWait = millis();
  while (millis() - beginWait < 2000) {
    int size = Udp.parsePacket();
    if (size >= NTP_PACKET_SIZE) {
      Serial.println("Receive NTP Response");
      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+0) * SECS_PER_HOUR;
    }
  }
  s7s.print("Err");
  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();
}
// Send the clear display command (0x76)
//  This will clear the display and reset the cursor
void clearDisplay()
{
  s7s.write(0x76);  // Clear display command
}
// Turn on any, none, or all of the decimals.
//  The six lowest bits in the decimals parameter sets a decimal
//  (or colon, or apostrophe) on or off. A 1 indicates on, 0 off.
//  [MSB] (X)(X)(Apos)(Colon)(Digit 4)(Digit 3)(Digit2)(Digit1)
void setDecimals(byte decimals)
{
  s7s.write(0x77);
  s7s.write(decimals);
}
void setBrightness(byte value)
{
  s7s.write(0x7A);  // Set brightness command byte
  s7s.write(value);  // brightness data byte
}

void fetchNtpTime()
{
int timeAdjust = 0;
int returnedTime = 0;
while (returnedTime == 0) {
  clearDisplay();
  s7s.print("sInc");
   delay(2000);
//setTime(getNtpTime());
  returnedTime = getNtpTime();
  setTime(returnedTime);
}
if (month() < 3 || month() > 10)  timeAdjust = 0;
if (month() > 3 && month() < 10)  timeAdjust = 1;
int previousSunday = day() - weekday();
if (month() == 3 && weekday()==1 && day() >= 25) timeAdjust = 1;
if (month() == 10 && weekday()==1 && day() < 25) timeAdjust = 1;

timeAdjust = timeAdjust + hour();

setTime(timeAdjust, minute(), second(), day(), month(), year());
clearDisplay();

}

No comments:

Post a Comment