• Thanks to Wilberforce I was able to write a constant string to flash memory. While a constant string is nice, my long term goal is to serve a single page app from flash or a SD card with a file size of 1, 2MB or more. My next baby step was to write a single file of ~800 bytes to flash. Unfortunately after dropping my baud all the way down to 9600 to send a file in binary form to my ESP32, I still lose arbitrary blocks of data. It might be 1 to 20 bytes, but it means I am a long way from sending 200-500 KB file to the my ESP32 flash or SD card. Here's my code(mangled from over a week of hacking) and sample file. The need to write data from a serial link is not only for saving server files, but also for being able to save production data from other devices.

    
    var formatFlashDrive = false;
    var writeFile = true;
    
    Serial2.setup(9600, { tx: D17, rx: D16 });
    var fileWriteStarted = false;
    var fileName = 'index.html';
    var fs = require("fs");
    var intervalID, existingBlinkRate = 0, timerCount = 0;
    Serial2.print("Are you there?");
    
    if(formatFlashDrive){
      console.log('Formatting FS - only need to do once');   
      try {
        console.log("Flash Files: ",fs.readdirSync());
        } catch (e) { 
          console.log('Uncaught Error: Unable to mount media : NO_FILESYSTEM'.e);
           E.flashFatFS({addr : int=0x300000, sectors : int=256, format : bool=false }); 
      }
    }
    if(!writeFile){console.log("Ready to write to fs");}
    if(!writeFile){
      console.log("Reading index.html from flash");
      try{
        console.log(fs.readFileSync("index.html"­)); // prints "Hello World!!!"
        Serial2.write(fs.readFileSync("index.htm­l"));
      }catch(e){
        console.log("File Read Error = ",e); 
        Serial2.write('File Read Error');
      }
      //flashLED(10);
    }
    Serial2.on('data',function(data){
      if(!writeFile)return;
      if(!fileWriteStarted){ 
        setTimeout(function(){
          console.log('Reading ',fileName);
          Serial2.write(fs.readFileSync("index.htm­l"));
        },2000);
        fileWriteStarted = true;
        console.log('Writing data');
        fs.writeFileSync(fileName,data);
        return;
      }
      fs.appendFileSync(fileName, data);
      console.log('Appending data');
    });
    

    1 Attachment

About

Avatar for miCatotonic @miCatotonic started