Week 12: Interface and Application Programming
In order to create a graphical interface for the recorded sensor data, I had to install some programs first.
Software Installations
Arduino IDE I had already written on my calculator and I would continue to use it for this task to capture the data from the sensor.First of all, I installed Python in the older version 2.7.9 , because I wanted to use a specific package of the program, which should communicate best with the old version of the program.
For Python to receive the data from Arduino I had to install PySerial . This is the Python package that sends reading the data from the serial interface (usb serial) to Python.
Since the serial interface had disappeared again and again from my computer, I had to reinstall the following package FTDI USB serial a couple of times and then restart my computer.
For data plotting live from Arduino I found out that "Matplotlib" as a package of Python is a very good tool! I used Matlab bevore but it was allwas the Data from a database which I anlysed. Now I am happy to do such interessting thing like Python data loging and showing it in a graph.
Matplotlib Download
For installing Matplotlib I used following command lines. Imortant was to add the Version of the Python I use to the command line:
python3 -mpip install -U matplotlib
Even if the official site does not say so...
I also needed to download Drawnow Python Package to plot data.
Groupassignement: Connection between Arduino and Python over serial USB
Following settings were needed to get Python speaking to serial USB:
I had to put the Serial Port name, which is to see in the image above to the Python code. The data format had to much the format which Python needed to understand.
So I had to comment out some strings, which Arduino seriall monitor displayed.
For the test of the connection between Arduino and Python over USB-serial I used this simple C-code on Arduino IDE:
Than I used the C-code for readings of ultrasonic senosor HC-SR04 using Arduino IDE and run it.
I found out that the data should not be displayed in the graph if those are not in one line or (with comma ore space) separated. So I had to change the c code for data transfer.
Individual Part: Live Data Visualisation
As the connection between my ATmega328p board and Python worked I could finaly plot data using Pyhon packeges Matplotlib and Drawnow.Following commands I used for plot with Matplotlib:
plot(duration, 'ro')
where the "duration" data as red circles are plotted.
When I was displaying the readings from serial port (Arduino code) I could not display the data in the same time on Arduino serial monitor and Arduino serial plotter.
In the following you can see the Python code which I assembled.
The first graph I could see was this:
First there were no further data incomming, just two points which also changed their position. The reason for that was that I did not attached the command "append" to the variable. The code you can see above has allready this command included.
After data dispalying worked some time, suddenly the programm stopped. I found out that there was one more problem in the C code which I did not noticed. The Arduino code gave some string "No readings" out when the sensor changed the position and so could not read any data for that moment. I changed it to "Null" and after that the programm could run.
As I wanted to display two variables which are varring in the size I added another Y-Axses, as to se below.
As to see in following images it is possible to pan the graph while it is displayed.
Here you can see an animation of data logging with Python and Matplotlib:
Another Trails of Data Visualisation
For further data visualisation I installed VPython using following instruction:VPython is a 3D visualisation software package of Python.
This tutorial I think is so good to understand the connection between Arduino and Python.
After some trials first only the data transfer worked as to see in the following image:
Later I could also see the virtual rod which should show the distance between Ultrasonic Sensor HC-RS04 and the wall. But somehow this rod did not move as it shoud. It stayed motionless. Python also crashed after running the code...
As I taught that the problem could be on my Mac Book Air, I wanted to test the code on another Windows PC. Unfortunately, after installing all other programms on Windows PC, I found out that the AVR ISP-Programmer could not be recognised from the Windows 10 operating system.
Now I tried another code which should do the same virtual "world" of the ultrasonic sensor. Hier I had the same problem... as to see in imag below...
I planed to test the data base software MySQL for Python, that is why I installed it. But I did not have much time for testing it. I hope I can do this later on.
Download MySQL for Mac
Oracle.com - Registration required
Downloads
C Code for Arduino reading Ultrasonic Sensor Data | download |
Python Code for Data Displaying | download |
VPython Code for 3D Visu | download |