The  Internet of things (IoT) is the inter-networking of physical devices, vehicles (also referred to as “connected devices” and “smart devices”), buildings, and other items embedded with electronics, software, sensors, actuators, and network connectivity which enable these objects to collect and exchange data.

The  IoT allows objects to be sensed or controlled remotely across existing network infrastructure, creating opportunities for more direct integration of the physical world into computer-based systems, and resulting in improved efficiency, accuracy and economic benefit in addition to reduced human intervention.

Internet of things with Python and Flask

Raspberry Pi can not only be used to get data from the internet but it can also act as a stand alone server.This is a great way to trigger physical events, store data, or check the value of a sensor remotely via a web browser.


Flask is a web framework. This means flask provides you with tools, libraries and technologies that allow you to build a web application.Flask is part of the categories of the micro-framework. Micro-framework are normally framework with little to no dependencies to external libraries.


We’re going to use a Python microframework called Flask to turn the Raspberry Pi into web server.

To install Flask, you’ll need to have pip installed. Run the following commands to update your Pi and install pip.

pi@raspberrypi ~ $ sudo apt-get update
pi@raspberrypi ~ $ sudo apt-get upgrade
pi@raspberrypi ~ $ sudo apt-get install python-pip

Now we can install Flask and its dependencies.

pi@raspberrypi ~ $ sudo pip install flask

This will install Flask on your pi.

Now we could do some coding on python to create a webpage using Flask.


First create a folder “program” in which you are going  to save your programs.Now open python and type the following code,

from flask import Flask

app = Flask(__name__)

def index():
    return 'Hello world'

if __name__ == '__main__':, host='')

Save the file as “” in the folder you just created.

Open the terminal and type,

hostname -I

This will give your PI’s ip address.Note the IP address.Here in my case it is “”

Now to run the program open the terminal and go to the folder “program”and type,

sudo python

We will get an output like this

* Running on
* Restarting with reloader

In your Pi’s browser and go to ““.You could see a white screen like below,


Now that you have created a basic webpage using Flask and python,lets do something more.

Add HTML templates to  webpage

For this create another folder called “templates” inside “program”.From main menu open text editor under accessories and type the following command

<h1>Hello from a template!</h1>

Save this as “template.html” in the templates folder.

Open python and type the following commands,

rom flask import Flask,render template

app = Flask(__name__)

def index():
    return render_template('template.html')

if __name__ == '__main__':, host='')

save this as “” in the program folder.Then run it as mentioned earlier and go to “” .You will see a screen as below,


Until now we were discussing about Flask and its basics.Now we can go into the interaction with the real world.Lets try to switch on and off an LED connected to the raspberry pi  using the webpage we created.

In the next topic we will be learning how to control an led from the webpage.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s