Internet of Things With Raspberry Pi
Internet of Things With Raspberry Pi
Raspberry Pi
Build Internet of Things Projects Using
the Raspberry Pi Platform
Marco Schwartz, PhD
Contents
Legal
Acknowledgments
About the Author
About the Companion Website
Introduction
1 Who Is This Book For?
2 Organization Of The Book
3 Prerequisites
Chapter 1 Building a Cloud Data Logger
1.1 Hardware & Software Requirements
1.2 Hardware Configuration
1.3 Sending Data to the Cloud
1.4 Visualizing Data
1.5 How to Go Further
Chapter 2 Controlling a Lamp From Anywhere
2.1 Hardware & Software Requirements
2.2 Hardware Configuration
2.3 Creating Your Lelylan Dashboard
2.4 Controlling Your Lamp From Anywhere
2.5 How to Go Further
Chapter 3 Making a Cloud Security Camera
3.1 Hardware & Software Requirements
3.2 Hardware Configuration
3.3 Testing the Camera
3.4 Local Video Streaming
3.5 Cloud Video Streaming
3.6 How to Go Further
Chapter 4 Conclusion
4.1 How to Go Further
Chapter 5 Resources
5.1 General Information About the Raspberry Pi
5.2 Components
5.3 Suggested Reading
Legal
Copyright © 2015 by Marc-Olivier Schwartz
All rights reserved. No part of this book may be used or reproduced in any
manner whatsoever without permission except in the case of brief quotations
embodied in critical articles or reviews.
Acknowledgments
To all my friends who encouraged me while writing this book.
All the code that can be found in this book can also be accessed online at
https://github.com/openhomeautomation/iot-raspberry-pi-book. This
GitHub repository for the book contains all the latest up-to-date code for all
the projects you will find in this book.
To download the code from GitHub, you can simply go on the GitHub page
and click on the “Download ZIP” button.
Also, if at any moment you are encountering an issue while reading this
book, like some code that won’t compile or a project that doesn’t work,
please contact me directly on the following email:
Introduction
You probably have heard all about what you can do with the Raspberry Pi.
This credit-card sized computer can be plugged into your TV or any HDMI
monitor to replace a typical computer. This little device is used in many
computer projects, DIY electronics projects and even as a learning tool for
kids who want to learn the basics of computer programming.
But among all, the Raspberry Pi is the perfect board for the Internet of
Things. It is cheap, powerful, and can be interfaced with many sensors and
actuators that can be used for Internet of Things applications. By using the
Raspberry Pi, you can easily build Internet of Things projects.
We will see that as we can install a full Linux distribution on our Rasperry Pi,
it allows us to use powerful frameworks like Node.js, which is a JavaScript
framework used to build server-side applications. This might sound a little
complicated, but this will make it really easy to interface with cloud-based
services in order to build Internet of Things applications.
The Raspberry Pi also have an amazing online community that are already
building Internet of Things projects, so you can be sure to get immediate
help in case you need some.
All of these points make the Raspberry Pi the ideal platform to build Internet
of Things projects, and this is exactly what you will learn to do in this book.
1 Who Is This Book For?
This book is for all the people who want to use the Raspberry Pi platform to
build Internet of Things projects.
This book is also for the people who are currently building Internet of Things
projects with other platforms like Arduino, and who want to extend their
knowledge, for example by using the Raspberry Pi for more advanced
Internet of Things projects.
Finally, this book is also for people who just want to learn more about how to
use the Raspberry Pi and get knowledge in electronics & programming.
In the first chapter, Building a Cloud Data Logger, we are going to connect a
basic temperature & humidity sensor to the Raspberry, and monitor the data
from an online dashboard, that is accessible from anywhere in the world.
Finally, in the third chapter, Making a Cloud Security Camera, we are going
to make our own DIY version of a security camera, so you can monitor your
home for example from anywhere in the world.
3 Prerequisites
To use this book, you will need to have some basic skills in programming &
electronics. It is recommended that you have some basic experience with
JavaScript and with client/server interactions.
To use all the projects found in this book, you will also need a fully
functioning Raspberry Pi, configured with the Raspbian Linux distribution,
and connected to the Internet. If you need help doing that, you will find all
the required resources on the official Raspberry Pi webite:
https://www.raspberrypi.org/
In this book, I will also usually access my Raspberry Pi via SSH, from my
own computer. You can learn more about how to do that here:
https://www.raspberrypi.org/documentation/remote-access/ssh/
However, you can perfectly do all the projects of this book directly on your
Raspberry Pi, for example if it is connected to an external display.
Chapter 1
Building a Cloud Data Logger
In this first project of the book, we will learn how to use the Raspberry Pi to
monitor some data remotely, from anywhere in the world. This is a project
that you can use for any monitoring applications, as you will be able to
change the sensors used in the project by your own sensors, to monitor the
data that you need.
Then, you will need all the supporting hardware to make the Pi work: a
microSD card, a microUSB power supply, and an HDMI cable if you want to
connect the Pi to a screen. If you want to use WiFi for this project, you will
also need an USD WiFi dongle.
You will also need a DHT11 sensor to measure the temperature & humidity.
Finally, you will need a Raspberry Pi cobbler kit, jumper wires and
breadboard to make the connections between the Raspberry Pi and the
sensor.
This is a list of all the components you will need in this projects:
Raspberry Pi (https://www.adafruit.com/product/2358)
DHT11 sensor with 4.7K Ohm resistor
(https://www.adafruit.com/product/386)
USB WiFi dongle (https://www.adafruit.com/product/1012)
Adafruit Raspberry Pi Cobbler kit
(https://www.adafruit.com/product/1754)
Jumper wires (https://www.adafruit.com/products/758)
Breadboard (https://www.adafruit.com/products/64)
Note that the Raspberry Pi has to be fully configured with the Raspbian
distribution in order to start this project.
On the software side, you will also need to install several components. The
first one is driver for the BCM chip of the Raspberry Pi. You can install it by
following the instructions from this link:
http://www.airspayce.com/mikem/bcm2835/
Finally, you will also need to have Node.js installed on your Pi. You can get it
by typing the following commands in a terminal:
wget http://node-arm.herokuapp.com/node_latest_armhf.deb
sudo dpkg -i node_latest_armhf.deb
The DHT sensor signal pin (pin number 2 of the sensor) has to be connected
on pin 4 of the Raspberry Pi. Also connect the VCC pin (pin number 1 of the
sensor) of the sensor to the RPi 3.3V, and GND to GND. Finally, insert a 4.7K
Ohm resistor between pin number 1 and 2.
This is the final result, without the connection between the Raspberry Pi and
the breadboard:
This is a top view of the whole system:
1.3 Sending Data to the Cloud
We are now going to configure the project so it sends data to the cloud. For
that, we will a cloud service called Dweet.io. This is a very simple way to store
data in the cloud, as it doesn’t require any registration. You can find more
information about Dweet.io at:
http://dweet.io/
This is the main page of Dweet.io, where you can find more information
about their API:
We are now going to write the code that we will use to send data to Dweet.io.
This code will be based on Node.js that we installed earlier. This is the
complete code for this part:
// Send to Dweet
dweetio.dweet({temperature: temperature,
humidity: humidity}, function(err, dweet){
setTimeout(function () {
sensor.read();
}, 10000);
}
};
Let’s now see the details of this code. It starts by importing the required
modules, a module for the DHT sensor, and a module for Dweet.io:
After that, every 10 seconds, we read data from the DHT11 sensor:
Then, we send this data to Dweet.io using the instance we defined before:
Note that the complete code is also available inside the GitHub repository of
the book:
https://github.com/openhomeautomation/iot-raspberry-pi-book
Now, put all this file inside a folder on your Pi, and access this folder via a
terminal. Then, type this in a terminal:
Write down the name of your Dweet.io device (here, “instinctive-horses”) that
is created automatically. Now, as long as this Node.js app is running, data is
logged on Dweet.io every 10 seconds.
https://www.freeboard.io/
After that, you should see that data is coming from the board:
Then create new Pane, and then new widget. We are going to create a Gauge
widget to visualize the current temperature:
These settings will link the widget to the datasource we created before, and to
the temperature field sent by the Raspberry Pi.
Congratulations, you just built your own Internet of Things dashboard! You
will then see that the two gauges are automatically updated every 10 seconds,
as data comes from the Raspberry Pi. Also note that this dashboard is
available from any web browser, so you can monitor this data from anywhere!
1.5 How to Go Further
Let’s first summarise what we did in this chapter. We connected a
temperature & humidity sensor to your Pi, and then monitored this data from
a cloud dashboard that can be accessed from anywhere in the world.
There are many things you can do to use what you learned in this project and
to build your own projects. You can for example add more sensors to the
project to monitor other variables, like the barometric pressure. You could
also add several of these projects at different locations, and then monitor all
of them from a single dashboard.
Chapter 2
Controlling a Lamp From
Anywhere
In this chapter, we are going to do a completely different project using your
Raspberry Pi. We are not going to send any data from your Raspberry Pi to
the cloud, but on the contrary we are going to send data from the cloud to
your Raspberry Pi. As an example, we are going to see how to control a lamp
from anywhere in the world.
This is a list of all the components you will need in this projects:
Raspberry Pi (https://www.adafruit.com/product/2358)
PowerSwitch Tail Kit (https://www.adafruit.com/product/268)
USB WiFi dongle (https://www.adafruit.com/product/1012)
Adafruit Raspberry Pi Cobbler kit
(https://www.adafruit.com/product/1754)
Jumper wires (https://www.adafruit.com/products/758)
Breadboard (https://www.adafruit.com/products/64)
Note that the Raspberry Pi has to be fully configured with the Raspbian
distribution in order to start this project.
The first step is to place the cobbler kit on the breadboard. Then, connect the
pin number 1 of the PowerSwitch to pin number 23 of the cobbler kit. Finally,
connect the two other pins of the PowerSwitch to the GND pin of the cobbler
kit.
You can now connect an electrical device (I used a 30W desk lamp) to the
PowerSwitch, and then also connect it to the mains electricity.
http://lelylan.github.io/devices-dashboard-ng/#/login
When you have an account, you will be redirected to your dashboard which is
empty for now. You can then create a new device and give it a name:
After that, you will be asked to choose the device type. Select Basic Light.
Then, on the next page, choose Connect with MQTT, as we will use the MQTT
protocol to connect our Raspberry Pi to the cloud.
Once the device is created, you will be able to get the details of your new
device:
Write down the ID & Secret of the device, you will need them in a moment to
modify the code on the Raspberry Pi.
You will also see that your lamp is turned off for now:
2.4 Controlling Your Lamp
From Anywhere
It’s now time to write some code, to link the Raspberry Pi to the Lelylan
dashboard. As I mentioned before, we are going to use the MQTT protocol
for that purpose. MQTT is a lightweight protocol that was designed
specifically for the Internet of Things. You can find more information about it
on:
http://mqtt.org/
This is the complete code for this part:
// Required modules
var gpio = require('rpi-gpio');
var mqtt = require('mqtt');
// Topics
in_topic = 'devices/' + device_id + '/get';
out_topic = 'devices/' + device_id + '/set';
// Connect event
client.on('connect', function () {
client.subscribe(in_topic);
});
// Message is Buffer
console.log(message.toString());
json_data = JSON.parse(message.toString());
if (value == 'on') {
gpio.write(23, true, function(err) {
if (err) throw err;
console.log('Written to pin');
});
}
if (value == 'off') {
gpio.write(23, false, function(err) {
if (err) throw err;
console.log('Written to pin');
});
}
// Confirm to Lelylan
client.publish(out_topic, message.toString())
});
Let’s now see the important parts of this code. The code starts by declaring
the device ID and device secret, which you need to modify with your own data
you got before from Lelylan:
var device_id = "device_id";
var device_secret = "device_secret";
Then, we import the two required modules for this project: the rpi-gpio
module to control the GPIO pins of the board, and the MQTT module. This is
done by the following lines of code:
After that, we set pin 23 of the Raspberry Pi (on which the PowerSwitch is
connected) as an output:
gpio.setup(23, gpio.DIR_OUT);
Then, we check what are the incoming messages from the online dashboard.
If it’s a ‘on’ message, we set pin 23 to 1, which will activate the lamp
connected to the PowerSwitch. If we receive the off message, it will set the
pin to 0:
if (value == 'on') {
gpio.write(23, true, function(err) {
if (err) throw err;
console.log('Written to pin');
});
}
if (value == 'off') {
gpio.write(23, false, function(err) {
if (err) throw err;
console.log('Written to pin');
});
}
Note that the complete code is also available inside the GitHub repository of
the book:
https://github.com/openhomeautomation/iot-raspberry-pi-book
It’s now time to test the project. Make sure you put the app.js file in a folder
on your Raspberry Pi, and then go to this folder via a terminal. Then, simply
type:
You can now go back to the dashboard you created before. Test the project by
clicking on the button to turn the lamp on:
You should see that the lamp is turning on immediately. You will also see
some feedback coming from the Raspberry Pi, as the status of the lamp will
be set to “on” inside the dashboard. You can now access this dashboard from
anywhere in the world and control your lamp.
You can of course have several of these modules in different places, and
control them all from a single interface. It’s then easy to control a whole
lighting system in a home from a remote location. You can also mix this
project with sensors, that you can also monitor from the dashboard we
created in this chapter.
Chapter 3
Making a Cloud Security Camera
In this chapter we will use the Raspberry Pi camera module and stream the
video so it can be accessed anywhere in the world. This will transform your
Raspberry Pi in a customizable video surveillance camera that you can place
anywhere you want.
To do that, we will first connect the Raspberry Pi camera module to your Pi,
and then test if it works correctly. After that, we will configure the Raspberry
Pi so it streams the video continuously on a web page. Finally, we will see
how to access this video stream from anywhere.
Raspberry Pi (https://www.adafruit.com/product/2358)
Raspberry Pi camera module
(https://www.adafruit.com/products/1367)
USB WiFi dongle (https://www.adafruit.com/product/1012)
In order to enable the camera, you need to open the config tool from a
terminal:
sudo raspi-config
raspistill -o cam.jpg
The camera LED should turn red, which means it is active and currently
taking a picture. Then, simply check the image that was created by the
command.
3.4 Local Video Streaming
We will now stream video locally using the mjpg-streamer module. This
module is really great for video stream, as it will create a nice web page from
which you can access the video stream.
However, the module is not available for the Raspberry Pi camera in the
official repository. But luckily for us, a developer made a version available for
the Raspberry Pi camera, and we are going to install it now. You can find all
the details at:
https://github.com/jacksonliam/mjpg-streamer
export LD_LIBRARY_PATH=.
http://rpi_ip_address:8080
Simply go on ‘Stream’ in the menu, and you should see live streaming from
your Pi!
3.5 Cloud Video Streaming
That’s good, but we are not there yet. We now want to make this page
accessible from anywhere in the world, so you can monitor your video stream
from any web browser just by having an URL.
To do that, we are going to use ngrok, which is a simple utility that takes a
local web server (here, the streaming server we accessed before) and makes it
available on the web. You can download it from:
https://ngrok.com/
Then, simply put it inside a folder on your Pi, and make sure streaming is still
active. Then, go to this folder and type the following command:
./ngrok 8080
This will basically tell ngrok to take the server running on port 8080 (our
mjpg-streamer app) and make it available on the web. You will be taken to
the following inside the terminal:
See the “Forwarding” URL? This is the address on which your streaming web
page is available now. Then, just copy this URL in a browser. You should see
your stream just as before:
Of course, you are probably seeing this from your computer, but note that this
can be done from anywhere in the world!
First, we built a cloud data logger project, that allowed us to monitor data
coming from your Pi, from anywhere in the world.
Finally, we built a DIY version of a video security camera, that can stream
video and can be accessed from any web browser.
You can add more sensors to your projects, and put your projects around
your home to monitor data from anywhere in the world. You can also
perfectly mix what you learned in the different projects of this book, for
example to have a project that logs data in the cloud & stream live video at
the same time. Thanks to the power of the Raspberry Pi, this is perfectly
possible.
Using the Raspberry Pi 2, you can also exploit the computing power of the
board to do amazing applications, like live a face detection application that
you could monitor from anywhere.
You can also use what you learned about Node.js to interface your projects
with web APIs, like Twitter. I also recommend checking out the Temboo
(https://temboo.com/) website, that makes it really easy to interface your
projects to web APIs. Really, the only limit is your imagination, so
experiment, and have fun!
Chapter 5
Resources
The following is a list of the best resources concerning Internet of Things
with the Raspberry Pi. I organized this chapter in different categories so it is
easier for you to find the information you need.
5.2 Components
SparkFun (https://www.sparkfun.com/): A website selling many
components that can be interfaced with the Raspberry Pi. All their
products are open-source and you can download the source files
directly from their product descriptions.
Adafruit (http://www.adafruit.com/): A company based in New York
that sells high quality products for the Raspberry Pi platform.
SeeedStudio (http://www.seeedstudio.com/): A Chinese company that
sells many original products for the Raspberry Pi platform. They also
offer their own PCB production & assembly services.