Advertisement
ppoesel

ARDUINO_ESP32_NDP

Mar 29th, 2023
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | Help | 0 0
  1. #include <Arduino.h>
  2. #include <WiFi.h>
  3. #include "time.h"
  4.  
  5. // Replace with your network credentials
  6. // Set your Static IP address
  7. IPAddress local_IP(10, 0, 0, 110);
  8. // Set your Gateway IP address
  9. IPAddress gateway(10, 0, 0, 138);
  10. IPAddress subnet(255, 255, 255, 0);
  11. const char* ssid = "HOME-FRI";
  12. const char* password = "willy4711";
  13. const char* ntpServer = "pool.ntp.org";
  14. const long gmtOffset_sec = 3600;
  15. const int daylightOffset_sec = 3600;
  16.  
  17. time_t now;
  18.  
  19. void printLocalTime()
  20. {
  21. struct tm timeinfo;
  22. if(!getLocalTime(&timeinfo)){
  23. Serial.println("Failed to obtain time");
  24. return;
  25. }
  26. Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");
  27. time(&now);
  28. }
  29.  
  30. // Initialize WiFi
  31. void initWiFi() {
  32. WiFi.mode(WIFI_STA);
  33. WiFi.config(local_IP, gateway, subnet);
  34. WiFi.begin(ssid, password);
  35. Serial.print("Connecting to WiFi ..");
  36. while (WiFi.status() != WL_CONNECTED) {
  37. Serial.print('.');
  38. delay(1000);
  39. }
  40. Serial.println(WiFi.localIP());
  41. }
  42.  
  43.  
  44. void setup()
  45. {
  46. Serial.begin(115200);
  47.  
  48. //connect to WiFi
  49. initWiFi();
  50.  
  51. //init and get the time
  52. configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
  53. printLocalTime();
  54.  
  55. //disconnect WiFi as it's no longer needed
  56. WiFi.disconnect(true);
  57. WiFi.mode(WIFI_OFF);
  58. }
  59.  
  60. void loop()
  61. {
  62. delay(1000);
  63. printLocalTime();
  64. Serial.println(now);
  65. }
  66.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement