Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions examples/simple_repeater/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ SimpleMeshTables tables;
MyMesh the_mesh(board, radio_driver, *new ArduinoMillis(), fast_rng, rtc_clock, tables);

void halt() {
delay(5000);
board.reboot();
while (1) ;
}

Expand Down Expand Up @@ -205,6 +207,26 @@ void loop() {
#ifdef HAS_EXTERNAL_WATCHDOG
external_watchdog.loop();
#endif

uint16_t reboot_hours = the_mesh.getNodePrefs()->auto_reboot_hours;
if (reboot_hours > 0) {
unsigned long reboot_interval_ms = (unsigned long)reboot_hours * 3600000UL;
#if defined(ESP_PLATFORM)
bool ota_active = board.isSleepInhibited();
#else
bool ota_active = false;
#endif
if (!ota_active && millis() >= reboot_interval_ms) {
if (!the_mesh.hasPendingWork() || millis() >= (reboot_interval_ms + 10000UL)) {
if (the_mesh.getNodePrefs()->isDirty()) {
the_mesh.savePrefs();
}
Serial.println("Scheduled auto_reboot: cycling peripheral power and restarting...");
board.reboot();
}
}
}

if (the_mesh.getNodePrefs()->powersaving_enabled && !the_mesh.hasPendingWork()) {
#if defined(NRF52_PLATFORM)
board.sleep(0); // nrf ignores seconds param, sleeps whenever possible
Expand Down
13 changes: 8 additions & 5 deletions merge-bin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import os

Import("env", "projenv")
Import("env")

board_config = env.BoardConfig()
firmware_bin = "${BUILD_DIR}/${PROGNAME}.bin"
Expand All @@ -13,9 +13,12 @@

def merge_bin_action(source, target, env):
flash_images = [
*env.Flatten(env.get("FLASH_EXTRA_IMAGES", [])),
"$ESP32_APP_OFFSET",
source[0].get_abspath(),
f'"{env.subst(str(x))}"' if " " in env.subst(str(x)) else env.subst(str(x))
for x in [
*env.Flatten(env.get("FLASH_EXTRA_IMAGES", [])),
"$ESP32_APP_OFFSET",
source[0].get_abspath(),
]
]
merge_cmd = " ".join(
[
Expand All @@ -25,7 +28,7 @@ def merge_bin_action(source, target, env):
board_config.get("build.mcu", "esp32"),
"merge_bin",
"-o",
merged_bin,
f'"{env.subst(merged_bin)}"',
"--flash_mode",
board_config.get("build.flash_mode", "dio"),
"--flash_freq",
Expand Down
23 changes: 23 additions & 0 deletions src/helpers/CommonCLI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,7 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, char* command, char* re

void CommonCLI::handleSetCmd(uint32_t sender_timestamp, char* command, char* reply) {
const char* config = &command[4];
while (*config == ' ') config++;

if (memcmp(config, "allow.read.only ", 16) == 0) {
_prefs->allow_read_only = memcmp(&config[16], "on", 2) == 0;
Expand Down Expand Up @@ -657,6 +658,21 @@ void CommonCLI::handleSetCmd(uint32_t sender_timestamp, char* command, char* rep
savePrefs();
strcpy(reply, "OK");
#endif
} else if (memcmp(config, "auto_reboot ", 12) == 0 || memcmp(config, "auto.reboot ", 12) == 0) {
const char* val = &config[12];
while (*val == ' ') val++;
int hours = _atoi(val);
if (hours >= 0 && hours <= 720) {
_prefs->auto_reboot_hours = (uint16_t)hours;
savePrefs();
if (hours == 0) {
strcpy(reply, "OK - auto_reboot disabled");
} else {
sprintf(reply, "OK - auto_reboot every %d hours", hours);
}
} else {
strcpy(reply, "Error: hours must be between 0-720");
}
} else if (memcmp(config, "adc.multiplier ", 15) == 0) {
_prefs->adc_multiplier = atof(&config[15]);
if (_board->setAdcMultiplier(_prefs->adc_multiplier)) {
Expand Down Expand Up @@ -700,6 +716,7 @@ void CommonCLI::handleSetCmd(uint32_t sender_timestamp, char* command, char* rep

void CommonCLI::handleGetCmd(uint32_t sender_timestamp, char* command, char* reply) {
const char* config = &command[4];
while (*config == ' ') config++;
if (memcmp(config, "allow.read.only", 15) == 0) {
sprintf(reply, "> %s", _prefs->allow_read_only ? "on" : "off");
} else if (memcmp(config, "flood.advert.interval", 21) == 0) {
Expand Down Expand Up @@ -791,6 +808,12 @@ void CommonCLI::handleGetCmd(uint32_t sender_timestamp, char* command, char* rep
#else
strcpy(reply, "Error: unsupported");
#endif
} else if (memcmp(config, "auto_reboot", 11) == 0 || memcmp(config, "auto.reboot", 11) == 0) {
if (_prefs->auto_reboot_hours == 0) {
strcpy(reply, "> 0 (disabled)");
} else {
sprintf(reply, "> %u", (unsigned)_prefs->auto_reboot_hours);
}
} else if (memcmp(config, "adc.multiplier", 14) == 0) {
float adc_mult = _board->getAdcMultiplier();
if (adc_mult == 0.0f) {
Expand Down
2 changes: 2 additions & 0 deletions src/helpers/CommonCLI.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class NodePrefs : public ConfigSerializer {
char bridge_secret[16]; // for XOR encryption of bridge packets (ESP-NOW only)
// Power setting
uint8_t powersaving_enabled = 0; // boolean
uint16_t auto_reboot_hours = 0; // hours between automatic reboots (0 = disabled)
// Gps settings
uint8_t gps_enabled = 0;
uint32_t gps_interval = 0; // in seconds
Expand Down Expand Up @@ -171,6 +172,7 @@ class NodePrefs : public ConfigSerializer {
void structure() override {
def("adc_mult", _parent->adc_multiplier);
def("pwr_sav_en", _parent->powersaving_enabled);
def("auto_reboot", _parent->auto_reboot_hours);
}
public:
PowerPrefs(NodePrefs* parent) : _parent(parent) { }
Expand Down
42 changes: 42 additions & 0 deletions src/helpers/ESP32Board.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,48 @@ bool ESP32Board::startOTAUpdate(const char* id, char reply[]) {
}
#endif

void ESP32Board::reboot() {
#ifdef DISPLAY_CLASS
display.turnOff();
#endif
radio_driver.powerOff();

if (sensors.getLocationProvider() != NULL) {
sensors.getLocationProvider()->stop();
}

#if defined(P_LORA_PA_POWER) && P_LORA_PA_POWER >= 0
pinMode(P_LORA_PA_POWER, OUTPUT);
digitalWrite(P_LORA_PA_POWER, LOW);
#endif

#if defined(SX126X_RXEN) && SX126X_RXEN >= 0
pinMode(SX126X_RXEN, OUTPUT);
digitalWrite(SX126X_RXEN, LOW);
#endif

#if defined(P_LORA_RESET) && P_LORA_RESET >= 0
pinMode(P_LORA_RESET, OUTPUT);
digitalWrite(P_LORA_RESET, LOW);
#endif

#if defined(PIN_VEXT_EN) && PIN_VEXT_EN >= 0
pinMode(PIN_VEXT_EN, OUTPUT);
digitalWrite(PIN_VEXT_EN, !PIN_VEXT_EN_ACTIVE);
#endif

Serial.flush();

// Hold radio & external power rails in reset/off state for 5 seconds
for (int i = 0; i < 50; i++) {
esp_task_wdt_reset();
delay(100);
}

rtc_clock.tick();
esp_restart();
}

void ESP32Board::powerOff() {
enterDeepSleep(0); // Do not wakeup
}
Expand Down
21 changes: 17 additions & 4 deletions src/helpers/ESP32Board.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <Wire.h>
#include "soc/rtc.h"
#include "esp_system.h"
#include <esp_task_wdt.h>
#include <driver/rtc_io.h>
#include <helpers/KeyValueStore.h>

Expand Down Expand Up @@ -49,7 +50,15 @@ class ESP32Board : public mesh::MainBoard {
#endif
#else
Wire.begin();
#endif
#endif

// Enable 60-second hardware Task Watchdog Timer for unattended reliability
esp_task_wdt_init(60, true);
esp_task_wdt_add(NULL);
}

void loop() override {
esp_task_wdt_reset();
}

void attachDynamicPrefs(KeyValueStore* prefs) { } // no-op
Expand Down Expand Up @@ -103,7 +112,9 @@ class ESP32Board : public mesh::MainBoard {
gpio_wakeup_enable((gpio_num_t)wakeupPin, GPIO_INTR_HIGH_LEVEL); // Wake up when receiving a LoRa packet

// MCU enters light sleep
esp_task_wdt_reset();
esp_light_sleep_start();
esp_task_wdt_reset();

// Avoid ISR flood during wakeup due to HIGH LEVEL interrupt
gpio_wakeup_disable(wakeupPin);
Expand Down Expand Up @@ -153,16 +164,18 @@ class ESP32Board : public mesh::MainBoard {
return "Generic ESP32";
}

void reboot() override {
esp_restart();
}
void reboot() override;

bool startOTAUpdate(const char* id, char reply[]) override;

void setInhibitSleep(bool inhibit) {
inhibit_sleep = inhibit;
}

bool isSleepInhibited() const {
return inhibit_sleep;
}

uint32_t getResetReason() const override {
return esp_reset_reason();
}
Expand Down
6 changes: 6 additions & 0 deletions variants/heltec_v4/HeltecV4Board.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "HeltecV4Board.h"
#include "target.h"

void HeltecV4Board::begin() {
ESP32Board::begin();
Expand Down Expand Up @@ -32,6 +33,11 @@ void HeltecV4Board::begin() {
loRaFEMControl.setRxModeEnable();
}

void HeltecV4Board::reboot() {
loRaFEMControl.setSleepModeEnable();
ESP32Board::reboot();
}

void HeltecV4Board::powerOff() {
// Turn off PA
digitalWrite(P_LORA_PA_POWER, LOW);
Expand Down
1 change: 1 addition & 0 deletions variants/heltec_v4/HeltecV4Board.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class HeltecV4Board : public ESP32Board {

void onBeforeTransmit(void) override;
void onAfterTransmit(void) override;
void reboot() override;
void powerOff() override;
uint16_t getBattMilliVolts() override;
bool setAdcMultiplier(float multiplier) override {
Expand Down
2 changes: 1 addition & 1 deletion variants/heltec_v4/LoRaFEMControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ void LoRaFEMControl::init(void)

esp_reset_reason_t reason = esp_reset_reason();
if (reason != ESP_RST_DEEPSLEEP) {
delay(1); // FEM startup time after cold power-on
delay(10); // FEM startup time after cold power-on or scheduled power-cycle
}

// Auto-detect FEM type via shared GPIO2 default pull level.
Expand Down
1 change: 1 addition & 0 deletions variants/xiao_s3_wio/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ build_flags = ${esp32_base.build_flags}
-I variants/xiao_s3_wio
-UENV_INCLUDE_GPS
-D SEEED_XIAO_S3
-D PIN_VBAT_READ=1 ; D0
-D P_LORA_DIO_1=39
-D P_LORA_NSS=41
-D P_LORA_RESET=42 ; RADIOLIB_NC
Expand Down