String Stand = "10.04.2020"; String Programm_Nr = "WiFi_Uhr_ESP_SSD1306"; // ---------- ---------------------- //**** SSD1306 *********** //OLED > NANO ESP8266 u. (D1 mini) //--------------------------------------------------- // SCL > A5 D1 // SDA > A4 D2 // VCC > V5 bzw. V3.3 V3.3 // GND > GND GND //*************************************************** #include #include #include #include #define OLED_ADDR 0x3C // Standardeinstellung //#define OLED_ADDR 0x3D #include //https://github.com/esp8266/Arduino #include "WiFiManager.h" //https://github.com/tzapu/WiFiManager #include //https://github.com/JChristensen/Timezone #include //https://github.com/PaulStoffregen/Time #include int x_m = 72; int y_m = 32; int zeiger_s = 29; int zeiger_m = 23; int zeiger_h = 18;//alt x_m = 64 int i ; int xx = 1 ; int y = 1 ; float winkel = 0 ; float inx = 0; float iny = 0; float h_12 = 0;float s_s = 0;float m_m = 0;float h_h = 0; //********************************************** int Rahmen = 1 ; // 0= ohne Rahmen, 1= mit Rahmen //********************************************** Adafruit_SSD1306 display(-1); int h, m, s, w, mo, ye, d; // Variablen für die lokale Zeit int k, l; // Zaehler int f_std = 0; // Fuenfer Stunden int e_std = 0; // Einer Stunden int f_min = 0; // Fuenfer Minuten int e_min = 0; // Einer Minuten // ***** NTP Serverpool für die Schweiz: static const char ntpServerName[] = "ch.pool.ntp.org"; //Finde lokale Server unter http://www.pool.ntp.org/zone/ch // **** static const char ntpServerName[] = "de.pool.ntp.org"; const int timeZone = 0; // 0 wenn mit gearbeitet wird sonst stimmt es nachher nicht WiFiUDP Udp; unsigned int localPort = 8888; // lokaler Port zum Abhören von UDP-Paketen time_t getNtpTime(); void digitalClockDisplay(); void printDigits(int digits); void sendNTPpacket(IPAddress &address); // - Timezone. - // // Bearbeiten Sie diese Zeilen entsprechend Ihrer Zeitzone und Sommerzeit. // TimeZone Einstellungen Details https://github.com/JChristensen/Timezone TimeChangeRule CEST = {"CEST", Last, Sun, Mar, 2, 120}; //Central European Time (Frankfurt, Paris) TimeChangeRule CET = {"CET ", Last, Sun, Oct, 3, 60}; //Central European Time (Frankfurt, Paris) Timezone CE(CEST, CET); //++++++++++++++++ void setup() { Serial.begin(115200); Wire.begin(); display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR); display.clearDisplay(); display.display(); // Bildschirm-Contrast einstellen // display.ssd1306_command(SSD1306_SETCONTRAST); // display.ssd1306_command(128); //gewünschter Kontrast-Wert von 0-255 // display.setRotation(0);// Bildschirmdrehung 0= 0°, (>0 and <45)=90°CCW, // (>45 and <90)=180°CCW, (>90 and <135)=270°CCW // (0° = Kabelstrang unten) WiFiManager wifiManager; //wifiManager.resetSettings(); // Mit diesem Befehl kannst die gespeicherten Werte loeschen //******************************************************************************************* wifiManager.autoConnect("WiFi_UHR_Moin"); Serial.println("verbunden!"); Udp.begin(localPort); Serial.print("lokaler Port: "); Serial.println(Udp.localPort()); Serial.println("Warten auf die Synchronisation"); setSyncProvider(getNtpTime); setSyncInterval(86400); // Anzahl der Sekunden zwischen dem erneuten Synchronisieren ein. 86400 = 1 Tag } time_t prevDisplay = 0; // wenn die Digitaluhr angezeigt wurde /////////// void loop() { y = y + 1; display.clearDisplay(); display.setTextSize(1); display.setTextColor(WHITE); //------------------------------ if (xx <= 1) { xx = 2; display.clearDisplay(); display.setCursor(0, 0);display.print("IP: "); display.println(WiFi.localIP()); display.setCursor(0, 16);//////////// display.print("Stand: "); display.println(Stand); display.setCursor(0, 28); display.println("Programm:"); display.setCursor(0, 42); display.println(Programm_Nr); display.display(); delay(7000); display.clearDisplay(); } //-------------------------------------- if (timeStatus() != timeNotSet) { // wenn die Zeit über NTP gesetzt wurde lokaleZeit(); digitalClockDisplay(); delay(100); } display.setTextSize(1); display.setTextColor(WHITE); } void lokaleZeit() { time_t tT = now(); time_t tTlocal = CE.toLocal(tT); w = weekday(tTlocal); d = day(tTlocal); mo = month(tTlocal); ye = year(tTlocal); h = hour(tTlocal); m = minute(tTlocal); s = second(tTlocal); } void digitalClockDisplay() { // digitale Uhrzeitanzeige Serial.print(h); printDigits(m); printDigits(s); Serial.print(" "); if (d <= 9) Serial.print("0"); Serial.print(d); Serial.print("."); if (mo <= 9) Serial.print("0"); Serial.print(mo); Serial.print("."); Serial.println(ye); //******************************** display.setTextSize(1); display.setTextColor(WHITE); if (s <= 20) //if (s >= 300) { display.setCursor(20, 44); if (w == 1 ) display.print("So. "); if (w == 2 ) display.print("Mo. "); if (w == 3 ) display.print("Di. "); if (w == 4 ) display.print("Mi. "); if (w == 5 ) display.print("Do. "); if (w == 6 ) display.print("Fr. "); if (w == 7 ) display.print("Sa. "); if (d <= 9) display.print("0"); display.print(d); display.print("."); if (mo <= 9) display.print("0"); display.print(mo); display.print("."); display.print(ye); //***************************** display.setTextSize(2); display.setCursor(14, 12); if (h <= 9) display.print("0"); display.print(h); display.print(":"); if (m <= 9) display.print ("0"); display.print(m); display.print(":"); if (s <= 9) display.print ("0"); display.print(s); display.drawLine(15, y_m + 4, 107, y_m + 4, SSD1306_WHITE); //********************************************************* } if (s >= 21) { display.setTextSize(1); display.setCursor(x_m-4, y_m+17); display.print("M"); display.setCursor(x_m, y_m+17); display.print("H"); display.setCursor(0, 0); if (w == 1 ) display.print("So."); if (w == 2 ) display.print("Mo."); if (w == 3 ) display.print("Di."); if (w == 4 ) display.print("Mi."); if (w == 5 ) display.print("Do."); if (w == 6 ) display.print("Fr."); if (w == 7 ) display.print("Sa."); display.print(d); display.print("."); if (mo <= 9) display.print("0"); display.print(mo); display.print("."); display.setCursor(0, 16);// 15 display.print(ye); display.setCursor(115, 0); if (s <= 9) display.print("0"); display.print(s); display.setTextSize(2); display.setCursor(17, 48); if (h <= 9) display.print("0"); display.print(h); display.setCursor(104, 48); if (m <= 9) display.print ("0"); display.print(m); if (s >= 40) display.drawCircle(x_m, y_m, 31, SSD1306_WHITE); // Sekunden s_s=s; display.drawLine(x_m, y_m, (x_m + int(zeiger_s * sin(s_s * 6 * DEG_TO_RAD))), (y_m - int(zeiger_s * cos(s_s * 6 * DEG_TO_RAD))), SSD1306_WHITE); // Minuten+Sekunden m_m = m; display.drawLine(x_m, y_m, (x_m + int(zeiger_m * sin((m_m + (s_s / 3600)) * 6 * DEG_TO_RAD))), (y_m - int(zeiger_m * cos((m_m + (s_s / 3600)) * 6 * DEG_TO_RAD))), SSD1306_WHITE); // Stunden+Minuten+Sekunden h_h =h; // Serial.print(" Std = ");Serial.println(h_h + (m_m / 60) + (s_s / 3600),3); if ((h_h + (m_m / 60) + (s_s / 3600)) >= 12.0001) h_h = h_h-12; if (h_h<=12) { //h_h =h; display.drawLine(x_m, y_m, (x_m + int(zeiger_h * sin((h_h + (m_m / 60) + (s_s / 3600)) * 30 * DEG_TO_RAD))), (y_m - int(zeiger_h * cos((h_h + (m_m / 60) + (s_s / 3600)) * 30 * DEG_TO_RAD))), SSD1306_WHITE); } display.drawLine(x_m - 31, y_m, x_m - 28, y_m, SSD1306_WHITE); display.drawLine(x_m + 28, y_m, x_m + 31, y_m, SSD1306_WHITE); display.drawLine(x_m, y_m - 31, x_m, y_m - 28, SSD1306_WHITE); display.drawLine(x_m, y_m + 28, x_m, y_m + 31, SSD1306_WHITE); } if (s <= 10) { if (Rahmen == 1) { //display.ssd1306_command(SSD1306_SETCONTRAST); display.ssd1306_command(1); //Vertikale Linien // display.drawLine(127, 0, 127, 63, SSD1306_WHITE); display.drawLine(122, 5, 122, 58, SSD1306_WHITE); display.drawLine(127, 63, 0, 63, SSD1306_WHITE); display.drawLine(122, 58, 5, 58, SSD1306_WHITE); display.drawLine(0, 63, 0, 0, SSD1306_WHITE); display.drawLine(5, 58, 5, 5, SSD1306_WHITE); display.drawLine(0, 0, 127, 0, SSD1306_WHITE); display.drawLine(5, 5, 122, 5, SSD1306_WHITE); } } display.display();//delay(622); } // ******************************************************************* void printDigits(int digits) { // Utility für digitale Uhrenanzeige: druckt vor Doppelpunkt und führende 0 Serial.print(":"); if (digits < 10) Serial.print('0'); Serial.print(digits); } /*-------- NTP code ----------*/ const int NTP_PACKET_SIZE = 48; // NTP-Zeit in den ersten 48 Bytes der Nachricht byte packetBuffer[NTP_PACKET_SIZE]; //Puffer für eingehende und ausgehende Pakete time_t getNtpTime() { IPAddress ntpServerIP; // NTP server's ip Adresse while (Udp.parsePacket() > 0) ; // alle zuvor empfangenen Pakete verwerfen Serial.println("Transmit NTP Request"); // einen zufälligen Server aus dem Pool holen 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); // Paket in den Puffer einlesen unsigned long secsSince1900; // vier Bytes ab Position 40 in eine lange Ganzzahl umwandeln 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("Keine NTP Antwort. "); return 0; // gibt 0 zurück, wenn die Zeit nicht ermittelt werden kann. } // send an NTP request to the time server at the given address void sendNTPpacket(IPAddress & address) { // alle Bytes im Puffer auf 0 setzen memset(packetBuffer, 0, NTP_PACKET_SIZE); // Initialisieren von Werten, die für die Bildung von NTP-Requests benötigt werden. // (siehe URL oben für Details zu den Paketen) 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; // alle NTP-Felder wurden jetzt mit Werten versehen // Sie können ein Paket senden, das einen Zeitstempel anfordert.: Udp.beginPacket(address, 123); //NTP-Requests sollen auf Port 123 erfolgen Udp.write(packetBuffer, NTP_PACKET_SIZE); Udp.endPacket(); }