LinkIt Smart Duo: Running a Flask server

The objective of this post is to explain how to install and run a Flask server on the LinkIt Smart 7688 Duo.


Introduction

Flask is a web micro framework for Python which allows us to create and deploy simple web applications very easily.

In this tutorial, we will install Flask and create a very simple hello world application, as we did in a previous post, which introduces Flask.

For this tutorial, we assume that the LinkIt Smart is already configured, connected to a WiFi network and is accessible using WinSCP and Putty. You can check all the tutorials needed to configure these tools for the LinkIt Smart in the related posts section.

So, to install Flask on the LinkIt Smart 7688 Duo, just open Putty, connect to the LinkIt Smart and type the following command:

pip install flask

The installation may take a while since it will install all the required dependencies.


The code

The code needed to run the server is very simple and is similar to the one specified in this previous post.

To make the editing of the code easier, we can use WinSCP to interact with the LinkIt Smart. So, I’m assuming the use of this tool to upload the Python file with the code for the Flask server. You can check a detailed guide here on how to use WinSCP with the LinkIt Smart.

Open WinSCP and create a file called testFlask.py.  You can create a custom folder to develop your applications or use an existing one. In my case, as seen in figure 1, I created the file on the /IoT/examples folder, which is a default folder that comes with the Operating System installation.

linkit-smart-test-flask-server-with-winscp

Figure 1 – Creating the Flask Python script with WinSCP.

Now, double click the created file to write the code for the server.

First of all, we import the Flask class from the flask module, so all the functionality we need becomes available. Then, we create an instance of the Flask class. In the constructor, we pass the name of the module of our application, using the __name__ global variable.

from flask import Flask
app = Flask(__name__)

Now, we will define a route. A route is basically a decorator that allows to specify an URL associated with a Python function. When a request is done to that URL, the corresponding function is executed.

For our example, we will specify an URL  called “/hello” that will trigger a function that returns a greeting message, as shown bellow.

@app.route('/hello')
def helloWorldHandler():
    return 'Hello World from Flask on LinkIt Smart!'

 Finally, we run our application with the run method.

app.run(host='0.0.0.0', port= 8090)
So, as indicated by the arguments of the run method, our server will be listening on port 8090 and on the machine default IP address, since we specified the 0.0.0.0 IP address.
The final complete code can be seen bellow.
from flask import Flask

app = Flask(__name__)

@app.route('/hello')
def helloWorldHandler():
    return 'Hello World from Flask on LinkIt Smart!'

app.run(host='0.0.0.0', port= 8090)
Don’t forget to click the save button on WinSCP, as shown in figure 2, so the file is uploaded to the LinkIt Smart.
 winscp-save-flask-file
Figure 2 – Code for the Flask server.


Testing the code

To test the code, we need to go back to Putty and navigate to the folder where the Python file was created.

First of all, we need to know the IP address of the LinkIt Smart 7688 Duo on the WiFi network. To find it, just type the command bellow. You can read more about the ifconfig command here.

ifconfig

You should get something similar to figure 3. The local IP of the LinkIt Smart in the WiFi network should be the one in the apcli0 interface, as highlighted in the figure.

linkit-smart-ifconfig

Figure 3 – Using ifconfig command on LinkIt Smart.

 

After finding the IP of the device, from the same folder, just type the following command, to run the Flask server application:

python testFlask.py

You should get something similar to figure 4. Please note that the command may take a while to run.

running-flask-on-the-linkit-smart

Figure 4 – Running the Flask server on the LinkIt Smart.

Now, open a web browser and type http://IPaddress:8090/hello, where IPaddress is the IP address of the LinkIt Smart found previously with the ifconfig command. You should get the greeting message defined, as shown in figure 5.

flask-running-on-linkit-smart

Figure 5 – Testing Flask server on the LinkIt Smart 7688 Duo.

In alternative, if your computer supports mDNS resolution, you don’t need to use the IP address of the LinkIt Smart. Instead, as shown in figure 6, you can just type the following URL on a web browser: mylinkit.local:8090/hello. The name will be automatically resolved to a local IP using mDNS.

flask-running-on-linkit-smart-with-mdns

Figure 6 – Contacting the Flask server using mDNS.

We can check on Putty that the HTTP requests are being received and the corresponding information is being printed to the command line, as shown in figure 7.

linkit-smart-flask-server-received-http-requests

Figure 7 – Received HTTP requests.

To quit the server, just press Ctrl+C on Putty.


Related Posts

Advertisements
This entry was posted in LinkIt Smart and tagged , , , , , , , , , , , , . Bookmark the permalink.

One Response to LinkIt Smart Duo: Running a Flask server

  1. Pingback: Flask: Parsing JSON data | techtutorialsx

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