This is a useful program in real-life and can be part of your home automation. You will be able to get temperature information from BeagleBone to any Internet connected smartphone/computer. We need to create one HTML file and one Python file for this exercise. Let's write an HTML file first. This HTML file is not hard-coded to show temperature value. It will get temperature value at run time. This type of HTML file defines a pattern or model of how a webpage should look at run time. So, they are called templates in Flask. A template HTML file needs to be created inside the directory templates. So, open Cloud9 IDE. Inside the left-side workspace pane, create a new directory called templates
under cloud9
directory. Create a new file named temperature.html
inside the templates
directory. The total path of this newly created file will be /var/lib/cloud9/templates/temperature.html
. Write the following code in it and save:
<!DOCTYPE html> <html> <body> <h1>{{temperature}}</h1> </body> </html>
This HTML file has no information other than declaration of variable temperature
. This variable will get value at run time.
Create a TMP36 sensor circuit setup like we did in Chapter 5, Reading from Analog Sensors. Click on the cloud9
folder in the left pane to get back to the default cloud9
directory. Create a new file with the name flask_temperature.py
and write the following code in it. Save and run the program. Then open the web browser from your smartphone/computer connected to same router your BeagleBone is connected. Put this address in the address bar: http://<Beaglebone's IP address>:3002
. You should be able to see the temperature value. If you touch the TMP36 sensor for a few seconds and refresh the page, you should see an increase in temperature. The code for flask_temperature.py
is as follows:
import Adafruit_BBIO.ADC as ADC from flask import Flask, render_template, request app = Flask(__name__) ADC.setup() tmp36 = 'P9_40' @app.route("/") def print_temperature(): volts = ADC.read(tmp36)* 1.8 temperature = (volts * 100) - 50 print(" Current Temperature is " , temperature) return render_template('temperature.html', temperature = temperature) app.run('0.0.0.0', 3002)
In our Python program we imported Adafruit_BBIO.ADC
because we need to read from the TMP36 analog sensor. We imported the class Flask
and some other functionalities from the module flask
. Then we created an instance of this class. We provided the parameter __name__
which is the name of the application's module or package. Using the __name__
parameter allows this program to be used as an application as well as being importable in another application.
We used the route()
decorator to tell Flask which URL should trigger the function defined next. The route('/')
command means only website address without specifying any HTML file. So when the BeagleBone IP address (without addition of any HTML file) is entered in the web browser, it will call the function print_temperature()
in our program. This function is similar to the temperature printing program we did in Chapter 8, Physical Computing in Python. We called the function render_template()
which will load temperature.html
and update the variable temperature
in that HTML file with the current temperature we read from tmp36
.
The last line app.run()
actually runs our application on port 3002
and makes it accessible to 0.0.0.0
, which means to any IP address. When the remote web browser opens the BeagleBone IP address with the correct port, it internally executes the HTTP command GET
for page /
. Our web server application calls the print_temperature()
function and responds to it.
Following are some troubleshooting techniques:
404
means page not found. Most likely you entered the wrong URL.temperature.html
file exists inside the templates
directory.debug=True
to the function app.run()
for further debugging. Do not forget to remove the parameter when debugging is done. The app in debug mode which is exposed to the Internet brings security vulnerabilities.ps aux | grep flask
. Then you can kill it using the command kill -9 <pid>
.3.141.46.130