|
@ -50,15 +50,10 @@ uint8_t temprature_sens_read(); |
|
|
|
|
|
|
|
|
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ 16, /* clock=*/ 15, /* data=*/ 4); |
|
|
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ 16, /* clock=*/ 15, /* data=*/ 4); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
char timeStr[20]; |
|
|
|
|
|
char weatherStr[32]; |
|
|
|
|
|
char statusStr[32]; |
|
|
|
|
|
unsigned char udprecvbuf[1460]; |
|
|
unsigned char udprecvbuf[1460]; |
|
|
RTC_DATA_ATTR static int boot_count = 0; |
|
|
|
|
|
RTC_DATA_ATTR struct tm alarmTime; |
|
|
RTC_DATA_ATTR WEBRADIO_STATE_PERSISTANT state_persistant; |
|
|
RTC_DATA_ATTR bool alarmArmed = false; |
|
|
WEBRADIO_STATE state; |
|
|
RTC_DATA_ATTR bool displaySleep = true; |
|
|
|
|
|
|
|
|
|
|
|
//Create a new Basecamp instance called iot
|
|
|
//Create a new Basecamp instance called iot
|
|
|
Basecamp iot; |
|
|
Basecamp iot; |
|
@ -97,7 +92,7 @@ void setup() { |
|
|
pinMode(18, OUTPUT); |
|
|
pinMode(18, OUTPUT); |
|
|
digitalWrite(18, HIGH); // disable LoRa_CS
|
|
|
digitalWrite(18, HIGH); // disable LoRa_CS
|
|
|
|
|
|
|
|
|
boot_count++; |
|
|
state_persistant.boot_count++; |
|
|
|
|
|
|
|
|
// static wifi_country_t wifi_country = {.cc="EU", .schan=1, .nchan=13, .policy=WIFI_COUNTRY_POLICY_AUTO};
|
|
|
// static wifi_country_t wifi_country = {.cc="EU", .schan=1, .nchan=13, .policy=WIFI_COUNTRY_POLICY_AUTO};
|
|
|
// esp_wifi_set_country(&wifi_country);
|
|
|
// esp_wifi_set_country(&wifi_country);
|
|
@ -160,7 +155,7 @@ void setup() { |
|
|
if (timeinfo.tm_year < (2016 - 1900)) { |
|
|
if (timeinfo.tm_year < (2016 - 1900)) { |
|
|
// time not set
|
|
|
// time not set
|
|
|
} |
|
|
} |
|
|
strftime(timeStr, sizeof(timeStr), "%H:%M", &timeinfo); |
|
|
strftime(state.timeStr, sizeof(state.timeStr), "%H:%M", &timeinfo); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
char strftime_buf[64]; |
|
|
char strftime_buf[64]; |
|
@ -185,12 +180,12 @@ void setup() { |
|
|
obtain_time(); |
|
|
obtain_time(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
double seconds = difftime(now, mktime(&alarmTime)); |
|
|
double seconds = difftime(now, mktime(&(state_persistant.alarmTime))); |
|
|
Serial.printf("alarm in %f seconds\n", seconds); |
|
|
Serial.printf("alarm in %f seconds\n", seconds); |
|
|
strftime(strftime_buf, sizeof(strftime_buf), "%c", &alarmTime); |
|
|
strftime(strftime_buf, sizeof(strftime_buf), "%c", &(state_persistant.alarmTime)); |
|
|
Serial.print(" Alarm time: "); Serial.println(strftime_buf); |
|
|
Serial.print(" Alarm time: "); Serial.println(strftime_buf); |
|
|
|
|
|
|
|
|
if (!alarmArmed || -seconds > secondsToSleep) suspend(); |
|
|
if (!state_persistant.alarmArmed || -seconds > secondsToSleep) suspend(); |
|
|
else if (-seconds > 5*60) suspend(5*60); |
|
|
else if (-seconds > 5*60) suspend(5*60); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -228,7 +223,7 @@ void setup() { |
|
|
time(&now); |
|
|
time(&now); |
|
|
} |
|
|
} |
|
|
localtime_r(&now, &timeinfo); |
|
|
localtime_r(&now, &timeinfo); |
|
|
strftime(timeStr, sizeof(timeStr), "%H:%M:%S", &timeinfo); |
|
|
strftime(state.timeStr, sizeof(state.timeStr), "%H:%M:%S", &timeinfo); |
|
|
|
|
|
|
|
|
if (timeinfo.tm_hour < 7 || timeinfo.tm_hour >= 22) { |
|
|
if (timeinfo.tm_hour < 7 || timeinfo.tm_hour >= 22) { |
|
|
Serial.println("night mode"); |
|
|
Serial.println("night mode"); |
|
@ -254,7 +249,7 @@ void setup() { |
|
|
if (wakeup_reason == ESP_SLEEP_WAKEUP_EXT1) { |
|
|
if (wakeup_reason == ESP_SLEEP_WAKEUP_EXT1) { |
|
|
uint64_t wakeup_pinmask = esp_sleep_get_ext1_wakeup_status(); |
|
|
uint64_t wakeup_pinmask = esp_sleep_get_ext1_wakeup_status(); |
|
|
if (wakeup_pinmask & (1ULL << ext_wakeup_pin_1)) { |
|
|
if (wakeup_pinmask & (1ULL << ext_wakeup_pin_1)) { |
|
|
uint16_t statusPacketIdSub = iot.mqtt.publish(sensorTopic.c_str(), 0, false, timeStr); |
|
|
uint16_t statusPacketIdSub = iot.mqtt.publish(sensorTopic.c_str(), 0, false, state.timeStr); |
|
|
sensorON = true; |
|
|
sensorON = true; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
@ -352,13 +347,13 @@ void suspend(uint32_t secondsToSleep) { |
|
|
//
|
|
|
//
|
|
|
////esp_deep_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_OFF); // power down all peripherals
|
|
|
////esp_deep_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_OFF); // power down all peripherals
|
|
|
|
|
|
|
|
|
if (!displaySleep) { |
|
|
if (!state.displaySleep) { |
|
|
u8g2.begin(); |
|
|
u8g2.begin(); |
|
|
u8g2.setPowerSave(0); |
|
|
u8g2.setPowerSave(0); |
|
|
u8g2.clearBuffer(); |
|
|
u8g2.clearBuffer(); |
|
|
u8g2.sendBuffer(); |
|
|
u8g2.sendBuffer(); |
|
|
u8g2.setFont(u8g2_font_inb19_mf); |
|
|
u8g2.setFont(u8g2_font_inb19_mf); |
|
|
u8g2.drawStr(0, 20, timeStr); |
|
|
u8g2.drawStr(0, 20, state.timeStr); |
|
|
rtc_gpio_hold_en((gpio_num_t)16); // 16 not connected to ulp processor :(
|
|
|
rtc_gpio_hold_en((gpio_num_t)16); // 16 not connected to ulp processor :(
|
|
|
esp_sleep_enable_timer_wakeup(1000000LL * 60); |
|
|
esp_sleep_enable_timer_wakeup(1000000LL * 60); |
|
|
} |
|
|
} |
|
@ -370,27 +365,27 @@ void suspend(uint32_t secondsToSleep) { |
|
|
void suspend() { |
|
|
void suspend() { |
|
|
time_t now; |
|
|
time_t now; |
|
|
time(&now); |
|
|
time(&now); |
|
|
double seconds = difftime(now, mktime(&alarmTime)); |
|
|
double seconds = difftime(now, mktime(&(state_persistant.alarmTime))); |
|
|
if (-seconds > secondsToSleep || !alarmArmed) suspend(secondsToSleep); |
|
|
if (-seconds > secondsToSleep || !state_persistant.alarmArmed) suspend(secondsToSleep); |
|
|
else suspend(5*60); |
|
|
else suspend(5*60); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void setAlarmTime(struct tm time) { |
|
|
void setAlarmTime(struct tm time) { |
|
|
alarmTime = time; |
|
|
state_persistant.alarmTime = time; |
|
|
alarmArmed = true; |
|
|
state_persistant.alarmArmed = true; |
|
|
char strftime_buf[64]; |
|
|
char strftime_buf[64]; |
|
|
strftime(strftime_buf, sizeof(strftime_buf), "%c", &alarmTime); |
|
|
strftime(strftime_buf, sizeof(strftime_buf), "%c", &(state_persistant.alarmTime)); |
|
|
Serial.print("Setting Alarm time: "); Serial.println(strftime_buf); |
|
|
Serial.print("Setting Alarm time: "); Serial.println(strftime_buf); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
struct tm getAlarmTime() { |
|
|
struct tm getAlarmTime() { |
|
|
return alarmTime; |
|
|
return state_persistant.alarmTime; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
bool isAlarmActive(){ |
|
|
bool isAlarmActive(){ |
|
|
time_t now; |
|
|
time_t now; |
|
|
time(&now); |
|
|
time(&now); |
|
|
double seconds = difftime(now, mktime(&alarmTime)); |
|
|
double seconds = difftime(now, mktime(&(state_persistant.alarmTime))); |
|
|
if (seconds > -24*60*60 && seconds <=0) return true; |
|
|
if (seconds > -24*60*60 && seconds <=0) return true; |
|
|
else return false; |
|
|
else return false; |
|
|
} |
|
|
} |
|
@ -436,15 +431,15 @@ void loop() |
|
|
time(&now); |
|
|
time(&now); |
|
|
localtime_r(&now, &timeinfo); |
|
|
localtime_r(&now, &timeinfo); |
|
|
|
|
|
|
|
|
double seconds = difftime(now, mktime(&alarmTime)); |
|
|
double seconds = difftime(now, mktime(&(state_persistant.alarmTime))); |
|
|
|
|
|
|
|
|
bool stayAwake = false; |
|
|
bool stayAwake = false; |
|
|
if (alarmArmed && seconds >= -2*5*60) stayAwake = true; |
|
|
if (state_persistant.alarmArmed && seconds >= -2*5*60) stayAwake = true; |
|
|
if (!mp3.playing && millis() - lastActive >= stayOnTime && !stayAwake) suspend(); |
|
|
if (!mp3.playing && millis() - lastActive >= stayOnTime && !stayAwake) suspend(); |
|
|
|
|
|
|
|
|
Serial.printf("alarm in %f seconds (%d)\n", seconds, alarmArmed); |
|
|
Serial.printf("alarm in %f seconds (%d)\n", seconds, state_persistant.alarmArmed); |
|
|
if (seconds >= 0 && alarmArmed) { |
|
|
if (seconds >= 0 && state_persistant.alarmArmed) { |
|
|
alarmArmed = false; |
|
|
state_persistant.alarmArmed = false; |
|
|
lastActive = millis(); |
|
|
lastActive = millis(); |
|
|
led.wakeUpLight(0); |
|
|
led.wakeUpLight(0); |
|
|
Serial.println("WAKEUP TIME!!!!!"); |
|
|
Serial.println("WAKEUP TIME!!!!!"); |
|
@ -462,16 +457,16 @@ void loop() |
|
|
mp3.start("http://radioessen.cast.addradio.de/radioessen/simulcast/high/stream.mp3"); |
|
|
mp3.start("http://radioessen.cast.addradio.de/radioessen/simulcast/high/stream.mp3"); |
|
|
led.changeAnimation(2, 0); |
|
|
led.changeAnimation(2, 0); |
|
|
} |
|
|
} |
|
|
if (alarmArmed && seconds >= -5*60 && seconds <= 0) { |
|
|
if (state_persistant.alarmArmed && seconds >= -5*60 && seconds <= 0) { |
|
|
led.wakeUpLight(255*(seconds+300)/300); |
|
|
led.wakeUpLight(255*(seconds+300)/300); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
int voltage = rom_phy_get_vdd33(); |
|
|
int voltage = rom_phy_get_vdd33(); |
|
|
Serial.printf("voltage: %d\n", voltage); |
|
|
Serial.printf("voltage: %d\n", voltage); |
|
|
|
|
|
|
|
|
strftime(timeStr, sizeof(timeStr), "%H:%M:%S", &timeinfo); |
|
|
strftime(state.timeStr, sizeof(state.timeStr), "%H:%M:%S", &timeinfo); |
|
|
sprintf(weatherStr, "%.1f°C %.1f%% %.0fhPa", bme280.readTemperature(), bme280.readHumidity(), bme280.readPressure()); |
|
|
sprintf(state.string1, "%.1f°C %.1f%% %.0fhPa", bme280.readTemperature(), bme280.readHumidity(), bme280.readPressure()); |
|
|
sprintf(statusStr, "%.1f %dmV %ddBm %ukB %s", temperatureRead(), voltage, WiFi.RSSI(), ESP.getFreeHeap()/1024, isAlarmActive()?"A":"_"); |
|
|
sprintf(state.string2, "%.1f %dmV %ddBm %ukB %s", temperatureRead(), voltage, WiFi.RSSI(), ESP.getFreeHeap()/1024, isAlarmActive()?"A":"_"); |
|
|
|
|
|
|
|
|
Serial.print("Free Heap: "); |
|
|
Serial.print("Free Heap: "); |
|
|
Serial.println(ESP.getFreeHeap()); |
|
|
Serial.println(ESP.getFreeHeap()); |
|
@ -491,7 +486,7 @@ void loop() |
|
|
/* if (digitalRead(sensorPin) == HIGH) {
|
|
|
/* if (digitalRead(sensorPin) == HIGH) {
|
|
|
lastActive = millis(); |
|
|
lastActive = millis(); |
|
|
if (sensorON == false && digitalRead(sensorPin) == HIGH) { |
|
|
if (sensorON == false && digitalRead(sensorPin) == HIGH) { |
|
|
uint16_t statusPacketIdSub = iot.mqtt.publish(sensorTopic.c_str(), 0, false, timeStr); |
|
|
uint16_t statusPacketIdSub = iot.mqtt.publish(sensorTopic.c_str(), 0, false, state.timeStr); |
|
|
sensorON = true; |
|
|
sensorON = true; |
|
|
} |
|
|
} |
|
|
} */ |
|
|
} */ |
|
|