IOT WITH PYTHON AND FLASK IN RASPBERRY PI

INTERNET OF THINGS

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.

ABOUT FLASK

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.

INSTALLING FLASK ON RASPBERRY PI

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.

CREATING A WEBPAGE WITH PYTHON AND 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__)

@app.route('/')
def index():
    return 'Hello world'

if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0')

Save the file as “program.py” 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 “120.0.0.1”

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

sudo python program.py

We will get an output like this

* Running on http://0.0.0.0:5000/
* Restarting with reloader

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

whitesv.png

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

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

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__)

@app.route('/')
def index():
    return render_template('template.html')

if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0')

save this as “programtemplate.py” in the program folder.Then run it as mentioned earlier and go to “http://127.0.0.1:5000/” .You will see a screen as below,

template.png

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:

WordPress.com Logo

You are commenting using your WordPress.com 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