Reporting Rasp Pi data on a Node.js web server


In this experiment, you'll be using a DHT sensor. These kinds of sensors are common in basic electronic experiment sets for Arduino and are pretty great: Apart from giving you some real-world information to play with, they do the hard work of translating the analog data they collect. This is useful because, although Arduinos can read analog data, Rasp Pis can't. A DHT saves you the trouble of having to put an analog-to-digital converter (ADC) between the sensor and the Rasp Pi.

The model I have, pictured in Figure 2, is a DHT22 (DHT11s are usually blue), and it comes ready to be hooked up to the Rasp Pi directly. If you have one without the little bit of electronics, all you have to do is connect the sensor's VCC pin to the Rasp Pi's 3V3 or 5V pin (if one doesn't work for you, try the other) and a resistor with a resistance between 4.7K and 10K. For more information, see the Adafruit tutorial [3].

You can then connect the sensor's signal pin, the one that carries the data, to one of your GPIO pins. In Figure 4, you can see that I connected the sensor's VCC pin to one of the Rasp Pi's 5V pins (pin 2), the sensor's GND pin to pin 6 (Ground), and the sensor's signal pin to GPIO18 (pin 12). As for hardware, that's all there is to it.

Figure 4: Hook up your sensor's VCC pin to a 5V or a 3V3 pin (if you have problems with one, try the other), its GND pin to a Ground pin on the Rasp Pi, and its signal pin to one of your GPIO pins.

Something else that makes this project super-easy is that Node.js has a module for these kinds of sensors! So convenient.

DHT Server

First, give your project somewhere to live by creating a directory:

mkdir dht_server

Then, cd into it (e.g., here, cd dht_server).

Before you install the DHT module with npm (see the "NPM" box for more on this tool), the module's documentation says that first you have to install the Broadcom BCM 2835 library to access GPIO pins directly.


Nearly as important as the node interpreter is npm, the Node Package Manager. NPM works much like Raspbian's Apt, but it installs Node.js libraries – or "modules," as they are known in Node parlance.

To install a module enter:

npm install <modulename>

As with other package managers, NPM will install not only the module, but all of its dependencies, as well.

However, you have installed the module "system wide" (i.e., for all of your Node.js projects), but one of the cool things about Node.js is that it allows you to build very compact servers for very specific uses, so installing every module system wide goes against the main philosophy of Node.js.

NPM offers you a way to install modules locally for your current project only. To do that, use the --save modifier:

npm install <modulename> --save

The module is now installed in the project's directory. NPM will also set up the module out of the way, in its own directory under node_modules/, somewhere the node interpreter is able to find it when you run your server.

To remove a package you can use npm rm <modulename>.

Beyond Package Managing

Despite its name, NPM can be used for far more than adding and removing packages. Try this in your project folder:

npm init

You will see that NPM asks you several questions, including your project's name and the name of the main script. When you're done, NPM creates a file called package.json with all the information you gave it (e.g., Listing 3). Also, every time you use NPM to install a new package for your application, it adds a new entry in the dependencies section, which is useful because, if you want to take you application to another machine or share it online, you just need to share the stuff you wrote and the package.json file. You can forget all about the other stuff you installed from the NPM repos, because when you (or another user) sets up an application in a new location, you can just run

npm install

without arguments; NPM will fetch and install everything it needs.

You will also be able to run your server with

npm start

regardless of what you called your main .js file. This is useful if a third party (or another program) needs to run your server and doesn't necessarily know what you called it.

Run npm help to find out what else NPM can do for you.

Listing 3

npm init

01 $ npm init
02 This utility will walk you through creating a package.json file. It only covers the most common items, and tries to guess sensible defaults.
04 See `npm help json` for definitive documentation on these fields and exactly what they do.
06 Use `npm install <pkg> --save` afterwards to install a package and save it as a dependency in the package.json file.
08 Press ^C at any time to quit.
09 name: (dht_server)
10 version: (1.0.0)
11 description: Show temperature and humidity data on a web page.
12 entry point: (index.js) server.js
13 test command:
14 git repository:
15 keywords: temperature, humidity, DHT, Raspberry Pi, web
16 author: Paul Brown
17 license: (ISC)
18 About to write to /home/pi/Node/dht_server/package.json:
20 {
21  "name": "dht_server",
22  "version": "1.0.0",
23  "description": "Show temperature and humidity data on a web page.",
24  "main": "server.js",
25  "dependencies": {
26    "node-dht-sensor": "^0.0.28"
27  },
28  "devDependencies": {},
29  "scripts": {
30    "test": "echo \"Error: no test specified\" && exit 1"
31  },
32  "keywords": [
33    "temperature",
34    "humidity",
35    "DHT",
36    "Raspberry",
37    "Pi",
38    "web"
39  ],
40  "author": "Paul Brown",
41  "license": "ISC"
42 }
44 Is this ok? (yes)

You will not find this library in your Rasp Pi's Raspbian repositories, so first you have to download it,


uncompress it,

tar xvf bcm2835-1.50.tar.gz

and cd into the directory it creates, so you can build and install the library:

sudo make install

Now you can cd back out (cd ..) and install the Node.js DHT module:

npm install node-dht-sensor

Run npm init to set up your project. My setup looks like Listing 3. See lines 14-20 for the information I entered in response to the program queries. (Pressing Return accepts the default values shown in parentheses.)

As you can see, NPM "remembers" that you already installed node-dht-sensor and has included it as a dependency of your project (line 29).

Now check whether your sensor works with a very bare-bones script (Listing 4).

Listing 4


01 var sensorLib = require("node-dht-sensor");
02 var sensorResult =, 18);
03 console.log("Temperature: " + sensorResult.temperature.toFixed(1) + "°  / Humidity: " +  sensorResult.humidity.toFixed(1) + "%");

Line 1 brings in the node-dht-sensor module and creates an object called sensorLib. In line 2, you then read the sensor with This module takes two parameters: 22 is the type of sensor – if you have a DHT11, change this value to 11 – and 18 is the GPIO pin to which the sensor is connected. The function returns a JavaScript object containing the temperature and humidity detected by the sensor, which prints out to the command line (line 3).

To run the script, enter node DHTtest.js, and you'll see the temperature and humidity your sensor is currently detecting. For more fun, breathe on the sensor and you'll see the humidity shoot up.

Buy this article as PDF

Express-Checkout as PDF

Pages: 8

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content