Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /****************************************************************************************************************************
- NTP-Generic library usage template to extend your code by Tomasz Majcher (2021/07/31)
- Based on and modified from Arduino NTPClient Library (https://github.com/arduino-libraries/NTPClient)
- to support other boards such as ESP8266/ESP32, SAMD21, SAMD51, Adafruit's nRF52 boards, SAM DUE, etc.
- and Ethernet/WiFi/WiFiNINA shields
- Copyright (C) 2015 by Fabrice Weinberg and licensed under MIT License (MIT)
- Built by Khoi Hoang https://github.com/khoih-prog/NTPClient_Generic
- Licensed under MIT license
- Version: 3.4.0
- *****************************************************************************************************************************/
- //
- //
- //
- //
- // ------------------------------------------------------------------------------------------------------------------- //
- // H I S T O R Y V E R S I O N L O G
- //
- // v0.01 add function : SerialInit()
- // v0.02 add function : WiFiInit()
- // v0.03 add function : WiFiStatus()
- // v0.04 add function : WiFiReconect()
- // v0.05 add function : NTPInit()
- // v0.06 add function : Show_NTP()
- // v0.07 add structure : MyNTP
- // v0.07 add function : MyNTP_Update()
- //
- // ------------------------------------------------------------------------------------------------------------------- //
- // --- WiFi Setup ---------------------------------------------------------------------------------------------------- //
- // ------------------------------------------------------------------------------------------------------------------- //
- #include <ESP8266WiFi.h> // Library for WiFi Network (biblioteka do obsługi sieci WiFi)
- #include <WiFiUdp.h> // Library for NTP Server (biblioteka do obsługi serwera NTP)
- char ssid[] = "xxxxxx"; // your network SSID (nazwa twojej sieci WiFi)
- char pass[] = "xxxxxxxxxxx"; // your network password (hasło dostępu do sieci WiFi)
- WiFiUDP ntpUDP;
- unsigned long previousMillis = 0; // for WiFiReconect
- unsigned long interval = 30000; // for WiFiReconect
- // ------------------------------------------------------------------------------------------------------------------- //
- // --- NTP server ---------------------------------------------------------------------------------------------------- //
- // ------------------------------------------------------------------------------------------------------------------- //
- #define NTP_DBG_PORT Serial // where Debug NTP ---> Serial
- #define _NTP_LOGLEVEL_ 0 // 0 = no NTP debug
- #include <NTPClient_Generic.h> // Library for NTP Server
- char timeServer[] = "pl.pool.ntp.org"; // Time Server (Europe/Poland)
- #define TIME_ZONE_OFFSET_HRS (2) // Time Zone (Europe/Warsaw)
- #define NTP_UPDATE_INTERVAL_MS 60000L // Ntp Update (60 seconds)
- NTPClient timeClient(ntpUDP, timeServer, (3600 * TIME_ZONE_OFFSET_HRS), NTP_UPDATE_INTERVAL_MS);
- // ------------------------------------------------------------------------------------------------------------------- //
- // --- L A N G U A G E S E T U P ----------------------------------------------------------------------------------- //
- // ------------------------------------------------------------------------------------------------------------------- //
- #define LANGUAGE 0 // 0 (Polish), 1 (English)
- #if LANGUAGE == 0 // if POLISH Language
- #define POLISH 1
- #define ENGLISH 0
- #endif
- #if LANGUAGE == 1 // if ENGLISH Language
- #define POLISH 0
- #define ENGLISH 1
- #endif
- // ------------------------------------------------------------------------------------------------------------------- //
- // --- S E R I A L S E T U P --------------------------------------------------------------------------------------- //
- // ------------------------------------------------------------------------------------------------------------------- //
- #define SERIAL true
- #define SerialSpeed 57600
- // ------------------------------------------------------------------------------------------------------------------- //
- // --- M Y N T P S T R U C T --------------------------------------------------------------------------------------- //
- // ------------------------------------------------------------------------------------------------------------------- //
- struct TIMEDATA
- {
- // *** U T C T I M E & D A T A *** ///
- int iUTCYear; // year UTC integer
- int iUTCMonth; // month UTC integer
- String sUTCMonth;
- String sUTCDoW;
- int iUTCDay;
- int iUTCHours;
- String sUTCHours;
- int iUTCMinutes;
- String sUTCMinutes;
- int iUTCSeconds;
- String sUTCSeconds;
- // *** L O C T I M E & D A T A *** ///
- int iLOCYear;
- String sLOCMonth;
- int iLOCMonth;
- String sLOCWeekDay;
- int iLOCDay;
- int iLOCHours;
- String sLOCHours;
- int iLOCMinutes;
- String sLOCMinutes;
- int iLOCSeconds;
- String sLOCSeconds;
- }
- MyNTP;
- // ------------------------------------------------------------------------------------------------------------------- //
- // --- S E R I A L I N I T ---------------------------------------------------------------------------------------- //
- // ------------------------------------------------------------------------------------------------------------------- //
- void SerialInit()
- {
- #if SERIAL
- Serial.begin(SerialSpeed); // init Serial and set speed
- while (!Serial); // wait if not ready
- Serial.printf("\n\n"); // next line please
- #endif
- }
- // ------------------------------------------------------------------------------------------------------------------- //
- // --- D R A W L I N E --------------------------------------------------------------------------------------------- //
- // ------------------------------------------------------------------------------------------------------------------- //
- void DrawLine(byte l, char s)
- {
- #if SERIAL
- Serial.printf("\n");
- for (byte i=0; i<l; i++) Serial.print(s);
- #endif
- }
- // ------------------------------------------------------------------------------------------------------------------- //
- // --- W i F i I N I T --------------------------------------------------------------------------------------------- //
- // ------------------------------------------------------------------------------------------------------------------- //
- void WiFiInit()
- {
- WiFi.mode(WIFI_STA);
- WiFi.begin(ssid, pass);
- #if SERIAL
- #if ENGLISH
- Serial.printf("\n[ESP] [WiFI] Connecting to WiFi ..");
- #endif
- #if POLISH
- Serial.printf("\n[ESP] [WiFI] Łącze z siecią WiFi ..");
- #endif
- #endif
- while (WiFi.status() != WL_CONNECTED)
- {
- Serial.print('.');
- delay(200);
- }
- //statWiFi();
- WiFi.setAutoReconnect(true);
- WiFi.persistent(true);
- }
- // ------------------------------------------------------------------------------------------------------------------- //
- // --- WiFiStat ------------------------------------------------------------------------------------------------------ //
- // ------------------------------------------------------------------------------------------------------------------- //
- void WiFiStat()
- {
- switch (WiFi.status())
- {
- case WL_NO_SSID_AVAIL:
- #if SERIAL
- #if ENGLISH
- Serial.printf("\n[ESP] [WiFI] Configured SSID cannot be reached");
- #endif
- #if POLISH
- Serial.printf("\n[ESP] [WiFI] Nie można osiągnąć skonfigurowanego identyfikatora SSID");
- #endif
- #endif
- break;
- case WL_CONNECTED:
- #if SERIAL
- #if ENGLISH
- Serial.printf("\n[ESP] [WiFI] Connection successfully established");
- Serial.printf("\n[ESP] [WiFI] Your IP : %s", WiFi.localIP().toString().c_str());
- Serial.printf("\n[ESP] [WiFI] Received Signal Strength Indication : %d", WiFi.RSSI());
- #endif
- #if POLISH
- Serial.printf("\n[ESP] [WiFI] Pomyślnie nawiązano połączenie");
- Serial.printf("\n[ESP] [WiFI] Twój adres IP : %s", WiFi.localIP().toString().c_str());
- Serial.printf("\n[ESP] [WiFI] Wskazanie siły odbieranego sygnału : %d", WiFi.RSSI());
- #endif
- #endif
- break;
- case WL_CONNECT_FAILED:
- #if SERIAL
- #if ENGLISH
- Serial.printf("\n[ESP] [WiFI] Connection failed");
- #endif
- #if POLISH
- Serial.printf("\n[ESP] [WiFI] Połączenie nieudane");
- #endif
- #endif
- break;
- }
- }
- // ------------------------------------------------------------------------------------------------------------------- //
- // --- reconect Wi-Fi ------------------------------------------------------------------------------------------------ //
- // ------------------------------------------------------------------------------------------------------------------- //
- void WiFiReconect()
- {
- unsigned long currentMillis = millis();
- if ((WiFi.status() != WL_CONNECTED) && (currentMillis - previousMillis >=interval)) // if no WiFi connect now
- {
- #if SERIAL
- #if ENGLISH
- Serial.printf("\n[ESP] [WiFI] Reconnecting to WiFi...");
- #endif
- #if POLISH
- Serial.printf("\n[ESP] [WiFI] Ponowne łączenie z Wi-Fi...");
- #endif
- #endif
- WiFi.disconnect();
- WiFi.begin(ssid, pass);
- WiFiStat(); // show WiFi Status
- previousMillis = currentMillis;
- }
- }
- // ------------------------------------------------------------------------------------------------------------------- //
- // ---N T P I N I T ------------------------------------------------------------------------------------------------ //
- // ------------------------------------------------------------------------------------------------------------------- //
- int NTPInit()
- {
- timeClient.update();
- if (timeClient.updated())
- {
- #if SERIAL
- DrawLine(60,'*');
- #if ENGLISH
- Serial.printf("\n[ESP] [NTP] Time Update :-)");
- Serial.printf("\n[ESP] [NTP] Using NTP Server : %s", timeClient.getPoolServerName().c_str());
- #endif
- #if POLISH
- Serial.printf("\n[ESP] [NTP] Czas został zaktualizowany :-)");
- Serial.printf("\n[ESP] [NTP] Używasz Serwera NTP : %s", timeClient.getPoolServerName().c_str());
- #endif
- #endif
- return 1; // return 1 if time is update
- }
- else
- {
- #if SERIAL
- #if ENGLISH
- Serial.printf("\n[ESP] [NTP] Time in not Update !!!");
- #endif
- #if POLISH
- Serial.printf("\n[ESP] [NTP] Czas nie został zaktualizowany !!!");
- #endif
- #endif
- return 0; // return 0 if time is not update
- }
- }
- // ------------------------------------------------------------------------------------------------------------------- //
- // --- S H O W N T P ----------------------------------------------------------------------------------------------- //
- // ------------------------------------------------------------------------------------------------------------------- //
- void Show_NTP()
- {
- #if SERIAL
- DrawLine(60,'*');
- #if POLISH
- Serial.printf("\n[ESP] [TIME] UTC Czas : %s", timeClient.getFormattedUTCTime().c_str());
- Serial.printf("\n[ESP] [TIME] UTC Data : %d/%d/%d", timeClient.getUTCDay(),timeClient.getUTCMonth(),timeClient.getUTCYear());
- Serial.printf("\n[ESP] [TIME] LOC Czas : %s", timeClient.getFormattedTime().c_str());
- Serial.printf("\n[ESP] [TIME] LOC Data : %d/%d/%d", timeClient.getDay(),timeClient.getMonth(),timeClient.getYear());
- #endif
- #if ENGLISH
- Serial.printf("\n[ESP] [TIME] UTC Time : %s", timeClient.getFormattedUTCTime().c_str());
- Serial.printf("\n[ESP] [TIME] UTC Data : %d/%d/%d", timeClient.getUTCDay(),timeClient.getUTCMonth(),timeClient.getUTCYear());
- Serial.printf("\n[ESP] [TIME] LOC Time : %s", timeClient.getFormattedTime().c_str());
- Serial.printf("\n[ESP] [TIME] LOC Data : %d/%d/%d", timeClient.getDay(),timeClient.getMonth(),timeClient.getYear());
- #endif
- #endif
- }
- // ------------------------------------------------------------------------------------------------------------------- //
- // --- U P D A T E M Y N T P S T R U C T U R E ------------------------------------------------------------------- //
- // ------------------------------------------------------------------------------------------------------------------- //
- void MyNTP_Update()
- {
- timeClient.update();
- // *** U T C T I M E & D A T A *** ///
- MyNTP.iUTCYear = timeClient.getUTCYear();
- MyNTP.iUTCMonth = timeClient.getUTCMonth();
- MyNTP.sUTCMonth = timeClient.getUTCMonthStr();
- MyNTP.sUTCDoW = timeClient.getUTCDoW();
- MyNTP.iUTCDay = timeClient.getUTCDay();
- MyNTP.iUTCHours = timeClient.getUTCHours();
- MyNTP.sUTCHours = timeClient.getUTCStrHours();
- MyNTP.iUTCMinutes = timeClient.getUTCMinutes();
- MyNTP.sUTCMinutes = timeClient.getUTCStrMinutes();
- MyNTP.iUTCSeconds = timeClient.getUTCSeconds();
- MyNTP.sUTCSeconds = timeClient.getUTCStrSeconds();
- // *** L O C T I M E & D A T A *** ///
- MyNTP.iLOCYear = timeClient.getYear();
- MyNTP.sLOCMonth = timeClient.getMonthStr();
- MyNTP.iLOCMonth = timeClient.getMonth();
- MyNTP.sLOCWeekDay = timeClient.getDoW();
- MyNTP.iLOCDay = timeClient.getDay();
- MyNTP.iLOCHours = timeClient.getHours();
- MyNTP.sLOCHours = timeClient.getStrHours();
- MyNTP.iLOCMinutes = timeClient.getMinutes();
- MyNTP.sLOCMinutes = timeClient.getStrMinutes();
- MyNTP.iLOCSeconds = timeClient.getSeconds();
- MyNTP.sLOCSeconds = timeClient.getStrSeconds();
- }
- // ------------------------------------------------------------------------------------------------------------------- //
- // --- M A I N S E T U P ------------------------------------------------------------------------------------------- //
- // ------------------------------------------------------------------------------------------------------------------- //
- void setup()
- {
- SerialInit(); // check and init serial monitor
- WiFiInit(); // start WiFi Connection
- WiFiStat(); // show WiFi Status
- WiFiReconect(); // check WiFi Connection and conect if loss !!!
- timeClient.begin(); // start timeClient
- DrawLine(60,'*'); // Draw separator *
- if ( WiFi.status() == WL_CONNECTED ) // if WiFi is OK
- {
- while(!NTPInit()); // init NTP server
- Show_NTP(); // show NTP time & data
- MyNTP_Update(); // copy Time & Data to structure MyNTP
- }
- }
- // ------------------------------------------------------------------------------------------------------------------- //
- // --- M A I N L O O P --------------------------------------------------------------------------------------------- //
- // ------------------------------------------------------------------------------------------------------------------- //
- void loop()
- {
- }
- // ------------------------------------------------------------------------------------------------------------------- //
Add Comment
Please, Sign In to add comment