Interacting with Arduino Node-RED PDF

Title Interacting with Arduino Node-RED
Course Electronica 1
Institution Universidad de San Carlos de Guatemala
Pages 2
File Size 114.3 KB
File Type PDF
Total Downloads 79
Total Views 146

Summary

NODE-RED...


Description

Node-RED home about blog documentation forum flows github



docs • faq • arduino V Serial Firmata Installation Blink Capabilities Johnny-Five Blink 2

Interacting with Arduino There are several ways to interact with an Arduino using Node-RED. They all assume the Arduino is connected to the host computer via a USB serial connection. Note: you can’t use both the Arduino IDE and the Arduino nodes at the same time as they will conflict. You will need to stop Node-RED running if you wish re-program the Arduino from the IDE.

Serial As the Arduino appears as a Serial device, the Serial in/out nodes can be used to communicate with it. This is normally the case if you program the Arduino with the IDE, as you can then send and receive input over the serial port to interact with your creation. Just make sure you set the serial port speed (baud rate) to be the same at both ends.

Firmata Firmata is a protocol for communicating between an Arduino (as well as other microcontrollers) and the host computer, providing direct access to the IO pins. Installation First you need to load the default Firmata sketch onto the Arduino using the standard Arduino software download tools. This is usually found in the Arduino IDE under the menu: Files - Examples - Firmata - Standard Firmata

You then need to install the Node-RED Arduino nodes into the palette. Please check that `npm -v` is at least version 2.x - if not - update it using sudo npm i -g [email protected] hash -r Change directory to your Node-RED user directory, this is normally ~/.node-red cd ~/.node-red

Then install the Arduino nodes npm install node-red-node-arduino

Finally restart Node-RED, and reload the editor in the browser. There should now be two new Arduino nodes in the palette. Blink To run a “blink” flow that uses LED 13, copy the following flow and paste it into the Import Nodes dialog (Import From - Clipboard in the dropdown menu, or Ctrl-i, Ctrl-v). After clicki okay, click in the workspace to place the new nodes. [{"id":"d7663aaf.47194","type":"arduino-board","device":""},{"id":"dae8234f.2517e","type":"inject","name":"0.5s tick","topic":"","payload":"","payloadType":"date","repeat

This flow is set to automatically try to detect the board on a a serial port. If you need to change that, double click the node labelled Pin 13 - the Arduino node. Click the pencil icon and change the port definition as needed. Click the deploy button and the flow should start running. LED 13 should start toggling on and off once a second. Capabilities The Arduino output node currently supports three modes of operation: Digital - 0 or 1 Analogue - 0 to 255 Servo - 0 to 180 The Arduino input node, available in the palette but not used in this example, can support both Digital and Analog pins. The input will send a message whenever it detects a change. This may be okay for digital inputs as they tend to be fairly stable, but analog readings often end up being at the full sample rate (default: 40 times a second…). This can be changed in the configuration of the serial port to reduce it to a more manageable rate

Details of the Node.js arduino-firmata library can be found here.

Johnny-Five You may also use the popular Johnny-Five library as this adds capabilities like I2C. One way to use it is via Luis Montes’ node-red-contrib-gpio node, which also adds support for a number of other boards, such as Raspberry Pi, BeagleBone Black, Galileo/Edison, Blend Micro, LightBlue Bean, Electric Imp and Spark Core, in a consistent manner. Another way is to make it available within functions. This can be achieved by editing the globalContextSettings sections of settings.js to be functionGlobalContext: { jfive:require("johnny-five"), j5board:require("johnny-five").Board({repl:false}) },

// this is the reference to the library // this actually starts the board link...

We start the board link here so that multiple functions within the workspace can use it, though you should be careful to only access each pin once. Finally install the npm from within your Node-RED home directory cd ~/.node-red npm install johnny-five

and then you may access all the richness of Johnny-Five from within functions… var five = context.global.jfive; var led = new five.Led(13); led.blink(500);

// create a shorter alias // instatiate the led // blink it every 500 ms

Note: this is a simple, but poor example as the led pin is created each time the function is called… so only ok if you only deploy it and call it once. Blink 2 The flow below shows a more advanced example that turns on and off a flashing led, and shows the use of context to hold the state and a single instance of the led pin. It can be imported to the workspace by using ctrl-c (copy) / ctrl-i (import) / ctrl-v (paste). [{"id":"62f58834.9d0a78","type":"inject","name":"","topic":"","payload":"1","payloadType":"string","repeat":"","crontab":"","once":false,"x":226,"y":326,"z":"359a4b52.ca6

Node-RED: Low-code programming for event-driven applications. A project of the OpenJS Foundation. GitHub npm Documentation APIs Flow Library About Code of Conduct Community Blog Twitter Forum Slack...


Similar Free PDFs