Skip to content

Commit

Permalink
Added firmware update via SD card
Browse files Browse the repository at this point in the history
  • Loading branch information
saschagu committed Jan 22, 2021
1 parent afe22ae commit 428d0aa
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 6 deletions.
12 changes: 6 additions & 6 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ platform = espressif32
board = esp-wrover-kit
framework = arduino
monitor_speed = 115200
board_build.partitions = huge_ap.csv
board_build.partitions = min_spiffs.csv
build_flags = -DHAL=2
-DBOARD_HAS_PSRAM
-mfix-esp32-psram-cache-issue
Expand All @@ -54,7 +54,7 @@ platform = espressif32
board = lolin32
framework = arduino
monitor_speed = 115200
board_build.partitions = huge_app.csv
board_build.partitions = min_spiffs.csv
lib_deps =
${common.lib_deps_builtin}
${common.lib_deps_external}
Expand All @@ -70,7 +70,7 @@ platform = espressif32
board = lolin_d32
framework = arduino
monitor_speed = 115200
board_build.partitions = huge_app.csv
board_build.partitions = min_spiffs.csv
lib_deps =
${common.lib_deps_builtin}
${common.lib_deps_external}
Expand All @@ -86,7 +86,7 @@ platform = espressif32
board = lolin_d32_pro
framework = arduino
monitor_speed = 115200
board_build.partitions = huge_app.csv
board_build.partitions = min_spiffs.csv
lib_deps =
${common.lib_deps_builtin}
${common.lib_deps_external}
Expand All @@ -104,7 +104,7 @@ platform = espressif32
board = nodemcu-32s
framework = arduino
monitor_speed = 115200
board_build.partitions = huge_app.csv
board_build.partitions = min_spiffs.csv
lib_deps =
${common.lib_deps_builtin}
${common.lib_deps_external}
Expand All @@ -120,7 +120,7 @@ platform = espressif32
board = az-delivery-devkit-v4
framework = arduino
monitor_speed = 115200
board_build.partitions = huge_app.csv
board_build.partitions = min_spiffs.csv
lib_deps =
${common.lib_deps_builtin}
${common.lib_deps_external}
Expand Down
68 changes: 68 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <ESP32Encoder.h>
#include "Arduino.h"
#include <WiFi.h>
#include <Update.h>
#ifdef MDNS_ENABLE
#include <ESPmDNS.h>
#endif
Expand Down Expand Up @@ -4143,6 +4144,68 @@ void printWakeUpReason() {
}
#endif


void performUpdate(Stream &updateSource, size_t updateSize) {
if (Update.begin(updateSize)) {
size_t written = Update.writeStream(updateSource);
if (written == updateSize) {
Serial.println("Written : " + String(written) + " successfully");
}
else {
Serial.println("Written only : " + String(written) + "/" + String(updateSize) + ". Retry?");
}
if (Update.end()) {
Serial.println("OTA done!");
if (Update.isFinished()) {
Serial.println("Update successfully completed. Rebooting.");
}
else {
Serial.println("Update not finished? Something went wrong!");
}
}
else {
Serial.println("Error Occurred. Error #: " + String(Update.getError()));
}

}
else
{
Serial.println("Not enough space to begin OTA");
}
}

// check given FS for valid update.bin and perform update if available
void updateFromFS(fs::FS &fs) {
File updateBin = fs.open("/update.bin");
if (updateBin) {
if(updateBin.isDirectory()){
Serial.println("Error, update.bin is not a file");
updateBin.close();
return;
}

size_t updateSize = updateBin.size();

if (updateSize > 0) {
Serial.println("Try to start update");
performUpdate(updateBin, updateSize);
}
else {
Serial.println("Error, file is empty");
}

updateBin.close();

// whe finished remove the binary from sd card to indicate end of the process
fs.remove("/update.bin");
}
else {
Serial.println("Could not load update.bin from sd root");
}
}



void setup() {
Serial.begin(115200);
esp_sleep_enable_ext0_wakeup((gpio_num_t) DREHENCODER_BUTTON, 0);
Expand Down Expand Up @@ -4303,6 +4366,11 @@ void setup() {
Serial.println(F("UNKNOWN"));
}

// for update from SD card see example at:
// https://github.com/espressif/arduino-esp32/blob/master/libraries/Update/examples/SD_Update/SD_Update.ino
Serial.println("looking for update.bin");
updateFromFS(SD);

#ifdef HEADPHONE_ADJUST_ENABLE
pinMode(HP_DETECT, INPUT);
headphoneLastDetectionState = digitalRead(HP_DETECT);
Expand Down

0 comments on commit 428d0aa

Please sign in to comment.