16. Interface and application programming¶
[x] individual assignment: write an application that interfaces with an input &/or output device that you made
[x] group assignment: compare as many tool options as possible
This week I learned the python language - I struggled with the its syntax…- all together with its graphical interface Tkinter, for receiving and sending data through the serial interface. I used my mainboard with the DHT222 sclick in I2C mode ( before killing it..) and 1-wire mode, receive the data on my PC and display the results on a window, together with the corresponding graphs. I also used Tkinter to send a PWM value to fade in and fade out a LED…
The idea is to develop, for my final project, a Smartphone interface with my microcontroller, to drive hte motor and get the temperature measurements done.
In a first instance, in the frame of this week assignment, I will develop an interface on my Windows PC.
Serial communication with Python (pyserial)¶
Having python already installed, I need to install pyserial soft in order to be able to read my serial ports. Therefore, I follow the following tuto (in french, sorry)
In a second step, to start interacting with my mainboard, I followed this tuto
I write a small python program, with the help of Programmer’s Notepad to receive the data sent from my mainboard to the serial monitor. I’m basically replacing the Arduino embedded monitor by a python console. Corresponding code on my mainboard is the one used during week14.
Coming from the C world, the syntax of python seems to me too much flexible, eg, relying on the indenting or not of a line to consider it as being part of the routine or not is quite confusing, and requires extra attention!!
I’ve also looked at following tutorial
import serial import time ser = serial.Serial('COM9', 115200) while 1: try: text = ser.readline() print(text) time.sleep(1) except ser.SerialTimeoutException: print('Data could not be read') time.sleep(1)
Next step would be to take control of the mainboard through the serial connection. I need to adapt my mainboard code to accept command from the Serial port, eg activating a led on one of the ATmega328p pin. I’ll do it later, let’s focus on the presentation of the received data…
Graphical interface with Python (Tkinter)¶
The graphic interface tkinter is installed by default in python3.7.2 on my PC.
(to check this, open a python console and type
import tkinter: no reaction means evrything’s fine!)
I followed these tutos:
Several graphical tools are available in Tkinter, called widgets, that can be given several options. The main ones I use here are:
- The Frame widget, providing a rectangular region on the screen, is mainly used as a geometry master for other widgets, or to provide padding between other widgets.
- The Label widget is a standard widget used to display a text or image on the screen
- The Button widget is a standard widget used to implement various kinds of buttons, that we can associate with a python function or method, function or method that is automatically called when the button is pressed.
- The Canvas widget provides structured graphics facilities for Tkinter. It can be used to draw graphs and plots, create graphics editors, and implement various kinds of custom widgets.
- The Scale widget allows the user to select a numerical value by moving a “slider” knob along a scale
In addition, I installed matplotlib in order to be able to draw graphics (python -m pip install -U matplotlib)
I had to play around with the several frames size to get a acceptable and readable window, which is a little bit impractical…
I’m still using my mainboard to receive the temperature and humidity data from my DHT222, but this time, I reuse it in 1-wire mode....
Working (too!) late last night, I swapped the power connections on my sensor, and as expected the latter died, as well as the I2C part of my microcontroller. Luckily I had a second sensor, and I will have to replace my microcontroller to reuse the I2C protocol…
I modified a bit the Arduino file to adapt the format of the data sent to the Serial port to the requirements of the python program.
Launching the python programs requires to give the port number on the command line:
python test2.py COM9. This enables me to change the port number outside the program.
So here is the result when we blow on the sensor, and when we warm it up: It works!
The related python and Arduino files can be found here
In this third test, I use python to have a LED fading in and out up to a maximum value given through the serial interface and python.
Fine, it works…
The related python and Arduino files can be found here
In the group, we tested mainly 2 options:
- The easy way (as Gilles): using Python with Tkinter and Pyserial.
- And the hard way (as Axel), who worked on an Inkscape plugin using Python, to interface our machine.
Comparing the results, we can highlight several points:
- Tkinter and Pyserial are simple libraries that allow a quick learning curve, and a basic use can be enough to collect or send simple data.
- However, they are limited. As example, Tkinter become quiclky havy and complex if you want more fancy interfaces. For example, here is the interface that Gilles obtained: not fancy at all but functional.
- More, if you want more complex interfaces, for example to control a machine, you’ll need other tools, that become more complicated to use, but allow more possibilities, as Axel did and documented.
Still to do if I had time....¶
I d like to have a look into optimization process of python code, as the sending/receiving process doesn’t seem to happen on real-time, I got up to ten seconds before my LED reacts to the change of PWM value… And also look at more efficient ( or easy?) graphical tools…