Advertisement
uwezi

20210712_ntp

Jul 12th, 2021
1,069
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <NTPClient.h>
  2. #include <ESP8266WiFi.h>
  3. #include <ESP8266WiFiMulti.h>
  4. #include <WiFiUdp.h>
  5. #include <ESP8266WebServer.h>   // Include the WebServer library
  6.  
  7. ESP8266WiFiMulti wifiMulti;      // Create an instance of the ESP8266WiFiMulti class, called 'wifiMulti'
  8.  
  9. WiFiUDP ntpUDP;                  // Create an instance of the WiFiUDP class to send and receive
  10.  
  11. // You can specify the time server pool and the offset, (in seconds)
  12. // additionaly you can specify the update interval (in milliseconds).
  13. NTPClient timeClient(ntpUDP, "se.pool.ntp.org", 0, 60000);
  14.  
  15. void setup()
  16. {
  17.   Serial.begin(115200);          // Start the Serial communication to send messages to the computer
  18.   delay(10);
  19.   Serial.println("\r\n");
  20.  
  21.   startWiFi();                   // Try to connect to some given access points. Then wait for a connection
  22.  
  23.   timeClient.begin();
  24. }
  25.  
  26. char buffer[200];
  27.  
  28. void loop()
  29. {
  30.   timeClient.update();
  31.  
  32.   uint32_t actualTime = timeClient.getEpochTime();
  33. }
  34.  
  35. void startWiFi()
  36. { // Try to connect to some given access points. Then wait for a connection
  37.  
  38.   wifiMulti.addAP("*******", "********");   // add Wi-Fi networks you want to connect to
  39.  
  40.   Serial.println("Connecting");
  41.   while (wifiMulti.run() != WL_CONNECTED)
  42.   {  // Wait for the Wi-Fi to connect
  43.     delay(250);
  44.     Serial.print('.');
  45.   }
  46.   Serial.println("\r\n");
  47.   Serial.print("Connected to ");
  48.   Serial.println(WiFi.SSID());             // Tell us what network we're connected to
  49.   Serial.print("IP address:\t");
  50.   Serial.print(WiFi.localIP());            // Send the IP address of the ESP8266 to the computer
  51.   Serial.println("\r\n");
  52. }
  53.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement