Hallo,
die Zeitangabe in meinem Programm funktioniert nicht (Uhrzeit: Thursday:02:00:05)
wenn ich die WLAN-Verbindung mit fester IP-Adresse verwende (WiFi.config(staticIP, gateway, subnet);).
[code]
// Test Zeitangabe und WiFi-config
#include <ESP8266WebServer.h>
#include <ArduinoOTA.h> // https://arduino-esp8266.readthedocs.io/en/latest/ota_updates/readme.html
#include <FS.h>
#include <TimeLib.h>
#include <ESP8266WiFi.h>
struct tm tm;
uint32_t aktuelleMillis;
const long INTERVALL = 1000;
char befehl[13]; // buffer for incoming packets
const char* const PROGMEM ntpServer[] = {"fritz.box", "de.pool.ntp.org", "at.pool.ntp.org", "ch.pool.ntp.org", "ptbtime1.ptb.de", "europe.pool.ntp.org", "za.pool.ntp.org"};
const char* const PROGMEM monthNames[] = {"Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember"};
const char* const PROGMEM monthShortNames[] = {"Jan", "Feb", "Mrz", "Apr", "Mai", "Jun", "Jul", "Aug", "Sep", "Okt", "Nov", "Dez"};
//const char* const PROGMEM wochentag[] = {"Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag", "Sonntag"};
const char* const PROGMEM weekDays[7]={"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
//Month names
String months[12]={"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
const char* ssid = "ssid"; // << kann bis zu 32 Zeichen haben
const char* password = "password";
#define SSR_1 0 // IO0 ESP07
#define SSR_2 12 // IO12 ESP07
void SetupTime(void);
char* localTime(void);
#ifdef CONFIG
IPAddress staticIP(192, 168, 1, 22); // statische IP des NodeMCU ESP8266
IPAddress gateway(192, 168, 1, 1); // IP-Adresse des Router
IPAddress subnet(255, 255, 255, 0); // Subnetzmaske des Netzwerkes
IPAddress dns(192, 168, 1, 1); // DNS Server
#endif
void setup() {
Serial.begin(115200);
delay(500);
Serial.println("");
Serial.println("*******************************");
Serial.println("Test Zeitangabe und WiFi-config");
Serial.println("*******************************");
pinMode(SSR_1, OUTPUT);
digitalWrite(SSR_1,HIGH);
pinMode(SSR_2, OUTPUT);
digitalWrite(SSR_2,HIGH);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
#ifdef CONFIG
WiFi.config(staticIP, gateway, subnet);
#endif
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println("\nVerbunden mit: " + WiFi.SSID());
Serial.println("ESP07 IP: " + WiFi.localIP().toString());
setupTime();
}
void loop() {
static auto letzteMillis = 0;
aktuelleMillis = millis();
if (aktuelleMillis - letzteMillis >= 1e3) {
letzteMillis = aktuelleMillis;
localTime();
}
}
void setupTime() { // CHANGE THE POOL WITH YOUR CITY. SEARCH AT https://www.ntppool.org/zone/@ --> global
configTime(0, 0, ntpServer[6]); // deinen NTP Server einstellen (von 0 - 6 aus obiger Liste)
//setenv("TZ", "CET-1CEST,M3.5.0/02,M10.5.0/03", 1); // Zeitzone einstellen https://github.com/nayarsystems/posix_tz_db/blob/master/zones.csv
//setenv("TZ", "GMT-2BST",1); // Zeitzone South Africa, Nelspruit
//setenv("TZ", "SAST-2",1);
setenv("TZ", "CAT-2",1); // Zeitzone Africa/Maputo
delay(250);
}
char* localTime() {
static char buf[19]; // je nach Format von "strftime" eventuell anpassen
static time_t lastsek = 0;
time_t now = time(&now);
localtime_r(&now, &tm);
if (tm.tm_sec != lastsek) {
lastsek = tm.tm_sec;
strftime (buf, sizeof(buf), "%A:%T", &tm); // Anzeige: Wochentag:Uhrzeit
// http://www.cplusplus.com/reference/ctime/strftime/
// https://books.google.co.za/books?id=YR5CqZcFJU8C&pg=PA980&lpg=PA980&dq=strftime%2Buhrzeit%2Bwochentag&source=bl&ots=QJEkdbCjaM&sig=ACfU3U1E1nye3OAAMcBEOajn5-D4hfPIrA&hl=de&sa=X&ved=2ahUKEwjN1rTb3rrnAhU98uAKHTNLCCEQ6AEwCHoECAcQAQ#v=onepage&q=strftime%2Buhrzeit%2Bwochentag&f=false
if (!(time(&now) % 43200)) { // zweimal am Tag die Zeit vom NTP Server holen o. jede Stunde "% 3600" aller zwei "% 7200"
setupTime();
}
}
Serial.print("Uhrzeit: "); Serial.println(buf);
return buf;
}
[/code]
MfG
Juergen B.