1+ // This arduino sketch for ESP32 announces the time every hour.
2+ // A connection to the Internet is required
3+ // 1) to synchronize with the internal RTC on startup
4+ // 2) so that GoogleTTS can be reached
5+
6+
7+
8+ #include < Arduino.h>
9+ #include " WiFiMulti.h"
10+ #include " Audio.h"
11+ #include " time.h"
12+ #include " esp_sntp.h"
13+
14+ // ------------------------USER SETTINGS / GPIOs-------------------------------------------------------------------------
15+ String ssid = " xxxx" ;
16+ String password = " xxxx" ;
17+ uint8_t I2S_BCLK = 27 ;
18+ uint8_t I2S_LRC = 26 ;
19+ uint8_t I2S_DOUT = 25 ;
20+ // ------------------------OBJECTS /GLOBAL VARS--------------------------------------------------------------------------
21+ Audio audio (false , 3 , 0 );
22+ WiFiMulti wifiMulti;
23+ uint32_t sec1 = millis();
24+ String time_s = " " ;
25+ char chbuf[200 ];
26+ int timeIdx = 0 ;
27+ // ------------------------TIME / SNTP STUFF-----------------------------------------------------------------------------
28+ #define TZName " CET-1CEST,M3.5.0,M10.5.0/3" // https://remotemonitoringsystems.ca/time-zone-abbreviations.php
29+ char strftime_buf[64 ];
30+ struct tm timeinfo;
31+ time_t now;
32+ boolean obtain_time (){
33+ time_t now = 0 ;
34+ int retry = 0 ;
35+ Serial.println (" Initializing SNTP" );
36+ sntp_setoperatingmode (SNTP_OPMODE_POLL);
37+ sntp_setservername (0 , " pool.ntp.org" );
38+ sntp_init ();
39+
40+ const int retry_count = 10 ;
41+ while (timeinfo.tm_year < (2016 - 1900 ) && ++retry < retry_count) {
42+ Serial.printf (" Waiting for system time to be set... (%d/%d)\n " , retry, retry_count);
43+ vTaskDelay (uint16_t (2000 / portTICK_PERIOD_MS));
44+ time (&now);
45+ localtime_r (&now, &timeinfo);
46+ }
47+ setenv (" TZ" , TZName, 1 );
48+ tzset ();
49+ localtime_r (&now, &timeinfo);
50+ strftime (strftime_buf, sizeof (strftime_buf), " %c" , &timeinfo);
51+ if (retry < retry_count) return true ;
52+ else return false ;
53+ }
54+
55+ const char * gettime_s (){ // hh:mm:ss
56+ time (&now);
57+ localtime_r (&now, &timeinfo);
58+ sprintf (strftime_buf," %02d:%02d:%02d" , timeinfo.tm_hour , timeinfo.tm_min , timeinfo.tm_sec );
59+ return strftime_buf;
60+ }
61+ // -----------------------SETUP------------------------------------------------------------------------------------------
62+ void setup () {
63+ Serial.begin (115200 );
64+ wifiMulti.addAP (ssid.c_str (), password.c_str ());
65+ wifiMulti.run ();
66+ obtain_time ();
67+ audio.setPinout (I2S_BCLK, I2S_LRC, I2S_DOUT);
68+ audio.setVolume (15 );
69+ }
70+ // -----------------------LOOP-------------------------------------------------------------------------------------------
71+ void loop () {
72+ audio.loop ();
73+ if (sec1 < millis ()){ // every second
74+ sec1 = millis () + 1000 ;
75+ time_s = gettime_s ();
76+ Serial.println (time_s);
77+ if (time_s.endsWith (" 00:00" )){ // time announcement every full hour
78+ char am_pm[5 ] = " am." ;
79+ int h = time_s.substring (0 ,2 ).toInt ();
80+ if (h > 12 ){h -= 12 ; strcpy (am_pm," pm." );}
81+ sprintf (chbuf, " It is now %i%s and %i minutes" , h, am_pm, time_s.substring (3 ,5 ).toInt ());
82+ Serial.println (chbuf);
83+ audio.connecttospeech (chbuf, " en" );
84+ }
85+ }
86+ }
87+ // ------------------EVENTS----------------------------------------------------------------------------------------------
88+ void audio_info (const char *info){
89+ Serial.printf (" info: %s\n " , info);
90+ }
0 commit comments