2018-07-22 20:27:58 +02:00
|
|
|
/* This routine is called by interrupt in regular intervals */
|
|
|
|
/* Interval can be set in paxcounter.conf (HOMECYCLE) */
|
|
|
|
|
|
|
|
// Basic config
|
2018-10-03 23:07:22 +02:00
|
|
|
#include "cyclic.h"
|
2018-07-22 20:27:58 +02:00
|
|
|
|
|
|
|
// Local logging tag
|
2019-02-27 00:52:27 +01:00
|
|
|
static const char TAG[] = __FILE__;
|
2018-07-22 20:27:58 +02:00
|
|
|
|
2020-10-05 13:56:41 +02:00
|
|
|
Ticker cyclicTimer;
|
2019-03-03 00:30:57 +01:00
|
|
|
|
2020-02-03 15:29:32 +01:00
|
|
|
#if (HAS_SDS011)
|
|
|
|
extern boolean isSDS011Active;
|
|
|
|
#endif
|
|
|
|
|
2020-10-04 14:29:57 +02:00
|
|
|
void setCyclicIRQ() {
|
2019-04-13 18:44:25 +02:00
|
|
|
xTaskNotifyFromISR(irqHandlerTask, CYCLIC_IRQ, eSetBits, NULL);
|
|
|
|
}
|
2019-03-03 00:30:57 +01:00
|
|
|
|
2018-07-22 20:27:58 +02:00
|
|
|
// do all housekeeping
|
2018-09-21 18:23:34 +02:00
|
|
|
void doHousekeeping() {
|
|
|
|
|
2018-07-25 00:17:31 +02:00
|
|
|
// update uptime counter
|
|
|
|
uptime();
|
2018-09-15 18:59:20 +02:00
|
|
|
// check if update mode trigger switch was set
|
2019-10-20 20:05:13 +02:00
|
|
|
if (RTC_runmode == RUNMODE_UPDATE) {
|
|
|
|
// check battery status if we can before doing ota
|
2019-12-25 23:22:11 +01:00
|
|
|
if (batt_sufficient()) {
|
2019-10-20 20:05:13 +02:00
|
|
|
do_reset(true); // warmstart to runmode update
|
2019-12-25 23:22:11 +01:00
|
|
|
} else {
|
2020-04-11 21:30:09 +02:00
|
|
|
ESP_LOGE(TAG, "Battery level %d%% is too low for OTA", batt_level);
|
2019-12-25 23:22:11 +01:00
|
|
|
RTC_runmode = RUNMODE_NORMAL; // keep running in normal mode
|
2019-10-20 20:05:13 +02:00
|
|
|
}
|
|
|
|
}
|
2018-08-12 23:42:39 +02:00
|
|
|
|
2020-11-02 12:25:30 +01:00
|
|
|
// heap and task storage debugging
|
|
|
|
ESP_LOGD(TAG, "Heap: Free:%d, Min:%d, Size:%d, Alloc:%d, StackHWM:%d",
|
|
|
|
ESP.getFreeHeap(), ESP.getMinFreeHeap(), ESP.getHeapSize(),
|
|
|
|
ESP.getMaxAllocHeap(), uxTaskGetStackHighWaterMark(NULL));
|
2019-01-06 19:41:42 +01:00
|
|
|
ESP_LOGD(TAG, "IRQhandler %d bytes left | Taskstate = %d",
|
|
|
|
uxTaskGetStackHighWaterMark(irqHandlerTask),
|
|
|
|
eTaskGetState(irqHandlerTask));
|
2020-11-07 22:32:36 +01:00
|
|
|
ESP_LOGD(TAG, "MACprocessor %d bytes left | Taskstate = %d",
|
|
|
|
uxTaskGetStackHighWaterMark(macProcessTask),
|
|
|
|
eTaskGetState(macProcessTask));
|
2019-08-02 21:53:05 +02:00
|
|
|
#if (HAS_LORA)
|
|
|
|
ESP_LOGD(TAG, "LMiCtask %d bytes left | Taskstate = %d",
|
|
|
|
uxTaskGetStackHighWaterMark(lmicTask), eTaskGetState(lmicTask));
|
2019-08-30 18:54:53 +02:00
|
|
|
ESP_LOGD(TAG, "Lorasendtask %d bytes left | Taskstate = %d",
|
2019-09-18 22:38:39 +02:00
|
|
|
uxTaskGetStackHighWaterMark(lorasendTask),
|
|
|
|
eTaskGetState(lorasendTask));
|
2019-08-02 21:53:05 +02:00
|
|
|
#endif
|
2019-04-13 18:44:25 +02:00
|
|
|
#if (HAS_GPS)
|
2019-09-18 22:38:39 +02:00
|
|
|
ESP_LOGD(TAG, "Gpsloop %d bytes left | Taskstate = %d",
|
|
|
|
uxTaskGetStackHighWaterMark(GpsTask), eTaskGetState(GpsTask));
|
2018-09-27 15:13:15 +02:00
|
|
|
#endif
|
2019-08-03 11:35:16 +02:00
|
|
|
#ifdef HAS_SPI
|
|
|
|
ESP_LOGD(TAG, "spiloop %d bytes left | Taskstate = %d",
|
|
|
|
uxTaskGetStackHighWaterMark(spiTask), eTaskGetState(spiTask));
|
|
|
|
#endif
|
2020-05-21 19:42:42 +02:00
|
|
|
#ifdef HAS_MQTT
|
|
|
|
ESP_LOGD(TAG, "MQTTloop %d bytes left | Taskstate = %d",
|
|
|
|
uxTaskGetStackHighWaterMark(mqttTask), eTaskGetState(mqttTask));
|
|
|
|
#endif
|
2019-08-03 11:35:16 +02:00
|
|
|
|
2019-04-08 22:51:12 +02:00
|
|
|
#if (defined HAS_DCF77 || defined HAS_IF482)
|
2019-02-08 23:08:14 +01:00
|
|
|
ESP_LOGD(TAG, "Clockloop %d bytes left | Taskstate = %d",
|
|
|
|
uxTaskGetStackHighWaterMark(ClockTask), eTaskGetState(ClockTask));
|
2019-02-03 21:19:08 +01:00
|
|
|
#endif
|
2018-09-11 16:15:39 +02:00
|
|
|
|
2018-10-03 00:25:05 +02:00
|
|
|
#if (HAS_LED != NOT_A_PIN) || defined(HAS_RGB_LED)
|
2019-01-06 19:41:42 +01:00
|
|
|
ESP_LOGD(TAG, "LEDloop %d bytes left | Taskstate = %d",
|
|
|
|
uxTaskGetStackHighWaterMark(ledLoopTask),
|
|
|
|
eTaskGetState(ledLoopTask));
|
2018-10-03 00:25:05 +02:00
|
|
|
#endif
|
2018-09-27 15:13:15 +02:00
|
|
|
|
2018-07-22 20:27:58 +02:00
|
|
|
// read battery voltage into global variable
|
2020-04-19 19:51:23 +02:00
|
|
|
#if (defined BAT_MEASURE_ADC || defined HAS_PMU || defined HAS_IP5306)
|
2020-04-11 21:30:09 +02:00
|
|
|
batt_level = read_battlevel();
|
2019-09-18 22:38:39 +02:00
|
|
|
#ifdef HAS_PMU
|
2019-09-23 15:45:47 +02:00
|
|
|
AXP192_showstatus();
|
2019-09-19 17:06:09 +02:00
|
|
|
#endif
|
2020-04-19 19:51:23 +02:00
|
|
|
#ifdef HAS_IP5306
|
|
|
|
printIP5306Stats();
|
|
|
|
#endif
|
2018-12-27 17:09:40 +01:00
|
|
|
#endif
|
|
|
|
|
2019-07-29 20:23:27 +02:00
|
|
|
// display BME680/280 sensor data
|
|
|
|
#if (HAS_BME)
|
2019-03-08 18:13:51 +01:00
|
|
|
#ifdef HAS_BME680
|
2019-01-06 19:41:42 +01:00
|
|
|
ESP_LOGI(TAG, "BME680 Temp: %.2f°C | IAQ: %.2f | IAQacc: %d",
|
|
|
|
bme_status.temperature, bme_status.iaq, bme_status.iaq_accuracy);
|
2019-07-29 20:23:27 +02:00
|
|
|
#elif defined HAS_BME280
|
2019-03-16 09:08:25 +01:00
|
|
|
ESP_LOGI(TAG, "BME280 Temp: %.2f°C | Humidity: %.2f | Pressure: %.0f",
|
2019-03-08 18:13:51 +01:00
|
|
|
bme_status.temperature, bme_status.humidity, bme_status.pressure);
|
2019-12-08 12:58:12 +01:00
|
|
|
#elif defined HAS_BMP180
|
2019-12-25 23:22:11 +01:00
|
|
|
ESP_LOGI(TAG, "BMP180 Temp: %.2f°C | Pressure: %.0f", bme_status.temperature,
|
|
|
|
bme_status.pressure);
|
2019-07-29 20:23:27 +02:00
|
|
|
#endif
|
2019-03-08 18:13:51 +01:00
|
|
|
#endif
|
|
|
|
|
2018-12-22 14:37:47 +01:00
|
|
|
// check free heap memory
|
|
|
|
if (ESP.getMinFreeHeap() <= MEM_LOW) {
|
2018-08-04 14:37:41 +02:00
|
|
|
ESP_LOGI(TAG,
|
2018-07-22 20:27:58 +02:00
|
|
|
"Memory full, counter cleared (heap low water mark = %d Bytes / "
|
|
|
|
"free heap = %d bytes)",
|
2019-01-06 19:41:42 +01:00
|
|
|
ESP.getMinFreeHeap(), ESP.getFreeHeap());
|
2019-02-03 21:19:08 +01:00
|
|
|
reset_counters(); // clear macs container and reset all counters
|
|
|
|
get_salt(); // get new salt for salting hashes
|
2018-08-04 18:09:25 +02:00
|
|
|
|
2019-01-06 19:41:42 +01:00
|
|
|
if (ESP.getMinFreeHeap() <= MEM_LOW) // check again
|
2019-10-20 20:05:13 +02:00
|
|
|
do_reset(true); // memory leak, reset device
|
2018-07-22 20:27:58 +02:00
|
|
|
}
|
2018-12-22 14:37:47 +01:00
|
|
|
|
|
|
|
// check free PSRAM memory
|
|
|
|
#ifdef BOARD_HAS_PSRAM
|
|
|
|
if (ESP.getMinFreePsram() <= MEM_LOW) {
|
|
|
|
ESP_LOGI(TAG, "PSRAM full, counter cleared");
|
2019-02-03 21:19:08 +01:00
|
|
|
reset_counters(); // clear macs container and reset all counters
|
|
|
|
get_salt(); // get new salt for salting hashes
|
2018-12-22 14:37:47 +01:00
|
|
|
|
|
|
|
if (ESP.getMinFreePsram() <= MEM_LOW) // check again
|
2019-10-20 20:05:13 +02:00
|
|
|
do_reset(true); // memory leak, reset device
|
2018-12-22 14:37:47 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-02-03 15:29:32 +01:00
|
|
|
#if (HAS_SDS011)
|
2020-04-11 23:36:17 +02:00
|
|
|
if (isSDS011Active) {
|
|
|
|
ESP_LOGD(TAG, "SDS011: go to sleep");
|
|
|
|
sds011_loop();
|
|
|
|
} else {
|
|
|
|
ESP_LOGD(TAG, "SDS011: wakeup");
|
|
|
|
sds011_wakeup();
|
|
|
|
}
|
2020-02-03 15:29:32 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
} // doHousekeeping()
|
|
|
|
|
2020-12-09 20:37:03 +01:00
|
|
|
uint64_t uptime() { return millis(); }
|
2020-02-03 15:29:32 +01:00
|
|
|
|
|
|
|
uint32_t getFreeRAM() {
|
|
|
|
#ifndef BOARD_HAS_PSRAM
|
|
|
|
return ESP.getFreeHeap();
|
|
|
|
#else
|
|
|
|
return ESP.getFreePsram();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void reset_counters() {
|
|
|
|
#if ((WIFICOUNTER) || (BLECOUNTER))
|
2020-10-05 13:56:41 +02:00
|
|
|
macs.clear(); // clear all macs container
|
2020-02-03 15:29:32 +01:00
|
|
|
macs_wifi = 0;
|
|
|
|
macs_ble = 0;
|
|
|
|
#ifdef HAS_DISPLAY
|
2020-03-29 12:10:42 +02:00
|
|
|
dp_plotCurve(0, true);
|
2020-02-03 15:29:32 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif
|
|
|
|
}
|