You are on page 1of 22

IOT: -

Definition:-  The term Internet of Things generally refers to scenarios where network
connectivity and computing capability extends to objects, sensors and everyday items not
normally considered computers, allowing these devices to generate, exchange and consume
data with minimal human intervention. The concept of combining computers, sensors, and
networks to monitor and control devices has existed for decades. There is, however, no
single, universal definition.
Components used:-
Raspberry Pi:- We used Raspberry Pi 3 V1.2. The Raspberry Pi is a single-board computer
developed by the Raspberry Pi Foundation, a UK-based charity organization. Originally
designed to provide young people with an affordable computing option to learn how to
program, it has developed a massive following in the maker and DIY communities because of
its compact size, full Linux environment, and general-purpose input–output (GPIO) pins.

With all the features and capabilities that are packed into this small board, there’s no shortage
of projects and use cases for the Raspberry Pi.

Some example projects include the following:

Line-following robot
Home weather station
Retro gaming machine
Real-time object detection camera
Minecraft server
Button-controlled music box
Media center
Remote experiments on the International Space Station
If you can think of a project that would benefit from having a credit card–sized computer
attached to it, then someone has probably used a Raspberry Pi to do it. The Raspberry Pi is a
fantastic way to bring your Python project ideas to life.
Hardware Structure: -

Pin no 1 & 17 can be used to give power in sensors which required 3.3 V

Pin no 2 & 4 can be used to give power in sensors which required 5 V

Pin GPIO (General Purpose Input/Ouput) can be used for connecting device Input/ouput data
from sensor)
GPIO pin has only two values 0 or 1
1 means it makes the ouput high will give 5 V output

to connect Analog device with GPIO Pins we use Analog to Digital Converter (Modulator)
Pin GND are used to give ground to devices

Reserved Pins are used to connect External Memory hence not used to connect sensors

I2C pin can be used for Some sensors that work on I2C Protocol (Inter Integrated Circuit )

SPI Pins can be used for Serial Pheripheral Interface can be used for data communications
between sensor and microprocessors

UART (Universal Asynchronous Receiver and Transmitter) to connect Bluetooth/Wifi/GSM


modules.

Operating Systems:-

Arduino: - We have used Arduino UNO R3. Arduino is Microcontroller, that runs a script in
loop.

Arduino Hardware Structure: -


Raspberry Pi vs Arduino: -
People often wonder what the difference is between a Raspberry Pi and an Arduino. The
Arduino is another device that is widely used in physical computing. While there is some
overlap in the capabilities of the Arduino and the Raspberry Pi, there are some distinct
differences.

The Arduino platform provides a hardware and software interface for programming
microcontrollers. A microcontroller is an integrated circuit that allows you to read input from
and send output to electronic components. Arduino boards generally have limited memory, so
they’re often used to repeatedly run a single program that interacts with electronics.

The Raspberry Pi is a general-purpose, Linux-based computer. It has a full operating system


with a GUI interface that is capable of running many different programs at the same time.

The Raspberry Pi comes with a variety of software preinstalled, including a web browser, an
office suite, a terminal, and even Minecraft. The Raspberry Pi also has built-in Wi-Fi and
Bluetooth to connect to the Internet and external peripherals.

NodeMCU: -
We have used NodeMCU LoLin V3. NodeMCU is a low-cost open source IoT platform.  The
name "NodeMCU" combines "node" and "MCU" (micro-controller unit). The term
"NodeMCU" strictly speaking refers to the firmware rather than the associated development
kits.
NodeMCU is an open-source platform based on ESP8266 which can connect objects and let
data transfer using the Wi-Fi protocol. In addition, by providing some of the most important
features of microcontrollers such as GPIO, PWM, ADC, and etc, it can solve many of the
project's needs alone.
PROJECT: -
Raspberry Pi Surveillance Robotic Car: -
Motion (Surveillance Software) is free, open source motion detector CCTV software,
developed for Linux. It detects the motion and start recording video of it. With ‘Motion’
installed in your Raspberry Pi, you can magically turn your Raspberry Pi into a Security
Camera. It is used for getting live video feed, making timelapse videos and taking snapshots
at regular interval. It records and saves the Video whenever it detects Motion or any
disturbance in the view area. Live Video feed can be watched on the web browser by entering
the IP address of Pi along with the port.

We have created a detailed Tutorial on Using Motion with Raspberry Pi and USB Camera,
here we are briefly explaining its installing on Raspberry pi for our Robot to send Live video
streaming to webpage.

Here you need to run only few commands to start getting you first video feed over the
network. Before that, properly check that your Raspberry Pi is connected to the internet,
either using LAN or Wi-Fi and then follow below steps:

Step 1: First run the below command to update the Raspbian OS on Raspberry Pi:

sudo apt-get update


Step 2: Then install ‘Motion’ Library by using below command:

sudo apt-get install motion

Step 3: Now set Motion daemon to yes by editing the file: /etc/default/motion so that it will
be always running. Edit this file using ‘nano’ editor with ‘sudo’ like given below:

sudo nano /etc/default/motion

Then save the file by pressing ‘CTRL+X’, then ‘Y’ and the Enter.

Step 4: Now we need to set the permission for the Target Directory (/var/lib/motion/), in
which Motion saves all the Video recordings and picture files. We need to set ‘Motion’ as
owner of this directory by issuing below command:

sudo chown motion:motion /var/lib/motion/

This permission is necessary otherwise you will get error, when you check Motion service
Status.

You can check service status by using this command: sudo service motion status

Step 5: Now we are almost done, only we need to change one config option in Motion
configuration file (/etc/motion/motion.conf) which is stream_localhost off. We have to turn
off this local host streaming, otherwise we will not be able to access the Video feed on our
network and it will be only accessible from the Raspberry Pi itself. To doing so, edit the
Motion Configuration file with ‘nano’ editor and turn it off, like shown below:

sudo nano /etc/motion/motion.conf

D
Now we are done and ready to get our live feed from the USB web camera connected to Pi.
Just start (or restart) the Motion service using below command and open your Raspberry Pi’s
IP, with port 8081, in your browser (like 192.168.43.199:8081). In this project we have
embed this IP in our HTML code in img src tag.

sudo /etc/init.d/motion restart

And you will see the live feed from your web camera. Here we have used a low cost USB
web camera which worked smoothly with our Raspberry Pi, but you can further use a good
quality camera for better resolution. As it will show in browser, you can use any device, to
watch the feed, which supports web browser like Mobile, tablet etc.

Try rebooting the Raspberry Pi as a troubleshooting step when necessary:


sudo reboot

This is all about using Motion for our Surveillance Robot, apart from that, it has several
configuration options which we have already discussed in our previous tutorial.

Note: If you are Raspberry Pi model below the version 3, then you may need a Wi-Fi dongle
to wirelessly connect raspberry Pi to router.

Flask Setup in Raspberry Pi for Controlling Robot through Webpage:


Here, we have created a web server using Flask, which provides a way to send the commands
from webpage to Raspberry Pi to control the Robot over the network. Flask allows us to run
our python scripts through a webpage and we can send & receive data from Raspberry Pi to
web browser and vice versa. Flask is a microframework for Python. This tool is Unicode
based having built-in development server and debugger, integrated unit testing support,
support for secure cookies and its easy to use, these things make it useful for the hobbyist.

Install a flask support package into the Raspberry Pi by using given command:

$ pip install Flask

Then we can use the Flask by just importing it in our program, like we have imported
following packages of flask for this project:

from flask import Flask


from flask import Flask, render_template, request

You can learn more about the programming using Flask here, also check our previous
projects where we have used Flask to send the message from Webpage to Raspberry Pi and
send weight value to Raspberry Pi in Smart Container.
HTML code for webpage:
We have created a web page using HTML language for displaying control links (Left, Right,
Forward, backward) to move the Robot from web browser. We have used jQuery script to
call the functions in our Python Program. There are five functions in Python Code to move
the Robot Left, Right, Forward, Backward and to stop it. Complete Python Code has been
given at the end. These functions will be executed by clicking on the Control Links on
webpage and motors will move depending on the link being clicked. Here we have written
the code in such way that Robot will move in certain direction while clicking and holding the
link, and as soon as we release the mouse button Robot will stop. Below is the HTML code
for webpage including the jQuery:

/home/pi/templates/robot.html

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
</head>
<body>

<img src="http://192.168.43.199:8081" /> <!--Enter the IP Address of your Raspberry Pi--


>
<div style="float:right">

</div>
<div style=" height:400px; width:300px; float:right;">
<center>
<h1><span style="color:#5C5C5C;">Circuit</span><span style="color:#139442">
Digest</span></h1>
<h2>Surveillance Robot</h2><br><br>
<a href="#" id="up" style="font-size:30px;text-decoration:none;">
&#x1F881;&#x1F881;<br>Forward</a><br><br></center>
<a href="#" id="left" style="font-size:30px;text-decoration:none;">
&#x1F880;&#x1F880;Left</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#" id="right" style="font-size:30px; text-decoration:none;"> Right
&#x1F882;&#x1F882;</a><br><br>
<center><a href="#" id="down" style="font-size:30px;text-decoration:none;">
Backward<br> &#x1F883;&#x1F883;</a></center>
</div>

<script>
$( document ).ready(function(){
$("#down").on("mousedown", function() {
$.get('/down_side');
}).on('mouseup', function() {
$.get('/stop');
});
$("#up").on("mousedown", function() {
$.get('/up_side');
}).on('mouseup', function() {
$.get('/stop');
});
$("#left").on("mousedown", function() {
$.get('/left_side');
}).on('mouseup', function() {
$.get('/stop');
});
$("#right").on("mousedown", function() {
$.get('/right_side');
}).on('mouseup', function() {
$.get('/stop');
});
});
</script>
</body>
</html>

Here you can see the we have embed the IP address, on which the Video is streaming, into
the webpage by using img src tag. Change the IP address according to your Raspberry Pi but
keep the port same.

<img src="http://192.168.43.199:8081" /> <!--Enter the IP Address of your Raspberry Pi--


>

User needs to copy-paste the above given HTML code in some text editor (notepad) and save
the file with .HTML extension (robot.html). Then put this HTML file in the /templates folder
with respect to your python script location. Means you need to create a folder named
templates, where you have put your Python Code file for this Raspberry Surveillance Robot,
then put robot.html file in this templates folder. This step is important, otherwise our project
won’t work. You can directly open the robot.html file by double clicking on it to see how
your control links will look. Further check the whole process in Demonstration Video at the
end. After we have done with the programming and all, we can just run the Python code in
Raspberry Pi and open the IP_address_of_your_Pi:5010 in web Browser (like
http://192.168.43.199:5010)

You can check the IP address of your Raspberry Pi by using ifconfig command:

ifconfig

Circuit Diagram and Setup:


After testing the Live Video feed and HTML code, we need to build a robot by using
handmade or ready-made robot chassis, wheels, and nut-bolts. Then place Power bank over it
for powering the Raspberry pi and then place the Raspberry Pi and web camera over the
power bank and fix the setup using Cello tape or rubber strips, connect the USB camera with
Raspberry Pi.
In this IoT project, we don’t need to do many connections, we only need to connect some
wires for Motor Driver IC L293D and DC Motors. Connections are shown in circuit diagram
below. Here we have used a General Purpose PCB for mounting L293D IC to reduce the
space but you can also use small Breadboard for connecting DC motors with L293D.

Code

from flask import Flask


from flask import render_template, request
import RPi.GPIO as GPIO
import time

app = Flask(__name__)

m11=18
m12=23
m21=24
m22=25

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(m11, GPIO.OUT)
GPIO.setup(m12, GPIO.OUT)
GPIO.setup(m21, GPIO.OUT)
GPIO.setup(m22, GPIO.OUT)
GPIO.output(m11 , 0)
GPIO.output(m12 , 0)
GPIO.output(m21, 0)
GPIO.output(m22, 0)
print "DOne"

a=1
@app.route("/")
def index():
return render_template('robot.html')

@app.route('/left_side')
def left_side():
data1="LEFT"
GPIO.output(m11 , 0)
GPIO.output(m12 , 0)
GPIO.output(m21 , 1)
GPIO.output(m22 , 0)
return 'true'
@app.route('/right_side')
def right_side():
data1="RIGHT"
GPIO.output(m11 , 1)
GPIO.output(m12 , 0)
GPIO.output(m21 , 0)
GPIO.output(m22 , 0)
return 'true'

@app.route('/up_side')
def up_side():
data1="FORWARD"
GPIO.output(m11 , 1)
GPIO.output(m12 , 0)
GPIO.output(m21 , 1)
GPIO.output(m22 , 0)
return 'true'

@app.route('/down_side')
def down_side():
data1="BACK"
GPIO.output(m11 , 0)
GPIO.output(m12 , 1)
GPIO.output(m21 , 0)
GPIO.output(m22 , 1)
return 'true'

@app.route('/stop')
def stop():
data1="STOP"
GPIO.output(m11 , 0)
GPIO.output(m12 , 0)
GPIO.output(m21 , 0)
GPIO.output(m22 , 0)
return 'true'

if __name__ == "__main__":
print "Start"
app.run(host='0.0.0.0',port=5010)

How to operate:
Operation and Working this Surveillance Robot is very easy. Create a python file (.py
extension) and copy the above code into it then save it on your Raspberry Pi. Then put the
HTML file in the templates folder as explained above. Don’t forget to change the IP address
in HTML file.

Then Run the Python code by entering below command:

python name_of_file.py

Then open your Raspberry Pi IP address with port 5010 like http://192.168.43.199:5010
(again replace IP address with your address). Now you will see the web page having four
robot control links and live streaming video. User can control the robot by clicking and
holding the links. If user will click and hold the links then robot will move according to
clicked link and when will user release the link then robot automatically stop.
Obstacle Avoiding Robotic Car: -
To start raspberry program automatically apply this command on terminal
$ sudo nano /home/pi/.config/lxsession/LXDE-pi/autostart
then add this statement in file
@python /home/pi/Obstacle.py
Motor Stop
Motor a = 0
Motor b = 0

Motor Forward Direction


Motor a = 1
Motor b = 0

Motor Reverse Direction


Motor a = 0
Motor b = 1

Example:
m1a = 3
m1b = 4

m2a = 5
m2b = 6

GPIO.output(m1a, LOW)
GPIO.output(m1b, HIGH)
Wiring Diagram: -
Now the code will look like this

import RPi.GPIO as GPIO


import time
GPIO.setmode(GPIO.BCM)
trig = 17
echo = 27

led = 26

m1a = 23
m1b = 24
m2a =25
m2b = 8
GPIO.setup(trig, GPIO.OUT)
GPIO.setup(echo, GPIO.IN)

GPIO.setup(m1a, GPIO.OUT)
GPIO.setup(m1b, GPIO.OUT)
GPIO.setup(m2a, GPIO.OUT)
GPIO.setup(m2b, GPIO.OUT)

GPIO.output(led, 1)

time.sleep(5)

def stop():
print "stop"
GPIO.output(m11, 0)
GPIO.output(m12, 0)
GPIO.output(m21, 0)
GPIO.output(m22, 0)

def forward():
GPIO.output(m11, 1)
GPIO.output(m12, 0)
GPIO.output(m21, 1)
GPIO.output(m22, 0)
print "Forward"

def back():
GPIO.output(m11, 0)
GPIO.output(m12, 1)
GPIO.output(m21, 0)
GPIO.output(m22, 1)
print "back"

def left():
GPIO.output(m11, 0)
GPIO.output(m12, 0)
GPIO.output(m21, 1)
GPIO.output(m22, 0)
print "left"

def right():
GPIO.output(m11, 1)
GPIO.output(m12, 0)
GPIO.output(m21, 0)
GPIO.output(m22, 0)
print "right"

stop()

count=0
while True:
i=0
avgDistance=0
for i in range(5):
GPIO.output(TRIG, False) #Set TRIG as LOW
time.sleep(0.1) #Delay

GPIO.output(TRIG, True) #Set TRIG as HIGH


time.sleep(0.00001) #Delay of 0.00001 seconds
GPIO.output(TRIG, False) #Set TRIG as LOW
while GPIO.input(ECHO)==0: #Check whether the ECHO is LOW
GPIO.output(led, False)
pulse_start = time.time()

while GPIO.input(ECHO)==1: #Check whether the ECHO is HIGH


GPIO.output(led, False)
pulse_end = time.time()
pulse_duration = pulse_end - pulse_start #time to get back the pulse to sensor

distance = pulse_duration * 17150 #Multiply pulse duration by 17150 (34300/2) to get


distance
distance = round(distance,2) #Round to two decimal points
avgDistance=avgDistance+distance

avgDistance=avgDistance/5
print avgDistance
flag=0
if avgDistance < 15: #Check whether the distance is within 15 cm range
count=count+1
stop()
time.sleep(1)
back()
time.sleep(1.5)
if (count%3 ==1) & (flag==0):
right()
flag=1
else:
left()
flag=0
time.sleep(1.5)
stop()
time.sleep(1)
else:
forward()
flag=0

Advantages of IoT

Internet of things facilitates the several advantages in day-to-day life in the business sector.
Some of its benefits are given below:

o Efficient resource utilization: If we know the functionality and the way that how
each device work we definitely increase the efficient resource utilization as well as
monitor natural resources.
o Minimize human effort: As the devices of IoT interact and communicate with each
other and do lot of task for us, then they minimize the human effort.
o Save time: As it reduces the human effort then it definitely saves out time. Time is
the primary factor which can save through IoT platform.
o Enhance Data Collection:
o Improve security: Now, if we have a system that all these things are interconnected
then we can make the system more secure and efficient.

Disadvantages of IoT

As the Internet of things facilitates a set of benefits, it also creates a significant set of
challenges. Some of the IoT challenges are given belowDifference between JDK, JRE, and
JVM

Next
Stay

o Security: As the IoT systems are interconnected and communicate over networks.
The system offers little control despite any security measures, and it can be lead the
various kinds of network attacks.
o Privacy: Even without the active participation on the user, the IoT system provides
substantial personal data in maximum detail.
o Complexity: The designing, developing, and maintaining and enabling the large
technology to IoT system is quite complicated.

You might also like