Advertisement
learnelectronics

ESP32 NTP Example w/ OLED

Jul 30th, 2017
4,752
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.70 KB | None | 0 0
  1. /*
  2.  
  3.  Udp NTP Client
  4.  
  5.  Get the time from a Network Time Protocol (NTP) time server
  6.  Demonstrates use of UDP sendPacket and ReceivePacket
  7.  For more on NTP time servers and the messages needed to communicate with them,
  8.  see http://en.wikipedia.org/wiki/Network_Time_Protocol
  9.  
  10.  created 4 Sep 2010
  11.  by Michael Margolis
  12.  modified 9 Apr 2012
  13.  by Tom Igoe
  14.  
  15.  This code is in the public domain.
  16.  
  17.  */
  18.  
  19. #include <SPI.h>
  20. #include <WiFi.h>
  21. #include <WiFiUdp.h>
  22. #include <Adafruit_SSD1306.h>
  23. #define OLED_RESET 4
  24. Adafruit_SSD1306 display(OLED_RESET);
  25.  
  26. int status = WL_IDLE_STATUS;
  27. char ssid[] = "ATT6s3ENT4";  //  your network SSID (name)
  28. char pass[] = "69z5f2s4+e4p";       // your network password
  29. int keyIndex = 0;            // your network key Index number (needed only for WEP)
  30.  
  31. unsigned int localPort = 2390;      // local port to listen for UDP packets
  32.  
  33. IPAddress timeServer(129, 6, 15, 28); // time.nist.gov NTP server
  34.  
  35. const int NTP_PACKET_SIZE = 48; // NTP time stamp is in the first 48 bytes of the message
  36.  
  37. byte packetBuffer[ NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets
  38.  
  39. // A UDP instance to let us send and receive packets over UDP
  40. WiFiUDP Udp;
  41.  
  42. void setup()
  43. {
  44.   // Open serial communications and wait for port to open:
  45.   Serial.begin(9600);
  46.   while (!Serial) {
  47.     ; // wait for serial port to connect. Needed for native USB port only
  48.   }
  49.  
  50. display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  51. display.clearDisplay();
  52. display.setTextSize(1);
  53. display.setTextColor(WHITE);
  54. display.setCursor(0,0);
  55.  
  56.  
  57.  
  58.   // attempt to connect to Wifi network:
  59.   while ( status != WL_CONNECTED) {
  60.     display.print("Attempting to connect to SSID: ");
  61.     display.println(ssid);
  62.     display.display();
  63.     // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
  64.     status = WiFi.begin(ssid, pass);
  65.  
  66.     // wait 10 seconds for connection:
  67.     delay(10000);
  68.   }
  69.  
  70.   display.println("Connected to wifi");
  71.   printWifiStatus();
  72.  
  73.   display.println("\nStarting connection to server...");
  74.   Udp.begin(localPort);
  75.   display.display();
  76. }
  77.  
  78. void loop()
  79. {
  80.   sendNTPpacket(timeServer); // send an NTP packet to a time server
  81.   // wait to see if a reply is available
  82.   delay(1000);
  83.   if ( Udp.parsePacket() ) {
  84.     display.println("packet received");
  85.     display.display();
  86.     // We've received a packet, read the data from it
  87.     Udp.read(packetBuffer, NTP_PACKET_SIZE); // read the packet into the buffer
  88.  
  89.     //the timestamp starts at byte 40 of the received packet and is four bytes,
  90.     // or two words, long. First, esxtract the two words:
  91.  
  92.     unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
  93.     unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
  94.     // combine the four bytes (two words) into a long integer
  95.     // this is NTP time (seconds since Jan 1 1900):
  96.     unsigned long secsSince1900 = highWord << 16 | lowWord;
  97.     display.print("Seconds since Jan 1 1900 = " );
  98.     display.println(secsSince1900);
  99.     display.display();
  100.  
  101.     // now convert NTP time into everyday time:
  102.     display.print("Unix time = ");
  103.     // Unix time starts on Jan 1 1970. In seconds, that's 2208988800:
  104.     const unsigned long seventyYears = 2208988800UL;
  105.     // subtract seventy years:
  106.     unsigned long epoch = secsSince1900 - seventyYears;
  107.     // print Unix time:
  108.     display.println(epoch);
  109.     display.display();
  110.  
  111.  
  112.     // print the hour, minute and second:
  113.     display.print("The UTC time is ");       // UTC is the time at Greenwich Meridian (GMT)
  114.     display.print((epoch  % 86400L) / 3600); // print the hour (86400 equals secs per day)
  115.     display.print(':');
  116.     if ( ((epoch % 3600) / 60) < 10 ) {
  117.       // In the first 10 minutes of each hour, we'll want a leading '0'
  118.       display.print('0');
  119.     }
  120.     display.print((epoch  % 3600) / 60); // print the minute (3600 equals secs per minute)
  121.     display.print(':');
  122.     if ( (epoch % 60) < 10 ) {
  123.       // In the first 10 seconds of each minute, we'll want a leading '0'
  124.       display.print('0');
  125.     }
  126.     display.println(epoch % 60); // print the second
  127.     display.display();
  128.   }
  129.   // wait ten seconds before asking for the time again
  130.   delay(10000);
  131. }
  132.  
  133. // send an NTP request to the time server at the given address
  134. unsigned long sendNTPpacket(IPAddress& address)
  135. {
  136.   //Serial.println("1");
  137.   // set all bytes in the buffer to 0
  138.   memset(packetBuffer, 0, NTP_PACKET_SIZE);
  139.   // Initialize values needed to form NTP request
  140.   // (see URL above for details on the packets)
  141.   //Serial.println("2");
  142.   packetBuffer[0] = 0b11100011;   // LI, Version, Mode
  143.   packetBuffer[1] = 0;     // Stratum, or type of clock
  144.   packetBuffer[2] = 6;     // Polling Interval
  145.   packetBuffer[3] = 0xEC;  // Peer Clock Precision
  146.   // 8 bytes of zero for Root Delay & Root Dispersion
  147.   packetBuffer[12]  = 49;
  148.   packetBuffer[13]  = 0x4E;
  149.   packetBuffer[14]  = 49;
  150.   packetBuffer[15]  = 52;
  151.  
  152.   //Serial.println("3");
  153.  
  154.   // all NTP fields have been given values, now
  155.   // you can send a packet requesting a timestamp:
  156.   Udp.beginPacket(address, 123); //NTP requests are to port 123
  157.   //Serial.println("4");
  158.   Udp.write(packetBuffer, NTP_PACKET_SIZE);
  159.   //Serial.println("5");
  160.   Udp.endPacket();
  161.   //Serial.println("6");
  162. }
  163.  
  164.  
  165. void printWifiStatus() {
  166.   // print the SSID of the network you're attached to:
  167.   display.print("SSID: ");
  168.   display.println(WiFi.SSID());
  169.  
  170.   // print your WiFi shield's IP address:
  171.   IPAddress ip = WiFi.localIP();
  172.   display.print("IP Address: ");
  173.   display.println(ip);
  174.  
  175.   // print the received signal strength:
  176.   long rssi = WiFi.RSSI();
  177.   display.print("signal strength (RSSI):");
  178.   display.print(rssi);
  179.   display.println(" dBm");
  180.   display.display();
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement