147 void setTime(time_t epoch, E_TIME_SOURCE source);
157 time_t tnow = time(
nullptr);
158 localtime_r(&tnow, &timeinfo);
159 strftime(tbuf, 25,
"%Y-%m-%d %H:%M:%S", &timeinfo);
184 if (supplyVoltage > 3500)
186 busVoltage = supplyVoltage;
188 else if (batteryVoltage > 2000)
190 busVoltage = batteryVoltage;
198#if defined(ARDUINO_ESP32S3_POWERFEATHER)
204 res = Board.checkSupplyGood(supply_good);
205 if ((res == Result::Ok) && supply_good)
210 res = Board.getBatteryCharge(soc);
211 log_i(
"SOC = %d %%", soc);
212 if (res == Result::Ok && soc <= PowerFeatherCfg.soc_critical)
214 log_i(
"Battery low!");
218#elif defined(ARDUINO_M5STACK_CORE2)
228 int16_t vbus = M5.Power.getVBUSVoltage();
229 log_i(
"VBUS = %d mV", vbus);
230 int8_t soc = M5.Power.getBatteryLevel();
231 log_i(
"SOC = %d %%", soc);
234 log_d(
"Charging: %u", M5.Power.isCharging());
235 log_d(
"Battery current = %d mA", M5.Power.getBatteryCurrent());
237 if (M5.Power.getVBUSVoltage() >= VOLTAGE_CRITICAL)
242 if (soc <= M5StackCfg.soc_critical)
244 log_i(
"Battery low!");
258 if (busVoltage > 0 && busVoltage <= voltage_critical)
260 log_i(
"Battery low!");
281#if defined(ARDUINO_ESP32S3_POWERFEATHER)
282 return getBattlevelPowerfeather();
283#elif defined(ARDUINO_M5STACK_CORE2)
284 return getBattlevelM5core2();
303 uint16_t voltage = batteryVoltage;
304 uint16_t limit_low = battery_discharge_lim;
305 uint16_t limit_high = battery_charge_lim;
313 else if (voltage > limit_high)
320 battLevel =
static_cast<uint8_t
>(
321 static_cast<float>(voltage - limit_low) /
static_cast<float>(limit_high - limit_low) * 255);
322 battLevel = (battLevel == 0) ? 1 : battLevel;
323 battLevel = (battLevel == 255) ? 254 : battLevel;
334 void gpsPower(
bool on)
336 if (GPS_PWR_EN_PIN < 0)
340 if (GPS_PWR_EN_ACTIVE)
342 pinMode(GPS_PWR_EN_PIN, OUTPUT);
343 digitalWrite(GPS_PWR_EN_PIN, on ? HIGH : LOW);
347 pinMode(GPS_PWR_EN_PIN, OUTPUT);
348 digitalWrite(GPS_PWR_EN_PIN, on ? LOW : HIGH);
353#if defined(ARDUINO_ESP32S3_POWERFEATHER)
365 uint8_t getBattlevelPowerfeather(
void)
370 res = Board.checkSupplyGood(supply_good);
371 if ((res == Result::Ok) && supply_good)
377 res = Board.getBatteryCharge(soc);
378 log_d(
"Battery SOC: %u %%", soc);
379 if (res == Result::Ok)
383 level =
static_cast<uint8_t
>(
static_cast<float>(soc) / 100.0f * 254.0f);
384 level = (level == 0) ? 1 : level;
391#if defined(ARDUINO_M5STACK_CORE2)
403 uint8_t getBattlevelM5core2(
void)
405 if (M5.Power.getVBUSVoltage() >= VOLTAGE_CRITICAL)
410 uint8_t soc = M5.Power.getBatteryLevel();
411 log_d(
"Battery SOC: %u %%", soc);
415 level =
static_cast<uint8_t
>(
static_cast<float>(soc) / 100.0f * 254.0f);
416 level = (level == 0) ? 1 : level;
484 bool getGPSData(time_t &gpsTime);
511 time_t t_now = time(
nullptr);
512 localtime_r(&t_now, &timeinfo);
529 void uplinkDelay(uint32_t timeUntilUplink, uint32_t uplinkInterval)
531 uint32_t uplinkIntervalMs = uplinkInterval * 1000UL;
532 uint32_t delayMs = max(timeUntilUplink, uplinkIntervalMs);
534 log_d(
"Sending uplink in %u s", delayMs / 1000);
536 esp_sleep_enable_timer_wakeup(delayMs * 1000);
537 esp_light_sleep_start();
552#if defined(ARDUINO_ARCH_RP2040)
553 gotoSleepRP2040(seconds);
555 gotoSleepESP32(seconds);
559#if defined(ARDUINO_M5STACK_CORE2)
560 void setupM5StackCore2(
void);
568 void getTimeFromExtRTC(
void);
575 void syncRTCWithExtRTC(
void);
578#if defined(ARDUINO_M5STACK_CORE2)
582 void getTimeFromExtRTC(
void);
585#if defined(ARDUINO_M5STACK_CORE2)
589 void syncRTCWithExtRTC(
void);
600 void gotoSleepESP32(uint32_t seconds);
603#if defined(ARDUINO_ARCH_RP2040)
619 void gotoSleepRP2040(uint32_t seconds);
624 void restoreRP2040(
void);
635 void print_wakeup_reason()
637 esp_sleep_wakeup_cause_t wakeup_reason = esp_sleep_get_wakeup_cause();
638 if (wakeup_reason == ESP_SLEEP_WAKEUP_TIMER)
640 log_i(
"Wake from sleep");
644 log_i(
"Wake not caused by deep sleep: %u", wakeup_reason);
649#if defined(ARDUINO_ESP32S3_POWERFEATHER)
650 void setupPowerFeather(
struct sPowerFeatherCfg &cfg);
654#if defined(ARDUINO_ESP32S3_POWERFEATHER)
655 struct sPowerFeatherCfg PowerFeatherCfg = {
656 .battery_capacity = BATTERY_CAPACITY_MAH,
657 .supply_maintain_voltage = PF_SUPPLY_MAINTAIN_VOLTAGE,
658 .max_charge_current = PF_MAX_CHARGE_CURRENT_MAH,
659 .soc_eco_enter = SOC_ECO_ENTER,
660 .soc_eco_exit = SOC_ECO_EXIT,
661 .soc_critical = SOC_CRITICAL,
662 .temperature_measurement = PF_TEMPERATURE_MEASUREMENT,
663 .battery_fuel_gauge = PF_BATTERY_FUEL_GAUGE};
664 struct sM5StackCfg M5StackCfg = {0};
665#elif defined(ARDUINO_M5STACK_CORE2)
666 struct sPowerFeatherCfg PowerFeatherCfg = {0};
667 struct sM5StackCfg M5StackCfg = {
668 .soc_eco_enter = SOC_ECO_ENTER,
669 .soc_eco_exit = SOC_ECO_EXIT,
670 .soc_critical = SOC_CRITICAL};
672 struct sPowerFeatherCfg PowerFeatherCfg = {0};
673 struct sM5StackCfg M5StackCfg = {0};
676 uint16_t voltage_eco_exit = VOLTAGE_ECO_EXIT;
677 uint16_t voltage_eco_enter = VOLTAGE_ECO_ENTER;
678 uint16_t voltage_critical = VOLTAGE_CRITICAL;
679 uint16_t battery_discharge_lim = BATTERY_DISCHARGE_LIM;
680 uint16_t battery_charge_lim = BATTERY_CHARGE_LIM;
681 uint16_t batteryVoltage = 0;
682 uint16_t supplyVoltage = 0;
683 uint16_t busVoltage = 0;
void getVoltages(void)
Get the voltages from ADC / Power Management Chip.
Definition SystemContext.h:179
uint8_t getBattlevelDefault(void)
Get the battery fill level (default implementation).
Definition SystemContext.h:301
uint32_t sleepDuration(void)
Compute sleep duration in seconds.
Definition SystemContext.h:502
void savePreferences(void)
Save preferences to flash memory.
Definition SystemContext.cpp:221
void printDateTime(void)
Definition SystemContext.h:152
uint8_t lw_stat_interval
preferences: LoRaWAN node status uplink interval
Definition SystemContext.h:99
bool isFirstBoot(void)
Check if this is the first boot of the system after power-on/HW reset.
Definition SystemContext.cpp:184
bool isRtcSynched(void)
Check if the RTC is synchronized to a time source.
Definition SystemContext.cpp:386
void uplinkDelay(uint32_t timeUntilUplink, uint32_t uplinkInterval)
LoRaWAN uplink delay.
Definition SystemContext.h:529
void sleepAfterFailedJoin(void)
Sleep after a failed join attempt.
Definition SystemContext.cpp:194
void resetFailedJoinCount(void)
Reset the failed join count.
Definition SystemContext.cpp:189
bool longSleepActive(void)
Check if long sleep is active.
Definition SystemContext.h:444
uint8_t getBattlevel(void)
Get the battery fill level.
Definition SystemContext.h:279
void sleepIfSupplyLow(void)
Sleep if battery voltage is low to prevent deep-discharging.
Definition SystemContext.h:256
void setTime(time_t epoch, E_TIME_SOURCE source)
Set RTC to epoch.
Definition SystemContext.cpp:208
void gotoSleep(uint32_t seconds)
Enter sleep mode.
Definition SystemContext.h:550
bool rtcNeedsSync(void)
Check if the RTC needs to be synchronized to a time source.
Definition SystemContext.cpp:398
void begin(void)
Initialize the system context.
Definition SystemContext.cpp:133
uint32_t sleepInterval(void)
Switch between normal and long sleep interval.
Definition SystemContext.cpp:278
uint16_t sleep_interval
preferences: sleep interval
Definition SystemContext.h:97
E_TIME_SOURCE getRtcTimeSource(void)
Get the RTC time source.
Definition SystemContext.cpp:392
uint16_t sleep_interval_long
preferences: sleep interval long
Definition SystemContext.h:98