Funktioniert soweit ganz gut, hab nur noch 1 Problem.
Wenn das Wlan nicht an ist sucht es sich einen Ast ab und die Zeit wird auch nicht angezeigt.
Es soll aber die Zeit vom DS3231 Anzeigen auch wenn mal kein Wlan vorhanden ist.
Hintergrund ist der das ich mir diese Uhr ins Ferienhaus mitnehmen will aber es dort kein Wlan gibt.
Zum Syncen alle paar Monate kann ich dann mein Handy benutzen.
Gibt es da eine möglichkeit?
Hier der Jetzige Code:
#include <SPI.h>
#include <U8g2lib.h>
#include <TimeLib.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <Wire.h> // must be included here so that Arduino library object file references work
#include <RtcDS3231.h>
RtcDS3231<TwoWire> Rtc(Wire);
#define countof(a) (sizeof(a) / sizeof(a[0]))
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE, /* clock=*/ 5, /* data=*/ 4); //
//U8G2_SSD1306_128X64_NONAME_1_HW_I2C u8g2(U8G2_R0);
const char ssid[] = "meinWlan"; // your network SSID (name)
const char pass[] = "meinPasswort"; // your network password
// NTP Servers:
//static const char ntpServerName[] = "ptbtime1.ptb.de";
static const char ntpServerName[] = "de.pool.ntp.org";
//IPAddress timeServer(192, 53, 103, 108); //ptbtime1.ptb.de //(192, 43, 244, 18);
const int timeZone = 2; // Central European Time
//const int timeZone = -5; // Eastern Standard Time (USA)
WiFiUDP Udp;
unsigned int localPort = 8888; // local port to listen for UDP packets
time_t getNtpTime();
void digitalClockDisplay();
void printDigits(int digits);
void sendNTPpacket(IPAddress &address);
void setup()
{
u8g2.begin();
Wire.begin();
Serial.begin(115200);
//while (!Serial) ; // Needed for Leonardo only
delay(250);
Rtc.Begin();
Serial.println("TimeNTP Example");
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED)
{
delay(1000);
Serial.print(".");
}
Serial.println(" ");
Serial.print("IP number assigned by DHCP is ");
Serial.println(WiFi.localIP());
Serial.println("Starting UDP");
Udp.begin(localPort);
Serial.print("Local port: ");
Serial.println(Udp.localPort());
Serial.println("waiting for sync");
setSyncProvider(getNtpTime);
//setSyncInterval(300);
if(timeStatus() != timeNotSet){
digitalClockDisplay();
Serial.println("here is another way to set rtc");
time_t t = now();
char d_mon_yr[12];
snprintf_P(d_mon_yr, countof(d_mon_yr), PSTR("%s %02u %04u"), monthShortStr(month(t)), day(t), year(t));
Serial.println(d_mon_yr);
char tim_set[9];
snprintf_P(tim_set, countof(tim_set), PSTR("%02u:%02u:%02u"), hour(t), minute(t), second(t));
Serial.println(tim_set);
Serial.println("Now its time to set up rtc");
RtcDateTime compiled = RtcDateTime(d_mon_yr, tim_set);
printDateTime(compiled);
Serial.println("");
if (!Rtc.IsDateTimeValid())
{
// Common Cuases:
// 1) first time you ran and the device wasn't running yet
// 2) the battery on the device is low or even missing
Serial.println("RTC lost confidence in the DateTime!");
// following line sets the RTC to the date & time this sketch was compiled
// it will also reset the valid flag internally unless the Rtc device is
// having an issue
}
Rtc.SetDateTime(compiled);
RtcDateTime now = Rtc.GetDateTime();
if (now < compiled)
{
Serial.println("RTC is older than compile time! (Updating DateTime)");
Rtc.SetDateTime(compiled);
}
else if (now > compiled)
{
Serial.println("RTC is newer than compile time. (this is expected)");
}
else if (now == compiled)
{
Serial.println("RTC is the same as compile time! (not expected but all is fine)");
}
// never assume the Rtc was last configured by you, so
// just clear them to your needed state
Rtc.Enable32kHzPin(false);
Rtc.SetSquareWavePin(DS3231SquareWavePin_ModeNone);
}
}
time_t prevDisplay = 0; // when the digital clock was displayed
void loop()
{
if (!Rtc.IsDateTimeValid())
{
// Common Cuases:
// 1) the battery on the device is low or even missing and the power line was disconnected
Serial.println("RTC lost confidence in the DateTime!");
}
Serial.println("ready to get date time");
RtcDateTime now = Rtc.GetDateTime();
printDateTime(now);
Serial.println();
//delay(10000);
// ######################################## OLED u8g2 ############################################
u8g2.clearBuffer(); // clear the internal memory
u8g2.setFont(u8g2_font_crox2hb_tr); // choose a suitable font
u8g2.setCursor(24, 10);
if (now.Day() < 10) {
u8g2.print("0");
}
u8g2.print(now.Day(), DEC);
u8g2.print('.');
if (now.Month() < 10) {
u8g2.print("0");
}
u8g2.print(now.Month(), DEC);
u8g2.print('.');
u8g2.print(now.Year(), DEC);
u8g2.setFont(u8g2_font_helvR24_tn);
u8g2.setCursor(2, 43);
if (now.Hour() < 10) {
u8g2.print("0");
}
u8g2.print(now.Hour(), DEC);
u8g2.print(':');
if (now.Minute() < 10) {
u8g2.print("0");
}
u8g2.print(now.Minute(), DEC);
u8g2.print(':');
if (now.Second() < 10) {
u8g2.print("0");
}
u8g2.print(now.Second(), DEC);
u8g2.sendBuffer(); // transfer internal memory to the display
delay (1000);
}
// ######################################## OLED u8g2 ############################################
void digitalClockDisplay()
{
// digital clock display of the time
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.print(" ");
Serial.print(day());
Serial.print(".");
Serial.print(month());
Serial.print(".");
Serial.print(year());
Serial.println();
}
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()
{
IPAddress ntpServerIP; // NTP server's ip address
while (Udp.parsePacket() > 0) ; // discard any previously received packets
Serial.println("Transmit NTP Request");
// get a random server from the pool
WiFi.hostByName(ntpServerName, ntpServerIP);
Serial.print(ntpServerName);
Serial.print(": ");
Serial.println(ntpServerIP);
sendNTPpacket(ntpServerIP);
uint32_t beginWait = millis();
while (millis() - beginWait < 1500) {
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 * SECS_PER_HOUR;
}
}
Serial.println("No NTP Response :-(");
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 printDateTime(const RtcDateTime& dt)
{
char datestring[20];
snprintf_P(datestring,
countof(datestring),
PSTR("%02u/%02u/%04u %02u:%02u:%02u"),
dt.Month(),
dt.Day(),
dt.Year(),
dt.Hour(),
dt.Minute(),
dt.Second() );
Serial.print(datestring);
}