Chapter 10. Internet of Things using Python

In the last chapter, we learned about different buses supported on BeagleBone. In this chapter we are going to take our physical computing exercises to the next level. In this chapter we are doing similar Internet of Things (IoT) exercises in Python that we did in Chapter 7, Internet of Things with BeagleBone. However, we will go one step further to upload and visualize our sensor data on the remote cloud. All our programs will be done using the Flask web framework. We will also implement a web app using the popular REST. The theory about the IoT is covered in Chapter 7. So, we will move to the programs directly. We will also cover security and other aspects in the last part of this chapter.

This chapter will cover:

  • Flask web application framework
  • Program to display temperature remotely
  • Program to control an LED through a web browser
  • RESTful web app to control a servo motor
  • Program to trigger an e-mail alert on over-temperature
  • Upload sensor data on cloud and visualize – ThingSpeak
  • What's next?
  • Security

Flask web application framework

We used the node.js framework to write IoT programs in Chapter 7. Let's create web applications in Python similar to what we did in Chapter 7. There are many HTTP frameworks available in Python. Most popular Python web application frameworks follow Model-View-Controller (MVC) design for coding, for example Django, and web2py. This design itself is a learning curve. So, we will skip them and choose another popular web framework that does not enforce MVC. Flask is an open source, lightweight web application framework for Python. Web applications can be written quickly in a single file. The Hello World program written on the Flask website homepage (which prints Hello World in the web browser) is just seven lines long. Flask web apps are more lightweight than MVC, which is good for embedded systems. Flask follows Web Server Gateway Interface (WSGI) specifications, which helps to create portable web applications. Many interesting web applications are written in Flask. For example, Flaskr is a blogging application:

http://github.com/mitsuhiko/flask/tree/master/examples/flaskr

Minitwit is a twitter clone:

https://github.com/mitsuhiko/flask/tree/master/examples/minitwit/

The following is a list of known websites that are running on Flask:

http://flask.pocoo.org/community/poweredby

Let us install it on BeagleBone.

Installation

Installing Flask is easy. Run the following command in the BeagleBone shell:

sudo pip install flask

This command installs Flask and all dependencies that are needed. Then you can import the Flask module and use functionalities provided by it in any Python program. Now that we have Flask up and running, let's write first a program to display temperature in a web browser remotely.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.50.252