Let's start from beginning. Some days ago, thanks to a my friend, i discovered the Node-RED project. A great visual tool for wiring IoT devices, and integrate them with API and services.
Another thing very nice is that you can install Node-RED on a RaspberryPi, and wire a lot of nodes. Yes Node-RED has a visual block tools and the blocks are nodes and its built on node.js, so prepare to be confused with all these nodes :). You can wire input nodes with output nodes and put between them functions node, switch nodes and many other useful nodes. There are a lot of core nodes shipped with basic Node-RED distribution.
I just have a RaspberryPi and i wrote a library for the Nunchuck controller in Java, so Node-RED it's a great chance for me to make a porting of the code in Javascript in an elegant way. With Node-RED i have to simply design my library as a set of nodes. Next, i will share with you how i you can use a Node-RED node for a nunchuck controller wired with a RaspberryPi,

What we need:

  • a Raspberry Pi Model B Revision 2.0 (512MB)
  • The latest Raspbian image installed (Jessie while i wrote these lines)
  • A cheap Nunchuck controller
  • An I2C adapter (or similar wiring)
  • A node.js installation
  • A Node-RED installation
  • The node-red-contrib-nunchuck module
  • A lot of patience

For the Raspberry and a Nunchuck it is a commonplace to say that you have to buy them. But don't worry, the entire bundle does not cost too much.
If you just have a Raspberry i suggest to upgrade to Raspbian Jessie so you can install a newer version of Node.Js. Furthermore, it comes with a lot of updated packages, one of them is gcc 4 that it's very important to compile some modules we need.

Wiring the nunchuck on the Pi

The nunchuck controller sends data over an I2C bus. There is a lot of documentation about the I2C protocol, because since October 10, 2006 no licensing fees are required to implement the I²C protocol. So the first thing we have to do is to wire the controller pins to the GPIO pins with the RaspberryPi board. There a lot of guides to do the hard work, i used this doc to understand the basic things, the interesting part for our purpose is to identify the pins for the I2C interface as shown in the image below:

p1-pins

If you have all well done, you should see wired pins as the image below:

pi-i2c1

On the other side and if you won't break the nunchuck you can use a simple adapter, i bought the mod-wii-icsp-nunchuck but you can use the WiiChuck too, the basic difference is that the second comes without pins. So i wired the pins on the ICSP adapter and this is the result:

pi-i2c2

Configuring I2C on the Raspberry

If you have Jessie installed on your Raspberry you don't have to do much work because I2C driver and kernel modules are just installed and ready to use. The only thing you have to check if your /boot/config.txt file has the correct parameters setted. Or simply use the raspi-config to enable I2C under "Advanced Options".
To manual enable I2C add the following line to /boot/config.txt:

dtparam=i2c_arm=on

The default I2C baudrate is 100000. You can modify using this line of code:

dtparam=i2c_arm_baudrate=400000

If you have modified your config.txt file you have to REBOOT the RaspberryPi. Now you can check that it's ok.

Test data reading from Nunchuck

Install the i2ctools and i2cdev libraries on your system:

sudo apt-get update
sudo apt-get install i2ctools libi2c-dev

Keep wired the nunchuck controller with the raspberry and launch to i2cdetect command:

i2cdetect -y 1

you should see an output similar to this shown below, if so your nunchuck controller is correctly wired with your PiBoard and now we can capture data from it using Node-RED:

i2cdetect_output

Installing Node.js, NPM and Node-RED

The official Node-RED site project provides a detailed documentation and there is a section dedicated to the Raspberry PI.
With Jessie distribution Node-RED comes preinstalled on the system but the bad news is that there is an older version of Node.Js and some modules are incompatible. I suggest to remove the default Node-RED installation and to upgrade to newer versione of node.js. First of all remove all node dependencies:

sudo apt-get remove nodered
sudo apt-get remove nodejs nodejs-legacy
sudo apt-get remove npm   

then you can install new dependencies:

wget http://node-arm.herokuapp.com/node_archive_armhf.deb
sudo dpkg -i node_archive_armhf.deb
sudo apt-get install build-essential python-dev python-rpi.gpio

if you don't have npm installed you can install it from Jessie repository:

sudo apt-get install npm

and then you can install node-red directly from npm:

sudo npm install -g --unsafe-perm  node-red

To start Node-RED you can launch this command from shell:

node-red-pi --max-old-space-size=128

a server was spawned on port 1880 and you can use it together with the core nodes of Node-RED. If you never used Node-RED you can follow this tutorial to create your first flow.

Installing the node-red-contrib-nunchuck module

Good guys. We have a RaspberryPi with a nunchuck controller wired on I2C bus interface and a working Node-RED installation, we are Node-RED flow master, so we are ready to capture controller input and create a great "Nunchuck2Pi" flow. :). Node-RED comes with no nodes for nunchuck so i created one. To install the nunchuck node execute these commands from shell:

cd /home/pi/.node-red/
npm install node-red-contrib-nunchuck

if you have compilation errors, probably you don't have lib-dev dependencies, to install them launch:

sudo apt-get update
sudo apt-get install i2ctools libi2c-dev

then you should see a node_modules directory with a node-red-contrib-nunchuck within. Restart Node-RED go to the web visual tool (http://:1880/) a nunchuck node should be available in the input category.

nunchuck-node-palette

Put the nunchuck node and configures it:

nunchuck-node-config

The nunchuck node has one output, a JSON object, for debug purpose you can config the nunchuck node with a low frequency (e.g.: 1000 ms) and connect a debug node to see the object properties sent by the nunchuck node.

nunchuck-first-flow

Creating your first flow with the nunchuck node

To create a simple nunchuck flow you have two ways:

  • import the flow shown below from code (copy and paste)
  • manually create your own flow

In any case you have to use a function node to process data and to recognize something was changed, this is because the node doesn't matter about what changed it simply reads and decodes data from controller at the requested frequency. In future releases i'm thinking to put a config check to change this behavior, such as "send data only if something was changed". The Node-RED nodes philosophy is to do simple things so i'm more disposed to split this nunchuck node in two nodes: the reader and the decoder :).

The example below shows a function node that pipe nunchuck output, recognizes stick moves from X axis and send an event.:

var xevent = msg.payload.x ;
if(context.xevent !== xevent){
 context.xevent = xevent; 
 node.send(msg);
}
return;

Put the function node between the nunchuck node and the debug node:

nunchuck-function-node

Or simply import this snippet:

[{"id":"3818fd96.c7e702","type":"function","z":"e8c028bd.173fd8","name":"processXStickData","func":"var xevent = msg.payload.x ;\nif(context.xevent !== xevent){\n context.xevent = xevent; \n node.send(msg);\n}\nreturn;","outputs":1,"noerr":0,"x":353,"y":158,"wires":[["e7a89ba0.185768"]]}]

Putting all together and create your own flow!! For example I used this simple nunchuck flow to move the PTZ of my ipcam.

put-all-together

Conclusions

Node-RED is a great tool and can be used on many others platforms than the Pi. The IoT world is very tricky and at the same time attractive. I think that Node-RED solves many problems to design what we have in mind and enables new IoT developers to access this fantastic world.

Goodbye from Luigi's Dev Corner.



  • submit to reddit
blog comments powered by Disqus