Realtime control of GPIO:s with Hydna

raspberry_pi_nodejs_hydna

Want to control the pins on you Raspberry Pi in real time? With the scaleable realtime platform from Hydna you can set how many that can access the GPIOs and when. Install the node modules gpio and hydna to controll your GPIO:s over the net.

Install node.js
sudo apt-get upgrade;
sudo apt-get update


sudo apt-get install nodejs npm node-semver
npm config set registry http://registry.npmjs.org/

Open the .bash_profile file with:
sudo nano /etc/profile

And enter the following:
NODE_JS_HOME=/usr/local/nodejs
PATH=$PATH:$NODE_JS_HOME/bin

NOTE! Log out and then in again in order to make the changes go through.

To run nodejs with with the command node, link them to with:
sudo ln -s /usr/bin/nodejs /usr/bin/node

Make sure to check where your node was installed and add it to NODE_JS_HOME

Install GPIO
npm install pi-gpio

Install GPIO-ADMIN

cd /home/pi
sudo git clone git://github.com/quick2wire/quick2wire-gpio-admin.git
cd quick2wire-gpio-admin
sudo make
sudo make install
sudo adduser pi gpio

Try out gpio-admin with the following line:
gpio-admin export 22

If you get this error
Error /bin/sh: 1: gpio-admin: Permission denied

follow these steps:

sudo groupadd gpio
sudo usermod -aG gpio
su
sudo chgrp gpio /sys/class/gpio/export
sudo chgrp gpio /sys/class/gpio/unexport
sudo chmod 775 /sys/class/gpio/export
sudo chmod 775 /sys/class/gpio/unexport

Install Hydna
npm install hydna
With Hydna you can send real time messages to your pi and allow a certain number of clients to connect to your Raspberry Pi. With Hydna’s Behaviours you set the behaviours of your different channels.

The following example code shows how to control your gpios with messages from Hydna:


var gpio=require("pi-gpio"); // Include the pi-gpio library to control the gpios

gpio.open(12,"output",function(err){
console.log("Pin open"); // Opens pin 12 as output
});

var hydna = require('hydna');
// Open channel in "read/write" mode. All data sent to channel will
// loop back from the remote connection.
var channel = hydna.createChannel('domainname.hydna.net/rpi', 'readwrite');

// Connect handler, is triggered once that the channel is connected
channel.on('connect', function () {
this.write('Status: ok');
});

// Data handler, is triggered each time DATA is received on the channel
channel.on('data', function (data) {

// Log data received on channel.
console.log(data);
if(data == "on") {
gpio.write(12, 1, function() {
});
} else if(data == "off") {
gpio.write(12, 0, function() {
});
} else if(data == "end") {
gpio.close(12); // Close pin
this.end(); //Close the channel, which terminates the underlying receive-loop.
}
});

Leave a Reply

Your email address will not be published. Required fields are marked *