0

Welcome, currently, I want to upload code to Arduino using JavaScript (NodeJS), I HATE using FIRMATA to upload code, I want to use the official Arduino create agent and Arduino create agent js client

https://github.com/arduino/arduino-create-agent-js-client

I just downloaded the arduino create agent, I run the below in empty directory

git clone https://github.com/arduino/arduino-create-agent-js-client.git

I inserted arduino nano(old bootloader) in COM7, thus I changed(edited) the file .\demo\app.jsx line 189-205 to,

handleUpload() {
    const target = {
      board: 'arduino:avr:nano',
      port: 'COM7',
      network: false
    };

    this.setState({ uploadingPort: target.port });
    daemon.boardPortAfterUpload.subscribe(portStatus => {
      if (portStatus.hasChanged) {
        this.setState({ uploadingPort: portStatus.newPort });
      }
    });

    // Upload a compiled sketch.
    daemon.uploadSerial(target, 'serial_mirror', { bin: HEX });
  }

then I run

npm run dev

since the local host runs on http://localhost:8000/ I edited the config file

C:\Users\USER\AppData\Roaming\ArduinoCreateAgent\config.ini

and set origins = http://localhost:8000

that is

gc = std  # Type of garbage collection. std = Normal garbage collection allowing system to decide (this has been known to cause a stop the world in the middle of a CNC job which can cause lost responses from the CNC controller and thus stalled jobs. use max instead to solve.), off = let memory grow unbounded (you have to send in the gc command manually to garbage collect or you will run out of RAM eventually), max = Force garbage collection on each recv or send on a serial port (this minimizes stop the world events and thus lost serial responses, but increases CPU usage)
hostname = unknown-hostname  # Override the hostname we get from the OS
regex = usb|acm|com  # Regular expression to filter serial port list
v = true  # show debug logging
appName = CreateAgent/Stable
updateUrl = https://downloads.arduino.cc/
origins = http://localhost:8000 #this is where I have setted
#httpProxy = http://your.proxy:port # Proxy server for HTTP requests
crashreport = false # enable crashreport logging

issue

it keep on uploading as shown above

please help

Neptotech -vishnu
  • 1,096
  • 8
  • 23

1 Answers1

2

There are a few steps to get this working:

  • Install the Arduino Create Agent
  • Modify the config.ini file to set origins = http://localhost:8000
  • Update the target board and port in demo\app.jsx
  • Replace the sketch to download to the board in demo\serial_mirror.js
  • Allow the demo app to reach builder.arduino.cc by disabling CORS in your browser

You have done the first two, part of the third and none of the last two.

Determine the target board

The Fully Qualified Board Name (FQBN) needs to be updated in demo\app.jsx. The FQBN can be obtained from the compilation output window in the Arduino app. When you build the sketch, the output window in the Arduino app will contain an -fqbn argument. e.g. -fqbn=arduino:avr:nano:cpu=atmega328

Copy this FQBN after the = and update the board property of the target object alongside the port for your board.

e.g.

    const target = {
      board: 'arduino:avr:nano:cpu=atmega328',
      port: 'COM7',
      network: false
    };

Replacing the sketch

The demo app hard codes the sketch to download in serial_mirror.js

This can be replaced with the desired sketch by exporting the compiled binary from the Arduino app. See screenshot below. The exported binary is saved to the sketch folder ("Show Sketch folder" on the "Sketch" menu will display this folder). Note that the sketch needs to be writable (e.g. the example sketchs do not produce binaries in this sketch folder).

Exporting a complied Binary

The exported binary needs to be base 64 encoded. The exported binary will have a hex or bin extension depending on the type of board you are using. e.g. for avr boards convert the file named <sketch>.ino.with_bootloader.standard.hex in the sketch folder.

You can convert the binary with the following command in WSL (Windows Subsystem for Linux):

base64 -w 0 sketch.ino.with_bootloader.standard.hex > exported.sketch.b64

Alternatively, using powershell:

[System.Convert]::ToBase64String([System.IO.File]::ReadAllBytes("c:\<path to sketch.ino.with_bootloader.standard.hex>")) | Set-Content -Path "C:\<path to exported.sketch.b64>"

The string in demo\serial_mirror.js needs to be replaced with the new sketch's base 64 string. i.e.

export const HEX = '<string from exported.sketch.b64>';

Disabling CORS in your browser

The developer console in the browser displays CORS errors when trying to upload the sketch.

CORS errors when uploading

You can temporarily disable this security check by starting a separate instance of your browser. e.g. start Chrome with the --disable-web-security flag:

"c:\Program Files\Google\Chrome\Application\chrome.exe" --disable-web-security --user-data-dir=c:\\windows\\temp

The demo app can then upload the sketch without any CORS errors.

See this other SO answer for an overview for why this fails.

The request to builder.arduino.cc that fails is required by the app to determine the command line and tool signature to use when programming the board.

Successful upload

Gotchas

You will need to close any other apps using the COM port and force your Arduino into programming mode (if necessary, by pressing the programming button on the board).

Ben T
  • 4,656
  • 3
  • 22
  • 22
  • do you mean `WSL` as `Windows Subsystem Linux` if so i do **not** have it how to convert to base 64?,P.S i trust a lot on this answer – Neptotech -vishnu Jan 11 '22 at 13:48
  • in the project directory there is only *`hex`* file [see this](https://ibb.co/Dg1DZjG) but in temporary directory there is both `hex`&`bin` [see this](https://ibb.co/mqQfJGy) ,what should i use (project or temp directory's file),also in both directories there are many files what to use?P.S i found [this](https://www.igorkromin.net/index.php/2017/04/26/base64-encode-or-decode-on-the-command-line-without-installing-extra-tools-on-linux-windows-or-macos/) to manage with converting to base64 but what to convert is my doubt now – Neptotech -vishnu Jan 11 '22 at 14:16
  • 1
    I have updated the answer with an equivalent powershell command to convert the binary to base64 and a comment about hex vs bin files. Your target board uses a hex file. I would recommend you try converting the hex file with bootloader. You can also find the exported compiled binary by selecting "Show sketch folder" from the "Sketch" menu. – Ben T Jan 11 '22 at 22:32
  • i think you did not understood second comment, [This is photo of sketch folder here there is only hex files and NOT BIN](https://i.ibb.co/4gFZH0s/image.png) , [but from previous knowledge I know-in temp directory there IS BIN and hex](https://i.ibb.co/fpmjM2N/image.png) , now my doubt is should I use files from the `sketch's` directory(where my `blink.ino` and `hex` files exist) *or* from `Temp` directory also there are more files in each directory [ex : `withbootloader.hex`,`eightanaloginput.hex`,`standard.hex`...] **please reply Which file to convert(to b64) and where is it(directory)?** – Neptotech -vishnu Jan 12 '22 at 05:40
  • 1
    try converting the bin file in your temp directory. The hex files will probably need an extra step to convert to a bin file first using something like https://www.keil.com/download/docs/7.asp I may have time to test with an avr board later today but you can give it a whirl first! :) – Ben T Jan 12 '22 at 06:29
  • ok as you said i done till that CORS and cors error gone! BUT [First it connects](https://ibb.co/ThbKR3J), [When I click upload it keep on upload](https://ibb.co/YPGcgMb), [after few minutes it gives blank page with errors in console](https://ibb.co/br80vws) – Neptotech -vishnu Jan 12 '22 at 10:55
  • I tested with an avr board and it works if I convert the hex file in the sketch directory. My initial comment was correct and you should convert the `Blink.ino.with_bootloader.standard.hex` file (or the other bootloader hex file if that corresponds to your sketch). I do get the same blank page as you if I use the bin file with my avr board. I have updated the answer accordingly. – Ben T Jan 12 '22 at 11:33
  • still same errors with blank page after few minutes(1 min),you may give your base64,I will cross verify(for default blink example),Or what to do. P.S if yours work but still I am not able to solve you may give the directory(upload somewhere online) – Neptotech -vishnu Jan 12 '22 at 15:21
  • I have exported the blink sketch and created the serial_mirror.js for nano in [serial_mirror.js for eightanaloginputs.hex](https://pastebin.com/bBF7iEmK) and [serial_mirror.js for standard.hex](https://pastebin.com/mFDLsSN5) – Ben T Jan 12 '22 at 22:31
  • This page is no longer available. It has either expired, been removed by its creator, or removed by one of the Pastebin staff. – Neptotech -vishnu Jan 13 '22 at 02:43
  • Try [serial mirror js files](https://replit.com/@blutter/SO70410584#serial_mirror.js) you'll need to click on "show files" to see the two example files. – Ben T Jan 13 '22 at 03:18
  • heeeeeeey! it worked i did it without that file itself My thoughts:we both use same platform how it works for you but not me,the difference between us is:our arduinos, As I have mentioned early mine is old bootloader I got this all while exploring: I used `board: 'arduino:avr:nano',` now I changed to `board: 'arduino:avr:nano:cpu=atmega328old',` But you helped me about converting hex and CORS so I grant my first bounty! – Neptotech -vishnu Jan 13 '22 at 09:41
  • EDIT QUEUE IS FULL SO Please add the below: – Neptotech -vishnu Jan 13 '22 at 10:37
  • You have to get your board name(used in target) by this process 1. select your board and processor in `Tools>Board` & `Tools>Processor` 2.Compile exported binary when you do so a temporary folder is created in `Temp` Directory you may need to enable `view system/hidden files` then You can goto the directory in my case its in `C:\Users\USER\AppData\Local\Temp` there you can goto latest modified/created directory the path will be like `~\Temp\arduino_build_241056` 3.in that path open `build.options.json` file there the `fqbn` tags value is the boards name(ex: arduino:avr:nano:cpu=atmega328old) – Neptotech -vishnu Jan 13 '22 at 10:38
  • Also how to disable CORS in code itself because when I host it as website i&my users don't want to use cmd and those stuffs like `--disable-web-security flag` – Neptotech -vishnu Jan 13 '22 at 15:20
  • 1
    There aren't any easy solutions to the CORS issue as it is a default browser feature and dependent on what `builder.arduino.cc` allows. There are heavy handed approaches such as inserting a proxy between your website and builder.arduino.cc. Alternatively, find another server (if one exists) to point the Arduino agent to (and use it in place of `new Daemon('https://builder.arduino.cc/v3/boards')`. The [link](https://stackoverflow.com/questions/25845203/understanding-cors/25845455#25845455) I referenced in the answer gives a summary of where the CORS error comes from. – Ben T Jan 14 '22 at 02:58
  • I have updated the answer to include how to find the FQBN. Feel free to mark it as the correct answer to help others! – Ben T Jan 14 '22 at 02:59
  • Thank you once more, if you are interested please help me in [related question](https://stackoverflow.com/q/71283855/14862885) (I will bounty it in 2 days) – Neptotech -vishnu Feb 27 '22 at 10:17