Upload Sketches directly from Geany

This was developed on Linux (Xubuntu and PuppyLinux Tahrpup 6.0 CE). It may be possible to adapt it for Windows or Mac.

I have been using Geany as my external editor for Arduino programming for some time.

Recently I have been trying out Puppy Linux - which has a lot of attractive simplicities. However I discovered that the Arduino IDE does not display properly - the menus get scrunched up and become unusable.

That got me wondering about compiling and uploading without using the Arduino IDE, which, in turn, got me wondering if I could make use of the Build menu in Geany.

I discovered that since version 1.5 there is a command line mechanism for Arduino. However the command line is long and error prone and I don't think there is any way to pass all of the Arduino specific values to it from Geany.

I have written a very short Python program which takes values from comments on the first 5 lines of an Arduino sketch and creates and executes the necessary arduino command line. The Python script is called from Geany which passes the path and filename of the sketch to it. Initial tests are positive.

This is a very simple Python program with no error checking other than to ensure that lines 1 and 5 have the required data - just to avoid havoc in case it is passed a file that is not intended for it. It expects lines 2,3 and 4 to have upload (or verify), the board name and the port name respectively. It does no checking of these values.

The Python code and a demo Arduino program are attached. The normal requirement for the .ino file to be in a directory of the same name applies.

In Geany's Build menu there is an option Set Build Commands which brings up a box where you can enter commands for different actions. I added a command called Upload and in the command box I entered

/mnt/sdb1/SGT-Prog/Arduino/GeanyPythonBuild/PythonBuildPuppy.py "%d/%f"

The first part is the path and filename for my Python script (obviously change that to whatever is appropriate for your system) and the second part produces the path and filename for the Arduino sketch.

I have added .ino files to Geany's filetype_extensions.conf and as far as I can tell it only associates my Build mods with .ino files.

Demo Arduino Sketch

// python-build-start
// upload
// arduino:avr:uno
// /dev/ttyACM0
// python-build-end


void setup() {
	Serial.begin(9600);
	Serial.println("Testino starting");
}

void loop() {
	
}

Python Program

#!/usr/bin/env python

# simple program to compile and upload Arduino code using the Arduino command line

import subprocess
import sys

#       next line must be edited to be appropriate for your PC
arduinoProg = "/mnt/sdb1/Programs/arduino-1.5.6-r2/arduino"

projectFile = sys.argv[1]

codeFile = open(projectFile, 'r')
startLine = codeFile.readline()[3:].strip()
actionLine = codeFile.readline()[3:].strip()
boardLine = codeFile.readline()[3:].strip()
portLine = codeFile.readline()[3:].strip()
endLine = codeFile.readline()[3:].strip()
codeFile.close()

#~ print projectFile
#~ print startLine
#~ print actionLine
#~ print boardLine
#~ print portLine
#~ print endLine

if (startLine != "python-build-start" or endLine != "python-build-end"):
	print "Sorry, can't process file"
	sys.exit()

arduinoCommand = arduinoProg + " --" + actionLine + " --board " + boardLine + " --port " + portLine + " " + projectFile

print "\n\n -- Arduino Command --"
print arduinoCommand

print "-- Starting %s --\n" %(actionLine)

presult = subprocess.call(arduinoCommand, shell=True)

if presult != 0:
	print "\n Failed - result code = %s --" %(presult)
else:
	print "\n-- Success --"

Have fun.

...R

Hi Robin2

your code works like a charm and made my day!

After getting frustrated with ms-"free-but-register"-license-crap while using the execellent plugin for visualstudio and then getting even more frustrated configuring the eclipse-enviroment for arduino - even while eclipse is my main ide at work - it feels like coming home to a cosy fire & hot cocoa using geany and just hit a a key to verify or upload :smiley:

I tweaked your code a little bit because i felt it more natural to have the switch for verify or upload in the command-line, so one can have for example verify on F8 and upload on F9.

In place of the action-command i moved the arduino-prog line into the build-macro.

Kind of taste-induced changes but i thought maybe somebody else would like these too:

Demo Arduino Sketch

// python-build-start
// E:\\Arduino\\arduino.exe
// arduino:avr:uno
// /dev/ttyACM0
// python-build-end


void setup() {
 Serial.begin(9600);
 Serial.println("Testino starting");
}

void loop() {
 
}

Python Program

#!/usr/bin/env python

# simple program to compile and upload Arduino code using the Arduino command line

import subprocess
import sys

actionLine = sys.argv[1]
projectFile = sys.argv[2]

codeFile = open(projectFile, 'r')
startLine = codeFile.readline()[3:].strip()
arduinoProg = codeFile.readline()[3:].strip()
boardLine = codeFile.readline()[3:].strip()
portLine = codeFile.readline()[3:].strip()
endLine = codeFile.readline()[3:].strip()
codeFile.close()

#~ print projectFile
#~ print startLine
#~ print actionLine
#~ print boardLine
#~ print portLine
#~ print endLine

if (startLine != "python-build-start" or endLine != "python-build-end"):
 print("\n Error in build-commands - can't process file")
 sys.exit()

arduinoCommand = arduinoProg + " --" + actionLine + " --board " + boardLine + " --port " + portLine + " --verbose " + projectFile

print("\n\n -- Arduino Command --")
print(arduinoCommand)

print("-- Starting %s --\n" %(actionLine))

presult = subprocess.call(arduinoCommand, shell=True)

if presult != 0:
 print("\n Failed - result code = %s --" %(presult))
else:
 print("\n-- Success --")

geany commands:

E:\Arduino\PythonBuildPuppy.py verify "%d\%f"

E:\Arduino\PythonBuildPuppy.py upload "%d\%f"

Many thanks for that code Robin2!

atvd

Thanks for your interest and your kind words. I must try that.

...R

I have developed an evolution of this project to facilitate keeping Archive copies of my Arduino code. See this Thread.

...R

I had the same problem and build a tool by my self, in my case a bash script and it works great for me with Geany.

To build is as simple as:

cd %d ; amake uno %f

and to upload it this:

cd %d ; amake uno %f arduino /dev/ttyUSB1

You can find more about it and how to configure/setup the utility in my github page of amake

As you may guessed 'uno' is the board name, 'arduino' is the kind of programmer and '/dev/ttyUSB1' is the port in which the arduino is connected; by now I has support for only the arduino boards I have at hand but I can add yours to up on request.

BTW, it keeps full compatibility and structure of the arduino projects, no need to change a thing.

Cheers to all, I would like to see comments and critics to improve it.

You seem to be doing a lot of work in your script that my Python program leaves to the Arduino system.

Most of my Python program is concerned with making archive copies of the files being used so that I can tell at a later date the exact source files (including library files) that were used to generate the code.

...R