Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Endless fun with Arduino and Eventmachine

Endless fun with Arduino and Eventmachine

After seven years of working as a java developer, Bodo fall in love with ruby and moved to cologne to work as a full time ruby developer. He started tinkering with hardware as a teenager with his C-64, his dad created the robots and he wrote the software that made them move. Besides coding Bodo loves diving, traveling and biking.

In his talk he will show how to connect an arduino with ruby using eventmachine. He will also show potential use cases and projects that can be created using this technique. Most of the projects can be finished on one weekend.

Bodo Tasche

July 03, 2014
Tweet

More Decks by Bodo Tasche

Other Decks in Programming

Transcript

  1. Hi!

  2. 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
  3. 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);! }
  4. const int BAUD_RATE = 9600;! ! void setup() {! Serial.begin(BAUD_RATE);!

    }! ! void loop() {! Serial.println(“Hello, world!”);! delay(1000);! }
  5. void setup() {! Serial.begin(9600);! for (int thisPin = 2; thisPin

    < 7; thisPin++) {! pinMode(thisPin, OUTPUT);! }! }
  6. 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);! }! }! }! }
  7. 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);! }! ! }
  8. 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!
  9. 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!
  10. $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