#include <WiFi.h>
#include <esp_wifi.h>
#include "esp_netif.h"
#include "time.h"
#define NTP_SERVER "time.windows.com"
#define UTC_OFFSET 0
#define UTC_OFFSET_DST 0
bool button = false;
bool printTime = false;
hw_timer_t *My_timer = NULL;
void IRAM_ATTR isr() {
button = true;
return;
}
void IRAM_ATTR onTimer(){
printTime = true;
}
void setup() {
Serial.begin(115000);
esp_err_t esp_netif_sntp_init();
My_timer = timerBegin(0, 80, true);
timerAttachInterrupt(My_timer, &onTimer, true);
timerAlarmWrite(My_timer, 1000000, true);
timerAlarmEnable(My_timer); //Just Enable
pinMode(18, INPUT_PULLUP);
attachInterrupt(18, isr, FALLING);
Serial.print("Connected to: ");
Serial.println(WiFi.localIP());
}
void loop() {
struct tm timeinfo;
if(button == true){
Serial.println("Connecting...");
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(25);
configTime(UTC_OFFSET, UTC_OFFSET_DST, NTP_SERVER);
setenv("TZ", "CET-2", 1);
//setenv("TZ", "CST-8", 1);
tzset();
getLocalTime(&timeinfo);
Serial.println(&timeinfo, "%H:%M:%S");
button = false;
}
}
//WiFi.disconnect();
if(printTime == true){
getLocalTime(&timeinfo);
Serial.println(&timeinfo, "%H:%M:%S");
printTime == false;
}
}