0% found this document useful (0 votes)
72 views24 pages

Lecture 27 - Introduction To Python Programming - II

This document provides an introduction and overview of Python programming for applications in Internet of Things (IoT). It discusses several examples including: 1) Performing basic file read/write operations on text files and CSV files to store and access sensor data. 2) Developing a network socket using Python to create a UDP client/server for communication. 3) Conducting basic image read/write operations using the Python PIL library. The document demonstrates how to open, read, write, and close files in Python. It also shows an example of writing and reading data to a CSV file to store sensor readings separated by commas.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views24 pages

Lecture 27 - Introduction To Python Programming - II

This document provides an introduction and overview of Python programming for applications in Internet of Things (IoT). It discusses several examples including: 1) Performing basic file read/write operations on text files and CSV files to store and access sensor data. 2) Developing a network socket using Python to create a UDP client/server for communication. 3) Conducting basic image read/write operations using the Python PIL library. The document demonstrates how to open, read, write, and close files in Python. It also shows an example of writing and reading data to a CSV file to store sensor readings separated by commas.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Introduction to Internet of Things

Prof. Sudip Misra


Department of Computer Science & Engineering
Indian Institute of Technology, Kharagpur

Lecture - 27
Introduction to Python Programming- II

Hello, in this lecture in Continuation to Python Programming, I will be covering a few


interesting applications, you can develop using normal python programming.

(Refer Slide Time: 00:34)

So, to start off with, we will be covering a few basic operations starting from file,
read/write, operations, we will be dealing with normal text files as well as csv files. Csv
are basically comma separated value files. So, each value in the file is separated by a
comma.

Next we will move on to your developing a network socket using python scripting. So,
we will develop two parts. First, we will develop a udp host or a server and a udp client
and in the third part, we will be dealing with some basic image, read, write operations
using python PIL library. So, to start off with the file read write operations as necessary
in normal IoT based applications, imagine your senses or lots and lots of sensors are
uploading the data on a particular database or a file system.
So, the data is separated, separately stored or data is collectively stored using various
time stamps or signatures or identifiers. It may be in a text file for small scale systems, it
may be in a csv file, but larger scale systems and then, again for much larger
implementations of IoT, people normally prefer using data bases like MySQL or Oracle
ecetera. So, python allows us the functionality to read and write files.

Generally no external module or library is required. It is available with the basic


distribution of python itself and while reading or writing a file, three basic steps have to
be followed. First and foremost is opening a file itself. Next is reading or writing
operation and third one is closing the file. So, keeping these three in mind, we will dive
into a bit more details about these.

(Refer Slide Time: 02:31)

So, the open function is used to open a file and return a file object. That means,
whenever you give the syntax, open first argument is filename and the second argument
is mode. There are the following four modes, r for read or w for write, a for append and r
plus for both read as well as write. Modes through reading from a file as you can see the
syntax is pretty simple.
(Refer Slide Time: 03:01)

So, imagine you have a file text, file named data data dot txt. So, we are going to attempt
to open it using this line file equal to open data dot txt comma r point to notice both the
filename as well as the mode are within quotations because these are taken as strings and
then, in the next line we have file dot read. So, this file equal to open function, the file is
actually a variable. You can assign any other name to it. Next is the writing to a file part.

So, whenever we are writing to a file, again we have to open the file. Only the mode
changes from r to w, then file dot write and within arguments, you just put in the string
here about to write to the file you can. Obviously, put various variables you can store
your incoming sensor data and some variable and iteratively write those variables using
the file dot write function.
(Refer Slide Time: 04:14)

Now, at the end, once a file access has been done, you must ensure that your file handle
is closed by using this function file dot close and whenever since in normal scenarios
specially IoT based scenarios, there is a significant need to iteratively access the same
file over and over again. So, using this script with open data dot txt in write mode as file
in file dot write, we write the string which has to be written to the text file and then, we
release the file handle by calling file dot closed.

(Refer Slide Time: 05:02)


So, it would be better if we give a bit of hands on to this thing. So, as you can see I have
already kept the program ready.

(Refer Slide Time: 05:29)

So, we just have to call a file, any file name we want to put over here. We have python
program dot txt. We can change it to something else also. So, my files and folders
basically recite in this directory. So, as you can see there is no python program dot txt
file in this list. So, maybe we can just change the name to live demo dot txt.

(Refer Slide Time: 06:26)


Then, in the next line for speed, iteratively we have to write something on it. So, with
open live demo, the txt as w or in write mode as file, let us call it by another name file 1.
Then, file 1 dot write, we are writing this writing data abc and then, we release the file
handle by calling file 1 dot close. So, once we run this thing, we have executed this
thing, these three lines of code. Again we will check the directory.

(Refer Slide Time: 07:40)

Here we have the file named as live demo dot txt. We open it as you can see the intended
data has been written to the text file. So, this was part one. Half of the job it is reading
that is writing to the file. Next part comes that is reading from that file.
(Refer Slide Time: 07:56)

So, now I have a filename live demo dot txt. So, in the same manner as before I will
open the same text file, but in read mode. So, I will change the mode to r initial. It was w,
now changing it to r.

So, I am opening the file mode in read opening, the file in reading mode assigning the
variable let us call file 1 or maybe just for the sake of it file 2 f equal to file 2 dot read.
Then, I print reading from the file and then, again I print whatever has been read and
again I release the file handle. I release this file 2 by calling file 2 dot close. So, once
again I will execute these lines.

See once this execution is complete, the first line has been printed reading from the file.
This was a first print statement and then, if you recall we had earlier written data to the
text file like writing data abc. So, this has been extracted from the text file and put up
and printed on the console. So, this was part one, reading and writing from normal text
files. So, they can be multiple operations, we can consider while doing file read write
operations.

So, not to go into that much details, you can easily avail various resources online to
check those.
(Refer Slide Time: 09:50)

Another common thing being used is this csv file format or comma separated values. You
can again check I do not have any csv file in my directory list. So, I am using this script
csv read write or py. So, once this has been executed, it will generate a file with the
extension dot csv.

For this I have to import an external library or csv library. Once this library has been
imported, I define the data. I want to write to the file. First I will write to the file, then
once the file has been created and the data written to it, I will read from that same file.
So, one point to remember is whenever I am trying to open a file in write mode, if the
file does not already exist, the system will automatically create that file, that same file.

So, I am giving the file name as output dot csv. Let us name the variable as file 1. The
data is within since we are putting up a string data. So, we are giving 1 2 3 4 upto 9 and
we split it using the comma character, you can use other characters also like semicolon,
colon so on, but for the sake of demonstration, I am just using a comma. Let us see what
happens when I execute this line. So, this script has been executed and I will check what
is stored in this.
(Refer Slide Time: 11:37)

So, you can see individually you have various strings separated by comma. The first
string is 1, second string is 2, and so on upto 9. So, whatever input you had given, it has
been individually separated into substrings. Then, next I open I create a variable file 1
and try to open output dot csv. So, in this directory, we have nothing called an output dot
csv file.

(Refer Slide Time: 12:16)

So, we are assigning this output dot csv string to variable file 1. Now, again using with
open file 1 in write mode as csv file, we call another variable writer equal to csv dot.
(Refer Slide Time: 12:35)

Writer in csv file delimiter is comma. Suppose you had given a semicolon as delimiter,
you could have replaced this comma by a semicolon and we print a normal statement to
check whether it is working fine or not and then, whatever data is being written into the
file.

So, we call the writer again and each row report in a separate value like 1 in the first row,
2 on the second row, 3 on the third row and so on. Then, eventually we call the close
function file dot close. Now, let us see what happens when we run this collectively. So,
my print statement writing csv has been executed. Let us check. So, the file name was
output dot csv. Yes we have a file called output dot csv over here.

So, by default it is opening with microsoft excel. So, for my system, the default csv file
viewer is microsoft excel. So, it is directly opening through excel. This will take some
time. Let it load. In the meanwhile, we will look into this writing from a csv file part.
Now, again we go on with open in a name variable file 1 in read mode as csv file and
reader equal to csv reader csv file.

We print that it is reading from a file that is we will just give an indicator to the user that
this script is executing and then, for each row being read by the reader, the value being
read will be printed one at a time and eventually, we will release the handle by calling the
filename dot close function.
(Refer Slide Time: 15:34)

So, I have finally managed to open my excel file. As you can see the values 1 2 3 4 have
been written in separate columns. So, instead of these static values, we can acquire data
from various sensors and we can keep on updating this csv file in a iterative manner. So,
this will give me a database, a rudimentary database, ok.

(Refer Slide Time: 16:28)

Now, I know my output dot csv file is in place. I will try reading from that file. Now, the
first after the script has finished executing, the first print statement was reading from the
csv file and it has read the values individually like 1 to 9. So, I hope this was quite easy.
We will move on to the next part.

So, we have already completed these. Now, the next part is a, but interesting since it will
deal with various images.

(Refer Slide Time: 17:05)

(Refer Slide Time: 17:07)


(Refer Slide Time: 17:10)

So, we will functionally work with image read write operations. Now, python supports
this library called PIL or python imaging library. This is generally used for image related
operations. So, if by default your PIL is not on the system, we use PIL or we acquire PIL
using sudo pip install PIL low.

So, normally PIL is supported in the python version 2.7 till 2.7, otherwise for higher
versions PIL needs to be installed to get this working.

(Refer Slide Time: 18:02)


So, for image read write operations, first of all we have to call this PIL library either we
import PIL directly or if we have any particular function in mind like the function image,
we just import that particular function.

So, for that we have from PIL import image, then opening that image file, image equal to
image dot open name of the image and for displaying the image, we just write image dot
show.

(Refer Slide Time: 18:36)

So, you have multiple functions related to images. You want to resize your image, you
call the resize function and within arguments you give the number of rows and columns
you want to rotate your image.
(Refer Slide Time: 18:59)

You call rotate and within arguments, the degrees you want to rotate your image, also
you can convert between various colour maps. So, for example you can convert your
normal images acquired from standard cameras. We call them as RGB images because of
the presence of three channels red, green and blue. We can convert this into grey scale
also. So, the conversion function is pretty simple. Image dot convert and within the
arguments within quotes, you write that mode. l stands for pay scale and RGB stands for
the normal RGB mode. So, you can alternatively convert between these two.

(Refer Slide Time: 19:40)


(Refer Slide Time: 19:45)

So, this is just for the sake of demonstration. See for example, once we execute, this
script will come to that in the hands on part. Once we execute the script, it will give you
the following output. The left hand side, this image was taken as the input image and
after conversion using image dot convert function, it has converted to your grey scale.
So, we will check out this function.

(Refer Slide Time: 20:24)

So, now I have from PIL import image and assigning a variable to the function image dot
open. So, I have a input image, I three dot jpeg in my folders, same folder as the script,
then I execute this im dot show. I will just execute these three lines you see. Once the im
dot show has been executed, the default image viewer application of your system opens
up and this is the input image null convert it to grey scale.

(Refer Slide Time: 21:31)

So, im dot convert and within quotes l and again, last the system to show the image. So,
as you can see again on the default image doing application, the image has been
converted to grey scale from RGB. Now, I need to save this image. So, since the
converted colour map of the grey scale image is stored in a variable grey underscore
image, I need to save this grey underscore image dot save and the name I want to save it
as.
(Refer Slide Time: 22:07)

So, once save function has executed, I get the required file in my directory. These are just
some of the very basic functions I can work with.

(Refer Slide Time: 22:29)

Next we will move on to basic networking in python. So, for this we will create udp
sockets. So, we will try to simulate two systems like you will have a client and then, you
will have a server. So, server will expect data from the client. Later on you can actually
now itself you can actually imagine your client to be deployed IoT devices which read
various sensorial data and transmit it over the network to the server. It works in a same
principle.

So, for starters we will just see how this networking part happens. So, maybe your server
is located far apart, but connected to the network using this same code, same principle.
With a bit of modifications, you can get your field deployed IoT devices to send data to a
remotely located server. So, python provides this network services using the client server
model and then, this socket support in operating system, it allows the clients to
implement servers.

It allows the implementation of clients and servers for both TCP IP that is connection
oriented as well as UDP protocols which are mainly connectionless protocol and python
has additionally libraries which allow higher access to specific application level network
protocols.

(Refer Slide Time: 23:57)

So, general syntax is locating the socket a variable which is equal to socket dot socket in
socket family socket type protocol. By default the protocol is 0. So, within socket family
you have AF underscore UNIX or AF underscore INET.

So, generally UNIX option is only available for UNIX base systems, where as INET
stand for all internet protocols. Socket type is sock stream or sock datagram and protocol
by default is 0.
(Refer Slide Time: 24:35)

So, we have created a TCP IP socket, we give the server address as this is a particular IP
address, then we give a particular port to which server your client will bind and just some
print statements and eventually, the main functions sock dot bind and server address, so
that this will be listening for connections and then, we have sock dot. Listen this
initializes the listening part that the server is expecting connections from client.

(Refer Slide Time: 25:04)

So, it would be better if I give a demonstration of this thing. So, I will need two consoles
for this. From 1, I will run the server and from another, I will run the client.
(Refer Slide Time: 25:30)

So, this is my socket server. For the server address, I have given a normal loopback
address that is 127.0.0.1

I given a random port 10000 and one and we intend to write the data which is being
generated into a text file. So, data log dot txt in write mode and this data socket will be
expecting the data which it receives from the client in junks of 4096 bytes and whenever
this data is received, it will convert the data to string and write the data to the file and
eventually once the transaction has finished, it will close the file.

(Refer Slide Time: 26:51)


So, till this part the socket operations take place and within this the data read write
operation take place, similarly for start I will get the server running. So, it does not show
anything because it is expecting data from a client. On the client side, I give the same
loopback address as the server, then the same port number. It is very important to have
the same port numbers.

Now, finally we just iteratively loop again and again and send two values h and t. I have
fixed it to 2 and 3 and this will keep on sending these values over the socket. You see this
sent equal to this sock dot sent to message, the message that is 2, 3 to the server address
that was this loopback address.

(Refer Slide Time: 27:42)

So, in the second console, I will run the client. You see the client is sending 2 3 2 3. It
will keep on sending this until I terminate the execution and at the server side, you are
receiving this 2 and 3.

So, this thing you can implement on two different machines connected to the network. It
may be in the same room, it will be in different cities, it may be in different countries,
but your server, your IP and port needs to be the same.
(Refer Slide Time: 28:19)

(Refer Slide Time: 28:22)


(Refer Slide Time: 28:25)

So, we have covered this client. You have seen the code for stand and outputs you have
seen. So, I hope you can now use this knowledge to generate some interesting
applications on python.

Thank you.

You might also like