PowerTGS440

NTP_GENERIC ready example

Jul 31st, 2021 (edited)
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 17.85 KB | None | 0 0
  1. /****************************************************************************************************************************
  2.  
  3.   NTP-Generic library usage template to extend your code by Tomasz Majcher (2021/07/31)
  4.  
  5.   Based on and modified from Arduino NTPClient Library (https://github.com/arduino-libraries/NTPClient)
  6.   to support other boards such as ESP8266/ESP32, SAMD21, SAMD51, Adafruit's nRF52 boards, SAM DUE, etc.
  7.   and Ethernet/WiFi/WiFiNINA shields
  8.  
  9.   Copyright (C) 2015 by Fabrice Weinberg and licensed under MIT License (MIT)
  10.  
  11.   Built by Khoi Hoang https://github.com/khoih-prog/NTPClient_Generic
  12.   Licensed under MIT license
  13.   Version: 3.4.0
  14.  
  15. *****************************************************************************************************************************/
  16. //
  17. //
  18. //
  19. //
  20. // ------------------------------------------------------------------------------------------------------------------- //
  21. // H I S T O R Y   V E R S I O N   L O G
  22. //
  23. //  v0.01 add function : SerialInit()
  24. //  v0.02 add function : WiFiInit()
  25. //  v0.03 add function : WiFiStatus()
  26. //  v0.04 add function : WiFiReconect()
  27. //  v0.05 add function : NTPInit()
  28. //  v0.06 add function : Show_NTP()
  29. //  v0.07 add structure : MyNTP
  30. //  v0.07 add function :  MyNTP_Update()
  31. //
  32. // ------------------------------------------------------------------------------------------------------------------- //
  33. // --- WiFi Setup ---------------------------------------------------------------------------------------------------- //
  34. // ------------------------------------------------------------------------------------------------------------------- //
  35.  
  36. #include <ESP8266WiFi.h>                // Library for WiFi Network (biblioteka do obsługi sieci WiFi)
  37. #include <WiFiUdp.h>                    // Library for NTP Server (biblioteka do obsługi serwera NTP)
  38.  
  39. char ssid[] = "xxxxxx";                 // your network SSID (nazwa twojej sieci WiFi)
  40. char pass[] = "xxxxxxxxxxx";            // your network password (hasło dostępu do sieci WiFi)
  41.  
  42. WiFiUDP ntpUDP;
  43.  
  44. unsigned long previousMillis = 0;       // for WiFiReconect
  45. unsigned long interval = 30000;         // for WiFiReconect
  46.  
  47. // ------------------------------------------------------------------------------------------------------------------- //
  48. // --- NTP server ---------------------------------------------------------------------------------------------------- //
  49. // ------------------------------------------------------------------------------------------------------------------- //
  50.  
  51. #define NTP_DBG_PORT                Serial                            // where Debug NTP ---> Serial
  52. #define _NTP_LOGLEVEL_              0                                 // 0 = no NTP debug
  53.  
  54. #include <NTPClient_Generic.h>                                        // Library for NTP Server
  55.  
  56. char timeServer[] = "pl.pool.ntp.org";                                // Time Server (Europe/Poland)
  57.  
  58. #define TIME_ZONE_OFFSET_HRS            (2)                           // Time Zone (Europe/Warsaw)
  59. #define NTP_UPDATE_INTERVAL_MS          60000L                        // Ntp Update (60 seconds)
  60.  
  61. NTPClient timeClient(ntpUDP, timeServer, (3600 * TIME_ZONE_OFFSET_HRS), NTP_UPDATE_INTERVAL_MS);
  62.  
  63. // ------------------------------------------------------------------------------------------------------------------- //
  64. // --- L A N G U A G E   S E T U P ----------------------------------------------------------------------------------- //
  65. // ------------------------------------------------------------------------------------------------------------------- //
  66.  
  67. #define LANGUAGE 0                                            // 0 (Polish), 1 (English)
  68.                                                                
  69. #if LANGUAGE == 0                                             // if POLISH Language                                            
  70.     #define POLISH 1
  71.     #define ENGLISH 0
  72. #endif
  73.    
  74. #if LANGUAGE == 1                                             // if ENGLISH Language
  75.     #define POLISH 0                                        
  76.     #define ENGLISH 1    
  77. #endif
  78.  
  79. // ------------------------------------------------------------------------------------------------------------------- //
  80. // --- S E R I A L   S E T U P --------------------------------------------------------------------------------------- //
  81. // ------------------------------------------------------------------------------------------------------------------- //
  82.  
  83. #define SERIAL true
  84. #define SerialSpeed 57600
  85.  
  86. // ------------------------------------------------------------------------------------------------------------------- //
  87. // --- M Y N T P   S T R U C T --------------------------------------------------------------------------------------- //
  88. // ------------------------------------------------------------------------------------------------------------------- //
  89.  
  90. struct TIMEDATA
  91. {
  92.     // *** U T C   T I M E   &   D A T A *** ///
  93.    
  94.     int       iUTCYear;                                     // year UTC integer
  95.     int       iUTCMonth;                                    // month UTC integer
  96.     String    sUTCMonth;
  97.     String    sUTCDoW;
  98.     int       iUTCDay;
  99.     int       iUTCHours;
  100.     String    sUTCHours;
  101.     int       iUTCMinutes;
  102.     String    sUTCMinutes;
  103.     int       iUTCSeconds;
  104.     String    sUTCSeconds;
  105.  
  106.     // *** L O C   T I M E   &   D A T A *** ///
  107.  
  108.     int       iLOCYear;
  109.     String    sLOCMonth;
  110.     int       iLOCMonth;
  111.     String    sLOCWeekDay;
  112.     int       iLOCDay;
  113.     int       iLOCHours;
  114.     String    sLOCHours;
  115.     int       iLOCMinutes;
  116.     String    sLOCMinutes;
  117.     int       iLOCSeconds;
  118.     String    sLOCSeconds;
  119.    
  120.    
  121. }
  122. MyNTP;
  123.  
  124. // ------------------------------------------------------------------------------------------------------------------- //
  125. // --- S E R I A L    I N I T ---------------------------------------------------------------------------------------- //
  126. // ------------------------------------------------------------------------------------------------------------------- //
  127.  
  128. void SerialInit()
  129. {
  130.     #if SERIAL
  131.         Serial.begin(SerialSpeed);                            // init Serial and set speed
  132.         while (!Serial);                                      // wait if not ready
  133.         Serial.printf("\n\n");                                // next line please
  134.     #endif    
  135. }
  136.  
  137. // ------------------------------------------------------------------------------------------------------------------- //
  138. // --- D R A W   L I N E --------------------------------------------------------------------------------------------- //
  139. // ------------------------------------------------------------------------------------------------------------------- //
  140.  
  141. void DrawLine(byte l, char s)
  142. {
  143.     #if SERIAL
  144.         Serial.printf("\n");
  145.         for (byte i=0; i<l; i++) Serial.print(s);        
  146.     #endif
  147. }
  148.  
  149. // ------------------------------------------------------------------------------------------------------------------- //
  150. // --- W i F i   I N I T --------------------------------------------------------------------------------------------- //
  151. // ------------------------------------------------------------------------------------------------------------------- //
  152.  
  153. void WiFiInit()
  154. {
  155.     WiFi.mode(WIFI_STA);
  156.     WiFi.begin(ssid, pass);
  157.  
  158.     #if SERIAL    
  159.         #if ENGLISH
  160.             Serial.printf("\n[ESP] [WiFI] Connecting to WiFi ..");
  161.         #endif
  162.         #if POLISH
  163.             Serial.printf("\n[ESP] [WiFI] Łącze z siecią WiFi ..");
  164.         #endif        
  165.     #endif
  166.      
  167.     while (WiFi.status() != WL_CONNECTED)
  168.     {
  169.         Serial.print('.');
  170.         delay(200);
  171.     }
  172.  
  173.     //statWiFi();
  174.          
  175.     WiFi.setAutoReconnect(true);
  176.     WiFi.persistent(true);
  177. }
  178.  
  179. // ------------------------------------------------------------------------------------------------------------------- //
  180. // --- WiFiStat ------------------------------------------------------------------------------------------------------ //
  181. // ------------------------------------------------------------------------------------------------------------------- //
  182.  
  183. void WiFiStat()
  184. {
  185.     switch (WiFi.status())
  186.     {
  187.         case WL_NO_SSID_AVAIL:
  188.                               #if SERIAL
  189.                                   #if ENGLISH
  190.                                       Serial.printf("\n[ESP] [WiFI] Configured SSID cannot be reached");
  191.                                   #endif
  192.                                   #if POLISH
  193.                                       Serial.printf("\n[ESP] [WiFI] Nie można osiągnąć skonfigurowanego identyfikatora SSID");
  194.                                   #endif                                  
  195.                               #endif        
  196.                               break;
  197.                              
  198.         case WL_CONNECTED:
  199.                               #if SERIAL
  200.                                   #if ENGLISH
  201.                                       Serial.printf("\n[ESP] [WiFI] Connection successfully established");
  202.                                       Serial.printf("\n[ESP] [WiFI] Your IP : %s", WiFi.localIP().toString().c_str());
  203.                                       Serial.printf("\n[ESP] [WiFI] Received Signal Strength Indication : %d", WiFi.RSSI());
  204.                                   #endif
  205.                                   #if POLISH
  206.                                       Serial.printf("\n[ESP] [WiFI] Pomyślnie nawiązano połączenie");
  207.                                       Serial.printf("\n[ESP] [WiFI] Twój adres IP : %s", WiFi.localIP().toString().c_str());
  208.                                       Serial.printf("\n[ESP] [WiFI] Wskazanie siły odbieranego sygnału : %d", WiFi.RSSI());
  209.                                   #endif                                
  210.                               #endif
  211.                               break;
  212.                              
  213.         case WL_CONNECT_FAILED:
  214.                               #if SERIAL
  215.                                   #if ENGLISH
  216.                                       Serial.printf("\n[ESP] [WiFI] Connection failed");
  217.                                   #endif
  218.                                   #if POLISH
  219.                                       Serial.printf("\n[ESP] [WiFI] Połączenie nieudane");
  220.                                   #endif                                  
  221.                               #endif
  222.                               break;
  223.       }      
  224. }
  225.  
  226. // ------------------------------------------------------------------------------------------------------------------- //
  227. // --- reconect Wi-Fi ------------------------------------------------------------------------------------------------ //
  228. // ------------------------------------------------------------------------------------------------------------------- //
  229.  
  230. void WiFiReconect()
  231. {
  232.     unsigned long currentMillis = millis();
  233.      
  234.     if ((WiFi.status() != WL_CONNECTED) && (currentMillis - previousMillis >=interval))     // if no WiFi connect now
  235.     {
  236.         #if SERIAL
  237.             #if ENGLISH
  238.                 Serial.printf("\n[ESP] [WiFI] Reconnecting to WiFi...");
  239.             #endif
  240.             #if POLISH
  241.                 Serial.printf("\n[ESP] [WiFI] Ponowne łączenie z Wi-Fi...");
  242.             #endif
  243.         #endif
  244.          
  245.         WiFi.disconnect();
  246.         WiFi.begin(ssid, pass);
  247.          
  248.         WiFiStat();                                       // show WiFi Status
  249.          
  250.         previousMillis = currentMillis;
  251.     }
  252. }
  253.  
  254. // ------------------------------------------------------------------------------------------------------------------- //
  255. // ---N T P   I N I T ------------------------------------------------------------------------------------------------ //
  256. // ------------------------------------------------------------------------------------------------------------------- //
  257.  
  258. int NTPInit()
  259. {
  260.     timeClient.update();
  261.  
  262.     if (timeClient.updated())
  263.     {
  264.         #if SERIAL
  265.             DrawLine(60,'*');
  266.             #if ENGLISH
  267.                 Serial.printf("\n[ESP] [NTP] Time Update :-)");
  268.                 Serial.printf("\n[ESP] [NTP] Using NTP Server : %s", timeClient.getPoolServerName().c_str());
  269.             #endif
  270.             #if POLISH
  271.                 Serial.printf("\n[ESP] [NTP] Czas został zaktualizowany :-)");
  272.                 Serial.printf("\n[ESP] [NTP] Używasz Serwera NTP : %s", timeClient.getPoolServerName().c_str());
  273.             #endif
  274.         #endif
  275.  
  276.         return 1;                     // return 1 if time is update
  277.     }
  278.     else
  279.     {
  280.         #if SERIAL
  281.             #if ENGLISH
  282.                 Serial.printf("\n[ESP] [NTP] Time in not Update !!!");
  283.             #endif
  284.             #if POLISH
  285.                 Serial.printf("\n[ESP] [NTP] Czas nie został zaktualizowany !!!");
  286.             #endif
  287.         #endif
  288.  
  289.         return 0;                     // return 0 if time is not update
  290.     }
  291. }
  292.  
  293. // ------------------------------------------------------------------------------------------------------------------- //
  294. // --- S H O W   N T P ----------------------------------------------------------------------------------------------- //
  295. // ------------------------------------------------------------------------------------------------------------------- //
  296.  
  297. void Show_NTP()
  298. {
  299.     #if SERIAL
  300.         DrawLine(60,'*');
  301.         #if POLISH
  302.             Serial.printf("\n[ESP] [TIME] UTC Czas : %s", timeClient.getFormattedUTCTime().c_str());
  303.             Serial.printf("\n[ESP] [TIME] UTC Data : %d/%d/%d", timeClient.getUTCDay(),timeClient.getUTCMonth(),timeClient.getUTCYear());
  304.             Serial.printf("\n[ESP] [TIME] LOC Czas : %s", timeClient.getFormattedTime().c_str());
  305.             Serial.printf("\n[ESP] [TIME] LOC Data : %d/%d/%d", timeClient.getDay(),timeClient.getMonth(),timeClient.getYear());
  306.         #endif
  307.         #if ENGLISH
  308.             Serial.printf("\n[ESP] [TIME] UTC Time : %s", timeClient.getFormattedUTCTime().c_str());
  309.             Serial.printf("\n[ESP] [TIME] UTC Data : %d/%d/%d", timeClient.getUTCDay(),timeClient.getUTCMonth(),timeClient.getUTCYear());
  310.             Serial.printf("\n[ESP] [TIME] LOC Time : %s", timeClient.getFormattedTime().c_str());
  311.             Serial.printf("\n[ESP] [TIME] LOC Data : %d/%d/%d", timeClient.getDay(),timeClient.getMonth(),timeClient.getYear());
  312.         #endif
  313.     #endif
  314. }
  315.  
  316. // ------------------------------------------------------------------------------------------------------------------- //
  317. // --- U P D A T E   M Y N T P   S T R U C T U R E ------------------------------------------------------------------- //
  318. // ------------------------------------------------------------------------------------------------------------------- //
  319.  
  320. void MyNTP_Update()
  321. {
  322.     timeClient.update();
  323.    
  324.     // *** U T C   T I M E   &   D A T A *** ///
  325.    
  326.     MyNTP.iUTCYear      = timeClient.getUTCYear();                                    
  327.     MyNTP.iUTCMonth     = timeClient.getUTCMonth();                                    
  328.     MyNTP.sUTCMonth     = timeClient.getUTCMonthStr();
  329.     MyNTP.sUTCDoW       = timeClient.getUTCDoW();
  330.     MyNTP.iUTCDay       = timeClient.getUTCDay();
  331.     MyNTP.iUTCHours     = timeClient.getUTCHours();
  332.     MyNTP.sUTCHours     = timeClient.getUTCStrHours();
  333.     MyNTP.iUTCMinutes   = timeClient.getUTCMinutes();
  334.     MyNTP.sUTCMinutes   = timeClient.getUTCStrMinutes();
  335.     MyNTP.iUTCSeconds   = timeClient.getUTCSeconds();
  336.     MyNTP.sUTCSeconds   = timeClient.getUTCStrSeconds();
  337.  
  338.     // *** L O C   T I M E   &   D A T A *** ///
  339.  
  340.     MyNTP.iLOCYear      = timeClient.getYear();
  341.     MyNTP.sLOCMonth     = timeClient.getMonthStr();
  342.     MyNTP.iLOCMonth     = timeClient.getMonth();
  343.     MyNTP.sLOCWeekDay   = timeClient.getDoW();
  344.     MyNTP.iLOCDay       = timeClient.getDay();
  345.     MyNTP.iLOCHours     = timeClient.getHours();
  346.     MyNTP.sLOCHours     = timeClient.getStrHours();
  347.     MyNTP.iLOCMinutes   = timeClient.getMinutes();
  348.     MyNTP.sLOCMinutes   = timeClient.getStrMinutes();
  349.     MyNTP.iLOCSeconds   = timeClient.getSeconds();
  350.     MyNTP.sLOCSeconds   = timeClient.getStrSeconds();
  351. }
  352.  
  353. // ------------------------------------------------------------------------------------------------------------------- //
  354. // --- M A I N   S E T U P ------------------------------------------------------------------------------------------- //
  355. // ------------------------------------------------------------------------------------------------------------------- //
  356.  
  357. void setup()
  358. {
  359.     SerialInit();                                         // check and init serial monitor
  360.     WiFiInit();                                           // start WiFi Connection
  361.     WiFiStat();                                           // show WiFi Status
  362.     WiFiReconect();                                       // check WiFi Connection and conect if loss !!!
  363.  
  364.     timeClient.begin();                                   // start timeClient
  365.  
  366.     DrawLine(60,'*');                                     // Draw separator *
  367.     if ( WiFi.status() == WL_CONNECTED )                  // if WiFi is OK
  368.     {
  369.         while(!NTPInit());                                // init NTP server                
  370.         Show_NTP();                                       // show NTP time & data
  371.         MyNTP_Update();                                   // copy Time & Data to structure MyNTP
  372.     }
  373. }
  374.  
  375. // ------------------------------------------------------------------------------------------------------------------- //
  376. // --- M A I N   L O O P --------------------------------------------------------------------------------------------- //
  377. // ------------------------------------------------------------------------------------------------------------------- //
  378.  
  379. void loop()
  380. {
  381.  
  382.  
  383. }
  384.  
  385. // ------------------------------------------------------------------------------------------------------------------- //
Add Comment
Please, Sign In to add comment