endless fun with arduino and eventmachine

53
Hi!

Upload: bodo-tasche

Post on 29-Jan-2018

4.909 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Endless fun with Arduino and Eventmachine

Hi!

Page 2: Endless fun with Arduino and Eventmachine

Bodo Tasche@bitboxer

Page 3: Endless fun with Arduino and Eventmachine
Page 4: Endless fun with Arduino and Eventmachine

Endless fun with Arduino and Eventmachine

Page 5: Endless fun with Arduino and Eventmachine

Eventmachine

!!!

!!

Page 6: Endless fun with Arduino and Eventmachine

Arduino?

?

? ?!

Page 7: Endless fun with Arduino and Eventmachine

Source: Wikipedia

Page 8: Endless fun with Arduino and Eventmachine

Two Parts

Page 9: Endless fun with Arduino and Eventmachine

1. Hardware

Page 10: Endless fun with Arduino and Eventmachine
Page 11: Endless fun with Arduino and Eventmachine

Arduino Uno

Page 12: Endless fun with Arduino and Eventmachine

Microcontroller

Operating Voltage

Input Voltage (recommended)

Input Voltage (limits)

Digital I/O Pins

Analog Input Pins

DC Current per I/O Pin

DC Current for 3.3V Pin

Flash Memory

SRAM

EEPROM

Clock Speed

ATmega328

5V

7-12V

6-20V

14

6

40 mA

50 mA

32 KB

2 KB

1 KB

16 MHz

Page 13: Endless fun with Arduino and Eventmachine

2. So!ware

Page 14: Endless fun with Arduino and Eventmachine
Page 15: Endless fun with Arduino and Eventmachine

http://arduino.cc

Page 16: Endless fun with Arduino and Eventmachine

Christopher Pike, isthis you?

Page 17: Endless fun with Arduino and Eventmachine
Page 18: Endless fun with Arduino and Eventmachine

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);}

Page 19: Endless fun with Arduino and Eventmachine

Have you brought any fruits or vegetables to the mars?

Two weeks.

Page 20: Endless fun with Arduino and Eventmachine

const int BAUD_RATE = 9600;

void setup() { Serial.begin(BAUD_RATE);}

void loop() { Serial.println(“Hello, world!”); delay(1000);}

Page 21: Endless fun with Arduino and Eventmachine

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

Page 23: Endless fun with Arduino and Eventmachine

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

Page 24: Endless fun with Arduino and Eventmachine

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); } } }}

Page 25: Endless fun with Arduino and Eventmachine

Eventmachine+

Arduino

Page 26: Endless fun with Arduino and Eventmachine

It‘s all about the volume!

Page 27: Endless fun with Arduino and Eventmachine

Source: Wikipedia

Page 28: Endless fun with Arduino and Eventmachine

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); }

}

Page 29: Endless fun with Arduino and Eventmachine

Source: Wikipedia

Page 30: Endless fun with Arduino and Eventmachine

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}"` endend

sp.close

Page 31: Endless fun with Arduino and Eventmachine

Talk to the hand

Page 32: Endless fun with Arduino and Eventmachine

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, DataBufferend

sp.close

Page 33: Endless fun with Arduino and Eventmachine

EventMachine::defer

Page 34: Endless fun with Arduino and Eventmachine

Don’t write a deferred operation that will block forever [...] We

might put in a timer to detect this

Page 35: Endless fun with Arduino and Eventmachine

$eventmachine_library = :pure_ruby # need to force pure rubyrequire '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 endend

https://github.com/eventmachine/eventmachine/wiki/Code-Snippets

Page 36: Endless fun with Arduino and Eventmachine

If you build it...

Page 37: Endless fun with Arduino and Eventmachine

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

Last.fm

Page 39: Endless fun with Arduino and Eventmachine

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

Page 40: Endless fun with Arduino and Eventmachine

Weather station

Page 41: Endless fun with Arduino and Eventmachine

Infrared Sender/Receiver

Page 42: Endless fun with Arduino and Eventmachine

Improve your „green thumb“

Page 43: Endless fun with Arduino and Eventmachine

CAN-bus

Connect your Arduino with your car

Page 44: Endless fun with Arduino and Eventmachine

Gong-o-bot

Page 45: Endless fun with Arduino and Eventmachine

Gong-o-bot

Page 46: Endless fun with Arduino and Eventmachine
Page 49: Endless fun with Arduino and Eventmachine

Find a hackerspace!

Page 50: Endless fun with Arduino and Eventmachine

delicious.com/bitboxer/arduino

Page 51: Endless fun with Arduino and Eventmachine

Don‘t be evil

Source: Wikipedia

Page 52: Endless fun with Arduino and Eventmachine

Be good!

Source: Wikipedia

Page 53: Endless fun with Arduino and Eventmachine

"ank you!