From cd6afd7cad53107069233adb208863393688de7d Mon Sep 17 00:00:00 2001 From: Elena ``of Valhalla'' Grandi Date: Sun, 30 Apr 2023 15:37:01 +0200 Subject: New version of the clock on an ESP32: start getting time from ntp --- arduino_sketch/fuzzy_alarm_clock_esp32/.gitignore | 1 + arduino_sketch/fuzzy_alarm_clock_esp32/config.h | 15 +++++++ .../fuzzy_alarm_clock_esp32.ino | 52 ++++++++++++++++++++++ 3 files changed, 68 insertions(+) create mode 100644 arduino_sketch/fuzzy_alarm_clock_esp32/.gitignore create mode 100644 arduino_sketch/fuzzy_alarm_clock_esp32/config.h create mode 100644 arduino_sketch/fuzzy_alarm_clock_esp32/fuzzy_alarm_clock_esp32.ino diff --git a/arduino_sketch/fuzzy_alarm_clock_esp32/.gitignore b/arduino_sketch/fuzzy_alarm_clock_esp32/.gitignore new file mode 100644 index 0000000..b6bf14b --- /dev/null +++ b/arduino_sketch/fuzzy_alarm_clock_esp32/.gitignore @@ -0,0 +1 @@ +local_config.h diff --git a/arduino_sketch/fuzzy_alarm_clock_esp32/config.h b/arduino_sketch/fuzzy_alarm_clock_esp32/config.h new file mode 100644 index 0000000..1e505b2 --- /dev/null +++ b/arduino_sketch/fuzzy_alarm_clock_esp32/config.h @@ -0,0 +1,15 @@ +#define WIFI_SSID "" +#define WIFI_PASS "" + +#define DEBUG 1 + +#define NTP_SERV_1 "pool.ntp.org" +#define NTP_SERV_2 "time.nist.gov" + +// TimeZone rule for Europe/Rome including daylight adjustment rules (optional) +// See https://github.com/esp8266/Arduino/blob/master/cores/esp8266/TZ.h +#define TZ "CET-1CEST,M3.5.0,M10.5.0/3" + +// Create the file local_config.h in this directory and override all of the +// #defines you need +#include "local_config.h" diff --git a/arduino_sketch/fuzzy_alarm_clock_esp32/fuzzy_alarm_clock_esp32.ino b/arduino_sketch/fuzzy_alarm_clock_esp32/fuzzy_alarm_clock_esp32.ino new file mode 100644 index 0000000..297b938 --- /dev/null +++ b/arduino_sketch/fuzzy_alarm_clock_esp32/fuzzy_alarm_clock_esp32.ino @@ -0,0 +1,52 @@ +// Create a file local_config.h with your network configuration and secrets (it +// is ignored by git); see config.h for details of what it expects. +#include "config.h" + +#include + +#include "time.h" +#include "sntp.h" + +WiFiClient wifiClient; + +void connect_wifi() { + WiFi.begin(WIFI_SSID, WIFI_PASS); + Serial.print("WiFi "); + char count = 0; + while (WiFi.status() != WL_CONNECTED) { + delay(500); +#ifdef DEBUG + Serial.print(WiFi.status()); +#endif + Serial.print("."); + count ++; + if (count > 10) { + Serial.println(" timeout"); + return; + } + } + Serial.println(" connected"); +} + +void setup() { + Serial.begin(115200); +#if defined(DEBUG) + Serial.setDebugOutput(true); +#endif + + configTzTime(TZ, NTP_SERV_1, NTP_SERV_2); + + connect_wifi(); + +} + +void loop() { + delay(5000); + struct tm timeinfo; + if(!getLocalTime(&timeinfo)){ + Serial.println("No time available (yet)"); + return; + } + Serial.print("Time is: "); + Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S"); +} -- cgit v1.2.3