Skip to content

Commit 9b09529

Browse files
authored
Add support for (emergency) OTA mode (#109)
* Add support for emergency OTA mode * Add protection for manual reboot loop
1 parent 53ffd46 commit 9b09529

6 files changed

Lines changed: 77 additions & 5 deletions

File tree

ESP32LapTimer/src/Buttons.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "Calibration.h"
99
#include "settings_eeprom.h"
1010
#include "TimerWebServer.h"
11+
#include "CrashDetection.h"
1112

1213
#include <stdint.h>
1314
#include <Arduino.h>
@@ -151,7 +152,7 @@ void newButtonUpdate() {
151152
fiveBeep();
152153
EepromSettings.defaults();
153154
delay(100);
154-
ESP.restart();
155+
restart_esp();
155156
}
156157
}
157158

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include "CrashDetection.h"
2+
3+
#include <rom/rtc.h>
4+
#include <Arduino.h>
5+
6+
// positive values indicate a crashing system. negative values a manual reboot loop
7+
RTC_NOINIT_ATTR static int crash_count = 0;
8+
9+
bool is_crash_mode() {
10+
return (crash_count > MAX_CRASH_COUNT) || (crash_count > -MAX_CRASH_COUNT);
11+
}
12+
13+
void init_crash_detection() {
14+
// crash reason is not sw reset, so not a crash!
15+
if(rtc_get_reset_reason(0) != 12) {
16+
crash_count = 0;
17+
} else {
18+
++crash_count;
19+
}
20+
}
21+
22+
int get_crash_count() {
23+
return crash_count;
24+
}
25+
26+
void reset_crash_count() {
27+
crash_count = 0;
28+
}
29+
30+
void restart_esp() {
31+
--crash_count;
32+
ESP.restart();
33+
}

ESP32LapTimer/src/CrashDetection.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#ifndef _CRASHDETECTION_H_
2+
#define _CRASHDETECTION_H_
3+
4+
#define MAX_CRASH_COUNT 5
5+
6+
bool is_crash_mode();
7+
void init_crash_detection();
8+
void restart_esp();
9+
int get_crash_count();
10+
void reset_crash_count();
11+
12+
#endif // _CRASHDETECTION_H_

ESP32LapTimer/src/ESP32LapTimer.cpp

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@
2424
#include "Laptime.h"
2525
#include "Wireless.h"
2626

27+
#include "CrashDetection.h"
28+
#ifdef USE_ARDUINO_OTA
29+
#include <ArduinoOTA.h>
30+
#endif
31+
2732
//#define BluetoothEnabled //uncomment this to use bluetooth (experimental, ble + wifi appears to cause issues)
2833

2934
static TaskHandle_t adc_task_handle = NULL;
@@ -47,13 +52,21 @@ void IRAM_ATTR adc_task(void* args) {
4752
}
4853

4954
void setup() {
55+
init_crash_detection();
5056

57+
Serial.begin(115200);
58+
Serial.println("Booting....");
59+
#ifdef USE_ARDUINO_OTA
60+
if(is_crash_mode()) {
61+
log_e("Detected crashing. Starting ArduinoOTA only!");
62+
InitWifiAP();
63+
ArduinoOTA.begin();
64+
return;
65+
}
66+
#endif
5167
#ifdef OLED
5268
oledSetup();
5369
#endif
54-
55-
Serial.begin(115200);
56-
Serial.println("Booting....");
5770
#ifdef USE_BUTTONS
5871
newButtonSetup();
5972
#endif
@@ -104,6 +117,13 @@ void setup() {
104117
}
105118

106119
void loop() {
120+
#ifdef USE_ARDUINO_OTA
121+
ArduinoOTA.handle();
122+
if(is_crash_mode()) return;
123+
#endif
124+
if(millis() > CRASH_COUNT_RESET_TIME_MS) {
125+
reset_crash_count();
126+
}
107127
rssiCalibrationUpdate();
108128
// touchMonitor(); // A function to monitor capacitive touch values, defined in buttons.ino
109129

ESP32LapTimer/src/HardwareConfig.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@
4242
// Enable TCP support. Currently this needs a special version of the app: https://github.com/Smeat/Chorus-RF-Laptimer/releases/tag/tcp_support
4343
//#define USE_TCP
4444

45+
// Enables the ArduinoOTA service. It allows flashing over WiFi and enters an emergency mode if a crashloop is detected.
46+
//#define USE_ARDUINO_OTA
47+
4548
// BELOW ARE THE ADVANCED SETTINGS! ONLY CHANGE THEM IF YOU KNOW WHAT YOUR ARE DOING!
4649

4750
#define EEPROM_VERSION_NUMBER 9 // Increment when eeprom struct modified
@@ -54,6 +57,8 @@
5457
// 800 and 2700 are about average min max raw values
5558
#define RSSI_ADC_READING_MAX 2700
5659
#define RSSI_ADC_READING_MIN 800
60+
// defines the time after which the crash loop detection assumes the operation is stable
61+
#define CRASH_COUNT_RESET_TIME_MS 300000
5762

5863
#include "targets/target.h" // Needs to be at the bottom
5964

ESP32LapTimer/src/TimerWebServer.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "ADC.h"
55
#include "RX5808.h"
66
#include "Calibration.h"
7+
#include "CrashDetection.h"
78

89
#include <esp_wifi.h>
910
#include <FS.h>
@@ -273,7 +274,7 @@ void InitWebServer() {
273274
response->addHeader("Connection", "close");
274275
req->send(response);
275276
Serial.println("off-updating");
276-
ESP.restart();
277+
restart_esp();
277278
}, [](AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final) {
278279
isHTTPUpdating = true;
279280
if(!index) {

0 commit comments

Comments
 (0)