James Walton

code and internet

Publishing Temperature Reading From a DS18B20 1-Wire Digital Temperature Sensor Using a Raspberry Pi and node.js

| Comments

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.

Node application to serve temperature readings As a GitHub Gist
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
var http = require('http');
var fs = require('fs');
var url = require('url');
var fileLocation = '/sys/bus/w1/devices/28-000003b74282/w1_slave';

http.createServer(function (req, res) {
  var request_url = url.parse(req.url).pathname;

  if (request_url == '/temperature.json') {
    fs.readFile(fileLocation, 'utf8', function(err, data) {
      if (err) throw err;
      matches = data.match(/t=([0-9]+)/);
      temperatureC = parseInt(matches[1]) / 1000;
      temperatureF = ((temperatureC * 1.8) + 32).toFixed(3);
      output = '{ "temperature": { "celcius": '+ temperatureC +', "fahrenheit": '+ temperatureF +' } }';
      res.writeHead(200, { 'Content-Type': 'application/json' });
      res.end(output);
    });
  } else {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('No route');
  }
}).listen(8124, "0.0.0.0");

console.log('Server running on port 8124');

A working example, providing my Pi hasn’t been fried, can be found at http://pi.jam.im:8124/temperature.json.

Digital Thermometer Temperature Readings With a Raspberry Pi

| Comments

You will need:

  • Raspberry Pi (I was using Raspbian)
  • DS18B20 1-Wire digital temperature sensor
  • 4.7Kohm resistor
  • 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!)

1
2
sudo aptitude update
sudo aptitude upgrade

Run the following commands to register the new sensor we’re going to connect, and add temperature support

1
2
sudo modprobe w1-gpio
sudo modprobe w1-therm

Now set up the circuit as follows:

DS18B20 1-Wire digital temperature sensor pins

  • 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.

DS18B20 and Pi Cobbler

This should have set up the Pi to report temperature readings via GPIO4. To find out the available device, issue the following commands

1
2
cd /sys/bus/w1/devices
ls

These will be 1-wire devices associated with the Pi.

1
2
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.

Example contents of w1_slave
1
2
3
37 00 4b 46 ff ff 07 10 1e : crc=1e YES

37 00 4b 46 ff ff 07 10 1e t=27312

Installing node.js on a Raspberry Pi

| Comments

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.

1
2
3
4
5
6
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.

1
2
3
4
5
6
var http = 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:

1
2
% curl http://127.0.0.1:8124
It works!

Installing Ruby 2 and RVM on a Raspberry Pi

| Comments

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.

  • Boot up your Raspberry Pi

  • Install Git and dependencies on your Pi

Install Git
1
2
sudo aptitude update
sudo aptitude install git-core
  • Install RVM and the latest stable version of Ruby. At the time of writing, this was ruby-2.0.0-p0.
Install RVM and Ruby
1
curl -L https://get.rvm.io | bash -s stable --ruby

Almost two hours later, RVM and Ruby 2.0.0 is installed.

To load RVM into the current shell you’ll need to run the following command, relative to your install

1
2
3
pi@raspberrypi ~ $ source /home/pi/.rvm/scripts/rvm
pi@raspberrypi ~ $ ruby -v
ruby 2.0.0p0 (2013-02-24 revision 39474) [armv6l-linux-eabihf]

Mac OSX Notifications With Guard

| Comments

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.

Add ‘terminal-notifier-guard’ to your Gemfile
1
2
3
group :development do
  gem 'terminal-notifier-guard'
end

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.

1
2
3
4
5
6
7
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'

RSpec results using OSX Notifications This should be the end result - short and sweet RSpec notifications.