Skip to content

MDSilber/BreakfastSerial

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

BreakfastSerial

A Firmata based framework for interacting with Arduinos over serial.

Arduino Setup

In order to use BreakfastSerial, you need to have an arduino running the standard firmata.

  1. Download the Arduino IDE from the arduino website
  1. Plug in your Arduino or Arduino compatible microcontroller via USB
  2. Open the Arduino IDE, select: File > Examples > Firmata > StandardFirmata
  3. Click the "Upload" button.

Installation

Using PyPi

pip install BreakfastSerial

From Source

git clone git://github.com/theycallmeswift/BreakfastSerial.git && cd BreakfastSerial

python setup.py install

Getting Started

The BreakfastSerial library provides a simple abstraction for a number of common components. Make sure your arduino is plugged in and is running firmata.

Arduino

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

Blink an LED

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()
sleep(2)

You can also use the blink method and pass it a number of milliseconds to automate the blinking process

millis = 200
led.blink(millis)

Push a button

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 changed
  • down - The button is pressed
  • up - The button is not being pressed
  • hold - The button was held for at least 1 second

Read a sensor

The Sensor component let's 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.

Moar!

There are a bunch of examples in the examples/ folder. Additional components will be added over time, so be sure to check back regularly.

About

A Firmata based framework for interacting with Arduinos over serial.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Python 100.0%