Showing posts with label Arduino. Show all posts
Showing posts with label Arduino. Show all posts

Friday, April 04, 2014

Running Python Scripts on Arduino Yun

Completely inexplicably, I am unable to find an example of running a python script using the Bridge library in the official Arduino Yun documentation, despite this being a main selling point of using the Yun.

What You Need to Know Beforehand

Python scripts in the www directory of your sketch get uploaded to /mnt/sd/arduino/www/YOUR_SKETCH_NAME_HERE
on the Yun.

However, when you create a process on the ATMega using the bridge library, it does not appear to run from that directory automatically, so you will need to provide a full path to your python script.

Additionally, output from stdout is captured by the bridge library and can be read on the ATMega side, but error output is NOT.  You can fix this by adding an additional parameter &2>1.  See below.

Below is an example.
Process p;
p.begin("python");
p.addParameter("/mnt/sd/arduino/www/mysketch/myscript.py");
p.addParameter("foo"); // A command line parameter for the script
p.addParameter("&2>1"); // pipe error output to stdout
p.run(); // blocking call to run python; ATMega execution halts until complete

while(p.available()>0) {
    Console.print(p.read());
}

runShellCommand() is Funky

I initially tried p.runShellCommand(), however, this did not appear to work reliably.  Some Python scripts ran, others didn't, and there was no error output available.  The only difference that I could discern between scripts that did work and that didn't was the length of the command string.  The documentation doesn't state any specific length limit.  I don't know.

How do I know if My Script Ran?

I took a simple approach; the very beginning of my Python script writes to a file.
file = open("/mnt/sd/arduino/www/mysketch/i_ran_yo.txt", "w")
file.write("code ran\n")
file.close()
Check the date on that script using "ls -l" to see if you're python script has been called.

The Arduino Documentation for available() is Wrong

p.available() returns the number of characters of script output that are available.  The documentation says if none are available, the script returns -1.  This is incorrect.  It returns 0.

Outstanding Questions

Does the blocking call to run() have any effect on interrupt handlers?

Dear Arduino Team

Please provide more complete documentation (or let us edit it somehow so we can fix it) and error logging in the bridge library.  

Thursday, March 20, 2014

Getting Started with Python on Windows

I'll be using Python on the Arduino Yun, but wanted to develop scripts on my Windows machine.  Need Python + the pip package manager (to install add on modules).
  1. Install Python (Yun uses 2.7): https://www.python.org/download/ 
  2. Install setuptools: http://www.lfd.uci.edu/~gohlke/pythonlibs/#setuptools
  3. Install pip: http://www.lfd.uci.edu/~gohlke/pythonlibs/#pip
Now you can start a shell by navigating to Start>Python 2.7>IDLE 

Install add ons by starting a Windows command shell (Windows+R and type cmd.exe and press enter).  CD to your Python install directory (C:\Python27 by default).  CD Scripts.  Then you can type something along the lines of:
pip install git+https://github.com/dgrtwo/ParsePy.git

And it will just magically install (hopefully).

Tuesday, November 08, 2011

Using an AVR ISP Mk II with the Arduino IDE

It is possible to program an AVR on an Arduino board using the AVR ISP Mk II programmer directly from the Arduino IDE. The process is faster and an Arduino bootloader is not required to be loaded on the AVR, so you can program AVRs purchased from Mouser, Digikey, etc and then transfer them to a permanent circuit board.

Simply edit boards.txt (location varies-- try searching your root arduino software folder) and add:


atmega328ii.name=Arduino Duemilanove ATmega328 AVR ISP Mk II

atmega328ii.upload.protocol=stk500
atmega328ii.upload.maximum_size=30720
atmega328ii.upload.speed=57600

atmega328ii.upload.using=avrispmkii
atmega328ii.build.mcu=atmega328p
atmega328ii.build.f_cpu=16000000L
atmega328ii.build.core=arduino

##############################################################


Then you can go to Tools>Boards> and select ATMega 328 ISP from the list. Attach your AVR ISP Mk II to the Arduino board (make sure the orientation is correct-- both of the AVR and the ISP cable). Hit program as normal. Voila!

Labels

Blog Archive

Contributors