Using Phidgets on a Raspberry Pi 2
by gepatto in Circuits > Raspberry Pi
2288 Views, 26 Favorites, 0 Comments
Using Phidgets on a Raspberry Pi 2
Hello Makers,
In this instructable I will be showing you how to prepare a Raspberry Pi2 for use with a Phidgets Interfacekit 8/8/8
The Products from Phidgets allow you to connect sensors an actuators to your pc/pi/mac through the usb port. Most of the sensors do not require any soldering.
There are plenty of tutorials on how to prepare your Raspberry Pi, so I will skip that part.
I will be using the Raspbian - Debian Wheezy - Release date: 2015-05-05
I will assume that you have installed your Raspbian and know how to use the commandline.
I will be installing the phidgets libraries, the phidgets webservice and Nodejs. Finally I will show a very simple nodejs script to read an analog sensor, in my case a rotary encoder ( a potentiometer).
Step 1: Installing and Compiling the Phidget Libraries
All Italic lines are commands in the shell.
First of all make sure that you have updated your pi2
sudo apt-get update
sudo apt-get upgrade
Because phidgets communicate via usb, libphidgets needs to have liubs-dev installed.
sudo apt-get install libusb-dev
make sure your are in your home-directory
cd /home/pi
make a directory where you will store some downloaded filee
mkdir phidgets
cd phidgets
now grab the libphidget libraries and unpack them
wget http://www.phidgets.com/downloads/libraries/libph...
tar zxf libphidget.tar.gz
next we will configure the libaries , compile and install them
cd libphidget-2.1.8.20150410
./configure
make
sudo make install
In theory your pi can now use the phidgetboards,
but we will go ahead and install the phidgetwebservice as well
The webservice will allow you to talk to your PhidgetBoards via the network.
First we get the needed files, unpack and compile and install them
wget http://www.phidgets.com/downloads/libraries/phidg...
tar zxf phidgetwebservice.tar.gz
cd phidgetwebservice-2.1.8.20150410/
./configure
make
sudo make install
Now lets start the webservice in the background
sudo phidgetwebservice21 -n myphidgetName &
Step 2: Attaching the Phidgetboard and Sensor
Attach a PhidgetInterfaceKit 8/8/8 via usb and connect an analog sensor to it.
I am using a potentiometer that I had left. It has different wiring from the phidget sensors so be careful.
If you have a sensor from Phidgets just connect the cable to one of the black analog ports.
That's it onto step 3
Step 3: Installing Nodejs and Writing a Script to Read Values
We need to install NodeJs. You will need version 0.10.36
Here is how you can install it. First go back to your home dir.
cd /home/pi
now get the proper nodejs package, and install it
wget http://node-arm.herokuapp.com/node_0.10.36_armhf....
sudo dpkg -i node_0.10.36_armhf.deb
you can check that you have installed it correctly by running
node --version
it should output
v0.10.36
now let's make a directory for our project
mkdir phidgettest
cd phidgettest
and initialize a nodejs project, just hit return a couple of times to get de default values,
they are not important at this moment
npm init
In order to communicate with the webservice we need a node-module called phidgetapi,
let's install it
npm install phidgetapi --save
And lastly we need a node-js script to read and output the values from an ananlog sensor
use a text-editor like nano to create a "index.js" script with the following content
--- CODE ---
var phidget = require('phidgetapi').phidget;
var IK=new phidget();
IK.on("log", function(data){ console.log('log ',data); } );
IK.on( "error", function(data){ console.log('error ',data); } );
IK.on( 'changed', function(data){
if( String(data.type) == 'Sensor'){
console.log('sensor data ',data.value);
if(parseInt(data.value) > 800){
console.log ("Whoooooops, we crossed a threshold");
}
}
});
IK.on( 'phidgetReady', function(){
console.log('InterfaceKit (IK) ready');
console.log(IK.data);
});
/* * Connect to phidget */
IK.connect( { type:'PhidgetInterfaceKit' } );
-- END CODE ---
All that's left now is to run the script
node index.js
I hope you enjoyed this rather technical Instructable