Wednesday 30 April 2014

Firmata - Raspberry Pi (part 2a, access from python)

Introdution


We have prepared earlier Arduino with an installed library Firmata (post how to prepare Arudino with firmata - link). Now we connect from the Raspberry to the Arduino with Firmata. We have the possibility to connect from most programming languages.



We need

  • Raspberry Pi with installed ArchLinux
  • Arduino(Mega) with installed Firmata library (Firmata - Arduino)
The ArduinoMega connected to the Raspberry Pi



Installation Python 2.7


refresh of all package list:
# pacman -Syy
install python:
# pacman -Sy python2 python2-pip python2-virtualenv
test installation:
# python2 --version
we should see something like:
Python 2.7.6
if all is ok, create python enviroment:
# mkdir robot   
# cd robot/
# virtualenv2 --no-site-packages python_firmata_test
# cd python_firmata_test/
# source bin/activate

install pySerial:
# pip2 install pyserial

install pyFirmata:
# pip2 install pyfirmata


Now we can write simple application that controls LED13 on Arduino, full code of this app is below (file binkingLed13.py):
from pyfirmata import ArduinoMega, time
board = ArduinoMega('/dev/ttyUSB0')

while True:
   board.digital[13].write(1)
   time.sleep(1)
   board.digital[13].write(0)
   time.sleep(1)

We can run this command:
# python binkingLed13.py
After a few seconds, the LED should start blinking at a frequency of 1 Hz. To exit, press ctrl + c
  
Localization LED13

If you have problems with usb port you can list usb ports:
# ls /dev/ttyUSB*



links:
https://www.python.org/
https://pypi.python.org/pypi/pyserial/2.7 
https://pypi.python.org/pypi/pyFirmata/0.9.5

No comments:

Post a Comment

Note: only a member of this blog may post a comment.