I'm installing pyFirmata on the Pi to communicate with the StandardFirmata sketch on the Gertduino.


I had already installed the pySerial library a couple of blog posts ago in order to enable the program-to-program sample code that I tested. So I can skip that step now and go directly to installing pyFirmata.


But I do need to install git in order to git pyFirmata. Some folks (for example, the author of the MagPi article referenced previously) install pyFirmata by using mercurial to access the pyFirmata repository, rather than git, however I'm going to use git:


sudo apt-get install git

git clone https://github.com/tino/pyFirmata.git

cd pyFirmata

sudo python setup.py install

OK, that was easy enough.


I'd like to do a simple test to see if I can set something digital on the Gertduino board from a script running on the Pi. The Raspberry Pi Cookbook has such a test, which I will modify slightly to use the serial port /dev/ttyAMA0 rather than the USB serial port. I will also set several of the Gertduino LEDs on instead of only one.


First, though, I need to bring up the Arduino IDE and then upload StandardFirmata to the Gertduino:




Now, I'll bring up the Python console and issue some interactive commands:




I now have all six of the Gertduino's blue LEDs brightly lit up.


This is fun. But using the Python console to send Firmata commands is also a bit tedious. So I'll write a Python script, similar to the example from the Raspberry Pi Cookbook, save the file as ledblink.py, and run it from the command line:


import pyfirmata
import time

board = pyfirmata.Arduino('/dev/ttyAMA0')
led_pin13 = board.get_pin('d:13:o')
led_pin9 = board.get_pin('d:9:o')
led_pin10 = board.get_pin('d:10:o')
led_pin3 = board.get_pin('d:3:o')
led_pin5 = board.get_pin('d:5:o')
led_pin6 = board.get_pin('d:6:o')

while True:



It's a fairly pedestrian program as Python scripts go, but it does the trick and alternates the blinking of the Gertduino's LEDs, three of them on and three of them off, and then vice-versa, every half second.


It took several seconds (perhaps eight or ten of them) for the script to load pyFirmata and start controlling the Gertduino over the serial port. If I had not been patient, I would have given up in despair. OK, in the interest of full disclosure, the first time that I tried it, I gave up after a few seconds of (apparently) nothing happening and hit Ctrl-C to terminate the script. Luckily, instead of smashing my fist through the monitor screen in disgust, or resolving to take up a safer hobby such as stamp collecting or sword swallowing, I gave it another try and waited it out.


Note that I needed to execute the script via sudo to get it to work (because it's accessing the GPIO port to use the serial Tx and Rx pins):


sudo python ledblink.py


Also note that I needed to call pyfirmata in lower case instead of pyFirmata in mixed case (because the module name is actually pyfirmata).


Here it is, blinking up a storm:

Remember, I have not loaded an alternating blink sketch onto the Gertduino, rather the Gertduino is running StandardFirmata and the Python script is sending digital write commands over the serial port in real time to control the state of each of the LEDs.


So it's definitely, positively, certifiably alive.


This is only the beginning, though - there's more to come (soon).