$30 off During Our Annual Pro Sale. View Details »

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!

    View Slide

  2. Bodo Tasche
    @bitboxer

    View Slide

  3. View Slide

  4. Endless fun with Arduino and
    Eventmachine

    View Slide

  5. Eventmachine

    View Slide

  6. Eventmachine
    !
    !
    !
    !
    !

    View Slide

  7. Arduino

    View Slide

  8. Arduino
    ?
    ?
    ? ?!

    View Slide

  9. Source: Wikipedia

    View Slide

  10. Two Parts

    View Slide

  11. 1. Hardware

    View Slide

  12. View Slide

  13. Arduino Uno

    View Slide

  14. 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

    View Slide

  15. 2. Software

    View Slide

  16. View Slide

  17. http://arduino.cc

    View Slide

  18. Christopher Pike, is
    this you?

    View Slide

  19. View Slide

  20. 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);!
    }

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  26. 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);!
    }!
    }!
    }!
    }

    View Slide

  27. Eventmachine
    +
    Arduino

    View Slide

  28. It‘s all about the volume!

    View Slide

  29. Source: Wikipedia

    View Slide

  30. 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);!
    }!
    !
    }

    View Slide

  31. Source: Wikipedia

    View Slide

  32. 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!

    View Slide

  33. Talk to the hand

    View Slide

  34. 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!

    View Slide

  35. EventMachine::defer

    View Slide

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

    View Slide

  37. $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

    View Slide

  38. If you build it...

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  42. Weather station

    View Slide

  43. Infrared Sender/Receiver

    View Slide

  44. Improve your
    „green thumb“

    View Slide

  45. CAN-bus

    View Slide

  46. Gong-o-bot

    View Slide

  47. Gong-o-bot

    View Slide

  48. View Slide

  49. fritzing.org/projects/

    View Slide

  50. fritzing.org/shop/starter-kit/

    View Slide

  51. Find a hackerspace!

    View Slide

  52. delicious.com/bitboxer/arduino

    View Slide

  53. Don‘t be evil
    Source: Wikipedia

    View Slide

  54. Be good!
    Source: Wikipedia

    View Slide

  55. Thank you!

    View Slide