Home  >  Article  >  Web Front-end  >  nodejs simple implementation of operating arduino

nodejs simple implementation of operating arduino

高洛峰
高洛峰Original
2017-01-04 16:47:201797browse

It is no longer unusual to use Javascript to operate hardware.

So as an FE majoring in electronics, I also plan to try using js to drive arduino;

If you want to operate these underlying hardware, you definitely need some tools, which I will introduce here. The main tools are cylonjs and gort

cylonjs is actually a js framework for operating "machines". The introduction on the official website is as follows:

Cylon.js is a JavaScript framework for robotics, physical computing , and the Internet of Things. It makes it incredibly easy to command robots and devices.

To put it simply, it means using JS to operate hardware. It supports many hardware platforms, such as arduino (something similar to a microcontroller)

nodejs simple implementation of operating arduino

Example part of the code:

var Cylon = require("cylon");
// Initialize the robot
Cylon.api('http');//这个是用来连接本地浏览器的
Cylon.robot({//调用robot函数,并传入一个大对象来配置
 // Change the port to the correct port for your Arduino.
 connections: {//连接项定义小对象,用来配置连接arduino
  arduino: { adaptor: 'firmata', port: '/dev/cu.wchusbserial1420' }//prot是我arduino的在mac上连接的端口
  //firmata是一个PC与MCU通讯的一个常用协议。其遵旨是能与任何主机PC软件包兼容,这样可方便地将对协议的支持加入软件系统中。Firmata起初是针对于PC与Arduino通讯的固件(Firmware),其目标是让开发者可以通过PC软件完全地控件Arduino。
 },
 
 devices: {//设备,定义要操作的具体器件,这里是led,pin口是1
  led: { driver: 'led', pin: 1 }//
 },
 
 work: function(my) {//驱动函数,
  every((3).second(), function() {//每3秒,闪一下((3).second()貌似是lodash,我猜的,没用过,嘿嘿)
   my.led.toggle();//my应该是指向他自己
  });
 }
}).start();//配置好了之后就开始工作

Let’s explain it in detail

Install cylonjs

Okay first The first step is definitely to install:

npm install cylon

New project

Create a new directory (mine is robot), and then in the terminal (I use For mac or window, do it in git bash) Enter

npm init

Basically just keep pressing enter, but I changed my entry file to main .js:

entry point: (index.js) main.js

nodejs simple implementation of operating arduino

After that, enter in the terminal:

touch main.js

 Just create a new main.js file, and Windows will build it manually.

Then in main.js:

var Cylon = require("cylon");
// Initialize the robot
Cylon.api('http');//这个是用来连接本地浏览器的
Cylon.robot({//调用robot函数,并传入一个大对象来配置
 // Change the port to the correct port for your Arduino.
 connections: {//连接项定义小对象,用来配置连接arduino
  arduino: { adaptor: 'firmata', port: '/dev/cu.wchusbserial1420' }//prot是我arduino的在mac上连接的端口
  //firmata是一个PC与MCU通讯的一个常用协议。其遵旨是能与任何主机PC软件包兼容,这样可方便地将对协议的支持加入软件系统中。Firmata起初是针对于PC与Arduino通讯的固件(Firmware),其目标是让开发者可以通过PC软件完全地控件Arduino。
 },
 
 devices: {//设备,定义要操作的具体器件,这里是led,pin口是1
  led: { driver: 'led', pin: 1 }//
 },
 
 work: function(my) {//驱动函数,
  every((3).second(), function() {//每3秒,闪一下((3).second()貌似是lodash,我猜的,没用过,嘿嘿)
   my.led.toggle();//my应该是指向他自己
  });
 }
}).start();//配置好了之后就开始工作

When I first ran node main.js, an error message showed that the cylon-firmata module could not be found

nodejs simple implementation of operating arduino

In fact, I have installed this module in the current directory. In fact, I also need to install cylon. The following operation is no problem

npm install cylon cylon-firmata

Then execute node main.js:

nodejs simple implementation of operating arduino

#Report error? Because I haven't connected the arduino yet.

How to find the port of arduino when connected to arduino? There is this code in main.js:

nodejs simple implementation of operating arduino

Where does the value of prot come from? Gort will be used at this time

gort introduction:

GORTis a Command Line Toolkit for RobotOps

I think it is a command line tool for detecting hardware,

Installation:

brew install hybridgroup/tools/gort

Of course, if you are windows or linux, you can read the official website instructions yourself: install gort

and then Execute the following code to detect the port

gort scan serial

nodejs simple implementation of operating arduino

and then execute:

gort arduino install

The result is as follows:

nodejs simple implementation of operating arduino

It means that the installation was successful, but the brew link step was not successful. The reason is that /usr/local/lib is not writable, that is, it is not written. Enter permissions,

So execute: (whoami is the username of your computer)

sudo chown -R whoami /usr/local/lib

Then execute :

nodejs simple implementation of operating arduino

Just execute the link again if there is no successful brew link.

Then:

gort arduino upload firmata / dev/tty.wchusbserial1420

nodejs simple implementation of operating arduino

succeeded. Execute again at this time: node main.js

nodejs simple implementation of operating arduino

arduino responds, the LED named tx flashes every 3 seconds, enter https://127.0 in the browser. 0.1:3000

nodejs simple implementation of operating arduino


For more nodejs simple implementation of arduino related articles, please pay attention to the PHP Chinese website!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn