IoT Final Lab
IoT Final Lab
IoT Final Lab
Python has grown into one of the most widely used general-purpose, high-level programming languages,
and is supported by one of the largest open source developer communities. Python is an open source
programming language that includes a lot of supporting libraries. These libraries are the best feature of
Python, making it one of the most extensible platforms. Python is a dynamic programming language, and
it uses an interpreter to execute code at runtime rather than using a compiler to compile and create
executable byte codes.
Compared to other popular object-oriented languages such as C++ and Java, Python has the following
major benefits for programmers:
Python comes in two versions: Python v2.x and Python v3.x. (Here, x represents an
appropriate version number.) While Python v2.x is a legacy branch and has better library support, Python
v3.x is the future of Python. Most Linux distributions and Mac OS X operating systems are equipped
with Python, and they have v2.x as their preferred and default version of Python. We will be using
Python v2.7 as the default version of Python for the rest of the book due to the following reasons:
Even though the code samples, exercises, and projects provided in this book should work
in any variant of Python 2.7.x, it’s better to have the latest version.
Installing Python
Linux
The majority of Linux distributions come with Python preinstalled. To check the latest
version of the installed Python, use the following command at the terminal window:
$ python –V
Make sure that you are using an uppercase V as the option for the previous command.
Once you execute it on the terminal, it will print the complete version number of your
current Python installation. If the version is 2.7.x, you are good to go and your Linux is
updated with the latest version of Python that is required for this book. However, if you
have any version that is less than or equal to 2.6.x, you will need to first upgrade Python to the latest
version. This process will require root privileges, as Python will be installed as a system component that
will replace the previous versions.
Ubuntu
If you are using Ubuntu 11.10 or later versions, you should already have Python v2.7.x
installed on your machine. You can still upgrade Python to the latest revision of v2.7.x
using the following command:
If you are running an older version of Ubuntu (such as 10.04 or older), you should have
2.6 as the default version. In this case, you will need to run the following set of commands
to install version 2.7:
The first command will add an external Ubuntu repository, which will allow you to install
any version of Python. The next command will update and index the list of available
packages. The last command will install the latest version of Python 2.7.
Fedora and Red Hat Linux also ships with Python as an in-built package. If you want to
upgrade the version of Python to the latest one, run the following command at the
terminal:
$ sudo yum upgrade python
Windows
http://www.python.org/getit.
You need to be careful about the version of Python that you are downloading. From the
system properties of your Windows OS, check whether the operating system is of 32 bit or
64 bit. At the time this book was being written, the latest version of Python was 2.7.6. So,
download the latest available version of Python, but make sure that it is 2.7.x and not 3.x.
For many third-party Python libraries, the installation binary files for Windows are
compiled for the 32-bit version. Due to this reason, we will recommend that you install the
32-bit version of Python for your Windows OS.
If you are really familiar with Python and know your way around installing libraries, you
can install the 64-bit version of Python. Select and run the downloaded file to install
Python. Although you can install it to any custom location, it is advisable to use the
default installation location as the upcoming configuration steps use the default location.
Once the installation is complete, you can find the Python command-line tool and IDLE
(Python GUI) from the Start menu.
Although you can always open these tools from the Start menu for basic scripting, we
will modify the Windows system parameters to make Python accessible through the
Windows command prompt. To accomplish this, we will have to set up PATH in
environment variables for the location of the Python installation directory.
Let’s open System Properties by right-clicking on My Computer and then selecting Properties.
Otherwise, you can also navigate to Start | Control Panel | System and Security |
System.
You will be able to see a window similar to the one that is displayed in the following
screenshot. The System window shows you the basic information about your computer,
including the type of Windows operating system that you are using (such as the 32-bit or
the 64-bit version)
In the System window, click on Advanced system settings in the left navigation bar to
open a window called System Properties. Click on the Environment Variables… button
in the System Properties window, which is located at the bottom of the window. This will
open an interface similar to the one shown in the following screenshot. In Environment
Variables, you need to update the PATH system variable to add Python to the default
operating system’s path.
Click on the PATH option as displayed in the following screenshot, which will pop up an
Edit System Variable window. Add C:\Python27 or the full path of your custom Python
installation directory at the end of your existing PATH variable. It is required to put a
semicolon (;) before the Python installation path. If you already see Python’s location in
the Path variable, your system is set up for Python and you don’t need to perform any
changes.
The main benefit of adding Python to the environment variables is to enable access to the
Python interpreter from the command prompt. In case you don’t know, the Windows
command prompt can be accessed by navigating to Start | Programs | Accessories |
Command Prompt.
/************WAP to display light on LED using arduino**************/
1 import pyfirmata
2import time
3
4board = pyfirmata.Arduino('/dev/ttyACM0')
5
6it = pyfirmata.util.Iterator(board)
7it.start()
8
9digital_input = board.get_pin('d:10:i')
10led = board.get_pin('d:13:o')
11
12while True:
13 sw = digital_input.read()
14 if sw is True:
15 led.write(1)
16 else:
17 led.write(0)
18 time.sleep(0.1)
/*********WAP to turn on the LED, based on the state of the push button using arduino***/
1 import pyfirmata
2import time
3
4board = pyfirmata.Arduino('/dev/ttyACM0')
5
6it = pyfirmata.util.Iterator(board)
7it.start()
8
9board.digital[10].mode = pyfirmata.INPUT
10
11while True:
12 sw = board.digital[10].read()
13 if sw is True:
14 board.digital[13].write(1)
15 else:
16 board.digital[13].write(0)
17 time.sleep(0.1)
/****************WAP to read Analog Inputs*******************************/
1 import pyfirmata
2import time
3
4board = pyfirmata.Arduino('/dev/ttyACM0')
5it = pyfirmata.util.Iterator(board)
6it.start()
7
8analog_input = board.get_pin('a:0:i')
9
10while True:
11 analog_value = analog_input.read()
12 print(analog_value)
13 time.sleep(0.1)
/***********WAP to change the frequency of the blinking LED***************/
1 import pyfirmata
2import time
3
4board = pyfirmata.Arduino('/dev/ttyACM0')
5it = pyfirmata.util.Iterator(board)
6it.start()
7
8analog_input = board.get_pin('a:0:i')
9led = board.get_pin('d:13:o')
10
11while True:
12 analog_value = analog_input.read()
13 if analog_value is not None:
14 delay = analog_value + 0.01
15 led.write(1)
16 time.sleep(delay)
17 led.write(0)
18 time.sleep(delay)
19 else:
20 time.sleep(0.1)
/**********WAP for adding Sensor to Trigger a Notification using arduino******/
import pyfirmata
2import time
3import tkinter
4from tkinter import messagebox
5
6root = tkinter.Tk()
7root.withdraw()
8
9board = pyfirmata.Arduino('/dev/ttyACM0')
10
11it = pyfirmata.util.Iterator(board)
12it.start()
13
14digital_input = board.get_pin('d:10:i')
15led = board.get_pin('d:13:o')
16
17while True:
18 sw = digital_input.read()
19 if sw is True:
20 led.write(1)
21 messagebox.showinfo("Notification", "Button was pressed")
22 root.update()
23 led.write(0)
24 time.sleep(0.1)
/********WAP to use the push button to send an email when pressed************/
1 import pyfirmata
2import time
3import smtplib
4import ssl
5
6def send_email():
7 port = 465 # For SSL
8 smtp_server = "smtp.gmail.com"
9 sender_email = "<your email address>"
10 receiver_email = "<destination email address>"
11 password = "<password>"
12 message = """Subject: Arduino Notification\n The switch was turned on."""
13
14 context = ssl.create_default_context()
15 with smtplib.SMTP_SSL(smtp_server, port, context=context) as server:
16 print("Sending email")
17 server.login(sender_email, password)
18 server.sendmail(sender_email, receiver_email, message)
19
20board = pyfirmata.Arduino('/dev/ttyACM0')
21
22it = pyfirmata.util.Iterator(board)
23it.start()
24
25digital_input = board.get_pin('d:10:i')
26
27while True:
28 sw = digital_input.read()
29 if sw is True:
30 send_email()
31 time.sleep(0.1)
sense = SenseHat()
r = 255
g = 255
b = 255
sense.clear((r, g, b))
/*******WAP to detect temperature with the Sense HAT*****************/
The Sense HAT has two sensors capable of reading the ambient temperature: the humidity sensor and
the pressure sensor. get_temperature_from_humidity reads the temperature from the humidity sensor
(get_temperature is a short version of the same command). get_temperature_from_pressure reads the
temperature from the pressure sensor. In a Python file, enter the following code:
from sense_hat
import SenseHat sense = SenseHat()
sense.clear()
temp = sense.get_temperature()
print(temp)
/*******WAP to detect humidity with the Sense HAT*****************/
from sense_hat
import SenseHat
sense = SenseHat()
sense.clear()
humidity = sense.get_humidity()
print(humidity)
/*******WAP to detect pitch, roll, and yaw with the Sense HAT*****************/
The Sense HAT has orientation sensors which detect pitch, roll, and yaw. Do the following to access
these data.
In a Python file, enter this code:
from sense_hat
import SenseHat sense = SenseHat()
sense.clear()
o = sense.get_orientation()
pitch = o["pitch"] roll = o["roll"] yaw = o["yaw"]
print("pitch {0} roll {1} yaw {2}".format(pitch, roll, yaw))
/*******WAP to Control the Led with the Android App************************/
A Line Follower Robot, as the name suggests, is an automated guided vehicle, which follow a visual line
embedded on the floor or ceiling. Usually, the visual line is the path in which the line follower robot goes
and it will be a black line on a white surface but the other way (white line on a black surface) is also
possible. Certain advanced Line Follower Robots use invisible magnetic field as their paths.
Large line follower robots are usually used in industries for assisting the automated production process.
They are also used in military applications, human assistance purpose, delivery services etc.
Line follower Robot is one of the first robots that beginners and students would get their first robotic
experience with. In this project, we have designed a simple Line Follower Robot using Arduino
Circuit Diagram
Components Required
The line follower robot built in this project is divided in to 4 blocks. The following image shows the
block diagram for line follower robot.
Controller (Arduino UNO): Arduino UNO is the main controller in the project. The data from the sensors
(IR Sensors) will be given to Arduino and it gives corresponding signals to the Motor Driver IC.
Motor Driver (L293D): L293D Motor Driver IC is used in this project to drive the motors of the robot. It
receives signals from Arduino based on the information from the IR Sensors.
Note: The power supply to the motors must be given from the motor driver IC. Hence, choose the
appropriate power supply which is sufficient for all the components including the motors.
Motors (Geared Motors): We have used two geared motors at the rear of the line follower robot. These
motors provide more torque than normal motors and can be used for carrying some load as well.
Working of Arduino Line Follower Robot
In this project, we have designed an Arduino based Line Follower Robot. The working of the project is
pretty simple: detect the black line on the surface and move along that line. The detailed working is
explained here.
As mentioned in the block diagram, we need sensors to detect the line. For line detection logic, we used
two IR Sensors, which consists of IR LED and Photodiode. They are placed in a reflective way i.e. side –
by – side so that whenever they come in to proximity of a reflective surface, the light emitted by IR LED
will be detected by Photo diode.
The following image shows the working of a typical IR Sensor (IR LED – Photodiode pair) in front of a
light coloured surface and a black surface. As the reflectance of the light coloured surface is high, the
infrared light emitted by IR LED will be maximum reflected and will be detected by the Photodiode.
In case of black surface, which has a low reflectance, the light gets completely absorbed by the black
surface and doesn’t reach the photodiode.
Using the same principle, we will setup the IR Sensors on the Line Follower Robot such that the two IR
Sensors are on the either side of the black line on the floor. The setup is shown below.
When the robot moves forward, both the sensors wait for the line to be detected. For example, if the IR
Sensor 1 in the above image detects the black line, it means that there is a right curve (or turn) ahead.
Arduino UNO detects this change and sends signal to motor driver accordingly. In order to turn right, the
motor on the right side of the robot is slowed down using PWM, while the motor on the left side is run at
normal speed.
Similarly, when the IR Sensor 2 detects the black line first, it means that there is a left curve ahead and
the robot has to turn left. For the robot to turn left, the motor on the left side of the robot is slowed down
(or can be stopped completely or can be rotated in opposite direction) and the motor on the right side is
run at normal speed.
Arduino UNO continuously monitors the data from both the sensors and turns the robot as per the line
detected by them.
Code
int mot1=9;
int mot2=6;
int mot3=5;
int mot4=3;
int left=13;
int right=12;
int Left=0;
int Right=0;
void LEFT (void);
void RIGHT (void);
void STOP (void);
void setup()
{
pinMode(mot1,OUTPUT);
pinMode(mot2,OUTPUT);
pinMode(mot3,OUTPUT);
pinMode(mot4,OUTPUT);
pinMode(left,INPUT);
pinMode(right,INPUT);
digitalWrite(left,HIGH);
digitalWrite(right,HIGH);
}
void loop()
{
analogWrite(mot1,255);
analogWrite(mot2,0);
analogWrite(mot3,255);
analogWrite(mot4,0);
while(1)
{
Left=digitalRead(left);
Right=digitalRead(right);
while(Left==0)
{
Left=digitalRead(left);
Right=digitalRead(right);
if(Right==0)
{
int lprev=Left;
int rprev=Right;
STOP();
while(((lprev==Left)&&(rprev==Right))==1)
{
Left=digitalRead(left);
Right=digitalRead(right);
}
}
analogWrite(mot1,255);
analogWrite(mot2,0);
}
analogWrite(mot3,255);
analogWrite(mot4,0);
}
void RIGHT (void)
{
analogWrite(mot1,0);
analogWrite(mot2,30);
while(Right==0)
{
Left=digitalRead(left);
Right=digitalRead(right);
if(Left==0)
{
int lprev=Left;
int rprev=Right;
STOP();
while(((lprev==Left)&&(rprev==Right))==1)
{
Left=digitalRead(left);
Right=digitalRead(right);
}
}
analogWrite(mot3,255);
analogWrite(mot4,0);
}
analogWrite(mot1,255);
analogWrite(mot2,0);
}
void STOP (void)
{
analogWrite(mot1,0);
analogWrite(mot2,0);
analogWrite(mot3,0);
analogWrite(mot4,0);