Code: Select all
#include <WiFi.h>
#include <esp_sleep.h>
const char* ssid = "XXX";
const char* password = "XXXXX";
/* Time Stamp */
#include <NTPClient.h>
#include <WiFiUdp.h>
#define NTP_OFFSET -3 * 60 * 60 // In seconds
#define NTP_INTERVAL 60 * 1000 // In miliseconds
#define NTP_ADDRESS "0.pool.ntp.org"
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, NTP_ADDRESS, NTP_OFFSET, NTP_INTERVAL);
void setup()
{
Serial.begin(115200);
connectWiFi();
timeClient.begin();
}
void loop()
{
Serial.println("light_sleep_enter");
esp_sleep_enable_timer_wakeup(2000000); //2 seconds
int ret = esp_light_sleep_start();
Serial.printf("light_sleep: %d\n", ret);
timeClient.update();
String formattedTime = timeClient.getFormattedTime();
Serial.println(formattedTime);
}
void connectWiFi(void)
{
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}