I've been trying to modify a working sketch provided with the TFT_HX8357 libraries. Basic setup was working, using an NTP demo on the WeMOS D1 (Retired) board. It's working fine, sending a UTC string and a local time string out its serial port every second. I had used the MegaMultiSerial demo sketch to read the data from the D1 and send it out the serial port connected to the computer. Worked great. When I started to modify the sketch to incorporate the TFT display, that's when I started having problems.
I'm trying to get a two-to-four line display showing the date and time in both UTC and local time. (Ham radio operator, so UTC is handy.) Eventually, once I get the basic functionality down, I can add alarms, timers, etc. But, I can't seem to get anything legible to display on the TFT, nor determine if I've got a line that is UTC or local time.
I'd love some help on this!
WeMOS D1 code (comments removed for space):
/*
Udp NTP Client
Get the time from a Network Time Protocol (NTP) time server
created 4 Sep 2010 by Michael Margolis
modified 9 Apr 2012 by Tom Igoe
updated for the ESP8266 12 Apr 2015 by Ivan Grokhotkov
This code is in the public domain.
*/
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <Time.h>
#include <Timezone.h>
#include <SoftwareSerial.h>
SoftwareSerial ESPSerial(0,1);
TimeChangeRule myDST = {"EDT", Second, Sun, Mar, 2, -240}; //Daylight time = UTC - 4 hours
TimeChangeRule mySTD = {"EST", First, Sun, Nov, 2, -300}; //Standard time = UTC - 5 hours
Timezone myTZ(myDST, mySTD);
TimeChangeRule *tcr; //pointer to the time change rule, use to get TZ abbrev
time_t utc, local;
char ssid[] = "*************"; // your network SSID (name)
char pass[] = "********"; // your network password
unsigned int localPort = 2390; // local port to listen for UDP packets
IPAddress timeServerIP; // time.nist.gov NTP server address
const char* ntpServerName = "time.nist.gov";
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
WiFiUDP udp;
void setup()
{
Serial.begin(9600);
Serial.println();
Serial.println();
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()
{
WiFi.hostByName(ntpServerName, timeServerIP);
sendNTPpacket(timeServerIP); // send an NTP packet to a time server
delay(1000);
int cb = udp.parsePacket();
if (!cb) {
}
else {
udp.read(packetBuffer, NTP_PACKET_SIZE); // read the packet into the buffer
unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
unsigned long secsSince1900 = highWord << 16 | lowWord;
const unsigned long seventyYears = 2208988800UL;
unsigned long epoch = secsSince1900 - seventyYears;
time_t utcCalc = epoch;
Serial.println();
utc = epoch;
printTime(utc, "UTC");
printDate(utc, "UTC");
local = myTZ.toLocal(utc, &tcr);
printTime(local, tcr -> abbrev);
printDate(local, tcr -> abbrev);
delay(100);
}
}
unsigned long sendNTPpacket(IPAddress& address)
{
memset(packetBuffer, 0, NTP_PACKET_SIZE);
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
packetBuffer[12] = 49;
packetBuffer[13] = 0x4E;
packetBuffer[14] = 49;
packetBuffer[15] = 52;
udp.beginPacket(address, 123); //NTP requests are to port 123
udp.write(packetBuffer, NTP_PACKET_SIZE);
udp.endPacket();
}
void printTime(time_t t, char *tz)
{
sPrintI00(hour(t));
sPrintDigits(minute(t));
sPrintDigits(second(t));
Serial.print(' ');
Serial.print(tz);
Serial.println();
}
void printDate(time_t t, char *tz)
{
Serial.print(dayShortStr(weekday(t)));
Serial.print(' ');
sPrintI00(day(t));
Serial.print(' ');
Serial.print(monthShortStr(month(t)));
Serial.print(' ');
Serial.print(year(t));
Serial.print(' ');
Serial.print(tz);
Serial.println();
}
void sPrintI00(int val)
{
if (val < 10) Serial.print('0');
Serial.print(val, DEC);
return;
}
void ESPsPrintI00(int val)
{
if (val < 10) ESPSerial.print('0');
ESPSerial.print(val, DEC);
return;
}
void sPrintDigits(int val)
{
Serial.print(':');
if(val < 10) Serial.print('0');
Serial.print(val, DEC);
}
void ESPsPrintDigits(int val)
{
ESPSerial.print(':');
if(val < 10) ESPSerial.print('0');
ESPSerial.print(val, DEC);
}
Mega2560/TFT code:
/*
A digital clock using a TFT LCD screen to show the time.
Uses NTP data from connected WeMOS D1
Based on clock sketch by Gilchrist 6/2/2014 1.0 and the example with the TFT_HX8357 libs.
*/
#include <TFT_HX8357.h>
#include <User_Setup.h>
#define TFT_GREEN 0x7E0
TFT_HX8357 tft = TFT_HX8357(); // Invoke custom library
unsigned int colour = 0;
void setup() {
tft.init();
tft.setRotation(1);
tft.fillScreen(TFT_BLACK);
tft.setTextSize(1);
tft.setTextColor(TFT_GREEN, TFT_BLACK);
// targetTime = millis() + 1000;
Serial.begin(9600);
Serial1.begin(9600);
}
void loop() {
int timeIn[22];
char *timeUTC, *timeLocal;
if (Serial1.available() >= 22) {
for (int i=0; i<=21; i++) {
timeIn[i] = Serial.read();
Serial.write(timeIn[i]);
}
timeIn[22] = '\0';
}
// Update digital time
int xpos = 90;
int ypos = 85; // Top left corner of clock text, about half way down
int ysecs = ypos + 24;
tft.setTextColor(TFT_GREEN, TFT_BLACK);
tft.setTextSize(2);
tft.drawString(timeUTC, xpos, ypos, 1);
tft.drawString(timeLocal, xpos, ypos+1, 2);
}