HI all, I hope someone can help me out here, I have made a simple IOT clock based on an article at IoT Based Analog/Digital Clock using OLED and ESP32 ESP8266
It is slightly modified to display time and day of the week and has been working fine for many months. However since the clocks go back 1 hour in the UK I noticed the time shown has not changed. So I thought, simple I'll just change the ofset back to "0" and all should be ok. However after doing this and uploading the code the date is now 1900 and day and time are miles out. I first thought my new sky hub was blocking access to the time server but this is not the case. What could be wrong here?` Code below.
}
// https://microcontrollerslab.com/iot-analog-digital-clock-oled-esp32-esp8266/
#ifdef ESP32
#include <WiFi.h>
#else
#include <ESP8266WiFi.h>
#endif
#include <time.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
Adafruit_SSD1306 display = Adafruit_SSD1306(128, 64, &Wire, -1);
const char* ssid = "SKYxxxxx";
const char* password = "xxxxxZWtNxxxxx";
int GMTOffset = 0; //Replace with your GMT Offset in seconds
int daylightOffset = 3600; // Replace with your daylight savings offset in seconds
int wday = 1;
void setup() {
Serial.begin(115200);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;)
;
}
delay(2000);
display.clearDisplay();
display.setTextSize(1);
display.setCursor(0, 0);
display.setTextColor(WHITE);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(2000);
Serial.println("Connecting...");
}
Serial.println("Connected to Wi-Fi!");
configTime(GMTOffset, daylightOffset, "pool.ntp.org", "time.nist.gov");
}
void loop() {
time_t rawtime = time(nullptr);
struct tm* timeinfo = localtime(&rawtime);
Serial.print("Time: ");
Serial.print(timeinfo->tm_hour);
Serial.print(":");
Serial.print(timeinfo->tm_min);
Serial.print(":");
Serial.println(timeinfo->tm_sec);
Serial.print(":");
Serial.println(timeinfo->tm_wday); // Day of week (0 = Sunday)
wday = (timeinfo->tm_wday);
// This section displays the updated time on OLED
display.clearDisplay();
display.setTextSize(4);
display.setTextColor(WHITE);
display.setCursor(4, 10);
if (timeinfo->tm_hour < 10)
display.setCursor(16, 10); //Recentre the time if hours are less than 10
display.print(timeinfo->tm_hour);
display.print(":");
if (timeinfo->tm_min < 10)
display.print("0");
display.print(timeinfo->tm_min);
// This section displays seconds below the time
display.setTextSize(2);
//display.setCursor(39, 50); // (Col,Row)
//display.print(":");
//if (timeinfo->tm_sec < 10)
// display.print("0");
//display.print(timeinfo->tm_sec);
//display.print(":");
//This section displays day of week instead of seconds
display.setCursor(5, 48);
if (wday == 0)
display.print(" Sunday");
if (wday == 1)
display.print(" Monday");
if (wday == 2)
display.print(" Tuesday");
if (wday == 3)
display.print("Wednesday");
if (wday == 4)
display.print(" Thursday");
if (wday == 5)
display.print(" Friday");
if (wday == 6)
display.print(" Saturday");
display.display();
delay(1000);
Ok, so I understand that the offset should happen automatically on the esp as per your reference, and that's what I was hoping for, but evenso why is it not displaying the correct time?
It is indeed interesting that it is so radically wrong.
The ESP8266 time handling code has had some breaking changes, possibly since the last version you compiled it under, for example, I think now time_t is 8 bytes. Switch on compiler warnings and show the compilation output.
Which time zone are you in ?
@mancavebob my ESP8266 based clocks update automatically using the code linked by (and written by) @noiasca above. I can give you the configuration for UK GMT/BST if you can't figure it out.
Ok, guys, thank you all for your input. I realise I'm a little out of my depth here so I've gone back to basics to try to understand what's happening. I've opened the basic NTPClient from the custom libraries example and inserted my WiFi credentials. Uploaded the code without any other modifications and it's reporting via the serial monitor Time as 0:11:22 then incrementing the seconds by 2 at each reading. The local time is 13:24. Surely this can't be right?
#include <NTPClient.h>
// change next line to use with another board/shield
#include <ESP8266WiFi.h>
//#include <WiFi.h> // for WiFi shield
//#include <WiFi101.h> // for WiFi 101 shield or MKR1000
#include <WiFiUdp.h>
const char *ssid = "SKYxxxxx";
const char *password = "xxxxxZWtNxxxxx";
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);
void setup(){
Serial.begin(115200);
WiFi.begin(ssid, password);
while ( WiFi.status() != WL_CONNECTED ) {
delay ( 500 );
Serial.print ( "." );
}
timeClient.begin();
}
void loop() {
timeClient.update();
Serial.println(timeClient.getFormattedTime());
delay(1000);
}
Ok Paul, thanks again but I'm now even more lost, surely in this basic for it should work. I can't see why this produces an output that is so far out, it's like the server is giving corrupted data but I can't see that being the case.
If it shows a time that's shortly after midnight after the Arduino starts up (or after upload) that just means it hasn't sync'd with the time server yet. Usually it takes only a few seconds but I have seen it take 30~60s.
If, after several minutes, it still doesn't have the correct time, I wonder if your Arduino has internet access. That's different from connecting to WiFi, which is just your local network, and doesn't necessarily mean you have internet connectivity.
Try running the "ping" example sketch. That will check connectivity with your router, and then to some internet servers.
Hi Paul, thank you for this, that makes a lot of sense. I'd not previously realised that the ESP8266 used an internal part of the core to process time after being synced with the server. So it does look like there is no data being received. Forgive my naivety but where do I find the "ping" example?
Eureka! finally figured it out. Nothing to do with the code or my programming at all. The "new" sky hub defaults to 5G wifi which the ESP8256 doesn't like. After setting a 2G option all is back to normal and my clock is working properly.
Thanks to everyone who offered help, all the suggestions eventually led me to the right place. Have a good day everyone!
Strange that the ESP was able to connect to it. You are correct, ESP8266 is 2.4GHz only, so there must be a 2.4GHz WiFi network available, or it would not be able to connect. Perhaps the 2.4GHz WiFi network is available but the hub isn't allowing internet access to it's clients? When you enabled the 2.4GHz maybe all that changes is that it allows internet access to 2.4GHz clients as well as to the 5GHz clients. Odd.
Did you try my suggested change to get the auto clock change to work?
Hi Paul, oddly the esp8266 did connect to the 5G network but took about 2 minutes to do so. I finally connected it to 2.4G on a wifi booster I have to enable internet connections in a workshop at the end of the garden. Connection is almost instant now and as I said all is working well. I've not yet tried to get the auto clock change implemented, not sure I fully understand what to do here.