Skip to content

Commit

Permalink
Add support for (emergency) OTA mode (#109)
Browse files Browse the repository at this point in the history
* Add support for emergency OTA mode

* Add protection for manual reboot loop
  • Loading branch information
Smeat authored Mar 29, 2020
1 parent 53ffd46 commit 9b09529
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 5 deletions.
3 changes: 2 additions & 1 deletion ESP32LapTimer/src/Buttons.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "Calibration.h"
#include "settings_eeprom.h"
#include "TimerWebServer.h"
#include "CrashDetection.h"

#include <stdint.h>
#include <Arduino.h>
Expand Down Expand Up @@ -151,7 +152,7 @@ void newButtonUpdate() {
fiveBeep();
EepromSettings.defaults();
delay(100);
ESP.restart();
restart_esp();
}
}

Expand Down
33 changes: 33 additions & 0 deletions ESP32LapTimer/src/CrashDetection.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include "CrashDetection.h"

#include <rom/rtc.h>
#include <Arduino.h>

// positive values indicate a crashing system. negative values a manual reboot loop
RTC_NOINIT_ATTR static int crash_count = 0;

bool is_crash_mode() {
return (crash_count > MAX_CRASH_COUNT) || (crash_count > -MAX_CRASH_COUNT);
}

void init_crash_detection() {
// crash reason is not sw reset, so not a crash!
if(rtc_get_reset_reason(0) != 12) {
crash_count = 0;
} else {
++crash_count;
}
}

int get_crash_count() {
return crash_count;
}

void reset_crash_count() {
crash_count = 0;
}

void restart_esp() {
--crash_count;
ESP.restart();
}
12 changes: 12 additions & 0 deletions ESP32LapTimer/src/CrashDetection.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef _CRASHDETECTION_H_
#define _CRASHDETECTION_H_

#define MAX_CRASH_COUNT 5

bool is_crash_mode();
void init_crash_detection();
void restart_esp();
int get_crash_count();
void reset_crash_count();

#endif // _CRASHDETECTION_H_
26 changes: 23 additions & 3 deletions ESP32LapTimer/src/ESP32LapTimer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
#include "Laptime.h"
#include "Wireless.h"

#include "CrashDetection.h"
#ifdef USE_ARDUINO_OTA
#include <ArduinoOTA.h>
#endif

//#define BluetoothEnabled //uncomment this to use bluetooth (experimental, ble + wifi appears to cause issues)

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

void setup() {
init_crash_detection();

Serial.begin(115200);
Serial.println("Booting....");
#ifdef USE_ARDUINO_OTA
if(is_crash_mode()) {
log_e("Detected crashing. Starting ArduinoOTA only!");
InitWifiAP();
ArduinoOTA.begin();
return;
}
#endif
#ifdef OLED
oledSetup();
#endif

Serial.begin(115200);
Serial.println("Booting....");
#ifdef USE_BUTTONS
newButtonSetup();
#endif
Expand Down Expand Up @@ -104,6 +117,13 @@ void setup() {
}

void loop() {
#ifdef USE_ARDUINO_OTA
ArduinoOTA.handle();
if(is_crash_mode()) return;
#endif
if(millis() > CRASH_COUNT_RESET_TIME_MS) {
reset_crash_count();
}
rssiCalibrationUpdate();
// touchMonitor(); // A function to monitor capacitive touch values, defined in buttons.ino

Expand Down
5 changes: 5 additions & 0 deletions ESP32LapTimer/src/HardwareConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
// Enable TCP support. Currently this needs a special version of the app: https://github.com/Smeat/Chorus-RF-Laptimer/releases/tag/tcp_support
//#define USE_TCP

// Enables the ArduinoOTA service. It allows flashing over WiFi and enters an emergency mode if a crashloop is detected.
//#define USE_ARDUINO_OTA

// BELOW ARE THE ADVANCED SETTINGS! ONLY CHANGE THEM IF YOU KNOW WHAT YOUR ARE DOING!

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

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

Expand Down
3 changes: 2 additions & 1 deletion ESP32LapTimer/src/TimerWebServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "ADC.h"
#include "RX5808.h"
#include "Calibration.h"
#include "CrashDetection.h"

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

0 comments on commit 9b09529

Please sign in to comment.