• No results found

f Connect the 3.3-volt power source

to the VCC line on the breadboard.

Figure 4.9 Connecting a DHT22 temperature and humidity sensor to the Pi

Because the DHT22 uses a special protocol, you’ll first need to install an additional driver on the Pi called the BCM 2835 C Library.34 The next listing shows how to install it.

$ cd chapter4-gpios/drivers $ tar zxvf bcm2835-1.50.tar.gz $ cd bcm2835-1.50

$ ./configure $ make

$ sudo make check $ sudo make install

You’re now ready to interact with the sensor using Node code. To do this, you’ll use a Node library called node-dht-sensor that you first need to install with npm install - -save node-dht-sensor. The code to run on the Pi is shown in the following listing.

var sensorLib = require('node-dht-sensor'); sensorLib.initialize(22, 12);

var interval = setInterval(function () { read();

}, 2000);

33You can use resistances from 4.7K to 10K Ohms.

Listing 4.8 Installing the BCM2835 driver

34See http://www.airspayce.com/mikem/bcm2835/index.html.

Listing 4.9 dht.js: communicating with the DHT22 sensor VCC (3.3 V)

We packaged a version of the driver for you. Unzip

the code.

This driver is written in C, so you need to compile it yourself.

22 is for DHT22/AM2302; 12 is the GPIO you connect to on the Pi.

Create an interval to read the values every two seconds.

107

Connecting sensors and actuators to your Pi

function read() {

var readout = sensorLib.read();

console.log('Temperature: ' + readout.temperature.toFixed(2) + 'C, ' + 'humidity: ' + readout.humidity.toFixed(2) + '%');

}; [...]

Save this file and run it as superuser with the sudo command because accessing the BCM 2835 driver requires it: sudo node dht.js. If everything works fine, you should see temperature and humidity values appearing every two seconds.

This concludes your first encounter with GPIOs, which are a great way to add func- tionality to your Pi—or any other embedded device, really!—so that it can sense or actuate the real world. Now that you have the basics, nothing prevents you from add- ing other sensors and actuators to your Pi; you’ll find plenty of tutorials on the web.

4.4.4 Beyond the book

If you want to learn more about embedded platforms, sensors, and hardware proto- typing, there are many excellent resources and books that can help. A good source of information for emerging embedded platforms is Postscapes35 and their IoT Toolkit.36

For the more industrial side of things, you can use the Embedded portal.37 For

resources about hardware prototyping and electronics, make sure you check the Make38 blog posts, books, and magazines. You might also look into the Instructables,39

Element 14,40 and Sparkfun communities,41 where you’ll find many step-by-step tutori-

als and lots of good advice.

The nerd corner—I want to see bits!

Libraries like onoff or node-dht-sensor prevent you from having to deal with the nitty- gritty details of interacting with low-level hardware sensors and actuators. This is also what the Web of Things is about: abstracting these (complex) details so that you can focus on your creative applications and build them using web tools. You might still want to know roughly how these libraries work. Basically, onoff puts watches on virtual files that Linux uses to update the values of GPIOs (1 or 0) using a library called epoll. node-dht-sensor needs to retrieve more complex binary data and uses a C library that communicates over the GPIOs using two popular protocols in the low-level embedded world of bits: I2C (Inter-Integrated Circuit) and SPI (Serial Peripheral Interface). If you want to dig deeper, reading about these protocols is a good start.

35http://postscapes.com 36http://postscapes.com/internet-of-things-resources/ 37http://www.embedded.com/ 38http://makezine.com/ 39http://www.instructables.com/ 40http://www.element14.com/community/welcome 41https://learn.sparkfun.com/

Read the sensor values.

Readout contains two values: temperature and humidity.

108 CHAPTER 4 Getting started with embedded systems

No doubt this chapter was a challenging one! Covering so many different technol- ogies and concrete skills in a single chapter isn’t easy, so you’ve done a great job mak- ing it so far. But getting your Pi ready for the Web of Things is only the beginning.

4.5

Summary

Many types of embedded platforms are commonly used in the Internet of

Things, and you learned how to pick the right one for your use case.

There are two main categories of operating systems for devices: RTOS and

Linux.

By installing Linux on a Raspberry Pi, you can easily access it remotely over SSH

and configure it to be ready for the Web of Things.

Embedded devices allow you to use breadboard, wires, and resistors to wire var-

ious sensors and actuators to the GPIO pins.

Using Node.js on your Pi makes it easy to write simple applications that read

data from your sensors and control the LED using the onoff library asynchro- nously over the GPIO ports.

Now that you have a real embedded device at home, it’s time to connect it to the Web of Things. The next chapters will be about making sure it seamlessly integrates with the World Wide Web of Things. The first step comes with the next chapter: architect- ing an API for your Pi, its sensors, and actuators and using REST, HTTP, WebSockets, and JSON. In the following chapters you’ll see how to make these sensors and actua- tors accessible through web APIs.

109

Building networks

Related documents