A Firmata based framework for interacting with Arduinos over serial.
In order to use BreakfastSerial, you need to have an arduino running the standard firmata.
- Download the Arduino IDE from the arduino website
- OSX
- Linux 32 bit
- Linux 64 bit
- Windows support coming soon.
- Plug in your Arduino or Arduino compatible microcontroller via USB
- Open the Arduino IDE, select: File > Examples > Firmata > StandardFirmata
- Click the "Upload" button.
Using PyPi
pip install BreakfastSerial
git clone git://github.com/theycallmeswift/BreakfastSerial.git && cd BreakfastSerial
python setup.py install
The BreakfastSerial library provides a simple abstraction for a number of common components. Make sure your arduino is plugged in and is running firmata.
If you create a Arduino
object without any parameters, it will attempt to auto discover
the serial port that the Arduino is attached to and connect automatically. Optionally,
you can supply the path to a serial port (Ex. "/dev/tty.usbmodem4111"
).
from BreakfastSerial import Arduino
board = Arduino() # This will autodiscover the device
To use the led object, import Led from BreakfastSerial
. The constructor takes an
Arduino object and a pin number as its arguments.
from BreakfastSerial import Arduino, Led
from time import sleep
board = Arduino()
pin = 13
led = Led(board, pin)
led.on()
sleep(2)
led.off()
You can also use the blink
method and pass it a number of milliseconds to automate the blinking process
millis = 200
led.blink(millis)
The Button
component has a number of helper methods that make it easy to work with buttons.
The constructor takes an Arduino object and a pin number as its arguments.
from BreakfastSerial import Button, Arduino
board = Arduino()
button = Button(board, 8)
def down_cb():
print "button down"
def up_cb():
print "button up"
def hold_cb():
print "button held"
button.down(down_cb)
button.up(up_cb)
button.hold(hold_cb)
The down
and up
functions are just nice wrappers around the underlying event emitter. The Button
component emits the following events:
change
- The button value changeddown
- The button is pressedup
- The button is not being pressedhold
- The button was held for at least 1 second
The RGBLed
component lets us change the colors of an RGB Led without having to
interact with the three underlying leds.
from BreakfastSerial import Arduino, RGBLed
from time import sleep
board = Arduino()
led = RGBLed(board, { "red": 10, "green": 9, "blue": 8 })
led.red()
sleep(1)
led.green()
sleep(1)
led.blue()
sleep(1)
led.yellow()
sleep(1)
led.cyan()
sleep(1)
led.purple()
sleep(1)
led.white()
sleep(1)
led.off()
The Sensor
component lets us read in data from a sensor (analog or digital). The constructor takes in
an Arduino object and a pin number.
from BreakfastSerial import Arduino, Sensor
from time import sleep
board = Arduino()
sensor = Sensor(board, "A0")
for i in range(40):
print sensor.value
sleep(0.5)
The value
property of a Sensor
object is the value of the underlying pin.
The Servo
component let's us control a servo. The constructor takes in
an Arduino object and a pin number.
from BreakfastSerial import Arduino, Servo
from time import sleep
board = Arduino()
servo = Servo(board, "10")
servo.set_position(180)
sleep(2)
servo.move(-135)
sleep(2)
servo.center()
sleep(2)
servo.reset()
The value
property of a Servo
object is the current position of the servo in degrees
There are a bunch of examples in the examples/
folder. Additional components
will be added over time, so be sure to check back regularly.