Go Down

Topic: Uploading arduino code interactively using python (Read 1 time) previous topic - next topic

nivpgir

Feb 25, 2016, 03:42 pm Last Edit: Feb 25, 2016, 06:09 pm by nivpgir
Hi all,

I work in a psychology lab, and in some of our experiments we use arduino to create some physical simulations in experiments.

usually we create a python script that runs on a computer, and a sketch for the arduino, and they both interface through serial to send the stimuli.
my issue is that after creating quite a large amount of experiments that work with the arduino, the experiments are getting mixed up and we often upload the wrong sketch to the arduino and it does not interact correctly with the script on the computer.

to fix that, and organize this mess, I would like make the script running from the computer to upload the right code to the arduino (since its already connected through serial), but I haven't found any library that can do it.

another option I thought of is writing a batch file that can be run through python, however I haven't found any windows commands to do this.

does anyone know of a solution to my problem? I prefer finding a library for python than write a batch file, however I will accept any solution that will upload the right sketch using an python script.

 my only limitation is that I might have to set it up on many computers so it would be best to keep the dependencies and first time setup to a minimum.

Thank you.


 

Robin2

#1
Feb 25, 2016, 07:51 pm Last Edit: Feb 25, 2016, 07:53 pm by Robin2
You can use Python to call the Arduino IDE and upload a program.

I illustrated this process in this Thread. You don't need to use the Geany editor.

In this more recent Thread I wrote an extended Python program that makes archive copies of the program that is uploaded and writes the name of the archive into the Arduino code so it can the seen in the Serial Monitor. This version also allows a program to be compiled and uploaded without the need for the .ino file to be in a directory of the same name. That could be useful for your situation.

It should be easy to dismember my code and use the parts that interest you.

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

nivpgir

thanks, this is pretty much what I wanted.

I have a few questions though:

1) the 'subprocess.call()' command should work the same on windows? should the command given to it be different?

2) on windows, the path to the arduinoIDE should be the path to the actual executable that runs the ide?

thanks for you response.

Robin2

#3
Feb 27, 2016, 10:14 am Last Edit: Feb 27, 2016, 10:17 am by Robin2
I am not sufficiently familiar with Windows to answer your questions.

Python seems to be well constructed to be portable.

/mnt/sda4/Programs/arduino-1.5.6-r2/arduino is a shell script on my Linux laptop. It came as part of the IDE. The Arduino IDE is just a Java program. This is the content of the script - it is regular Java stuff.
Code: [Select]
#!/bin/sh

CURDIR=`pwd`
APPDIR="$(dirname -- "$(readlink -f -- "${0}")" )"

cd "$APPDIR"

for LIB in \
    java/lib/rt.jar \
    java/lib/tools.jar \
    lib/*.jar \
    ;
do
    CLASSPATH="${CLASSPATH}:${LIB}"
done
export CLASSPATH

LD_LIBRARY_PATH=`pwd`/lib${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}
export LD_LIBRARY_PATH

export PATH="${APPDIR}/java/bin:${PATH}"

java -Dswing.defaultlaf=com.sun.java.swing.plaf.gtk.GTKLookAndFeel processing.app.Base --curdir $CURDIR "$@"



What happened when you tried the ideas you have suggested?

Is the JVM installed on your PC independently of the Arduino IDE?
If not it may simplify things to install it and then download the IDE without using the Windows installer.

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

amydh

Hi Robin,

Is there a way of running the code without using Geany? ie solely using python?

Thanks!

Robin2

Is there a way of running the code without using Geany? ie solely using python?
Yes. You can run the Python program from the command line. Just pass it the name of the .ino file

You seem to have started another Thread about the same problem. Don't ask the same question in 2 places. That just wastes everyone's time.

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

Go Up