#include <WiFi.h>
#include <time.h>
#include <ESP32Time.h>
#include "sntp.h"
//*************TIMER INTERRUPT*******************
volatile int interruptCounter;
int totalInterruptCounter;
hw_timer_t * timer = NULL;
portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED;
void IRAM_ATTR onTimer() {
portENTER_CRITICAL_ISR(&timerMux);
interruptCounter++;
portEXIT_CRITICAL_ISR(&timerMux);
}
//*************TIMER INTERRUPT*******************
ESP32Time rtc;
int mode=0;
int say=0;
int num=0;
int zaman =20;
int no = 0;
int ledState = LOW;
int durumNo = 0;
bool wifiDurum = true;
bool durum = true;
bool wifi_durum=true;
bool guncel = true;
// Global variables
char szMesg[40] = "";
char szTime[9]; // mm:ss\0
char szsecond[4]; // ss
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* ntpServer1 = "pool.ntp.org";
const char* ntpServer2 = "time.nist.gov";
const long gmtOffset_sec = 10800;
const int daylightOffset_sec = 0;
int period =10;
unsigned long time_now = 0;
unsigned long time_now_ = 0;
//**********************************************
void printLocalTime()
{
struct tm timeinfo;
if(!getLocalTime(&timeinfo)){
Serial.println("Zaman mevcut değil (henüz)");
return;
}
}
// Callback function (get's called when time adjusts via NTP)
void timeavailable(struct timeval *t)
{
Serial.println("zaman güncelleniyor!");
printLocalTime();
WiFi_off();
say = 0;
guncel = false;
}
//***************
void getsec(char *psz)
{
sprintf(psz, "%02d", rtc.getSecond());
}
//***********************
void getTime(char *psz)
{
sprintf(psz, "%02d%s%02d%s%02d", rtc.getHour(true),":", rtc.getMinute(), ":",rtc.getSecond());
}
//**********************************
void setup(void)
{
pinMode(2, OUTPUT);
digitalWrite(2, LOW); // LED YANAR
Serial.begin(115200);
rtc.setTime(0, 30, 21, 1, 1, 2020);
// set notification call-back function
sntp_set_time_sync_notification_cb( timeavailable );
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer1, ntpServer2);
//*************TIMER INTERRUPT*******************
timer = timerBegin(0, 80, true);
timerAttachInterrupt(timer, &onTimer, true);
timerAlarmWrite(timer, 1000000, true);
timerAlarmEnable(timer);
//*************TIMER INTERRUPT*******************
}
void loop(void)
{//********** loop ****************************
//static uint32_t lastTime = 0; // millis() memory
//static uint8_t display = 0; // current display mode
static bool flasher = false; // seconds passing flasher
if(millis() - time_now > 10000 and durum){
time_now = millis();
WiFi.begin(ssid, password);
durum = false;
Serial.println("--------------------------------------------- wifi bilgileri girildi");
}
//*****************************************
if(millis() - time_now_ > 60000){
time_now_ = millis();
WiFi_on();
Serial.println("60 saniye doldu");
}
//******************************
if (interruptCounter > 0) {//*************TIMER INTERRUPT*******************
portENTER_CRITICAL(&timerMux);
interruptCounter--;
portEXIT_CRITICAL(&timerMux);
//totalInterruptCounter++;
//Serial.println("kesme");
//Serial.print("An interrupt as occurred. Total number: ");
//Serial.println(totalInterruptCounter);
//*************************************************
getTime(szTime);
Serial.print("rtc saat :");
Serial.println(szTime);
//******************************************
//*******************************
if(WiFi.status() == WL_CONNECTED){
Serial.println("******************************** WiFi bağlandı....");
}else{
Serial.println("************************** bağlantı yok!..");
}
//*****************
say++;
Serial.print("say :");
Serial.println(say);
printLocalTime(); // it will take some time to sync time :)
//***********************************
//*******************************
if(ledState==HIGH){
ledState=LOW;
digitalWrite(2,ledState);
}else{
ledState=HIGH;
digitalWrite(2,ledState);
}
}//*************TIMER INTERRUPT*******************
}//********** loop ****************************
//*******************************
bool get_Local_Time(struct tm * info)
{
uint32_t start = millis();
time_t now;
while((millis()-start) <= 5000) {
time(&now);
localtime_r(&now, info);
if(info->tm_year > (2016 - 1900)){
return true;
}
delay(10);
}
return false;
}
//****************************
void WiFi_on(){
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer1, ntpServer2);
Serial.print("WiFi bağlanıyor...");
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
}
//********************
void WiFi_off(){
WiFi.disconnect(true);
WiFi.mode(WIFI_OFF);
Serial.println("WiFi kapatıldı");
Serial.flush();
}