Slide 1

Slide 1 text

Hi!

Slide 2

Slide 2 text

Bodo Tasche @bitboxer

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

Endless fun with Arduino and Eventmachine

Slide 5

Slide 5 text

Eventmachine

Slide 6

Slide 6 text

Eventmachine ! ! ! ! !

Slide 7

Slide 7 text

Arduino

Slide 8

Slide 8 text

Arduino ? ? ? ?!

Slide 9

Slide 9 text

Source: Wikipedia

Slide 10

Slide 10 text

Two Parts

Slide 11

Slide 11 text

1. Hardware

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

Arduino Uno

Slide 14

Slide 14 text

Microcontroller ATmega328 Operating Voltage 5V Input Voltage (recommended) 7-12V Input Voltage (limits) 6-20V Digital I/O Pins 14 Analog Input Pins 6 DC Current per I/O Pin 40 mA DC Current for 3.3V Pin 50 mA Flash Memory 32 KB SRAM 2 KB EEPROM 1 KB Clock Speed 16 MHz

Slide 15

Slide 15 text

2. Software

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

http://arduino.cc

Slide 18

Slide 18 text

Christopher Pike, is this you?

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

const int PIN_LED = 13;! ! void setup() {! pinMode(PIN_LED, OUTPUT);! }! ! void loop() {! digitalWrite(PIN_LED, HIGH);! delay(500);! digitalWrite(PIN_LED, LOW);! delay(500);! }

Slide 21

Slide 21 text

Have you brought any fruits or vegetables to the mars? ! Two weeks.

Slide 22

Slide 22 text

const int BAUD_RATE = 9600;! ! void setup() {! Serial.begin(BAUD_RATE);! }! ! void loop() {! Serial.println(“Hello, world!”);! delay(1000);! }

Slide 23

Slide 23 text

Well, well, Private Joker, I don't believe I heard you correctly!

Slide 24

Slide 24 text

http://arduino.cc/en/Tutorial/SwitchCase2

Slide 25

Slide 25 text

void setup() {! Serial.begin(9600);! for (int thisPin = 2; thisPin < 7; thisPin++) {! pinMode(thisPin, OUTPUT);! }! }

Slide 26

Slide 26 text

void loop() {! if (Serial.available() > 0) {! int inByte = Serial.read();! switch (inByte) {! case 'a': digitalWrite(2, HIGH);! break;! case 'b': digitalWrite(3, HIGH);! break;! case 'c': digitalWrite(4, HIGH);! break;! case 'd': digitalWrite(5, HIGH);! break;! case 'e': digitalWrite(6, HIGH);! break;! default:! for (int thisPin = 2; thisPin < 7; thisPin++) {! digitalWrite(thisPin, LOW);! }! }! }! }

Slide 27

Slide 27 text

Eventmachine + Arduino

Slide 28

Slide 28 text

It‘s all about the volume!

Slide 29

Slide 29 text

Source: Wikipedia

Slide 30

Slide 30 text

const int POT_PIN = 2;! ! int val;! ! void setup() {! Serial.begin(9600);! }! ! void loop() {! int newVal = map(analogRead(POT_PIN), 0, 1023, 7, 0);! ! if (val != newVal) {! val = newVal;! Serial.println(val);! }! ! }

Slide 31

Slide 31 text

Source: Wikipedia

Slide 32

Slide 32 text

require 'serialport'! ! sp = SerialPort.new('/dev/tty.your-usb-device', 9600, 8, 1, 0)! ! loop do! line = sp.gets! if line! puts "New volume : #{line}"! `osascript -e "set volume #{line}"`! end! end! ! sp.close!

Slide 33

Slide 33 text

Talk to the hand

Slide 34

Slide 34 text

require 'rubygems'! require 'serialport'! require 'eventmachine'! ! $sp = SerialPort.new('/dev/tty.your-usb-device', 9600, 8, 1, 0)! ! class DataBuffer < EM::Protocols::LineAndTextProtocol! ! def initialize! puts "hello stranger..."! end! ! def receive_data(data)! EventMachine::defer do! $sp.puts data.strip! send_data($sp.gets)! end ! end! ! end! ! EventMachine::run do! EventMachine::start_server "127.0.0.1", 8081, DataBuffer! end! ! sp.close!

Slide 35

Slide 35 text

EventMachine::defer

Slide 36

Slide 36 text

Don’t write a deferred operation that will block forever [...] We might put in a timer to detect this

Slide 37

Slide 37 text

$eventmachine_library = :pure_ruby # need to force pure ruby! require 'eventmachine'! gem_original_require 'serialport'! require 'smsrelay/gsmpdu'! ! module EventMachine! class EvmaSerialPort < StreamObject! def self.open(dev, baud, databits, stopbits, parity)! io = SerialPort.new(dev, baud, databits, stopbits, parity)! return(EvmaSerialPort.new(io))! end! ! def initialize(io)! super! end! ! ##! # Monkeypatched version of EventMachine::StreamObject#eventable_read so! # that EOFErrors from the SerialPort object (which the ruby-serialport! # library uses to signal the fact that there is no more data available! # for reading) do not cause the connection to unbind.! def eventable_read! @last_activity = Reactor.instance.current_loop_time! begin! if io.respond_to?(:read_nonblock)! 10.times {! data = io.read_nonblock(4096)! EventMachine::event_callback uuid, ConnectionData, data! }! else! data = io.sysread(4096)! EventMachine::event_callback uuid, ConnectionData, data! end! rescue Errno::EAGAIN, Errno::EWOULDBLOCK, EOFError! # no-op! rescue Errno::ECONNRESET, Errno::ECONNREFUSED! @close_scheduled = true! EventMachine::event_callback uuid, ConnectionUnbound, nil! end! end! end! ! class << self! def connect_serial(dev, baud, databits, stopbits, parity)! EvmaSerialPort.open(dev, baud, databits, stopbits, parity).uuid! end! end! ! def EventMachine::open_serial(dev, baud, databits, stopbits, parity,! handler=nil)! klass = if (handler and handler.is_a?(Class))! handler! else! Class.new( Connection ) {handler and include handler}! end! s = connect_serial(dev, baud, databits, stopbits, parity)! c = klass.new s! @conns[s] = c! block_given? and yield c! c! end! ! class Connection! # This seems to be necessary with EventMachine 0.12.x! def associate_callback_target(sig)! return(nil)! end! end! end! https://github.com/eventmachine/eventmachine/wiki/Code-Snippets

Slide 38

Slide 38 text

If you build it...

Slide 39

Slide 39 text

http://blog.last.fm/2008/08/01/quality-control Last.fm

Slide 40

Slide 40 text

http://urbanhonking.com/ideasfordozens/2010/05/19/the_github_stoplight/ github

Slide 41

Slide 41 text

by-nc-nd by seanb.murphy http://www.flickr.com/photos/greenstorm/2425378283

Slide 42

Slide 42 text

Weather station

Slide 43

Slide 43 text

Infrared Sender/Receiver

Slide 44

Slide 44 text

Improve your  „green thumb“

Slide 45

Slide 45 text

CAN-bus

Slide 46

Slide 46 text

Gong-o-bot

Slide 47

Slide 47 text

Gong-o-bot

Slide 48

Slide 48 text

No content

Slide 49

Slide 49 text

fritzing.org/projects/

Slide 50

Slide 50 text

fritzing.org/shop/starter-kit/

Slide 51

Slide 51 text

Find a hackerspace!

Slide 52

Slide 52 text

delicious.com/bitboxer/arduino

Slide 53

Slide 53 text

Don‘t be evil Source: Wikipedia

Slide 54

Slide 54 text

Be good! Source: Wikipedia

Slide 55

Slide 55 text

Thank you!