esp32indicator/esp32indicator.ino
2021-05-28 15:20:06 +02:00

77 lines
1.4 KiB
C++

#include <WiFi.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
const char* ssid = "Your WiFi SSID";
const char* password = "Your WiFi Password";
const int notify_hour = 20;
const int notify_minute = 0;
const int local_time_offset = 2 * 3600;
const int led = 1;
bool flashing = false;
bool skip = false;
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);
bool check_time() {
while(!timeClient.update()) {
timeClient.forceUpdate();
}
int hours = timeClient.getHours();
int minutes = timeClient.getMinutes();
if (skip && hours < notify_hour) skip = false;
if (!skip && (hours > notify_hour || (hours == notify_hour && minutes >= notify_minute))) {
return true;
}
return false;
}
void setup() {
// Serial.begin(115200);
pinMode(led, OUTPUT);
digitalWrite(led, HIGH);
// 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.print("IP address: ");
Serial.println(WiFi.localIP()); */
timeClient.begin();
timeClient.setTimeOffset(local_time_offset);
if (check_time()) skip = true;
}
void loop() {
if (!flashing) {
flashing = check_time();
if (!flashing) delay(30000);
return;
}
if (!skip) {
digitalWrite(led, LOW);
delay(500);
digitalWrite(led, HIGH);
delay(500);
}
}