Arduino创建代理JS客户端如何使用

人气:897 发布:2022-10-16 标签: javascript upload arduino

问题描述

欢迎光临, 目前,我想使用JavaScript(NodeJS)将代码上传到Arduino, 我讨厌用FIRMATA上传代码, 我想用官方Arduino create agentArduino create agent js client

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

我只是Arduino Create代理, 我在空目录中运行以下代码

git clone https://github.com/arduino/arduino-create-agent-js-client.git
我在COM7中插入了Arduino Nano(旧的引导加载程序), 因此,我将文件.demoapp.jsx第189-205行更改(编辑)为

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 });
  }

然后我运行

npm run dev
由于本地主机在http://localhost:8000/上运行 我编辑了配置文件

C:UsersUSERAppDataRoamingArduinoCreateAgentconfig.ini

和设置 origins = http://localhost:8000

那是

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

如上所示继续上传

请帮帮忙

推荐答案

有几个步骤可以使其正常工作:

安装Arduino Create Agent 修改config.ini文件以设置origins = http://localhost:8000 更新demoapp.jsx中的目标单板和端口 在demoserial_mirror.js中替换要下载到黑板上的草图 通过在浏览器中禁用CORS,允许演示应用访问builder.arduino.cc

您完成了前两个操作,是第三个操作的一部分,后两个操作均未完成。

确定目标板

demoapp.jsx中需要更新完全限定的主板名称(FQBN)。FQBN可以从Arduino应用程序的编译输出窗口中获得。构建草图时,Arduino应用程序中的输出窗口将包含一个-fqbn参数。例如-fqbn=arduino:avr:nano:cpu=atmega328

将此FQBN复制到=之后,并在您的板的端口旁边更新目标对象的板属性。

例如

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

替换草图

演示应用硬编码要在serial_mirror.js中下载的草图

通过从Arduino应用程序导出编译后的二进制文件,可以将其替换为所需的草图。请参见下面的屏幕截图。导出的二进制文件将保存到草图文件夹(&Quot;Sketch&Quot;菜单上的&Show Sketch Folders&Quot;将显示此文件夹)。请注意,草图需要是可写的(例如,示例草图不会在此草图文件夹中生成二进制文件)。

导出的二进制文件需要进行64位编码。导出的二进制文件将具有您正在使用的hexbin扩展名depending on the type of board。例如,对于AVR电路板,转换草图文件夹中名为<sketch>.ino.with_bootloader.standard.hex的文件。

您可以在WSL(Windows Subsystem For Linux)中使用以下命令转换二进制文件:

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

或者,使用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>"
demoserial_mirror.js中的字符串需要替换为新草图的base 64字符串。即

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

在浏览器中禁用CORS

浏览器中的开发人员控制台在尝试上载草图时显示CORS错误。

您可以通过启动浏览器的单独实例来暂时禁用此安全检查。例如,使用--disable-web-security标志启动Chrome:

"c:Program FilesGoogleChromeApplicationchrome.exe" --disable-web-security --user-data-dir=c:\windows\temp

然后,演示应用可以上载草图,而不会出现任何CORS错误。

有关失败原因的概述,请参阅此other SO answer。

应用程序需要向builder.arduino.cc发出失败的请求,以确定在编程电路板时要使用的命令行和工具签名。

Gotchas

您需要关闭使用COM端口的任何其他应用程序,并强制Arduino进入编程模式(如有必要,请按电路板上的编程按钮)。

683