|
| 1 | +/* |
| 2 | + Name: SD_Update.ino |
| 3 | + Created: 12.09.2017 15:07:17 |
| 4 | + Author: Frederik Merz <[email protected]> |
| 5 | + Purpose: Update firmware from SD card |
| 6 | +
|
| 7 | + Steps: |
| 8 | + 1. Flash this image to the ESP32 an run it |
| 9 | + 2. Copy update.bin to a SD-Card, you can basically |
| 10 | + compile this or any other example |
| 11 | + then copy and rename the app binary to the sd card root |
| 12 | + 3. Connect SD-Card as shown in SD_MMC example, |
| 13 | + this can also be adapted for SPI |
| 14 | + 3. After successfull update and reboot, ESP32 shall start the new app |
| 15 | +*/ |
| 16 | + |
| 17 | +#include <Update.h> |
| 18 | +#include <FS.h> |
| 19 | +#include <SD_MMC.h> |
| 20 | + |
| 21 | +// perform the actual update from a given stream |
| 22 | +void performUpdate(Stream &updateSource, size_t updateSize) { |
| 23 | + if (Update.begin(updateSize)) { |
| 24 | + size_t written = Update.writeStream(updateSource); |
| 25 | + if (written == updateSize) { |
| 26 | + Serial.println("Written : " + String(written) + " successfully"); |
| 27 | + } |
| 28 | + else { |
| 29 | + Serial.println("Written only : " + String(written) + "/" + String(updateSize) + ". Retry?"); |
| 30 | + } |
| 31 | + if (Update.end()) { |
| 32 | + Serial.println("OTA done!"); |
| 33 | + if (Update.isFinished()) { |
| 34 | + Serial.println("Update successfully completed. Rebooting."); |
| 35 | + } |
| 36 | + else { |
| 37 | + Serial.println("Update not finished? Something went wrong!"); |
| 38 | + } |
| 39 | + } |
| 40 | + else { |
| 41 | + Serial.println("Error Occurred. Error #: " + String(Update.getError())); |
| 42 | + } |
| 43 | + |
| 44 | + } |
| 45 | + else |
| 46 | + { |
| 47 | + Serial.println("Not enough space to begin OTA"); |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +// check given FS for valid update.bin and perform update if available |
| 52 | +void updateFromFS(fs::FS &fs) { |
| 53 | + File updateBin = fs.open("/update.bin"); |
| 54 | + if (updateBin) { |
| 55 | + if(updateBin.isDirectory()){ |
| 56 | + Serial.println("Error, update.bin is not a file"); |
| 57 | + updateBin.close(); |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + size_t updateSize = updateBin.size(); |
| 62 | + |
| 63 | + if (updateSize > 0) { |
| 64 | + Serial.println("Try to start update"); |
| 65 | + performUpdate(updateBin, updateSize); |
| 66 | + } |
| 67 | + else { |
| 68 | + Serial.println("Error, file is empty"); |
| 69 | + } |
| 70 | + |
| 71 | + updateBin.close(); |
| 72 | + |
| 73 | + // whe finished remove the binary from sd card to indicate end of the process |
| 74 | + fs.remove("/update.bin"); |
| 75 | + } |
| 76 | + else { |
| 77 | + Serial.println("Could not load update.bin from sd root"); |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +void setup() { |
| 82 | + uint8_t cardType; |
| 83 | + Serial.begin(115200); |
| 84 | + Serial.println("Welcome to the SD-Update example!"); |
| 85 | + |
| 86 | + // You can uncomment this and build again |
| 87 | + // Serial.println("Update successfull"); |
| 88 | + |
| 89 | + //first init and check SD card |
| 90 | + if (!SD_MMC.begin()) { |
| 91 | + Serial.println("Card Mount Failed"); |
| 92 | + goto end; |
| 93 | + } |
| 94 | + |
| 95 | + cardType = SD_MMC.cardType(); |
| 96 | + |
| 97 | + if (cardType == CARD_NONE) { |
| 98 | + Serial.println("No SD_MMC card attached"); |
| 99 | + goto end; |
| 100 | + } |
| 101 | + |
| 102 | + updateFromFS(SD_MMC); |
| 103 | + |
| 104 | +end: |
| 105 | + delay(1000); |
| 106 | + ESP.restart(); |
| 107 | +} |
| 108 | + |
| 109 | +//will not be reached |
| 110 | +void loop() { |
| 111 | + |
| 112 | +} |
0 commit comments