0% found this document useful (0 votes)
420 views9 pages

Pyfirmata PDF

The document discusses using Pyfirmata to interact with Arduino boards from Python. It provides examples of blinking an LED, reading analog sensor values, controlling a fan or servo with digital pins, and representing Arduino data over a TCP connection. Pyfirmata allows easy testing of code directly from the Python console and has limitations but covers most typical uses.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
420 views9 pages

Pyfirmata PDF

The document discusses using Pyfirmata to interact with Arduino boards from Python. It provides examples of blinking an LED, reading analog sensor values, controlling a fan or servo with digital pins, and representing Arduino data over a TCP connection. Pyfirmata allows easy testing of code directly from the Python console and has limitations but covers most typical uses.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Pyfirmata !

"mo interactuar con ar#uino usan#o $%thon % &irmata

Hackmeeting 2011 http://lagunak.gisa-elkartea.org/projects/krnl http://lagunak.gisa-elkartea.org/svn/krnl/recetario/ irmata GISA Elkartea Ales Zabala

Puesta a punto 'olcar en Ar#uino el sketch (tan#ar#&irmata. (tan#ar#&irmata )nstalar p% irmata: irmata
$ hg clone https://bitbucket.org/tino/pyfirmata $ cd pyfirmata; python setup.py install

(i necesitas servos:

$ svn export https://lagunak/svn/krnl/pyfirmata_servo/servo.patch . $ patch -p0 servo.patch $ python setup.py install

Hackmeeting 2011 http://lagunak.gisa-elkartea.org/projects/krnl http://lagunak.gisa-elkartea.org/svn/krnl/recetario/ irmata GISA Elkartea Ales Zabala

Hola mundo (blink)

*ste ejemplo hace parpa#ear el le# enchu a#o al pin #igital 1+ A#emas #e digital tambi,n ha% analog- % a#em.s #e write tambi,n ha% read :-/

import time from pyfirmata import !rduino board " !rduino#$/dev/tty%&'0$( )hile *: board.digital+*,-.)rite#*( time.sleep#*( board.digital+*,-.)rite#0( time.sleep#*(

Hackmeeting 2011 http://lagunak.gisa-elkartea.org/projects/krnl http://lagunak.gisa-elkartea.org/svn/krnl/recetario/ irmata GISA Elkartea Ales Zabala

Lecturas

$ara evitar 0ue el Ar#uino nos est, man#an#o cont1nuamente #atos en los pines #e lectura- se utili2a un )terator:
it " util..terator#board( it.start#( board.analog+0-.enable_reporting#( board.analog+0-.read#(

*l )terator es un threa#- por lo 0ue ha% 0ue matarlo al salir. 3o m.s acil es romper la cone4i"n con el puerso serie- el threa# se suici#a el solito.
board.exit#(

Hackmeeting 2011 http://lagunak.gisa-elkartea.org/projects/krnl http://lagunak.gisa-elkartea.org/svn/krnl/recetario/ irmata GISA Elkartea Ales Zabala

Ventilador (Digital)

pin.read() == 0..1 pin.write([0,1])

/0 /1 /2 /3 /4 /6 ,0 ,* ,/ ,, ,0

def start#self(: )hile self.loop: ne)value " self.board.analog+self.pot_pin-.read#( print ne)value if self.value 5" ne)value: if ne)value 7 0.1: self.board.digital+self.motor_pin-.)rite#*( else: self.board.digital+self.motor_pin-.)rite#0( self.value " ne)value time.sleep#0.1(
GISA Elkartea Ales Zabala

Hackmeeting 2011 http://lagunak.gisa-elkartea.org/projects/krnl http://lagunak.gisa-elkartea.org/svn/krnl/recetario/ irmata

PC Speaker (PWM)

pin.mode = PWM pin.write(0..1)

2 *3 /2 /3 /4 /6 ,0 ,* ,/ ,,

def __init__#self8 port8 pot_pin8 motor_pin(: +...self.board.digital+self.motor_pin-.mode " 9:; +...def start#self(: )hile self.loop: ne)value " self.board.analog+self.pot_pin-.read#( print ne)value if self.value 5" ne)value: self.board.digital+self.motor_pin-.)rite#ne)value( self.value " ne)value time.sleep#0.1(
GISA Elkartea Ales Zabala

Hackmeeting 2011 http://lagunak.gisa-elkartea.org/projects/krnl http://lagunak.gisa-elkartea.org/svn/krnl/recetario/ irmata

Ser os

pin.mode = SERVO pin.write(angu o)

2 *3 /2 /3 /4 /6 ,0 ,* ,/ ,, ,0

Hackmeeting 2011

def __init__#self8 port8 pot_pin8 motor_pin(: +...self.board.digital+self.motor_pin-.mode " &<=>? +...def start#self(: )hile self.loop: ne)value " self.board.analog+self.pot_pin-.read#( if #ne)value is not @one( and #self.value 5" ne)value(: angle " int#*40 A ne)value( printBCs "7 CsB C #ne)value8 angle( self.board.digital+self.motor_pin-.)rite#angle( self.value " ne)value time.sleep#0.1(
GISA Elkartea Ales Zabala

http://lagunak.gisa-elkartea.org/projects/krnl http://lagunak.gisa-elkartea.org/svn/krnl/recetario/ irmata

!tro e"emplo

5epresentacion #el esta#o #el Ar#uino con una 67) en $%89 :e igual manera po#r1amos usar sockets- la carga #el or#ena#or- consultas http...

Hackmeeting 2011 http://lagunak.gisa-elkartea.org/projects/krnl http://lagunak.gisa-elkartea.org/svn/krnl/recetario/ irmata GISA Elkartea Ales Zabala

Comentarios

*s mu% .cil #e usar. !on la consola #e p%thon po#emos hacer pruebas #irectamente en Ar#uino. 9ambi,n e4iste p%#uino- pero parece 0ue es un pro%ecto aban#ona#o. *n p% irmata ha% un #esarrolla#or ;un tanto espor.#ico/- % una persona 0ue env1a parches ;a#em.s #el m1o :-/. 9iene sus limitaciones ;:H911/. 9o#av1a le altan cosas para implementar completamente irmataaun0ue lo m.s t1pico %a est..
GISA Elkartea Ales Zabala

Hackmeeting 2011 http://lagunak.gisa-elkartea.org/projects/krnl http://lagunak.gisa-elkartea.org/svn/krnl/recetario/ irmata

You might also like