Go Down

Topic: Demo of Arduino control with a Python GUI program (Read 8733 times) previous topic - next topic

Robin2

Oct 07, 2014, 01:47 pm Last Edit: Oct 21, 2014, 10:39 am by Robin2 Reason: 1
I have seen a few questions recently where Forum members were enquiring about controlling an Arduino with a PC GUI program.

With the zeal of the newly converted I thought that others might be interested in the attached simple Python GUI program for controlling an Arduino. I do realize that this is more of a Python tutorial than an Arduino tutorial.

The GUI interface looks like this


The buttons allow you to switch on or off a couple of LEDs and the slider allows you to move a servo.

When the program starts it allows you to select the serial port for the Arduino. I have only tested this on Linux. It would be great if someone would try it on Windows or on a Mac.

The purpose of the Python code is simply to provide all of the parts for a simple working example as a starting point for whatever else someone may wish to do.

Apologies to anyone who is a Python expert and doesn't like my coding style. I understand it and I think it will be easily accessible to Arduino programmers.

The Python code for communicating with the Arduino is in the file arduinoComms.py and is very similar to my previous example here.

To try the example you need to save the two python files into a suitable directory and run it with python arduinoGUI.py  (after uploading the Arduino code, of course).

Have fun

...R

By the way this demo does not receive data from the Arduino after startup. I may produce an
extended version that does - now in Reply #3

Edit to add ...
The attachments seem to have been lost in the Upgrade on 20 Oct 2014.
I have added a zip file with the files as it won't accept any other file type.  ...R
Two or three hours spent thinking and reading documentation solves most programming problems.

robtillaart

Thanks for sharing, works very well,

missing: requesting a sensor from Arduino (e.g. analog port),
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Robin2


missing: requesting a sensor from Arduino (e.g. analog port),


Thanks for testing.

I'm working on Version 2 which will continually check for data from the Arduino and just display whatever is received - probably just something derived from millis() as I don't want to complicate the Arduino hardware.

If you have something else in mind let me know.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Robin2

#3
Oct 08, 2014, 12:52 pm Last Edit: Oct 08, 2014, 12:56 pm by Robin2 Reason: 1
In the updated versions of the Arduino and Python programs attached to this Post the Arduino sends a number (roughly the number of half-seconds since RESET) to the PC every 3.5 seconds and the Python program checks for and displays any new data every 2 seconds.

Broadly speaking there are two ways for the Python program to behave. It can check for data after specific events (for example everytime it sends something). Or it can check continuously at suitable intervals.

For this demo I have chosen the latter option as it illustrates the use of a separate Thread (running in parallel with the main code) to check for new data. When it finds new data it places it in a variable that is accessed by the main program and displayed at the bottom of the window. It would be a simple matter to do something else with the data - for example to save it to a database.

The code for the separate Thread is in the file arduinoCheckForData.py.

If the Python program only needed to check for incoming data when (for example) a button is pressed there would be no need for the separate Thread.

The display of the incoming data is, perhaps, a little bit too basic but I have left it like that as it illustrates the interaction of the different cycles of the Arduino (sends every 3.5 secs) and Python (checks every 2 secs). You can change either of these to see what happens. Obviously Python needs to check at least as often as the Arduino sends.

The Arduino program includes a function replyToPC() which is commented out in loop(). If you enable that function call in place of sendToPc() it will immediately send back the data it has just received. This works fine with a button press. But movements of the slider produce dozens of transmissions and equal numbers of replies which make the function rather impractical.

Finally, the code in arduinoCommsB.py has been updated slightly from arduinoComms.py in the first Post because I had not realized that the Python Serial.read() blocks until a character is received. This did not matter when Python was not checking for new data. The new version can be substituted for the version in the first Post if you wish.

Enjoy ...

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up