This week, we need to write an application that interfaces with an input &/or output device that I made. I have chosen to make an interface for my entry for week 11 which is a thermometer. I also had to choose a programming language to make this interface. After a little research, I decided to do the program in python. The python seemed to be a good choice because it offers libraries specially made for a graphical interface.
Write an application with an interface
Configure the environment
The first thing to do is to choose a development environment. I made it simple because I took the environment directly provided on the python site. The reason is that it is easier to configure than multi-language environments.
Here is also a short tutorial that helped me to install the software properly. Only the first part is useful. (up to 3:43) the second part of the video is for the next step. Tutorial
This is what the interface looks like. It's really a basic one but it does all we need.
Now that we have installed the program, we need to add the necessary libraries to build the program. In week 11, I transmitted the temperature using the serial port. So I'm going to need the serial libraire.
Here is the second part of the previous video but now to install pyserial . ( From 3:43) Tutorial
Now we need the library for the graphical interface. A very popular one is PyQt and I found the information I needed to do what I wanted.
Here is the link to download it but there is an easier way to install it that is proposed in this link. PyQT5
Simply execute the following command in the command prompt in administrator mode as in the video to install pyserial. The order is :
pip3 install PyQt5
Make the program
The first step was to receive the data from the temperature sensor. So I had to communicate with the serial port. This is when the pyserial library should be used. My serial port for communicating with my board is COM3. The comments are in the program.
Here is the result in the console. We see that the communication port is well opened with the "TRUE" which is the answer of the "TempData.is_open" function. We can also see on the second line that the name of the open port is "com3" which is the answer of "TempData.name" function. Then we see the data that arrived on the serial port. The board sends the data every 500 ms.
Now that I am able to receive the temperature data, I will build my graphical user interface (GUI). I found a tool to make it easier to build a GUI and it is the QT designer software. This software allows you to graphically produce the GUI and then convert it into code so that you can have the basis of our program.
The first step is to tell QtDesigner that you want to make a main windows.
Simply click and drag the left elements into the widgets box on your window. I identified the 3 components I used. Then, when I click on the Widget I have access to the parameter on the right side ( Color, minimum value, maximum values, gradation, etc...). This is my GUI.
QtDesigner saves with a.ui format that is not really the format we want because I program with the Python language.
There is a command that exist to converts the.ui to.py. This helps us a lot because we will have the basis of our program with the GUI included in the code. This is the command:
We now get a.py from our graphical interface that we designed with QtDesigner.
In the file we have a class that corresponds to our GUI. All lines are for positioning and configuring widgets. Luckily, this is all done for us by QtDesigner.
In my program, I used what are called threads. A thread is a mini program that is a sequence of instructions executed by the microprocessor. This is a way to execute two program parts in parallel. In parallel means that the instructions are executed almost simultaneously on a mono core microcontroller or at the same time when the thread is executed on two independent cores on a multi-core processor. I used two threads in my program to make the interface display correctly. I have a thread to display my GUI and a thread to update the information displayed in the GUI.
This is my main program that is only used to create and start the GUI thread. I start the thread update in the GUI thread to avoid any problems. You cannot update a GUI that is not yet displayed. This is the reason why both threads do not start at the same time.
Here is the video of my final result.
Group asignement
The person who documents the group assignment this week is philippe.
You can download all the files of this week right here.