diff --git a/src/main.cpp b/src/main.cpp index 9fa17b4..a44e87c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -118,6 +118,7 @@ void setup() { Serial.print(" Alarm time: "); Serial.println(strftime_buf); if (-seconds > secondsToSleep) suspend(); + else if (-seconds > 5*60) suspend(5*60); } u8g2.begin(); @@ -160,6 +161,7 @@ void setup() { mp3.begin(); led.setup(); + lastButtonPress = millis(); rotary.registerCallback(rotation); rotary.begin(rotaryPinA, rotaryPinB, rotaryPinButton); } @@ -205,9 +207,18 @@ void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties void suspend(uint32_t secondsToSleep) { DEBUG_PRINTLN("Entering deep sleep"); + + mp3.stop(); + led.stop(); + delay(1000); + u8g2.setPowerSave(1); + + //properly disconnect from the MQTT broker + iot.mqtt.disconnect(); + esp_sleep_enable_timer_wakeup(1000000LL * secondsToSleep); - const uint64_t ext_wakeup_rotarybtn_mask = 1ULL << rotaryPinButton; - esp_sleep_enable_ext1_wakeup(ext_wakeup_rotarybtn_mask, ESP_EXT1_WAKEUP_ANY_HIGH); + const uint64_t ext_wakeup_rotarybtn_mask = 1ULL << rotaryPinButton; + esp_sleep_enable_ext1_wakeup(ext_wakeup_rotarybtn_mask, ESP_EXT1_WAKEUP_ANY_HIGH); ////esp_sleep_enable_ext0_wakeup((gpio_num_t)sensorPin, 0); // const uint64_t ext_wakeup_pin_1_mask = 1ULL << ext_wakeup_pin_1; @@ -215,14 +226,17 @@ void suspend(uint32_t secondsToSleep) { //// esp_sleep_enable_ext1_wakeup(ext_wakeup_pin_1_mask | ext_wakeup_pin_2_mask, ESP_EXT1_WAKEUP_ANY_HIGH); // ////esp_deep_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_OFF); // power down all peripherals - //properly disconnect from the MQTT broker - iot.mqtt.disconnect(); + //send the ESP into deep sleep esp_deep_sleep_start(); } void suspend() { - suspend(secondsToSleep); + time_t now; + time(&now); + double seconds = difftime(now, mktime(&alarmTime)); + if (-seconds > secondsToSleep) suspend(secondsToSleep); + else if (-seconds > 5*60) suspend(5*60); } void setAlarmTime(struct tm time) { @@ -284,13 +298,17 @@ void loop() led.wakeUpLight(0); Serial.println("WAKEUP TIME!!!!!"); mp3.setVolume(2); - mp3.start(); + mp3.start("http://radioessen.cast.addradio.de/radioessen/simulcast/high/stream.mp3"); led.changeAnimation(2, 0); } - if (seconds >= -5*60 && seconds <= 0) { + if (alarmArmed && seconds >= -5*60 && seconds <= 0) { led.wakeUpLight(255*(seconds+300)/300); } + bool stayAwake = false; + if (alarmArmed && seconds >= -2*5*60) stayAwake = true; + if (!mp3.playing && millis() - lastButtonPress >= 60*1000 && !stayAwake) suspend(); + strftime(timeStr, sizeof(timeStr), "%H:%M:%S", &timeinfo); sprintf(weatherStr, "%.1f°C %.1f%% %.0fhPa", bme280.readTemperature(), bme280.readHumidity(), bme280.readPressure()); diff --git a/src/mp3.cpp b/src/mp3.cpp index 3fe4582..8ed9939 100644 --- a/src/mp3.cpp +++ b/src/mp3.cpp @@ -95,7 +95,7 @@ void MP3::mp3_decoder_task(void *pvParameters) { AudioOutputI2S *out = nullptr; - strcpy(titleStr, "loading..."); +// strcpy(titleStr, "loading..."); file = new AudioFileSourceICYStream(URL); file->RegisterMetadataCB(MDCallback, (void*)"ICY"); buff = new AudioFileSourceBuffer(file, preallocateBuffer, preallocateBufferSize); diff --git a/src/screen.cpp b/src/screen.cpp index 5e87467..6dda9d9 100644 --- a/src/screen.cpp +++ b/src/screen.cpp @@ -133,50 +133,62 @@ uint8_t StationMenu::select() { switch (current_pos) { case 0: mp3.start("http://streaming.shoutcast.com/80sPlanet?lang=en-US"); + strcpy(titleStr, string_list[0].c_str()); led.changeAnimation(2, 0); break; case 1: mp3.start("http://ice1.somafm.com/seventies-128-mp3"); + strcpy(titleStr, string_list[1].c_str()); led.changeAnimation(2, 0); break; case 2: mp3.start("http://streaming.radionomy.com/laradiostrong?lang=de"); + strcpy(titleStr, string_list[2].c_str()); led.changeAnimation(2, 0); break; case 3: mp3.start("http://184.171.163.20:8162/stream"); + strcpy(titleStr, string_list[3].c_str()); led.changeAnimation(2, 0); break; case 4: mp3.start("http://212.83.129.92:8028/;?type=http&nocache=683"); + strcpy(titleStr, string_list[4].c_str()); led.changeAnimation(2, 0); break; case 5: mp3.start("http://192.99.4.210:3574/stream"); + strcpy(titleStr, string_list[5].c_str()); led.changeAnimation(2, 0); break; case 6: mp3.start("http://188.165.192.5:8279/stream?icy=http"); + strcpy(titleStr, string_list[6].c_str()); led.changeAnimation(2, 0); break; case 7: mp3.start("http://69.167.190.234:8080/stream"); + strcpy(titleStr, string_list[7].c_str()); led.changeAnimation(2, 0); break; case 8: mp3.start("http://192.240.102.133:11790/stream"); + strcpy(titleStr, string_list[8].c_str()); led.changeAnimation(2, 0); break; case 9: mp3.start("http://192.99.41.102:5044/stream"); + strcpy(titleStr, string_list[9].c_str()); led.changeAnimation(2, 0); break; case 10: mp3.start("http://radioessen.cast.addradio.de/radioessen/simulcast/high/stream.mp3"); + strcpy(titleStr, string_list[10].c_str()); led.changeAnimation(2, 0); break; case 11: mp3.start("http://172.16.75.17:8000/mopidy"); + strcpy(titleStr, string_list[11].c_str()); led.changeAnimation(2, 0); break; case 12: @@ -357,9 +369,5 @@ uint8_t AlarmClockScreen::select() { } void SuspendScreen::draw() { - mp3.stop(); - led.stop(); - delay(1000); - u8g2.setPowerSave(1); suspend(30); }