Sei sulla pagina 1di 35

1/28/2018 A smart JPEG camera for home security – Tutorials For IoT-Makers

Tutorials For IoT-Makers


(Philosophy : Maker = Hacker )

A smart JPEG camera for home security

By Stephen Haesung LEE in raspberry-pi    

First Prize IoT Builders Contest 2016 (IBM Watson IoT)

Smart JPEG Camera With IBM Watson Visual Recognition V3

https://iotmakerblog.wordpress.com/2016/11/15/a-smart-jpeg-camera-for-home-security/ 1/35
1/28/2018 A smart JPEG camera for home security – Tutorials For IoT-Makers

A smart JPEG camera for home security

https://iotmakerblog.wordpress.com/2016/11/15/a-smart-jpeg-camera-for-home-security/ 2/35
1/28/2018 A smart JPEG camera for home security – Tutorials For IoT-Makers

https://iotmakerblog.wordpress.com/2016/11/15/a-smart-jpeg-camera-for-home-security/ 3/35
1/28/2018 A smart JPEG camera for home security – Tutorials For IoT-Makers

https://iotmakerblog.wordpress.com/2016/11/15/a-smart-jpeg-camera-for-home-security/ 4/35
1/28/2018 A smart JPEG camera for home security – Tutorials For IoT-Makers

Introduction

This instructable will cover the basic steps that you need to follow to get started
with open sources such as Watson nodes(Visual Recognition V3, Text To Speech)
for IBM Bluemix, Node-RED, OpenCV, MQTT v3.1. MQTT(Message Queueing
Telemetry Transport) is a Machine-To-Machine(M2M) or Internet of Things (IoT)
connectivity protocol that was designed to be extremely lightweight and useful
when low battery power consumption and low network bandwidth is at a premium.
It was invented in 1999 by Dr. Andy Stanford-Clark and Arlen Nipper and is now an
Oasis Standard.

https://iotmakerblog.wordpress.com/2016/11/15/a-smart-jpeg-camera-for-home-security/ 5/35
1/28/2018 A smart JPEG camera for home security – Tutorials For IoT-Makers

I’ve already published an instructable of the Smart Gas Valve For Safety. In
addition, I’m going to communicate between A Smart JPEG Camera and A Smart
Gas Valve for M2M Communication by MQTT. Speci cally, this instructable will
cover how to code the Node-RED on Raspberry Pi2 as a MQTT client by connecting
to your home wireless network and how to send sensor data. I will be using A Smart
Gas Valve for M2M communication by MQTT.

Step 1: Table of Contents

Step 0: Introduction
Step 1: Table of Contents
Step 2: Bill of Materials
Step 3: Setting up the Camera & PIR Sensor with Raspberry Pi
Step 4: Programming NodeRED on Raspberry Pi2
Step 5: Setting up MQTT v3.1 on Raspberry Pi2
Step 6: Checking your NodeRED codes with MQTT on Raspberry Pi2
Step 7: Programming Python JPEG Camera
Step 8: Adding IBM Watson, IBM NoSQL DB, Play-Audio, and Twilio
Step 9: Adding autostart les for every boot
Step 10: Testing M2M Communication
Step 11: (Optional) Using OpenCV
Step 12: Download list
Step 13: List of references

Step 2: Bill of Materials

Raspberry Pi2 X 1ea


Pi-1,2,3 are Possible.
Installation guide
Download image les
Installing VNC : You can connect Raspberry Pi2 with Macbook Pro or Windows.

Pi Camera Board X 1ea


https://iotmakerblog.wordpress.com/2016/11/15/a-smart-jpeg-camera-for-home-security/ 6/35
1/28/2018 A smart JPEG camera for home security – Tutorials For IoT-Makers

Description & Technical details

Wi dongle X 1ea
Description
If you have Pi-3, it’s unnecessary.
PIR motion sensor X 1ea
Overview
Android smartphone’s portable battery X 2ea
Nod-RED software X 1ea
Free open source
Use the version pre-installed in Raspbian Jessie image since November 2015
Installation guide

MQTT v3.1 software X 1ea


Free open source
Installation guide includes at Step 5
NodeRED’s IBM Watson Nodes for Bluemix
Text to speech node X 1ea
Visual Recognition X 1ea
Speaker X 1ea
Minion X 1ea
You can easily buy it from eBay.

Places to buy from?


Element14
Adafruit
DigiKey
Sparkfun
eBay
Amazon

Step 3: Setting up the Camera & PIR Sensor with


Raspberry Pi

https://iotmakerblog.wordpress.com/2016/11/15/a-smart-jpeg-camera-for-home-security/ 7/35
1/28/2018 A smart JPEG camera for home security – Tutorials For IoT-Makers

Assembly steps for Smart JPEG Camera

(1) Connect the Raspberry Pi2 with a PIR motion sensor as shown above in the
circuit diagram.

(2) Connect the PIR motion sensor with Raspberry Pi2.

https://iotmakerblog.wordpress.com/2016/11/15/a-smart-jpeg-camera-for-home-security/ 8/35
1/28/2018 A smart JPEG camera for home security – Tutorials For IoT-Makers

Raspberry Pi2 PIR motion Sensor


5V —————- VCC
GND ————- GND
GPIO 18 ——– OUT

(4) Assemble carefully the Pi camera with Raspberry Pi2.

(5) Connect a portable battery with Raspberry Pi2. (Use any portable battery to
connect with the same size connector cable on Raspberry Pi2. )

Assembly steps for Smart Gas Valve : here

Step 4: Programming NodeRED on Raspberry Pi2

https://iotmakerblog.wordpress.com/2016/11/15/a-smart-jpeg-camera-for-home-security/ 9/35
1/28/2018 A smart JPEG camera for home security – Tutorials For IoT-Makers

How to start Node-RED on web-browser.

(1) Write down command shown below to a terminal window. node-red-start

https://iotmakerblog.wordpress.com/2016/11/15/a-smart-jpeg-camera-for-home-security/ 10/35
1/28/2018 A smart JPEG camera for home security – Tutorials For IoT-Makers

(2) You can nd an IP address as below. ‘Once Node-RED has started, point a
browser at http://169.254.170.40:1880’ (It depends on your IP address)

(3) Open your web browser.

(4) Copy the IP address and paste on the web browser.

(5) It will display a visual editor of Node-RED on the web browser.

(6) You can start coding with visual editor on the web browser.

(7) Try dragging & dropping any node from the left-hand side to right-hand side.
It’s really easy to code. ( You can conveniently use the visual editor o ine as well as
online. ) Download the ‘SmartGasValve_NodeRED.txt’ le. (1) Click the number (1)
at the right-hand side corner shown in NodeRED on the web browser.

(2) Click the Import button on the drop down menu.

(3) Open the Clipboard shown in the above 1st picture.

(4) Lastly, paste the given JSON format text of


‘SmartJPGCameraNoCredits_NodeRED_ver0.1.txt‘ in Import nodes editor.

Step 5: Setting up MQTT v3.1 on Raspberry Pi2

https://iotmakerblog.wordpress.com/2016/11/15/a-smart-jpeg-camera-for-home-security/ 11/35
1/28/2018 A smart JPEG camera for home security – Tutorials For IoT-Makers

https://iotmakerblog.wordpress.com/2016/11/15/a-smart-jpeg-camera-for-home-security/ 12/35
1/28/2018 A smart JPEG camera for home security – Tutorials For IoT-Makers

Setting up MQTT v3.1 on Raspberry Pi2

This message broker(Mosquitto) is supported by MQTT v3.1 and it is easily installed


on the Raspberry Pi and somewhat less easy to con gure. Next, we step through
installing and con guring the Mosquitto broker. We are going to install & test the
MQTT “mosquitto” on the terminal window.

curl -O http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key

sudo apt-key add mosquitto-repo.gpg.key

rm mosquitto-repo.gpg.key

https://iotmakerblog.wordpress.com/2016/11/15/a-smart-jpeg-camera-for-home-security/ 13/35
1/28/2018 A smart JPEG camera for home security – Tutorials For IoT-Makers

cd /etc/apt/sources.list.d/

sudo curl -O http://repo.mosquitto.org/debian/mosquitto-jessie.list

sudo apt-get update

Next install the broker and command line clients:

mosquitto – the MQTT broker (or in other words, a server)


mosquitto-clients – command line clients, very useful in debugging
python-mosquitto – the Python language bindings

sudo apt-get install mosquitto mosquitto-clients python-mosquitto

As is the case with most packages from Debian, the broker is immediately started.
Since we have to con gure it rst, stop it.

sudo /etc/init.d/mosquitto stop

Now that the MQTT broker is installed on the Pi we will add some basic security.
Create a con g le:

cd /etc/mosquitto/conf.d/

sudo nano mosquitto.conf

https://iotmakerblog.wordpress.com/2016/11/15/a-smart-jpeg-camera-for-home-security/ 14/35
1/28/2018 A smart JPEG camera for home security – Tutorials For IoT-Makers

Let’s stop anonymous clients connecting to our broker by adding a few lines to
your con g le. To control client access to the broker we also need to de ne valid
client names and passwords. Add the lines:

allow_anonymous false

password_file /etc/mosquitto/conf.d/passwd

require_certificate false

Save and exit your editor (nano in this case).


From the current /conf.d directory, create an empty password le:

sudo touch passwd

We will use the mosquitto_passwd tool to create a password hash for user pi:

sudo mosquitto_passwd -c /etc/mosquitto/conf.d/passwd pi

You will be asked to enter your password twice. Enter the password you wish to use
for the user you de ned.

Testing Mosquitto on Raspberry Pi

Now that Mosquitto is installed we can perform a local test to see if it is working:
Open three terminal windows. In one, make sure the Mosquitto broker is running:

https://iotmakerblog.wordpress.com/2016/11/15/a-smart-jpeg-camera-for-home-security/ 15/35
1/28/2018 A smart JPEG camera for home security – Tutorials For IoT-Makers

mosquitto

In the next terminal, run the command line subscriber:

mosquitto_sub -v -t 'topic/test'

You should see the rst terminal window echo that a new client is connected.
In the next terminal, run the command line publisher:

mosquitto_pub -t 'topic/test' -m 'helloWorld'

You should see another message in the rst terminal window saying another client
is connected. You should also see this message in the subscriber terminal:

topic/test helloWorld

We have shown that Mosquitto is con gured correctly and we can both publish and
subscribe to a topic.
When you nish testing all, let’s set up below that.

sudo /etc/init.d/mosquitto start

Step 6: Checking your NodeRED codes with MQTT on


Raspberry Pi2
https://iotmakerblog.wordpress.com/2016/11/15/a-smart-jpeg-camera-for-home-security/ 16/35
1/28/2018 A smart JPEG camera for home security – Tutorials For IoT-Makers

https://iotmakerblog.wordpress.com/2016/11/15/a-smart-jpeg-camera-for-home-security/ 17/35
1/28/2018 A smart JPEG camera for home security – Tutorials For IoT-Makers

When you have already used the JSON format of the ‘SmartGasValve_NodeRED.txt’
on Node-RED, it’s automatically set up & coded each data. I have already set up the
each data in each node.

(1) Click each node.

(2) Check information inside each node has been pre lled.

(3) Please don’t change the set data.

(The above can be customized for more advanced users.)

Step 7: Programming Python JPEG Camera

https://iotmakerblog.wordpress.com/2016/11/15/a-smart-jpeg-camera-for-home-security/ 18/35
1/28/2018 A smart JPEG camera for home security – Tutorials For IoT-Makers

https://iotmakerblog.wordpress.com/2016/11/15/a-smart-jpeg-camera-for-home-security/ 19/35
1/28/2018 A smart JPEG camera for home security – Tutorials For IoT-Makers

Programming Python JPEG Camera


https://iotmakerblog.wordpress.com/2016/11/15/a-smart-jpeg-camera-for-home-security/ 20/35
1/28/2018 A smart JPEG camera for home security – Tutorials For IoT-Makers

First of all, you should test the camera module in the terminal window.

raspistill -o test.jpg

You should see the test.jpg in ‘/home/pi’

cd /home/pi

mkdir pythonPir

cd pythonPir

sudo nano pircameraNodeRED.py

Type the below (the enclosed le) Or Put ‘pircameraNodeRED.py’ le into


‘/home/pi/pythonPir’ folder.

import RPi.GPIO as GPIO


import time
import picamera
import datetime

timeFormat = 0

GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.IN) # For M2M Communication from Gas Valve signal
GPIO.setup(18, GPIO.IN)
https://iotmakerblog.wordpress.com/2016/11/15/a-smart-jpeg-camera-for-home-security/ 21/35
1/28/2018 A smart JPEG camera for home security – Tutorials For IoT-Makers

camera = picamera.PiCamera()

while True:
input17 = GPIO.input(17) #Pin number 17 activates
input18 = GPIO.input(18) #Pin number 18 activates
now = datetime.datetime.now()
timeFormat = now.strftime("%Y%m%d_%H%M_%S.%s") #To put date and time
in images

if input17 == True or input18 == True: #If PIR Sensor detects


something, the Picamera will take.
print('Motion_Detected_%s' %timeFormat)
camera.capture('image_%s.jpg' %timeFormat) #To take a
picture

time.sleep(1) #sleeping time 1 second

When you nish typing, you should press the keys ‘Control‘ + ‘x‘ and press ‘y‘ to
save this le.

Making an image le server

cd /home/pi

mkdir camserver

sudo nano requirements.txt

Type the below (the enclosed le) Or Put ‘requirements.txt’ le into


‘/home/pi/camserver’ folder.

https://iotmakerblog.wordpress.com/2016/11/15/a-smart-jpeg-camera-for-home-security/ 22/35
1/28/2018 A smart JPEG camera for home security – Tutorials For IoT-Makers

numpy==1.10.1
websocket-client==0.35.0
websocket-server==0.4
ibmiotf==0.2.3

pip install --user -r requirements.txt

Execute an image le server in /home/pi/ below.

cd /home/pi

python -m SimpleHTTPServer 7000

Step 8: Adding IBM Watson, IBM NoSQL DB, Play-Audio,


and Twilio

https://iotmakerblog.wordpress.com/2016/11/15/a-smart-jpeg-camera-for-home-security/ 23/35
1/28/2018 A smart JPEG camera for home security – Tutorials For IoT-Makers

Searching the Nodes

Node-RED comes with a core set of useful nodes, but there are a growing number of
additional nodes available for installing from both the Node-RED project as well as
the wider community. You can search for available nodes in the Node-RED library
or on the npm repository.

For example, we are going to search Twilio at the npm web. Click here.
Then, we are going to install Twilio on Raspberry pi.

Installing npm packaged node

To add additional nodes you must rst install the npm tool, as it is not included in
the default installation. The following commands install npm and then upgrade it
to the latest 2.x version.

sudo apt-get update

sudo apt-get install npm

sudo npm install -g npm@2.x

https://iotmakerblog.wordpress.com/2016/11/15/a-smart-jpeg-camera-for-home-security/ 24/35
1/28/2018 A smart JPEG camera for home security – Tutorials For IoT-Makers

hash -r

cd /home/pi/.node-red

For example, ‘npm install node-red-{example node name}’


Copy the ‘npm install node-red-node-twilio’ from the npm web. Paste it on a
terminal window.
Ex: node-red-node-watson, node-red-contrib-play-audio, node-red-
dashboard, and node-red-node-pidcontrol.

npm install node-red-node-twilio

You will need to restart Node-RED for it to pick-up the new nodes.

node-red-stop

node-red-start

Close your web browser and reopen the web browser.

Step 9: Adding autostart les for every boot.

https://iotmakerblog.wordpress.com/2016/11/15/a-smart-jpeg-camera-for-home-security/ 25/35
1/28/2018 A smart JPEG camera for home security – Tutorials For IoT-Makers

How to make autostart les at every boot.

Mosquitto

cd /etc/xdg/autostart/

sudo nano flyMosquitto.desktop

Type the below (this will enclose the le) Or Put ‘ yMosquitto.desktop’ le into
autostart folder.

[Desktop Entry]
Type=Application
Name=flyMosquitto
Comment=Fly my mosquitto
Exec=cd /etc/mosquitto/conf.d/
Exec=mosquitto

Node-RED

sudo systemctl enable nodered.service


https://iotmakerblog.wordpress.com/2016/11/15/a-smart-jpeg-camera-for-home-security/ 26/35
1/28/2018 A smart JPEG camera for home security – Tutorials For IoT-Makers

Python JPEG Camera

cd /etc/xdg/autostart/

sudo nano pircameraNodeRED.desktop

Type the description below or put the ‘pircameraNodeRED.desktop’ le into


/etc/xdg/autostart/ folder.

[Desktop Entry]
Type=Application
Name=pircameraNodeRED.py
Comment=Start my security camera
NoDisplay=false
Exec=python /home/pi/pythonPir/pircameraNodeRED.py
NotShowIn=GNOME;KDE;XFCE;
Name[en_US]=pircamera.py

Image le Server

cd /etc/xdg/autostart/

sudo nano imageFileServer.desktop

https://iotmakerblog.wordpress.com/2016/11/15/a-smart-jpeg-camera-for-home-security/ 27/35
1/28/2018 A smart JPEG camera for home security – Tutorials For IoT-Makers

Type the description below or put the ‘imageFileServer.desktop’ le into


/etc/xdg/autostart/ folder.

[Desktop Entry]
Type=Application
Name=imageFileServer
Comment=Start an image file server
NoDisplay=false
Exec=cd /home/pi
Exec=python -m SimpleHTTPServer 7000

Step 10: Testing M2M Communication.

https://iotmakerblog.wordpress.com/2016/11/15/a-smart-jpeg-camera-for-home-security/ 28/35
1/28/2018 A smart JPEG camera for home security – Tutorials For IoT-Makers

Importing the enclosed les in each NodeRED.

(1) Using a smart JPEG camera

Import the ‘M2M_SmartJPGCamera.txt‘ into the NodeRED of the smart JPEG


camera.

(2) Using a smart gas valve

Import the ‘M2M_SmartGasValve.txt‘ into the NodeRED of the smart gas valve.

(3) Check an IP address of the smart gas valve in the Raspberry Pi2.

Type ‘ifcon g’ on a terminal window as shown below.

ifconfig

When you see the IP address, copy the IP address in a terminal window.

(4) Put the IP address into the MQTT node in other Raspberry Pi2.

1. Click the MQTT node.


2. Put the IP address into Server.

https://iotmakerblog.wordpress.com/2016/11/15/a-smart-jpeg-camera-for-home-security/ 29/35
1/28/2018 A smart JPEG camera for home security – Tutorials For IoT-Makers

Step 11: (Optional) Using OpenCV

Installing & Using OpenCV on Raspberry Pi2

We have already used the IBM Watson Visual Recognition. Watson Visual
Recognition is very excellent whereas we can’t use it without connecting wi .

https://iotmakerblog.wordpress.com/2016/11/15/a-smart-jpeg-camera-for-home-security/ 30/35
1/28/2018 A smart JPEG camera for home security – Tutorials For IoT-Makers

OpenCV is possible to use without internet connection but It’s not very easy for a
beginner to install & code into OpenCV. So, I’m going to install the OpenCV.

Download ‘opencv-3.1.0.zip from opecv.org

Install dependencies

sudo apt-get update


sudo apt-get install build-essential
sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev
libavformat-dev libswscale-dev python-dev python-numpy libjpeg-dev libpng-
dev libtiff-dev libjasper-dev

(Optional) Install OpenCV 2

sudo apt-get install python-opencv

Install OpenCV 3

unzip ~/Downloads/opencv-3.1.0.zip
cd opencv-3.1.0/
mkdir build
cd build/
cmake -DCMAKE_BUILD_TYPE=Debug -DBUILD_TESTS=NO -DBUILD_PERF_TESTS=NO ..
make -j3
sudo make install
sudo ldconfig

Check which version of OpenCV you have in Python

https://iotmakerblog.wordpress.com/2016/11/15/a-smart-jpeg-camera-for-home-security/ 31/35
1/28/2018 A smart JPEG camera for home security – Tutorials For IoT-Makers

python
import cv2
cv2.__version__

Run the simple face detect sample, and look at its code to see how it works:
Before, you should connect an USB-cam with Raspberry Pi2

cd /home/pi
cd opencv-3.1.0
python ./facedetect.py

Step 12: Download list

SmartJPGCameraNoCredits_NodeRED_ver0.1.txt

M2M_SmartGasValve.txt

M2M_SmartJPGCamera.txt

pircameraNodeRED.py

requirements.txt

yMosquitto.desktop

https://iotmakerblog.wordpress.com/2016/11/15/a-smart-jpeg-camera-for-home-security/ 32/35
1/28/2018 A smart JPEG camera for home security – Tutorials For IoT-Makers

imageFileServer.desktop

pircameraNodeRED.desktop

tightvnc.desktop

Step 13: List of references

A developer’s guide to the Internet of Things (IoT) by IBM


MQTT.org: Version 3.1.1 becomes an OASIS Standard
MQTT.org: Documentation
Eclipse Paho
What is Really Small Message Broker(RSMB)? (IBM)
http://stanford-clark.com/
The House That Twitters – Andy Stanford-Clark

Node-RED.org: Documentation
Node-RED: Running on Raspberry Pi
Node-RED: Writing Functions
Node-RED: Node-RED Library
Node-RED: node-red-node-watson
Node-RED: Build a face detection app using Watson Visual Recognition v3 service
Node-RED: Build a face detection app usig AlchemyAPI service on Bluemix

Recipes: Internet of Things (IoT) by IBM


Twilio: Documentation

Raspberrpi.org: Installing oprerating system images


Raspberrypi.org: downloads
IBM developerWorks Recipes: RPi Cam Image Analysis using Visual Recognition
method
IBM Watson: Text to Speech service

https://iotmakerblog.wordpress.com/2016/11/15/a-smart-jpeg-camera-for-home-security/ 33/35
1/28/2018 A smart JPEG camera for home security – Tutorials For IoT-Makers

IBM Watson: Visual Recognition service

OpenCV: Documentation

Advertisements

Report this ad Report this ad

Share this:

 Twitter  Facebook 17  Google

 Like
Be the first to like this.

Author: iotmaker
I am interested in IoT, robot, gures & leadership. Also, I have spent almost every day of the
past 15 years making robots or electronic inventions or computer programs.
View all posts by iotmaker

iotmaker / November 15, 2016 / AI, Computer Vision, Electronics & Sensors, IoT, Robotics, Security,
Uncategorized

One thought on “A smart JPEG camera for home security”


https://iotmakerblog.wordpress.com/2016/11/15/a-smart-jpeg-camera-for-home-security/ 34/35
1/28/2018 A smart JPEG camera for home security – Tutorials For IoT-Makers

Balrockus
July 6, 2017 at 10:25 am

Wow. I just looked at Node red or my Pi a day ago and I think its great. You tutorial
has taught me masses in the initial read through. Over the next few days I will go
through it bit by bit. I can’t thank you enough.

 Liked by 1 person

Tutorials For IoT-Makers /

https://iotmakerblog.wordpress.com/2016/11/15/a-smart-jpeg-camera-for-home-security/ 35/35

Potrebbero piacerti anche