This is a follow up to my previous post detailing how to get temperature readings using a Pi and the DS18B20 digital temperature sensor. For this part I am going to expose the temperature readings via a JSON feed using node.js to serve the response.
On a request for /temperature.json the application reads the contents of w1_slave as utf-8, which is the data of the 1-Wire digital thermometer connected to pin #4. A regex is then used to extract the temperature reading before forming an application/json response for the temperature in both celcius and fahrenheit.
A breadboard and adafruit or similar breakout board (or breadboard and some jumper leads)
For one of my first Raspberry Pi projects I was looking for a way of measuring the temperature of a room and making that available via a web feed (which will be a follow up post).
I was recommended the DS18B20 1-Wire digital temperature sensor, so I ordered one on Amazon for a few pounds. I already had some spare 4.7Kohm resistors laying around.
First update and upgrade your version of Raspbian (this may take a little while!)
12
sudo aptitude update
sudo aptitude upgrade
Run the following commands to register the new sensor we’re going to connect, and add temperature support
12
sudo modprobe w1-gpio
sudo modprobe w1-therm
Now set up the circuit as follows:
Connect the 3.3v output (pin 1 of the Pi) to pin 3 of the sensor.
The GPIO#4 (pin 4 of the Pi) is connected to the data output (pin 2) of the sensor.
Finally the GND (pin 6 of the Pi) needs to be connected to pin1 of the sensor.
Insert the 4.7kΩ resistor between pin 2 and pin 3 of the temperature sensor.
This should have set up the Pi to report temperature readings via GPIO4. To find out the available device, issue the following commands
12
cd /sys/bus/w1/devices
ls
These will be 1-wire devices associated with the Pi.
12
cd 28-000003b74282
cat w1_slave
There will either be a YES or NO as the end of the first line. If it’s a NO, then cat the file again, as this indicates it isn’t a valid temperature reading.
If it’s YES, then the end of the second line should look something like it does above, with the t=27312 indicating the temperature in degrees celsius multiplied by 1000.
It’s easy to install node.js on a Raspberry Pi and makes for a great lightweight webserver to serve as an interface for web projects.
Before installing node you’ll need to install build-essential (and git-core if you are going to use git for version control).
1
sudo aptitude install build-essential git-core
I went with v0.11.0 of node, but you can check out the versions available here.
Building from source took a couple of hours, but the install step was relatively quick.
123456
wget http://nodejs.org/dist/v0.11.0/node-v0.11.0.tar.gz
tar xzf node-v0.11.0.tar.gz
cd node-v0.11.0.tar.gz
./configure
make
sudo make install
When node is finally installed, you can create a simple app to check everything is working correctly.
123456
varhttp=require('http');http.createServer(function(req,res){res.writeHead(200,{'Content-Type':'text/plain'});res.end('It works!\n');}).listen(8124,"127.0.0.1");console.log('Server running at http://127.0.0.1:8124/');
Firing up your browser and hitting http://127.0.0.1:8124/ should give you a page with the message “It works!”
Or you can quickly verify with curl:
I need to run some Ruby scripts on my Raspberry Pi, so I decided to go about installing Ruby using RVM.
After booting up my Pi, I connected to it via SSH and proceeded to install Git, RVM and Ruby.
Before you start, the installation of RVM and Ruby look almost 2 hours on my Pi, so be prepared for a long wait.
I’ve recently started using guard to monitor my rails application and run the appropriate tests. Guard is great, but alt-tabbing between text editor and terminal can be a bit of a time waster.
To alleviate this, users of Mountain Lion can have their RSpec test status displayed using the notification centre, in a similar fashion that Growl used to. This means you get regular feedback while still having focus on the text editor (and it even works in full screen mode!)
To get it working, add the ‘terminal-notifier-guard’ gem to the development group in your Gemfile.
After that, install the gem in your project using:
1
bundle install
The next time you fire up guard you should see something simlar to the following which lets you know that TerminalNotifier is sending notifications.
1234567
James-iMac% guard
15:55:35 - INFO - Guard uses TerminalNotifier to send notifications.
15:55:35 - INFO - Guard uses TerminalTitle to send notifications.
15:55:36 - INFO - Guard::RSpec is running
15:55:36 - INFO - Running all specs
... spec output ...
15:55:41 - INFO - Guard is now watching at '/Users/james/rails/blog'
This should be the end result - short and sweet RSpec notifications.