Raspberry pi WebServer using python's micro framework Flask
Create New

Raspberry pi WebServer using python's micro framework Flask

Project period

01/02/2020 - 01/31/2020

Views

120

2



Raspberry pi WebServer using python's micro framework Flask
Raspberry pi WebServer using python's micro framework Flask

By utilizing python's micro framework, Flask, we can create reliable and efficient web application and can also take control of any devices remotely. This project demonstrates how to build a web application which controlls the GPIO's (Generall purpose input & output) pins of raspberry pi or zero drone.

Flask is called a micro framework php because it does not require particular tools or libraries. It has no database abstraction layer, form validation, or any other components where pre-existing third-party libraries provide standard functions. However, Flask supports extensions that can add application features as if they were implemented in Flask itself.

Why: Problem statement

When it comes to remote access of devices most programmers and engineers often find difficult to control it. The connections which were made on raspberry pi or zero drone sometimes become laggy and dont deliver the output that we are expecting. Even a single line of wrong code can make a drastic negative impact on hardware which then causes failure.

How: Solution description

Here I used the python micro-framework tool Flask to overcome the above-mentioned problem. I created a web application using simple HTML and CSS & made to work with the python script using Flask. NOTE:  In this project Raspberry pi or zero drone act as a webserver.

Installation Steps:

  1. The first step to the project is to install the Flask and RPi Web server.
  2. Go to the terminal by pressing ctrl + shift + t. and then make a directory RPi web server by typing "mkdir RPi webserver" (without strings).

 

The above block diagram shows what we accomplish in the end.

Flask Installation:

  1. The first thing to do is to install flask on your Raspberry pi. Go to Terminal and enter. "sudo apt-get install python3-flask"
  2. Navigate to RPi web server directory. " cd /home/Desktop/RPi webserver"
  3. Also, make two new directories for reference namely, "static" and "Template". mkdir static && mkdir templates.
  4. The final folder tree looks like.

             /RPi webserver

                 /static

                /template

 

The python web server application:

Now we create python web server application 

from flask import Flask

app = Flask (__name__) #name of the index file

@app.route(/)

def index():

      return Hello world

if __name__ == __main__:

         app.run(debug = True, Port = 80,  host = 0.0.0.0)

 

Running the application:

  1. Go to the terminal and type "ifconfig"
  2. Under inet, there will be ip address similar to this "192.168.0.12"
  3. Now go to the web browser and type the exact ip.
  4. Our web page with gpio controll loads into the screen.
  5. Control led by turning on/off on the web interface.

How is it different from competition

  • The existing system uses python socket programming while I have used Python micro framework flask.
  • This project controls GPIO's of raspberry pi or zero drone over the internet. Turns on/off LEDs, control motor driver, remotely monitor sensors and feedback the values, etc.
  • Wrapping the source code as and single standalone application has been a major part of this project.

Who are your customers

  • Programmers who are interested in socket programming.
  • Software engineers & Product developers.
  • Electronics hobbyist & DIY makers.
  • Students and Innovators.

Project Phases and Schedule

Phase 1: UI design using HTML and CSS.

Phase 2: Python script using flask libraries.

Phase 3: Raspberry pi or zero drone GPIO connections.

 

Resources Required

Software required:

  1. Raspbian OS.
  2. Thonny or Geany or any IDE's.
  3. Python version 3.
  4. PIP version 3.
  5. Text editors for HTML and CSS.

Hardware Required:

  1. Raspberry pi model 3 b+ or above.
  2. Jumper cables.
  3. LEDs.
  4. Motors.

Download:
Project Code Code copy
/* Your file Name : flask_pthon.py */
/* Your coding Language : python */
/* Your code snippet start here */
'''
	Raspberry Pi GPIO Status and Control
'''
import RPi.GPIO as GPIO
from flask import Flask, render_template, request
app = Flask(__name__)
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
#define sensors GPIOs
button = 20
senPIR = 16
#define actuators GPIOs
ledRed = 13
ledYlw = 19
ledGrn = 26
#initialize GPIO status variables
buttonSts = 0
senPIRSts = 0
ledRedSts = 0
ledYlwSts = 0
ledGrnSts = 0
# Define button and PIR sensor pins as an input
GPIO.setup(button, GPIO.IN)   
GPIO.setup(senPIR, GPIO.IN)
# Define led pins as output
GPIO.setup(ledRed, GPIO.OUT)   
GPIO.setup(ledYlw, GPIO.OUT) 
GPIO.setup(ledGrn, GPIO.OUT) 
# turn leds OFF 
GPIO.output(ledRed, GPIO.LOW)
GPIO.output(ledYlw, GPIO.LOW)
GPIO.output(ledGrn, GPIO.LOW)
	
@app.route("/")
def index():
	# Read GPIO Status
	buttonSts = GPIO.input(button)
	senPIRSts = GPIO.input(senPIR)
	ledRedSts = GPIO.input(ledRed)
	ledYlwSts = GPIO.input(ledYlw)
	ledGrnSts = GPIO.input(ledGrn)
	templateData = {
      		'button'  : buttonSts,
      		'senPIR'  : senPIRSts,
      		'ledRed'  : ledRedSts,
      		'ledYlw'  : ledYlwSts,
      		'ledGrn'  : ledGrnSts,
      	}
	return render_template('index.html', **templateData)
	
@app.route("/<deviceName>/<action>")
def action(deviceName, action):
	if deviceName == 'ledRed':
		actuator = ledRed
	if deviceName == 'ledYlw':
		actuator = ledYlw
	if deviceName == 'ledGrn':
		actuator = ledGrn
   
	if action == "on":
		GPIO.output(actuator, GPIO.HIGH)
	if action == "off":
		GPIO.output(actuator, GPIO.LOW)
		     
	buttonSts = GPIO.input(button)
	senPIRSts = GPIO.input(senPIR)
	ledRedSts = GPIO.input(ledRed)
	ledYlwSts = GPIO.input(ledYlw)
	ledGrnSts = GPIO.input(ledGrn)
   
	templateData = {
	 	'button'  : buttonSts,
      		'senPIR'  : senPIRSts,
      		'ledRed'  : ledRedSts,
      		'ledYlw'  : ledYlwSts,
      		'ledGrn'  : ledGrnSts,
	}
	return render_template('index.html', **templateData)
if __name__ == "__main__":
   app.run(host='0.0.0.0', port=80, debug=True)

Comments

Leave a Comment

Post a Comment