Quick Intro to Node-RED
Raspberry Pi & Arduino Home Auto Project-#raspSerie 08
In this post, we are going to unveil the beauties of this new technology from IBM: Node-RED. It should be able to give you the gist at how everything works.
Node-RED? How cool is that? It’s time to check it out!!!
Ok, let’s jump in the cold water…
Let’s put the process in a step-wise-manner!!!
Get your RPi (Raspberry Pi Model B) and…
Task 00
Raspberry Pi:
The first task, download the newer version of raspberry pi:
RASPBIAN JESSIE WITH PIXEL
Node-RED comes pre-installed on the Raspberry Pi image. It can be used out of the box to begin creating IoT applications.
See this video to get your Rpi up and running!!
Task 01
sudo apt-get update
Task 02
sudo apt-get upgrade
Task 03
Access you RPi via SSH and type:
cd ~/.node-red
node-red-start
You should be able to see the service running in your machine.
Task 04
crtl+C
node-red-stop
We will stop the service now for installing spare nodes. Follow below.
Task 05
cd ..
sudo apt-get install npm
This is the node package manager.
Task 06
In the Node-RED Library page type: arduino.
Now click node-red-node-arduino lib and copy/paste and…
cd ~/.node-red
npm install node-red-node-arduino
This node to talk to an Arduino running standard firmata 2.2 or better.
Task 07
Continuing in the Node-RED Library page type: contrib-ui.
Go to the library page of node-red-contrib-ui and copy the command for installation…
cd ~/.node-red
npm install node-red-contrib-ui
Task 08
Now grab you Arduino Uno:
Great, if everything installs correctly, get your Arduino and load the firmata on it.
Examples> Firmata> StandardFirmata.
Task 09
Connect Arduino via USB on your beloved Rpi.
Make your breadboard. See schematic:
Task 10
Raspberry Pi:
Access your Pi via SSH (see my video explaining how!) again.
cd ~/.node-red
node-red-start
This command initializes the node-RED service with the additional nodes: Arduino and ui.
Task 11
Get your RPi IP address (see notes below).
Now you can now close SSH session and pick up your laptop and point your browser to the address of your RPi (note that in the initialization script of Node-RED you get this clue). In my case:
http://192.168.1.100:1880
Task 12
Great, the Node-RED interface is offered beautiful for our internet of things project !!!
Task 13
Now follow the instructions below, cataloged as a sequence of three flows. Please see the video and everything will be pretty clear to you!!! I swear!!!
Did you enjoy reading this and found it useful? if so please share it with your friends.
If you didn’t like it, I always like to hear how I can do better next time. Please feel free to contact me directly via youtube or tweeter.
Now the challenge is for you!!! It won’t be that hard since I got along well, right?
If you wish to continue reading follow my collections of notes about that fantastic piece of technology named Node-RED.
Bye!!!
References:
guide
flows
Arduino
vids inspiration: 1 and 2
intro Node-RED
Speed Notes About Node-RED
. Thanks to Conway-Jones and O’Leary from IBM we can hack together these little nodes;. IBM Researchers hit on the idea of building a toolbox of reusable code that people could use to more easily wire together software and devices into a useful system;. It’s simply an encapsulation of some JavaScript code in nodes;. With Node-RED you can wrap up any bit of functionality and encapsulate that function as a node in your pallete;. With Node-RED you drag-and-drop nodes to the canvas;. Nodes are drag-n-dropable icons on to the canvas that you wire them together to make your IoT app;. Node-RED is an event-processing engine; this event could be any type: digital (pressAButton), social (PostTweets), you name it!!!. It gives us agility to rapidly prototype; my home automation now has a tech peace ready!!!. It allows people to focus on the creating, rather them on the doing;. See this table:The Doing Let you playing around w/
--------- -------------------------
Spreedsheet Numbers
Youtube Videos
Node-RED Events (social, digital, whatever...). It glues: devices + services + software;. Node-RED comes with an excellent set of basic nodes within its pallet; however there are more out there and there is certainly nothing stopping you from writing your own!!!. Johnny Five is the JavaScript Robotics & IoT Platform; it works with the Node-RED; we'll save that for another future post!!!. With Node-RED the processing of messages is visualized as graphical flow between nodes representing input, output or processing by Node-RED;. See the video from Tony D to get an awesome knowledge about Node-RED. Thanks DiCola a tech guy from Adafruit!!!
— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — Here are the messages sent by the Node-RED from Raspberry Pi init routine (pay attention to the line that asks you to access your browser):
cd~/.node-red
sudo apt-get
Once Node-RED has started, point a browser at http://192.168.1.100:1880
On Pi Node-RED works better with the Firefox browser
Use node-red-stop to stop Node-RED
Use node-red-start to start Node-RED again
Use node-red-log to view the recent log output
Use sudo systemctl enable nodered.service to autostart Node-RED at every boot
Use sudo systemctl disable nodered.service to disable autostart on boot
To find more nodes and example flows — go to http://flows.nodered.org
You may also need to install and upgrade npm
sudo apt-get install npm
sudo npm i -g npm@2.x
Started Node-RED graphical event wiring tool..
Welcome to Node-RED
===================
12 Nov 16:54:41 — [info] Node-RED version: v0.15.2
12 Nov 16:54:41 — [info] Node.js version: v0.10.29
12 Nov 16:54:41 — [info] Linux 4.4.26+ arm LE
12 Nov 16:54:42 — [info] Palette editor disabled : npm command not found
12 Nov 16:54:42 — [info] Loading palette nodes
pi : TTY=unknown ; PWD=/home/pi ; USER=root ; COMMAND=/usr/bin/python -u /usr/lib/node_modules/node-red/nodes/core/hardware/nrgpio.py info
pam_unix(sudo:session): session opened for user root by (uid=0)
pam_unix(sudo:session): session closed for user root
12 Nov 16:56:00 — [info] Settings file : /home/pi/.node-red/settings.js
12 Nov 16:56:00 — [info] User directory : /home/pi/.node-red
12 Nov 16:56:00 — [info] Flows file : /home/pi/.node-red/flows_raspberrypi.json
12 Nov 16:56:00 — [info] Creating new flow file
12 Nov 16:56:00 — [info] Starting flows
12 Nov 16:56:01 — [info] Started flows
12 Nov 16:56:01 — [info] Server now running at http://127.0.0.1:1880/
— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —
Here is the stop result from ‘node-red-stop’ command:
`(…)
^[12 Nov 17:25:22 — [info] Stopping flows
12 Nov 17:25:22 — [info] Stopped flows
pi : TTY=unknown ; PWD=/home/pi ; USER=root ; COMMAND=/usr/bin/python -u /usr/lib/node_modules/node-red/nodes/core/hardware/nrgpio.py out 7
pam_unix(sudo:session): session opened for user root by (uid=0)
sudo service nodered stop
12 Nov 19:24:28 — [info] Stopping flows
(…)
pam_unix(sudo:session): session closed for user root
12 Nov 19:24:28 — [info] Stopped flows
12 Nov 19:24:28 — [info] Starting flows
12 Nov 19:24:29 — [info] Started flows
pi : TTY=unknown ; PWD=/home/pi ; USER=root ; COMMAND=/usr/bin/python -u /usr/lib/node_modules/node-red/nodes/core/hardware/nrgpio.py out 7
pam_unix(sudo:session): session opened for user root by (uid=0)
12 Nov 22:23:38 — [info] Stopping modified flows
12 Nov 22:23:38 — [info] Stopped modified flows
12 Nov 22:23:38 — [info] Starting modified flows
12 Nov 22:23:38 — [info] Started modified flows
— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — When running ‘sudo apt-get update’ or ‘upgrade’ have patience, it usually takes a lot of time (mine takes half a day, phew!!!) !!
sudo apt-get update -y
sudo apt-get upgrade -y
The “-y” on apt-get upgrade means to assume “yes” answers to any questions.
— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — Importing a flow using the Clipboard:
Importing a flow in Node-RED is really simple. Select and copy to your clipboard. Paste it into the modal window and click “Ok”.
For this project copy these simple flows, if you will:
— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —
flow 1:
[{"id":"aca5a8de.ad84f8","type":"tab","label":"Flow 1"},{"id":"1ffb5e91.6f1c91","type":"inject","z":"aca5a8de.ad84f8","name":"Hello World Name!!!","topic":"","payload":"Hello World payload!!!","payloadType":"str","repeat":"","crontab":"","once":false,"x":223,"y":119,"wires":[["f514b8fb.4565d8"]]},{"id":"f514b8fb.4565d8","type":"debug","z":"aca5a8de.ad84f8","name":"","active":true,"console":"false","complete":"false","x":538,"y":119,"wires":[]}]
— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —
flow 2:
[{"id":"409c3358.54636c","type":"tab","label":"Flow 2"},{"id":"bfb1f117.a7388","type":"function","z":"409c3358.54636c","name":"Toggle 0/1 on input","func":"\ncontext.state = context.state || 0;\n\n(context.state == 0) ? context.state = 1 : context.state = 0;\nmsg.payload = context.state;\n\nreturn msg;","outputs":1,"noerr":0,"x":391,"y":84,"wires":[["f2a20f1c.55ce9","9a56186f.2055f8"]]},{"id":"f2a20f1c.55ce9","type":"debug","z":"409c3358.54636c","name":"","active":true,"complete":"payload","x":630,"y":144.00000095367432,"wires":[]},{"id":"44914c1d.32cae4","type":"inject","z":"409c3358.54636c","name":"tick every 1 sec","topic":"","payload":"","payloadType":"date","repeat":"1","crontab":"","once":false,"x":190,"y":84.00000095367432,"wires":[["bfb1f117.a7388"]]},{"id":"9a56186f.2055f8","type":"rpi-gpio out","z":"409c3358.54636c","name":"Green_LED","pin":"7","set":false,"out":"out","x":647.8333625793457,"y":83.83333396911621,"wires":[]}]
— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —
flow 3 :
(Do not forget to edit your email and server otherwise it will not work …:-)
[{"id":"7f807755.5d0158","type":"tab","label":"Flow 3"},{"id":"843cf1de.847ac","type":"inject","z":"7f807755.5d0158","name":"0.5s tick","topic":"","payload":"","payloadType":"date","repeat":"0.5","crontab":"","once":false,"x":194,"y":74,"wires":[["b77bc2a1.0e8c"]]},{"id":"843099aa.ef0998","type":"arduino out","z":"7f807755.5d0158","name":"","pin":"13","state":"OUTPUT","arduino":"8e5393b6.604d3","x":614.5,"y":74,"wires":[]},{"id":"b77bc2a1.0e8c","type":"function","z":"7f807755.5d0158","name":"Toggle output on input","func":"\n// If it does exist make it the inverse of what it was or else initialise it to false\n// (context variables persist between calls to the function)\ncontext.level = !context.level || false;\n\n// set the payload to the level and return\nmsg.payload = context.level;\nreturn msg;","outputs":1,"noerr":0,"x":402,"y":74,"wires":[["843099aa.ef0998"]]},{"id":"aa5e18c3.3b1dc8","type":"ui_switch","z":"7f807755.5d0158","tab":"a88066b8.1444e8","name":"On/Off","topic":"","group":"Cooler","order":1,"onvalue":"100","offvalue":"0","x":161,"y":160,"wires":[["b6a1b818.68ce88"]]},{"id":"b6a1b818.68ce88","type":"rpi-gpio out","z":"7f807755.5d0158","name":"Orange_LED","pin":"11","set":false,"level":"0","out":"pwm","x":422.5,"y":160,"wires":[]},{"id":"758deb39.8c4254","type":"ui_slider","z":"7f807755.5d0158","tab":"a88066b8.1444e8","name":"percentage","topic":"","group":"Cooler","order":"2","min":0,"max":"100","x":194,"y":233,"wires":[["b6a1b818.68ce88"]]},{"id":"3f3a3905.1e74a6","type":"rpi-gpio in","z":"7f807755.5d0158","name":"btn_EmailMe","pin":"13","intype":"up","debounce":"25","read":false,"x":187,"y":317,"wires":[["ce7e1f4d.f1b4f","2868e97c.b222f6"]]},{"id":"ce7e1f4d.f1b4f","type":"e-mail","z":"7f807755.5d0158","server":"smtp.gmail.com","port":"465","name":"yourUserName@gmail.com","dname":"to J3 Email","x":415.5,"y":317,"wires":[]},{"id":"2868e97c.b222f6","type":"ui_text","z":"7f807755.5d0158","tab":"a88066b8.1444e8","name":"EmailToMe","group":"Alarm System","order":"3","format":"{{msg.payload}}","x":427,"y":371,"wires":[]},{"id":"4f6df1f5.f82c","type":"ui_slider","z":"7f807755.5d0158","tab":"a88066b8.1444e8","name":"actuator1","topic":"","group":"Living Room Curtain","order":"3","min":0,"max":"180","x":184,"y":450,"wires":[["7767a79c.5d65f8","65db7980.7dd3a8"]]},{"id":"7767a79c.5d65f8","type":"ui_gauge","z":"7f807755.5d0158","tab":"a88066b8.1444e8","name":"Angle","group":"Living Room Curtain","order":1,"format":"{{value}}","min":0,"max":"180","x":393,"y":447,"wires":[]},{"id":"65db7980.7dd3a8","type":"arduino out","z":"7f807755.5d0158","name":"Pin 9","pin":"9","state":"SERVO","arduino":"8e5393b6.604d3","x":387,"y":500,"wires":[]},{"id":"8e5393b6.604d3","type":"arduino-board","z":"","device":"/dev/ttyACM0"},{"id":"a88066b8.1444e8","type":"ui_tab","z":"","name":"Home","icon":"dashboard","order":"1"}]