- uint64_t SD_CARD_INFO[5] = {0,0,0,0,0}; //type and present, size, totalbyte, used, freebyte
- Preferences nvs_;
- void gatto_fw_set_writable(){
- nvs_.putBool("update",true);
- }
- void gatto_fw_set_protect(){
- nvs_.putBool("update",false);
- }
- bool gatto_fw_get_staus(){
- bool str = nvs_.getBool("update");
- return str;
- }
- void firmware_progress(size_t currSize, size_t totalSize) {
- Serial.printf("FIRMWARE: Update %d of %d bytes...\n", currSize, totalSize);
- }
- void firmware_update(){
- if(SD_CARD_INFO[0]>0){
- String pluto = gatto_fw_get_staus(); // enable update (?)
- if(pluto=="true"){
- if (SD.exists("/firmware.bin")) {
- SD.remove("/firmware.bak");
- if (SD.rename("/firmware.bin", "/firmware.bak")){
- // Serial.println(F("Firmware rename succesfully!"));
- gatto_fw_set_protect(); // disable update (!)
- File firmware = SD.open("/firmware.bak");
- if (firmware) {
- Update.onProgress(firmware_progress);
- Update.begin(firmware.size(), U_FLASH);
- Update.writeStream(firmware);
- if (Update.end()){
- // Serial.println(F("Update finished!"));
- }else{
- //Serial.println(F("Update error!"));
- //Serial.println(Update.getError());
- }
- firmware.close();
- // delay(2000);
- ESP.restart();
- }else{
- // Serial.println(F("error reading firmware"));
- }
- }else{
- // Serial.println(F("error rename firmware"));
- }
- }else{
- // Serial.println(F("No new firmware"));
- }
- }else{
- if (SD.exists("/firmware.bin")) {
- SD.remove("/firmware.bin");
- }
- }
- }
- }
- void setup_sdcard(){
- pinMode(SD_CS, OUTPUT);
- digitalWrite(SD_CS, HIGH);
- SPI.begin(SDMMC_CLK, SDMMC_D0, SDMMC_CMD);
- if(!SD.begin(SD_CS)){
- // if(!SD.begin(SD_CS)){
- SD_CARD_INFO[0] = 0;
- }else{
- SD_CARD_INFO[0] = SD.cardType();
- //onError["SD"]["type"] = tipo;
- SD_CARD_INFO[1] = SD.cardSize() / (1024 * 1024);
- //onError["SD"]["size"] = cardSize;
- SD_CARD_INFO[2] = SD.totalBytes() / (1024 * 1024);
- //onError["SD"]["totalbytes"] = totalBytes;
- SD_CARD_INFO[3] = SD.usedBytes() / (1024 * 1024);
- //onError["SD"]["usedbytes"] = usedBytes;
- SD_CARD_INFO[4] = (SD_CARD_INFO[2] - SD_CARD_INFO[3]); //free
- }
- }
- setup(){
- Serial.begin(115200);
- setup_sdcard();
- firmware_update();
- ....
(arduino) update on reboot, require sd, + webserver upload
(arduino) update on reboot, require sd, + webserver upload
Who is online
Users browsing this forum: No registered users and 6 guests