Using JavaScript to manipulate hardware has long been a rarity.
So as an electronic professional origin of Fe, I also intend to try to use JS to drive Arduino;
To operate these underlying hardware, there must be some tools, and the tools I'm introducing here are mainly Cylonjs and Gort.
Cylonjs is actually a operation "machine" JS framework, the official website of the introduction is such:
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.
The simple point is to use JS operating hardware, he supports a lot of hardware platforms, such as Arduino (a kind of things like SCM)
For example, part of the code:
var Cylon = require ("Cylon");
Initialize the robot
cylon.api (' http ');//This is used to connect to the local browser
Cylon.robot ({//Call robot function, and pass in a large object to configure
// Change the "port to" correct port for your Arduino.
Connections: {//connection item defines a small object that is used to configure the connection Arduino
Arduino: {adaptor: ' Firmata ', Port: '/dev/cu.wchusbserial1420 '}// Prot is my Arduino port connected to Mac
//firmata is a common protocol for PC and MCU communication. Its compliance is compatible with any host PC package, which facilitates the inclusion of protocol support into the software system. Firmata was originally intended for PC and Arduino communications firmware (firmware), with the goal of allowing developers to completely control Arduino through PC software.
Devices: {//equipment, define the specific device to be operated, here is the Led,pin port is 1
led: {driver: ' led ', Pin:1}//
},
work:function ( My) {//driver function,
every ((3). second (), function () {//every 3 seconds, Flash (3). second () seems to be lodash, I guess, no use, hey Hei)
my.led.toggle ( );//my should be pointing to himself
})
. Start ()//After the configuration is ready to begin work
Let's take a detailed account of the following
Install Cylonjs
Okay, the first step is definitely to install:
NPM Install Cylon
New Project
Create a new directory (mine is robot) and then type it in the terminal (I'm using Mac,window's words to operate in Git bash)
NPM Init
Basically always press ENTER on it, but I changed my entry file to Main.js:
Entry point: (Index.js) main.js
OK, then in the terminal input:
Touch Main.js
is to create a new Main.js file that Windows builds manually.
And then main.js in:
var Cylon = require ("Cylon");
Initialize the robot
cylon.api (' http ');//This is used to connect to the local browser
Cylon.robot ({//Call robot function, and pass in a large object to configure
// Change the "port to" correct port for your Arduino.
Connections: {//connection item defines a small object that is used to configure the connection Arduino
Arduino: {adaptor: ' Firmata ', Port: '/dev/cu.wchusbserial1420 '}// Prot is my Arduino port connected to Mac
//firmata is a common protocol for PC and MCU communication. Its compliance is compatible with any host PC package, which facilitates the inclusion of protocol support into the software system. Firmata was originally intended for PC and Arduino communications firmware (firmware), with the goal of allowing developers to completely control Arduino through PC software.
Devices: {//equipment, define the specific device to be operated, here is the Led,pin port is 1
led: {driver: ' led ', Pin:1}//
},
work:function ( My) {//driver function,
every ((3). second (), function () {//every 3 seconds, Flash (3). second () seems to be lodash, I guess, no use, hey Hei)
my.led.toggle ( );//my should be pointing to himself
})
. Start ()//After the configuration is ready to begin work
When I first ran node main.js, the error display could not find the Cylon-firmata module
In fact, I am in the current directory is installed this module, in fact, to install Cylon, the following operation no problem
NPM Install Cylon Cylon-firmata
Then execute node main.js:
Error? Because I haven't even Arduino.
Connect the Arduino how to find the port of Arduino, in main.js this code:
What's the value of prot? This time we're going to use Gort.
Gort Introduction:
Gortis a Command line Toolkit for Robotops
I think it's just a command-line tool for detecting hardware,
Installation:
Brew Install Hybridgroup/tools/gort
Of course if you are Windows or Linux, your own Reader Network description: Installation Gort
Then execute the following code detection port
Gort Scan Serial
Then execute:
Gort Arduino Install
The result appears as follows:
It means I did the installation. However, the brew link did not succeed because the/usr/local/lib is not writable, or not write permissions,
So execute: (WhoAmI is your computer's username)
sudo chown-r whoami/usr/local/lib
And then execute:
is to run link again with the success of no brew link above.
And then:
Gort Arduino upload firmata/dev/tty.wchusbserial1420
It worked. This time to execute again: node Main.js
Arduino has a reaction, the name is TX LED3 seconds a flash, in the browser input https://127.0.0.1:3000
Haha, successful, well, it is written here, and then continue to study to write related articles.
Yes, connecting Mac and Arduino also need to drive, my Arduino is Arduino uno. I installed ch340 this driver, download the address