Skip to content

Commit

Permalink
wakeup sync (experimental)
Browse files Browse the repository at this point in the history
  • Loading branch information
cyberman54 committed Mar 8, 2023
1 parent e7a3d24 commit eaa8bdf
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
4 changes: 4 additions & 0 deletions docs/configuration/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ Supported external time sources are GPS, LORAWAN network time and LORAWAN applic

If your LORAWAN network does not support network time, you can run a Node-Red timeserver application using the enclosed [**Timeserver code**](https://github.com/cyberman54/ESP32-Paxcounter/blob/master/src/Node-RED/Timeserver.json). Configure the MQTT nodes in Node-Red for the LORAWAN application used by your paxocunter device. Time can also be set without precision liability, by simple remote command, see section remote control.

## Syncing multiple paxcounters

A fleet of paxcounters can be synchronized to keep all devices wake up and start scanning at the same time. Synchronization is based on top-of-hour as common time point of reference. This feature requires time-of-day to be present on each device. Thus, `TIME_SYNC_INTERVAL` option, as explained above, must be enabled. Wake up syncing is enabled by setting `SYNCWAKEUP` in `paxcounter.conf` to a value X, greater than zero, and smaller than `SLEEPCYCLE`. This defines a time window, centered at top-of-hour, sized +/- X seconds. If a device, returning from sleep, would wakeup within this time windows, the wakeup will be adjusted to top-of-hour.

## Wall clock controller

Paxcounter can be used to sync a wall clock which has a DCF77 or IF482 time telegram input. Set `#define HAS_IF482` or `#define HAS_DCF77` in board's hal file to setup clock controller. Use case of this function is to integrate paxcounter and clock. Accurary of the synthetic DCF77 signal depends on accuracy of on board's time base, see above.
Expand Down
2 changes: 1 addition & 1 deletion include/reset.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
void reset_rtc_vars(void);
void do_reset(bool warmstart);
void do_after_reset(void);
void enter_deepsleep(const uint32_t wakeup_sec, const gpio_num_t wakeup_gpio);
void enter_deepsleep(uint32_t wakeup_sec, const gpio_num_t wakeup_gpio);
unsigned long long uptime(void);

enum runmode_t {
Expand Down
17 changes: 16 additions & 1 deletion src/reset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ void do_after_reset(void) {
}
}

void enter_deepsleep(const uint32_t wakeup_sec, gpio_num_t wakeup_gpio) {
void enter_deepsleep(uint32_t wakeup_sec, gpio_num_t wakeup_gpio) {
ESP_LOGI(TAG, "Preparing to sleep...");

RTC_runmode = RUNMODE_SLEEP;
Expand Down Expand Up @@ -146,6 +146,21 @@ void enter_deepsleep(const uint32_t wakeup_sec, gpio_num_t wakeup_gpio) {
// configure wakeup sources
// https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/system/sleep_modes.html

#ifdef TIME_SYNC_INTERVAL
#if (SYNCWAKEUP) && (SLEEPCYCLE)
time_t now;
uint16_t shift_sec;
time(&now);
shift_sec = 3600 - (now + wakeup_sec) %
3600; // 1..3600 remaining seconds between planned
// wakeup time and following top-of-hour
if (shift_sec <= SYNCWAKEUP) // delay wakeup to catch top-of-hour
wakeup_sec += shift_sec;
else if (shift_sec >= (3600 - SYNCWAKEUP))
wakeup_sec = 3600 - shift_sec; // shorten wake up to next top-of-hour
#endif
#endif

// set up RTC wakeup timer, if we have
if (wakeup_sec > 0) {
esp_sleep_enable_timer_wakeup(wakeup_sec * uS_TO_S_FACTOR);
Expand Down

0 comments on commit eaa8bdf

Please sign in to comment.