Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Internet of Things Programming with JavaScript

You're reading from  Internet of Things Programming with JavaScript

Product type Book
Published in Feb 2017
Publisher Packt
ISBN-13 9781785888564
Pages 298 pages
Edition 1st Edition
Languages

Table of Contents (15) Chapters

Internet of Things Programming with JavaScript
Credits
About the Author
www.packtpub.com
Customer Feedback
Preface
Getting Started with Raspberry Pi Zero Connecting Things to the Raspberry Pi Zero Connecting Sensors - Measure the Real Things Control-Connected Devices Adding a Webcam to Monitor Your Security System Building a Web Monitor and Controlling Devices from a Dashboard Building a Spy Police with the Internet of Things Dashboard Monitoring and Controlling Your Devices from a Smart Phone Putting It All Together

Chapter 7. Building a Spy Police with the Internet of Things Dashboard

In this chapter, we will look at several home domestic projects. You can combine these projects with the other tools that we have seen in previous chapters. Doing so will help you improve your knowledge and also let you develop your own. In this chapter, the following topics will be covered:

  • Spy microphone that detects noise

  • Regulating the current of an AC lamp dimmer

  • Controlling access with an RFID card

  • Detecting smoke

  • Building an alarm system using Raspberry Pi Zero

  • Monitoring the climate from a remote dashboard

Spy microphone that detects noise


In this section, we will look at a project that we can use in a house to detect noise or the level of sound so that we can detect when a person talks in front of the house. This project consists of a module that has a microphone, similar to the following image:

Software code

We need to make a program that can read the analog signal that the module sends to the Arduino board:

const int ledPin =  12;         // the number of the LED pin 
const int thresholdvalue = 400; //The threshold to turn the led on 
 
void setup() { 
 pinMode(ledPin, OUTPUT); 
 Serial.begin(9600); 
} 
   
void loop() { 
  int sensorValue = analogRead(A0);   //use A0 to read the electrical signal 
  Serial.print("Noise detected="); 
  Serial.println(sensorValue); 
  delay(100); 
  if(sensorValue > thresholdvalue) 
  digitalWrite(ledPin,HIGH);//if the value read from A0 is larger than 400,then light the LED 
  delay...

Regulating the current of an AC lamp dimmer


In this section, we will see how to regulate an AC lamp. For so many years I've wanted to explain and share a project like this, and I'm finally. This can be applied to regulate your lamps at home in order to decrease domestic power the consumption: the following sections will explain the project in more detail.

Hardware requirements

We need the following electronic components:

  • H-bridge

  • 24 AC transformer

  • Two resistors 22k (1 watt)

  • One integrated circuit (4N25)

  • One resistor 10k

  • One potentiometer of 5k

  • One resistor 330 ohms

  • One resistor 180 ohms

  • One integrated circuit MOC3011

  • One TRIAC 2N6073

In the following circuit diagram, we can see the connections for the dimmer from the Arduino board:

Software code

You can now either copy the code inside a file called Dimner.ino, or just get the complete code from the folder for this project:

int load = 10;  
int intensity = 128; 
 
void setup() 
{ 
pinMode(loaf, OUTPUT); 
attachInterrupt(0, cross_zero_int...

Controlling access with an RFID card


In this section, we will see how to control access via a door. In the last chapter, we saw how to control the lock and the lamps of a house. This project can complement the last one as it will enable you to control the opening of a door, a specific bedroom door, or lights in other rooms.

Hardware requirements

For this project, we need the following equipment:

  • Reading TAGS cards

  • RFID RC522 Module

  • Arduino Board

The following image shows the RFID tags for reading and controlling the access:

The following figure, shows the RFID card interface for Arduino:

Software requirements

We need to install the <MFRC522.h> library, this file can communicate with and configure the module for reading the tag cards. This library can be downloaded from https://github.com/miguelbalboa/rfid.

Software code

You can now either copy the code inside a file called RFID.ino, or just get the complete code from the folder for this project:

#include <MFRC522.h> 
#include <SPI...

Detecting smoke


In this section, we will test an MQ135 sensor which can detect smoke. This could also be used in a home to detect a gas leak. In this case, we will use it to detect smoke.

In home automation systems, putting all the sensors to detect something at home, we measure the real world: in this case we used the MQ135 sensor which can detect gas and smoke, as shown in the following image:

Software code

In the following code, we explain how program and detect smoke using the gas sensor:

const int sensorPin= 0; 
const int buzzerPin= 12; 
int smoke_level; 
 
void setup() { 
Serial.begin(115200);  
pinMode(sensorPin, INPUT); 
pinMode(buzzerPin, OUTPUT); 
} 
 
void loop() { 
smoke_level= analogRead(sensorPin); 
Serial.println(smoke_level); 
 
 
 
if(smoke_level > 200){  
digitalWrite(buzzerPin, HIGH); 
} 
 
else{ 
digitalWrite(buzzerPin, LOW); 
} 
} 

If it doesn't detect...

Building an alarm system using the Raspberry Pi Zero


In this section, we will build a simple alarm system with a PIR sensor connected to the Raspberry Pi Zero. This is an important project as it can be added to the home, including other sensors, in order to monitor it.

Motion sensor with Raspberry Pi Zero

For this project we need the Raspberry Pi Zero, a motion sensor PIR, and some cables. The hardware configuration for this project will actually be very simple. First, connect the VCC pin of the motion sensor to a 3.3V pin on the Raspberry Pi. Then, connect the GND pin of the sensor to one GND pin on the Pi. Finally, connect the OUT pin of the motion sensor to the GPIO17 pin on the Raspberry Pi. You can refer to the previous chapters to find out about pin mapping on the Raspberry Pi Zero board.

The following image shows the final circuit with the connections:

Software code

You can now either copy the code inside the folder called Project1, or just get the complete code from the folder for this...

Monitoring the climate from a remote dashboard


Today, most smart homes are connected to the Internet, and this allows the user to monitor their home. In this section, we are going to learn how to monitor your climate remotely. First, we are simply going to add a sensor to our Raspberry Pi Zero and monitor the measurements from a cloud dashboard. Let's see how it works.

The following image shows the final connections:

Exploring the sensor test

var sensorLib = require('node-dht-sensor'); 
var 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 () { 
            sensor.read(); 
        }, 2000); 
    } 
}; 
 
if (sensor.initialize()) { 
...

Summary


In this chapter, we learned how to build and integrate a modular security system based on Raspberry Pi Zero and Arduino boards. There are of course many ways to improve this project. For example, you can simply add more modules to the project, such as more motion sensors that trigger the same alarm. You can monitor the system, even if you are outside of the Wi-Fi network of your home.

In the next chapter, we are going to learn how to control your system from an Android application, and how to integrate a real system from your smartphone that's fantastic!

lock icon The rest of the chapter is locked
You have been reading a chapter from
Internet of Things Programming with JavaScript
Published in: Feb 2017 Publisher: Packt ISBN-13: 9781785888564
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}