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

Play program #163

Closed msaigla closed 4 months ago

msaigla commented 1 year ago

How can I run the program after downloading it to the controller? In node.js var esp = require("espruino"); esp.init(function() { Espruino.Config.RESET_BEFORE_SEND=true; esp.sendFile("COM10", ".\\UploadFile\\UploadFileJS.js", function() { console.log('Done!'); }) });

gfwilliams commented 1 year ago

By default it should upload to RAM, so the code will be executed automatically. Why do you think it's not being run?

ktrzeciaknubisa commented 4 months ago

I think I experience the same issue - the script seems to be uploaded successfully, but it does not execute. I am trying to run the LED1 blink sample

var  on = false;
setInterval(function() {
  on = !on;
  LED1.write(on);
}, 500);

and it does work when uploaded via Espruino IDE (Native) (the led is blinking). (side note: the browser version cannot connect via Web Serial connection)

Then I try to do the same with espruino tools or with espruino npm module, and despite I see no error, the led is not blinking.

Espruino tools:

C:\>espruino -p COM3 --board ESP32 "C:\blinkLed1.js"
Espruino Command-line Tool 0.1.55
-----------------------------------

Explicit board JSON supplied: "ESP32"
Connecting to 'COM3'
Connected
Upload Complete

C:\Prv\Espruino\EspruinoTools>

Espruino NPM Module

The js code is pretty much as the one used by @msaigla The output is way larger, but this is the tail of it:

Already initialised.
Noble: getPorts - disabled
Connected Ok
Got ""
No Prompt found, got undefined - issuing Ctrl-C to try and break out
Splitting for Ctrl-C, delay 250
Still no prompt - issuing another Ctrl-C
Splitting for Ctrl-C, delay 250
Sending...
---> "\u0010print(\"<\",\"<<\",JSON.stringify(process.env),\">>\",\">\")\n"
Sent
Receiving...
No result found for "process.env" - just got ""

Unable to retrieve board information.
Connection Error?
Minifying
Minification complete
Got ""
No Prompt found, got undefined - issuing Ctrl-C to try and break out
Splitting for Ctrl-C, delay 250
Still no prompt - issuing another Ctrl-C
Splitting for Ctrl-C, delay 250
Sending...
---> "\u0010reset();\n\u0010print()\n\u0010setTime(1714853595.315);E.setTimeZone(2)\n\u0010var  on = false;\n\u0010setInterval(function() {\u001b\n  on = !on;\u001b\n  LED1.write(on);\u001b\n}, 500);\n\n"
Splitting for reset(), delay 250
Sent
Done!

My environment

Windows 10

C:\>node --version
v16.15.0
C:\>npm --version
8.8.0

BTW. Which version of node/npm is know to be best compatible? I got some vulnerabilities errors / warnings when running npm i espruino

C:\Prv\Espruino\ET>npm i espruino
npm WARN deprecated domexception@1.0.1: Use your platform's native DOMException instead

added 70 packages, and audited 71 packages in 22s

5 vulnerabilities (1 moderate, 4 high)
ktrzeciaknubisa commented 4 months ago

I should also probably add that -e does not work as well:

C:\>espruino -p COM3 --board ESP32 -e "digitalWrite(LED1,1)"
Espruino Command-line Tool 0.1.55
-----------------------------------

Explicit board JSON supplied: "ESP32"
Connecting to 'COM3'
Connected
Upload Complete

but again, it does work when uploaded via Espruino IDE (Native) (the led is ON).

ktrzeciaknubisa commented 4 months ago

Many hours later... I think I found a workaround.

Few factors are required (if I omit at least one of them, those steps won't succeed):

  1. I needed to add -b 115200 explicitly
  2. run espruino tools command 2 times
  3. press a hardware RESET on Arduino board between the uploads

Those are the steps:

1. Upload first time

c:\>espruino c:\blinkLed1.js --board ESP32 -p COM3 -b 115200
Espruino Command-line Tool 0.1.55
-----------------------------------

Explicit board JSON supplied: "ESP32"
Connecting to 'COM3'
Connected
Upload Complete

At this point nothing happpened just yet (the script did not run - Led is not blinking)

2. Press hardware RESET button (on my ESP32 this is EN button)

3, Upload second time - with the very exact command

c:\>espruino c:\blinkLed1.js --board ESP32 -p COM3 -b 115200
Espruino Command-line Tool 0.1.55
-----------------------------------

Explicit board JSON supplied: "ESP32"
Connecting to 'COM3'
Connected
--]
--]  ____                 _
--] |  __|___ ___ ___ _ _|_|___ ___
--] |  __|_ -| . |  _| | | |   | . |
--] |____|___|  _|_| |___|_|_|_|___|
--]          |_| espruino.com
--]  2v21 (c) 2023 G.Williams
--]
--] Espruino is Open Source. Our work is supported
--] only by sales of official boards and donations:
--] http://espruino.com/Donate
--]
--] >
Upload Complete

Note, that the otuput differs now (Espruino logo is shown) and also, the LED is blinking!

4. Change the program and reupload - from now on it will work

From now on, I can change the blinkLed1.js file, then upload with the very same command from above and the new code will execute just fine!

gfwilliams commented 4 months ago

Glad you got it sorted!

I think as you found the main issue is the baud rate. It's probably why Web Serial didn't work for you either (there's a setting in the Web IDE that you can set).

The reason is that official Espruino boards use 9600 baud, but the folks working on the ESP8266/32 ports decided not to do the same and to use 115200 instead - so now all ESP32 users have to remember to change baud rate each time.

Probably the second run of the tools was needed because when running without the baud rate set, some corrupt data would have been sent to the board, and that needed 'flushing out' before the upload would work,