espruino / EspruinoTools

JavaScript library of tools for Espruino - used for the Web IDE, CLI, etc.
Apache License 2.0
150 stars 89 forks source link

Upload code to flash using node module #143

Open RobertSmart opened 3 years ago

RobertSmart commented 3 years ago

Hi, I'm trying to upload a file to the flash memory on an ESP32 so that I can stream it (I'm currently doing this using storage, but thought I would try and use the extra space in flash for something useful.)

I've run E.flashFatFS({ format: true }); so I know its set up to receive a file.

The file I'm trying to upload is a minififed html file.

using the cmd line and the --storage option I can upload this just fine to storage. However I'm trying to send this to the flashFile system using this code:

`var esp = require("espruino") var fs = require ('fs') var path = require('path') let filename = 'site.html' let fpath = './dist'

esp.init(function(e){ Espruino.Config.BAUD_RATE = "115200"; Espruino.Config.BLUETOOTH_LOW_ENERGY =false;

let fileContents = fs.readFileSync(path.join(fpath,filename))

let code = require('fs').writeFileSync('${filename}', '${fileContents}');

esp.sendCode("COM5", code, function(result) { console.log('GOT RESULT') console.log(result) }); });`

The error I'm getting is

SyntaxError: Unexpected token (1:2261) at Parser.pp$5.raise (D:\Code\Embedded\Sensor_Stick\node_modules\acorn\dist\acorn.js:3416:15) at Parser.pp.unexpected (D:\Code\Embedded\Sensor_Stick\node_modules\acorn\dist\acorn.js:741:10) at Parser.pp.expect (D:\Code\Embedded\Sensor_Stick\node_modules\acorn\dist\acorn.js:735:28) at Parser.pp$4.parseExprList (D:\Code\Embedded\Sensor_Stick\node_modules\acorn\dist\acorn.js:3283:14) at Parser.pp$4.parseSubscript (D:\Code\Embedded\Sensor_Stick\node_modules\acorn\dist\acorn.js:2645:27) at Parser.pp$4.parseSubscripts (D:\Code\Embedded\Sensor_Stick\node_modules\acorn\dist\acorn.js:2602:26) at Parser.pp$4.parseExprSubscripts (D:\Code\Embedded\Sensor_Stick\node_modules\acorn\dist\acorn.js:2586:23) at Parser.pp$4.parseMaybeUnary (D:\Code\Embedded\Sensor_Stick\node_modules\acorn\dist\acorn.js:2549:19) at Parser.pp$4.parseExprOps (D:\Code\Embedded\Sensor_Stick\node_modules\acorn\dist\acorn.js:2482:21) at Parser.pp$4.parseMaybeConditional (D:\Code\Embedded\Sensor_Stick\node_modules\acorn\dist\acorn.js:2465:21) { pos: 2261, loc: Position { line: 1, column: 2261 }, raisedAt: 2268 }

So I'm assuming its something to do with the quoted strings in the file?

I can upload a simple text string using this technique, but not a whole file. How does the cmd line encode the file so as not to fall foul of this same issue?

Thanks,

Rob Smart

RobertSmart commented 2 years ago

would converting to hex or something before hand solve the issue or will that cause other issues?

GermanWarez commented 2 years ago

From my experience with an ESP8266:

  1. Convert the source to hex, using small chunks that will fit into the memory of the ESP8266, and leave a lot of free memory available for the interpreter
  2. Write the decoded hex chunks to 'Storage'
  3. Hard reset the ESP8266
  4. Copy chunks that fit into memory from 'Storage' to 'fs'.

Tested with files up to 100KB, with html, javascript and images.