You can do this
Author | Abhinaya Balaji
Editor | Su Ma
WeChat ID | csdn_iot
There are many devices on the market that provide the ability to monitor the home environment from a central interface. In this article, we will DIY a set of such monitoring equipment. To do this, we will use the B+ model Raspberry Pi development board and the official Raspberry Pi camera module. Additionally, we will also use a temperature and humidity sensor for some measurement work.
By the end of the article, you will be able to build an interface to access camera and sensor recordings. We will also show how to access this interface from anywhere in the world. Let’s get started!
The first thing you need before starting the project is the Raspberry Pi B+ development board. It has powerful features (such as 4 USB ports), and you can also use older versions of the Raspberry Pi.
You will need to use the official Raspberry Pi camera module to take photos. The DHT11 (or DHT22) sensor will also be used to measure the temperature and humidity in the home.
Since we will be accessing the Raspberry Pi remotely, you will need a USB wireless network card.
You will also need to install the Adafruit Cobbler kit, breadboard, and some jumper wires. You need these to connect the Raspberry Pi, camera, and sensor.
Here is a list of components needed for this project:
-
Raspberry Pi B+ development board (with microSD card, microUSB cable, and HDMI cable)
-
Raspberry Pi camera module
-
DHT11 sensor with 4.7k ohm resistor
-
USB wireless network card
-
Adafruit Cobbler kit
-
Jumper wires
-
Breadboard
Check if your Raspberry Pi has a Linux distribution installed. This is to ensure you have a fully functional Pi. In this project, I used the Raspbian operating system.
If you haven’t completed the operating system installation, you can refer to this article: http://www.raspberrypi.org/help/quick-start-guide/.
Connect the Raspberry Pi to the local WiFi network and install the driver for the BCM2835 chip to read data from the DHT11 sensor.
You can follow the instructions below to download and install these drivers:
http://www.raspberry-projects.com/pi/programming-in-c/c-libraries/bcm2835-by-mike-mccauley.
The entire project is based on Node.js. It will act as a server from which we can access all the functionalities of our Raspberry Pi.
First, you need to install Node.js on the Pi. Note that you cannot use the apt-get tool to install Node package modules because you might install an old version. To install the latest version of Node.js, follow the guidelines below:
http://revryl.com/2014/01/04/nodejs-raspberry-pi/
You will also need to install drivers for the BCM2835 chip. You can download and install these drivers by visiting this page:
http://www.airspayce.com/mikem/bcm2835/
After that, download the files for this project from GitHub:
https://github.com/openhomeautomation/rpi-web-control
We need to install some packages and then access the Raspberry Pi on the local WiFi network through rapsberrypi.local. After we do this, we will not need to access the Pi through its IP address.
After that, you can log in to the Raspberry Pi via SSH or access it directly and type the following command:
sudo apt install avahi-daemon netatalk
If you follow the above steps, you should be able to configure the Raspberry Pi easily. Now let’s add other components.
First, we will connect the camera. Follow the instructions on the Raspberry Pi website for installation.
Here is an image of the camera I used. Next to it is my Raspberry Pi:
Please note that I used a plastic case for my Raspberry Pi. The plastic case is not necessary for this project.
Next, we will connect the DHT11 sensor to the Raspberry Pi using the Cobbler kit. After assembling the kit, connect it to the Raspberry Pi GPIO interface. Connect the other end of the cable to the breadboard through the PCB adapter.
For the DHT11 sensor, the pin distribution is as follows:
Connect the VCC pin to the Raspberry Pi 3.3V pin, GND to GND, and DATA to pin 4 on the GPIO connector. Finally, connect a 4.7K ohm resistor between the VCC and DATA pins. The following image shows the final connection of the sensor:
We are now going to test the sensor and camera one by one.
Since we are going to build a Node.js based application, we will use Node to connect to the DHT11 sensor. To do this, we will use an existing dedicated Node module.
You will find everything in the project code under a folder named sensors_test. Here is the complete code for this section:
var sensorLib = require(‘node-dht-sensor’);
var dht_sensor = {
initialize: function () {
return sensorLib.initialize(11, 4);
},
read: function () {
var readout = sensorLib.read();
console.log(‘Temperature: ‘ + readout.temperature.toFixed(2) + ‘C, ‘ +
‘humidity: ‘ + readout.humidity.toFixed(2) + ‘%’);
setTimeout(function () {
dht_sensor.read();
}, 2000);
}
};
if (dht_sensor.initialize()) {
dht_sensor.read();
} else {
console.warn(‘Failed to initialize sensor’);
}
In the above code, we first import the module required for the DHT sensor. Then, every 2000ms, we read data from the sensor and display it in the terminal using console.log(). The code for this section is available in the project’s GitHub repository at the following address:
https://github.com/openhomeautomation/rpi-web-control
Now let’s start testing the code. After downloading the code from GitHub, navigate to the sensors_test folder and type the following command:
sudo npm install node-dht-sensor
The execution of the above command will take some time, please be patient. Once completed, type the following command:
sudo node sensors_test.js
After executing the command, you should be able to see the temperature and humidity values printed in the terminal:
Temperature: 21.00C, humidity: 35.00%
Testing the camera is also very simple. Just go to the terminal and type the following command:
raspistill -o cam.jpg
Then, go to the folder where you executed this command, you should see a photo named cam.jpg.
We are now going to write a Node.js based application to remotely track the data from the sensor and camera. The code roughly consists of three parts:
-
Server code in Javascript
-
HTML page containing the interface
-
Client Javascript file connecting the above two
Let’s first look at the server-side Javascript code. It first includes the required Node modules:
-
node-dht-sensor module to handle the DHT sensor
-
Express to handle HTTP communication like a web server
Here’s the code:
var sensorLib = require(‘node-dht-sensor’);
var express = require(‘express’);
var app = express();
We also include the views and public directories. The views directory is where we will store the interface, while the public directory is where we will keep the JavaScript code and the recorded images:
app.set(‘views’, __dirname + ‘/views’)
app.set(‘view engine’, ‘jade’)
app.use(express.static(__dirname + ‘/public’))
We will create paths for the interface to access our project:
app.get(‘/interface’, function(req, res){
res.render(‘interface’);
});
We will include the Raspberry Pi version of the aREST API (http://arest.io/) so that we can control the Raspberry Pi via HTTP:
var piREST = require(‘pi-arest’)(app);
In addition, we will set an ID and name for the Raspberry Pi:
piREST.set_id(‘1’);
piREST.set_name(‘my_RPi’);
Finally, we call app.listen() to start our application:
var server = app.listen(3000, function() {
console.log(‘Listening on port %d’, server.address().port);
});
The interface itself is written in Jade (http://jade-lang.com/). This will give us an HTML formatted result. This file is stored in the application’s views directory. We add some titles, some containers for sensor measurements, and a field to store the images recorded by the camera:
doctype
html
head
title Raspberry Pi Interface
script(src=”/js/jquery-2.0.3.min.js”)
script(src=”/js/interface.js”)
link(rel=’stylesheet’, href=’/css/style.css’)
body
header
div.mainContainer
h1 Raspberry Pi Interface
h2 Sensor Readings
p Temperature:
span#temperature 0
span C
p Humidity:
span#humidity 0
span %
h2 Camera
img#camera(src=”)
As you can see in the code, we will include a Javascript file and a jQuery in the Jade template. The script file will call the Raspberry Pi’s aREST API every 2000ms to refresh the DHT11 sensor measurements:
setInterval(function() {
// Update temperature
$.get(“/temperature”, function(data) {
$(“#temperature”).html(data.temperature);
});
// Update humidity
$.get(“/humidity”, function(data) {
$(“#humidity”).html(data.humidity);
});
}, 2000);
The camera will take a picture every 10 seconds:
setInterval(function() {
// Take picture
$.get(“/camera/snapshot”);
}, 10000);
The image in the interface will refresh every second:
setInterval(function() {
// Reload picture
d = new Date();
$(“#camera”).attr(“src”,”pictures/image.jpg?” + d.getTime());
}, 1000);
Next, to test the interface, navigate to the pi_node folder and type the following command:
sudo npm install node-dht-sensor express pi-arest
Wait a moment, then continue to type the command:
sudo node pi_node.js
Finally, use any of your favorite browsers and enter the following address:
http://raspberrypi.local:3000/interface
If you are on the Raspberry Pi, just enter the following address:
http://localhost:3000/interface
If all goes well, you should see the project interface displayed as follows:
Be patient while the sensor measurement data and images appear on the display. Remember, the sensor measurements will have a 2-second delay, while images will have a 10-second delay.
Also, please note that the Node.js module for the camera is not perfect. There is a delay between taking the photo and displaying it in my system.
Overall, it is successful; you can now remotely access the sensor data and camera of the Raspberry Pi!
Now, we have seen how to access this interface from a computer or phone in a home environment.
This is already great, but it would be even cooler if we could access the project from anywhere in the world to check the images taken by the camera module. This is actually quite simple, and I will tell you how to achieve it next.
We will use a simple tool called Ngrok to implement this. This tool will create a tunnel between your Raspberry Pi and a remote server, so you can access the interface anytime, anywhere. The first step is to download Ngrok:
https://ngrok.com/
Place the file in a folder and access this folder via the terminal on the Raspberry Pi. Then type the following command:
./ngrok 3000
This will open a connection between the Raspberry Pi and the Ngrok server, confirming the message as shown below:
You can now try it out by entering the URL in your browser. You should see the same interface as in the previous section, which means you can control your system from anywhere in the world!
One last thing: be careful with this. Now anyone can access this interface with the correct URL, and right now it is just connected to a relay that is not actually connected to anything, but if your entire alarm system is connected to the Raspberry Pi, such arbitrary access is definitely risky! In this case, you better set up a robust login/password system on the server so that only you can access it.
We have built a Node.js based application to automatically access measurement data from the sensors. We also used a Node.js module to access the camera module of the Raspberry Pi. Finally, we displayed everything on a nice web interface, and we also showed how to access our project from anywhere in the world using Ngrok.
You can further expand the project by including more sensors. For example, you can add light intensity sensors and motion sensors, and it is very easy to display the status of these sensors in the Node.js application.
Original: MQTT – The Nerve System of IoT
Translator: An Xiang
1. Experts explain the complexity of embedded programming!
2. Some say you shouldn’t rely on technology for a lifetime, what do you think?
3. Learn the basics to start circuit design~
4. Goddess-level programmers who live-stream coding have something to say to you…
5. The August electronic issue of “Microcontroller and Embedded System Applications” is freshly out~
6. Porting μC/OS-II operating system on various processors
Disclaimer: This article is a network reprint, and the copyright belongs to the original author. If there are any copyright issues with the work, please contact us, and we will confirm the copyright based on the copyright certificate materials you provide and pay for the manuscript or delete the content.
Leave a Comment
Your email address will not be published. Required fields are marked *