The objective of this post is to explain how to install and run a Flask server on the LinkIt Smart 7688 Duo.
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 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.
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)
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)
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.
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.
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:
You should get something similar to figure 4. Please note that the command may take a while to run.
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.
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.
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.
Figure 7 – Received HTTP requests.
To quit the server, just press Ctrl+C on Putty.
- Flask: Hello World
- LinkIt Smart Duo: Connection to WiFi Network
- LinkIt Smart Duo: Transferring files with SCP
- Linkit Smart Duo: Getting started
- Linkit Smart Duo: Python support