This forum is in archive mode. You will not be able to post new content.

Author Topic: Script upload arduino sketch  (Read 705 times)

0 Members and 1 Guest are viewing this topic.

Offline techb

  • Soy Sauce Feeler
  • Global Moderator
  • King
  • *
  • Posts: 2350
  • Cookies: 345
  • Aliens do in fact wear hats.
    • View Profile
    • github
Script upload arduino sketch
« on: August 26, 2015, 03:36:31 PM »
I wrote this script to compile and upload an Arduino sketch because I don't like nor use the Arduino IDE. I can use my editor of choice now and not need to fire up the Arduino IDE just to upload my new firmware.

I do this by, at the top of my sketches making two comments that describe the board and serial port to use, and calling Arduino from the command line to compile it and upload it for me. I might add this to my editor I use for convenience if I feel spunky, but for now I just made it executable and plopped it in /usr/bin.

The script arduload.py:
Code: (python) [Select]
#! /usr/bin/python

# arduload.py
# By: Techb
# Date: Aug 26, 2015
# A script to compile and load arduino sketchs.
# This will check and make sure the tty is avail.
# In your sketch you need to have two comments at the
# the top of your file, example:
#     // --board arduino:avr:uno
#     // --port /dev/ttyUSB0
# --board, you will need the board your using.
# --port, is where your serail com is.
# Linux only, I don't plan to add Windows support
# since I don't use or have Windows. Feel free to add support.
# Tested on Arch Linux 4.1.5, Python 3.4.3

# Find more here: https://github.com/arduino/Arduino/blob/master/build/shared/manpage.adoc

import sys
import os

def usage():
    print("[??] Example: python arduload.py <file>")

def test_dev(p):
    tty = p.split("/")[-1]
    ld = os.listdir("/dev")
    if tty in ld:
        print("[+] Found %s, continuing." % p)
    else:
        print("[-] Port %s not found, try another..." % p)
        sys.exit("[!] Exiting...")

if len(sys.argv) < 2:
    usage()
    sys.exit()
else:
    sketch = sys.argv[1]

try:
    with open(sketch, "r") as fd:
        fl = [l for l in fd.readlines()]
        if "--board" in fl[0] and "--port" in fl[1]:
            board = fl[0].strip().split("--board")[-1].strip()
            print("[+] Using board %s" % board)
            # test the port
            port = fl[1].strip().split("--port")[-1].strip()
            test_dev(port)
            os.system("arduino --board %s --port %s --upload %s" % (board, port, sketch))
            print("[+] Done.")

except FileNotFoundError:
    print("[!] File not found. Try again.")
    usage()
    print("[!] Exiting...")

Example sketch:
Code: (cpp) [Select]
// --board arduino:avr:uno
// --port /dev/ttyUSB0

/*
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.

  Most Arduinos have an on-board LED you can control. On the Uno and
  Leonardo, it is attached to digital pin 13. If you're unsure what
  pin the on-board LED is connected to on your Arduino model, check
  the documentation at http://www.arduino.cc

  This example code is in the public domain.

  modified 8 May 2014
  by Scott Fitzgerald

  modified 23 Aug 2015
  by techb
  adapted to be SOS
 */

// dickbutt: -.. .. -.-. -.- -... ..- - -
// SOS: ... --- ...

// the setup function runs once when you press reset or power the board
void setup()
{
  // set baud rate
  Serial.begin(9600);
  // set pin 13 to output. Thre is an onboard LED attached.
  pinMode(13, OUTPUT);
  // set LED to low or off
  digitalWrite(13, LOW);
}

// the loop function runs over and over again forever
void loop()
{
  sos();
}

void sos()
{
  // SOS in morse code
  char txt[] = "-.. .. -.-. -.- -... ..- - -";

  // start SOS sequence
  for (int i= 0; i <= strlen(txt); i++)
  {
    if (txt[i] == '-')
    {
      digitalWrite(13, HIGH);
      delay(600);
      digitalWrite(13, LOW);
      delay(500);
    }
    else if (txt[i] == '.')
    {
      digitalWrite(13, HIGH);
      delay(100);
      digitalWrite(13, LOW);
      delay(500);
    }
    else
    {
      digitalWrite(13, LOW);
      delay(500);
    }
  }
  delay(1000);
}
>>>import this
-----------------------------

Offline x40a0e

  • Serf
  • *
  • Posts: 29
  • Cookies: 9
    • View Profile
Re: Script upload arduino sketch
« Reply #1 on: August 26, 2015, 07:16:28 PM »
Very nice. Yeah the Arduino "IDE" is terrible. I have tried using a few different solutions and ususally use Ino, which takes the same approach you use here.  It is basically a python script that overlays the compontents the IDE uses, allowing you to easily select the correct uC and what not.

 



Want to be here? Contact Ande, Factionwars or Kulverstukas on the forum or at IRC.