endless fun with arduino and eventmachine
TRANSCRIPT
Hi!
Bodo Tasche@bitboxer
Endless fun with Arduino and Eventmachine
Eventmachine
!!!
!!
Arduino?
?
? ?!
Source: Wikipedia
Two Parts
1. Hardware
Arduino Uno
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
2. So!ware
Christopher Pike, isthis you?
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);}
Have you brought any fruits or vegetables to the mars?
Two weeks.
const int BAUD_RATE = 9600;
void setup() { Serial.begin(BAUD_RATE);}
void loop() { Serial.println(“Hello, world!”); delay(1000);}
Well, well, Private Joker, I don't believe I heard you correctly!
http://arduino.cc/en/Tutorial/SwitchCase2
void setup() { Serial.begin(9600); for (int thisPin = 2; thisPin < 7; thisPin++) { pinMode(thisPin, OUTPUT); }}
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); } } }}
Eventmachine+
Arduino
It‘s all about the volume!
Source: Wikipedia
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); }
}
Source: Wikipedia
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
Talk to the hand
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
EventMachine::defer
Don’t write a deferred operation that will block forever [...] We
might put in a timer to detect this
$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
If you build it...
http://blog.last.fm/2008/08/01/quality-control
Last.fm
http://urbanhonking.com/ideasfordozens/2010/05/19/the_github_stoplight/
github
by-nc-nd by seanb.murphy http://www.flickr.com/photos/greenstorm/2425378283
Weather station
Infrared Sender/Receiver
Improve your „green thumb“
CAN-bus
Connect your Arduino with your car
Gong-o-bot
Gong-o-bot
fritzing.org/shop/starter-kit/
Find a hackerspace!
delicious.com/bitboxer/arduino
Don‘t be evil
Source: Wikipedia
Be good!
Source: Wikipedia
"ank you!