#include <NTPClient.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <WiFiUdp.h>
#include <ESP8266WebServer.h> // Include the WebServer library
ESP8266WiFiMulti wifiMulti; // Create an instance of the ESP8266WiFiMulti class, called 'wifiMulti'
WiFiUDP ntpUDP; // Create an instance of the WiFiUDP class to send and receive
// You can specify the time server pool and the offset, (in seconds)
// additionaly you can specify the update interval (in milliseconds).
NTPClient timeClient(ntpUDP, "se.pool.ntp.org", 0, 60000);
void setup()
{
Serial.begin(115200); // Start the Serial communication to send messages to the computer
delay(10);
Serial.println("\r\n");
startWiFi(); // Try to connect to some given access points. Then wait for a connection
timeClient.begin();
}
char buffer[200];
void loop()
{
timeClient.update();
uint32_t actualTime = timeClient.getEpochTime();
}
void startWiFi()
{ // Try to connect to some given access points. Then wait for a connection
wifiMulti.addAP("*******", "********"); // add Wi-Fi networks you want to connect to
Serial.println("Connecting");
while (wifiMulti.run() != WL_CONNECTED)
{ // Wait for the Wi-Fi to connect
delay(250);
Serial.print('.');
}
Serial.println("\r\n");
Serial.print("Connected to ");
Serial.println(WiFi.SSID()); // Tell us what network we're connected to
Serial.print("IP address:\t");
Serial.print(WiFi.localIP()); // Send the IP address of the ESP8266 to the computer
Serial.println("\r\n");
}
Comments