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
|
|
|
|
static const char TAG[] = "main";
|
|
|
|
|
2018-11-14 22:11:23 +01:00
|
|
|
uint32_t userUTCTime; // Seconds since the UTC epoch
|
2018-11-16 09:43:47 +01:00
|
|
|
unsigned long nextTimeSync = millis();
|
2018-11-14 22:11:23 +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-07-24 23:25:41 +02:00
|
|
|
|
2018-09-15 18:59:20 +02:00
|
|
|
// check if update mode trigger switch was set
|
|
|
|
if (cfg.runmode == 1)
|
2018-10-17 07:10:37 +02:00
|
|
|
do_reset();
|
2018-08-12 23:42:39 +02:00
|
|
|
|
2018-11-14 22:11:23 +01:00
|
|
|
spi_housekeeping();
|
2018-11-16 09:52:26 +01:00
|
|
|
lora_housekeeping();
|
2018-11-16 09:43:47 +01:00
|
|
|
|
2018-11-18 15:50:57 +01:00
|
|
|
// time sync once per TIME_SYNC_INTERVAL
|
|
|
|
#ifdef TIME_SYNC_INTERVAL
|
2018-11-16 09:43:47 +01:00
|
|
|
if (millis() >= nextTimeSync) {
|
2018-12-21 00:35:21 +01:00
|
|
|
nextTimeSync =
|
|
|
|
millis() + TIME_SYNC_INTERVAL * 60000; // set up next time sync period
|
2018-11-16 09:43:47 +01:00
|
|
|
do_timesync();
|
|
|
|
}
|
|
|
|
#endif
|
2018-11-14 22:11:23 +01:00
|
|
|
|
|
|
|
// task storage debugging //
|
2019-01-06 19:41:42 +01:00
|
|
|
ESP_LOGD(TAG, "Wifiloop %d bytes left | Taskstate = %d",
|
|
|
|
uxTaskGetStackHighWaterMark(wifiSwitchTask),
|
|
|
|
eTaskGetState(wifiSwitchTask));
|
|
|
|
ESP_LOGD(TAG, "IRQhandler %d bytes left | Taskstate = %d",
|
|
|
|
uxTaskGetStackHighWaterMark(irqHandlerTask),
|
|
|
|
eTaskGetState(irqHandlerTask));
|
2018-09-27 15:13:15 +02:00
|
|
|
#ifdef HAS_GPS
|
2019-01-06 19:41:42 +01:00
|
|
|
ESP_LOGD(TAG, "Gpsloop %d bytes left | Taskstate = %d",
|
|
|
|
uxTaskGetStackHighWaterMark(GpsTask), eTaskGetState(GpsTask));
|
2018-09-27 15:13:15 +02:00
|
|
|
#endif
|
2018-11-25 16:05:30 +01:00
|
|
|
#ifdef HAS_BME
|
2019-01-06 19:41:42 +01:00
|
|
|
ESP_LOGD(TAG, "Bmeloop %d bytes left | Taskstate = %d",
|
|
|
|
uxTaskGetStackHighWaterMark(BmeTask), eTaskGetState(BmeTask));
|
2018-11-25 16:05:30 +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
|
|
|
|
#ifdef HAS_BATTERY_PROBE
|
|
|
|
batt_voltage = read_voltage();
|
2018-12-27 17:09:40 +01:00
|
|
|
ESP_LOGI(TAG, "Voltage: %dmV", batt_voltage);
|
|
|
|
#endif
|
|
|
|
|
2019-01-06 19:41:42 +01:00
|
|
|
// display BME sensor data
|
2018-12-27 17:09:40 +01:00
|
|
|
#ifdef HAS_BME
|
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);
|
2018-07-22 20:27:58 +02: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());
|
2018-11-18 15:50:57 +01:00
|
|
|
SendPayload(COUNTERPORT); // send data before clearing counters
|
2018-12-21 00:35:21 +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
|
|
|
|
do_reset(); // 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");
|
|
|
|
SendPayload(COUNTERPORT); // send data before clearing counters
|
|
|
|
reset_counters(); // clear macs container and reset all counters
|
|
|
|
get_salt(); // get new salt for salting hashes
|
|
|
|
|
|
|
|
if (ESP.getMinFreePsram() <= MEM_LOW) // check again
|
2019-01-06 19:41:42 +01:00
|
|
|
do_reset(); // memory leak, reset device
|
2018-12-22 14:37:47 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-09-21 18:23:34 +02:00
|
|
|
} // doHousekeeping()
|
2018-07-23 08:25:23 +02:00
|
|
|
|
2018-07-23 13:20:06 +02:00
|
|
|
// uptime counter 64bit to prevent millis() rollover after 49 days
|
|
|
|
uint64_t uptime() {
|
|
|
|
static uint32_t low32, high32;
|
|
|
|
uint32_t new_low32 = millis();
|
|
|
|
if (new_low32 < low32)
|
|
|
|
high32++;
|
|
|
|
low32 = new_low32;
|
|
|
|
return (uint64_t)high32 << 32 | low32;
|
|
|
|
}
|
|
|
|
|
2018-12-22 14:37:47 +01:00
|
|
|
uint32_t getFreeRAM() {
|
|
|
|
#ifndef BOARD_HAS_PSRAM
|
|
|
|
return ESP.getFreeHeap();
|
|
|
|
#else
|
|
|
|
return ESP.getFreePsram();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-07-23 13:20:06 +02:00
|
|
|
void reset_counters() {
|
|
|
|
macs.clear(); // clear all macs container
|
|
|
|
macs_total = 0; // reset all counters
|
|
|
|
macs_wifi = 0;
|
|
|
|
macs_ble = 0;
|
|
|
|
}
|
|
|
|
|
2018-11-14 22:11:23 +01:00
|
|
|
void do_timesync() {
|
2018-11-18 15:50:57 +01:00
|
|
|
#ifdef TIME_SYNC_INTERVAL
|
2018-12-21 00:35:21 +01:00
|
|
|
|
2019-01-21 16:16:39 +01:00
|
|
|
// set system time to time source GPS, if we have valid gps time
|
2018-11-14 22:11:23 +01:00
|
|
|
#ifdef HAS_GPS
|
|
|
|
if (gps.time.isValid()) {
|
|
|
|
setTime(gps.time.hour(), gps.time.minute(), gps.time.second(),
|
|
|
|
gps.date.day(), gps.date.month(), gps.date.year());
|
2019-01-21 16:16:39 +01:00
|
|
|
// set RTC time to time source GPS, if RTC is present
|
2019-01-19 17:53:21 +01:00
|
|
|
#ifdef HAS_RTC
|
2019-01-28 00:38:31 +01:00
|
|
|
if (!set_rtctime(RtcDateTime(now())))
|
|
|
|
ESP_LOGE(TAG, "RTC set time failure");
|
|
|
|
time_t tt = myTZ.toLocal(now());
|
2019-01-21 16:16:39 +01:00
|
|
|
ESP_LOGI(TAG, "GPS has set system time to %02d/%02d/%d %02d:%02d:%02d",
|
2019-01-27 22:12:56 +01:00
|
|
|
month(tt), day(tt), year(tt), hour(tt), minute(tt), second(tt));
|
2019-01-28 09:44:30 +01:00
|
|
|
#endif
|
2018-11-14 22:11:23 +01:00
|
|
|
return;
|
|
|
|
} else {
|
2018-11-16 09:43:47 +01:00
|
|
|
ESP_LOGI(TAG, "No valid GPS time");
|
2018-11-14 22:11:23 +01:00
|
|
|
}
|
2018-12-21 00:35:21 +01:00
|
|
|
|
2019-01-28 00:38:31 +01:00
|
|
|
// set system time to time source LoRa Network, if network supports DevTimeReq
|
|
|
|
#elif defined LMIC_ENABLE_DeviceTimeReq
|
2019-01-21 16:16:39 +01:00
|
|
|
// Schedule a network time sync request at the next possible time
|
2018-11-14 22:11:23 +01:00
|
|
|
LMIC_requestNetworkTime(user_request_network_time_callback, &userUTCTime);
|
|
|
|
ESP_LOGI(TAG, "Network time request scheduled");
|
2019-01-28 00:38:31 +01:00
|
|
|
#endif // HAS_GPS
|
2018-12-21 00:35:21 +01:00
|
|
|
|
2018-11-18 15:50:57 +01:00
|
|
|
#endif // TIME_SYNC_INTERVAL
|
2018-11-16 09:43:47 +01:00
|
|
|
} // do_timesync()
|
2018-11-14 22:11:23 +01:00
|
|
|
|
2018-07-23 13:20:06 +02:00
|
|
|
#ifndef VERBOSE
|
|
|
|
int redirect_log(const char *fmt, va_list args) {
|
|
|
|
// do nothing
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|