Moin, ich möchte eine RTC an meinen ESP8266 anschließen, habe aber gelesen, dass man dazu einen CLK pin braucht. Meine RTC hat die Pins: SDA, SCL, SQW, 32K, und natürlich VCC/GND.
Außerdem möchte ich die Uhr über NTP synchronisieren. Dazu habe ich mir diesen Code kopiert und mein Netzwerk eingetragen. Die angezeigte Uhrzeit hängt aber 2h in der Vergangenheit und ich weiß nicht, wie ich ein Offset einbaue.
#include <RTClib.h>
#include <Wire.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <NTPClient.h>
#include <TimeLib.h>
RTC_DS3231 rtc;
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "asia.pool.ntp.org", 19800, 60000);
char t[32];
byte last_second, second_, minute_, hour_, day_, month_;
int year_;
void setup()
{
Serial.begin(9600); // Initialize serial communication with a baud rate of 9600
Wire.begin(); // Begin I2C communication
rtc.begin(); // Initialize DS3231 RTC module
// Connect to WiFi
char* ssid = "SSID"; // Replace with your WiFi SSID
char* password = "PASSWORD"; // Replace with your WiFi password
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("Connected to WiFi");
timeClient.begin(); // Start NTP client
timeClient.update(); // Retrieve current epoch time from NTP server
unsigned long unix_epoch = timeClient.getEpochTime(); // Get epoch time
rtc.adjust(DateTime(unix_epoch)); // Set RTC time using NTP epoch time
DateTime now = rtc.now(); // Get initial time from RTC
last_second = now.second(); // Store initial second
}
void loop()
{
timeClient.update(); // Update time from NTP server
unsigned long unix_epoch = timeClient.getEpochTime(); // Get current epoch time
second_ = second(unix_epoch); // Extract second from epoch time
if (last_second != second_)
{
minute_ = minute(unix_epoch); // Extract minute from epoch time
hour_ = hour(unix_epoch); // Extract hour from epoch time
day_ = day(unix_epoch); // Extract day from epoch time
month_ = month(unix_epoch); // Extract month from epoch time
year_ = year(unix_epoch); // Extract year from epoch time
// Format and print NTP time on Serial monitor
sprintf(t, "NTP Time: %02d:%02d:%02d %02d/%02d/%02d", hour_, minute_, second_, day_, month_, year_);
Serial.println(t);
DateTime rtcTime = rtc.now(); // Get current time from RTC
// Format and print RTC time on Serial monitor
sprintf(t, "RTC Time: %02d:%02d:%02d %02d/%02d/%02d", rtcTime.hour(), rtcTime.minute(), rtcTime.second(), rtcTime.day(), rtcTime.month(), rtcTime.year());
Serial.println(t);
// Compare NTP time with RTC time
if (rtcTime == DateTime(year_, month_, day_, hour_, minute_, second_))
{
Serial.println("Time is synchronized!"); // Print synchronization status
}
else
{
Serial.println("Time is not synchronized!"); // Print synchronization status
}
last_second = second_; // Update last second
}
delay(1000); // Delay for 1 second before the next iteration
}```
Im englischen Teil des Forum müssen die Beiträge und Diskussionen in englischer Sprache verfasst werden. Deswegen wurde diese Diskussion in den deutschen Teil des Forums verschoben.
Die RTC wird per I2C angesteuert, da brauchst du keinen Clock-Pin.
Deine falsche Zeit liegt vermutlich am NTP-SERVER den du nutzt.
Sieh dir mal die Beispiele bei fips an, und nutze die dort gezeigten NTP-SERVER.
Hallo
Wenn du eine ständige WiWi Verbindung hast dann kannst du auf die RTC verzichten.
Wenn du eine Fritzbox hast kannst du die auch als NTP Zeitserver angeben. Eventuell in der Fritzbox noch freischalten.
Das habe ich am Ende des Setups eingefügt. Leider läuft die Zeit immer noch 2h vor.
#include "RTClib.h"
#include <Wire.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <NTPClient.h>
#include <TimeLib.h>
RTC_DS3231 rtc;
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP,"ntp.web.de");
char t[32];
byte last_second, second_, minute_, hour_, day_, month_;
int year_;
void setup()
{
Serial.begin(9600); // Initialize serial communication with a baud rate of 9600
Wire.begin(); // Begin I2C communication
rtc.begin(); // Initialize DS3231 RTC module
// Connect to WiFi
char* ssid = "-"; // Replace with your WiFi SSID
char* password = "-"; // Replace with your WiFi password
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.println(".");
}
Serial.println("Connected to WiFi");
timeClient.begin(); // Start NTP client
timeClient.update(); // Retrieve current epoch time from NTP server
unsigned long unix_epoch = timeClient.getEpochTime(); // Get epoch time
rtc.adjust(DateTime(unix_epoch)); // Set RTC time using NTP epoch time
DateTime now = rtc.now(); // Get initial time from RTC
last_second = now.second(); // Store initial second
**configTime("CET-1CEST,M3.5.0,M10.5.0/3", "ntp.web.de");**
}
Ich habe keine dauerhafte Verbindung, darum soll die RTC mit rein. Funktioniert das wie beim Oled über die pins D1&D2 oder sind die pins bei I2C Geräten egal?
Erst die Zeit lesen und viel später die Zeitzone einstellen ist etwas unlogisch oder meinst Du nicht? Außerdem kann das Dein timeClient so nicht. Wenn Du den benutzen willst, dann musst Du das händisch machen (auch Sommer/Winterzeit selbst programmieren). Schau Dir die Beispiele zur Lib an.
Ansonsten nutze die eingebauten Möglichkeiten des ESP8266 und schau Dir an, wie das bei Fips gemacht wird.
Du solltest einfach alles lesen, was man dir schreibt. Die Hilfen sind oft sehr viel umfangreicher, als man selbst glaubt.
Was I2C betrifft, sollten die Pins richtig beschaltet werden. Angaben dazu findest du im Pinout deines ESP8266, den wir nicht kennen.
I²C ist ein Bus also alle I²C Geräte kommen auf die gleiche Pins also so wie beim OLED.
SCL auf SCL
SCK auf SCK
Zum auswählen welsches angesprochen wird, soll sein ist zuständig die I²C Adresse, die wird ja auch im Skript(Sketch) angegeben.