Академический Документы
Профессиональный Документы
Культура Документы
DAY 3
RASPBERRYPI AS A WEBSERVER
This tutorial explains how to use Flask as a web server in Raspberrypi device.
----------------------------------------------------------------------------------------------------------------------------------------
Flask Installation:
Using flask package, Implement a basic webserver in Raspberry pi device. Display “helloworld” in the
browser when URL : 127.0.0.1:5000 is browsed in your web browser.
------------------------------------------------------------------------------------------------------------------------------------------
• Open the File Manager and create a new folder for your project.
• Open MainMenu-> Programming->Python 3 from the main menu.
• Open a new window by clicking File- > New file, and save this as app.py inside the project folder
you created.
• You'll write your application code here and when you run your code, any printed messages or
errors will be shown in the Python shell window which opened first.
3. The lines below describe the route and function to be called when route URL is accessed from
browser
@app.route('/')
def index():
return 'Hello world'
‘Helloworld’ is the string returned to browser
4. Following is the main function. ‘app.run’ is used to start the server. ‘host=0.0.0.0’ server says
the URL is accessible with in network
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0')
OUTPUT: Open browser and type loopback url 127.0.0.1:5000. You can see ‘helloworld’ webpage using
flask
----------------------------------------------------------------------------------------------------------------------------------------
EXERCISE 2:
Modify the above program to build the webserver and add a new route to it
1. The lines below describe the route and function to be called when route URL is accessed from
browser. Here we use ‘food’ as a route. Add this to the EXERCISE 1 program
@app.route('/food')
def food():
return 'I like Yummy Cakes'
‘I like Yummy Cakes’ is the string returned to browser
OUTPUT: Open browser and type loopback url 127.0.0.1:5000/food. You can see ‘I like Yummy Cakes’
in webpage using flask
EXERCISE 3:
1. Create a HTML file named ‘index.html’. HTML Program should be saved in templates folder
inside the project folder
2. HTML folder content
<html>
<body>
<h1>
Hello from render template!
</h1>
</body>
</html>
@app.route('/')
def index():
return render_template('index.html')
EXERCISE 4:
app=Flask(__name__)
@app.route('/getajson')
def getajson():
data= {"temp":"25" }
return jsonify(data)
if __name__ =='__main__':
app.run(debug=True,host='0.0.0.0')
OUTPUT: Click on the GET TEMP DATA to receive current temperature of temperature sensor
data on WEBPAGE
<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<body class="container">
<header>
</header>
<main>
<div class="jumbotron">
</div>
<br>
<br>
<div id="tempvalue">
</form>
</main>
<footer>
</footer>
</body>
<script>
$(function(){
event.preventDefault();
alert("Temp="+data);
document.getElementById("tempvalue").innerHTML="TEMPERATURE= "+data;
});
});
});
</script>
</html>
3. Server side app.py program- [Sensor data acquisition will be discussed in Peripheral
Interfacing Session]
import smbus
import time
import sys
app=Flask(__name__)
bus = smbus.SMBus(1)
address = 0x4b
@app.route('/')
def index():
return render_template('index.html')
@app.route('/getjsontemp')
def getjsontemp():
t= gettemp()
return jsonify({"temp":t})
def gettemp():
tempData = dataReadFun()
data = tempData[0]
return temp
def dataReadFun():
try:
data = bus.read_i2c_block_data(address,0,2)
except IOError:
data =(0,0)
return data
if __name__=='__main__':
app.run(debug=True,host='0.0.0.0')