/************************************************************************************************* PROGRAMMINFO ************************************************************************************************** Funktion: Funktion: ESP32 ntp Datum und Zeit Digitalanzeite ************************************************************************************************** Version: 19.03.2022 ************************************************************************************************** Board: ESP32vn IoT UNO V1.0.4 ************************************************************************************************** C++ Arduino IDE V1.8.19 ************************************************************************************************** Einstellungen: https://dl.espressif.com/dl/package_esp32_index.json http://dan.drown.org/stm32duino/package_STM32duino_index.json https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_dev_index.json ************************************************************************************************** Librarys - WiFi.h V0.16.1 ************************************************************************************************** OLED Schriftarten: https://arduino-projekte.info/schriftarten-fuer-oled-display/#torussansbold8_8r **************************************************************************************************/ #include // for time calculations #include // for wifi #include // for udp via wifi #include // see https://github.com/olikraus/u8g2/wiki/u8g2reference //#include "SH1106.h" #include #include ////////////////////////////////////////////////////////////////////////////////////////////////////////////// // // Constants // ////////////////////////////////////////////////////////////////////////////////////////////////////////////// #define FONT_ONE_HEIGHT 8 // font one height in pixels #define FONT_TWO_HEIGHT 20 // font two height in pixels #define NTP_DELAY_COUNT 20 // delay count for ntp update #define NTP_PACKET_LENGTH 48 // ntp packet length #define TIME_ZONE (+2) // offset from utc BERLIN #define UDP_PORT 4000 // UDP listen port ////////////////////////////////////////////////////////////////////////////////////////////////////////////// // // Variables // ////////////////////////////////////////////////////////////////////////////////////////////////////////////// char chBuffer[128]; // general purpose character buffer char chSSID[] = "xxx"; // your network SSID char chPassword[] = "xxx"; // your network password bool bTimeReceived = false; // time has not been received //U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, 16, 15, 4); // OLED graphics //SH1106 display(0x3C, 21,22); //1,3" U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE); int nWifiStatus = WL_IDLE_STATUS; // wifi status WiFiUDP Udp; ////////////////////////////////////////////////////////////////////////////////////////////////////////////// // // Setup // ////////////////////////////////////////////////////////////////////////////////////////////////////////////// void setup() { // Serial. Serial.begin(115200); while(!Serial) { Serial.print('.'); } // OLED graphics. u8g2.begin(); u8g2.setFont(u8g2_font_6x10_tr); u8g2.setFontRefHeightExtendedText(); u8g2.setDrawColor(1); u8g2.setFontPosTop(); u8g2.setFontDirection(0); // Wifi. // Display title. u8g2.clearBuffer(); sprintf(chBuffer, "%s", "Connecting to:"); u8g2.drawStr(64 - (u8g2.getStrWidth(chBuffer) / 2), 0, chBuffer); sprintf(chBuffer, "%s", chSSID); u8g2.drawStr(64 - (u8g2.getStrWidth(chBuffer) / 2), 31 - (FONT_ONE_HEIGHT / 2), chBuffer); u8g2.sendBuffer(); // Connect to wifi. Serial.print("NTP clock: connecting to wifi"); WiFi.begin(chSSID, chPassword); while(WiFi.status() != WL_CONNECTED) { Serial.print("."); delay(500); } Serial.println(); sprintf(chBuffer, "NTP clock: WiFi connected to %s.", chSSID); Serial.println(chBuffer); // Display connection stats. // Clean the display buffer. u8g2.clearBuffer(); // Display the title. sprintf(chBuffer, "%s", "WiFi Stats:"); u8g2.drawStr(64 - (u8g2.getStrWidth(chBuffer) / 2), 0, chBuffer); // Display the ip address assigned by the wifi router. char chIp[81]; WiFi.localIP().toString().toCharArray(chIp, sizeof(chIp) - 1); sprintf(chBuffer, "IP : %s", chIp); u8g2.drawStr(0, FONT_ONE_HEIGHT * 2, chBuffer); // Display the ssid of the wifi router. sprintf(chBuffer, "SSID: %s", chSSID); u8g2.drawStr(0, FONT_ONE_HEIGHT * 3, chBuffer); // Display the rssi. sprintf(chBuffer, "RSSI: %d", WiFi.RSSI()); u8g2.drawStr(0, FONT_ONE_HEIGHT * 4, chBuffer); // Display waiting for ntp message. u8g2.drawStr(0, FONT_ONE_HEIGHT * 6, "Awaiting NTP time..."); // Now send the display buffer to the OLED. u8g2.sendBuffer(); // Udp. Udp.begin(UDP_PORT); }