Pro Yearly is on sale from $80 to $50! »

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.

F3c4a818db15623a3f4fd035d781e8ee?s=128

Bodo Tasche

July 03, 2014
Tweet

Transcript

  1. Hi!

  2. Bodo Tasche @bitboxer

  3. None
  4. Endless fun with Arduino and Eventmachine

  5. Eventmachine

  6. Eventmachine ! ! ! ! !

  7. Arduino

  8. Arduino ? ? ? ?!

  9. Source: Wikipedia

  10. Two Parts

  11. 1. Hardware

  12. None
  13. Arduino Uno

  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
  15. 2. Software

  16. None
  17. http://arduino.cc

  18. Christopher Pike, is this you?

  19. None
  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);! }
  21. Have you brought any fruits or vegetables to the mars?

    ! Two weeks.
  22. const int BAUD_RATE = 9600;! ! void setup() {! Serial.begin(BAUD_RATE);!

    }! ! void loop() {! Serial.println(“Hello, world!”);! delay(1000);! }
  23. Well, well, Private Joker, I don't believe I heard you

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

  25. void setup() {! Serial.begin(9600);! for (int thisPin = 2; thisPin

    < 7; thisPin++) {! pinMode(thisPin, OUTPUT);! }! }
  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);! }! }! }! }
  27. Eventmachine + Arduino

  28. It‘s all about the volume!

  29. Source: Wikipedia

  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);! }! ! }
  31. Source: Wikipedia

  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!
  33. Talk to the hand

  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!
  35. EventMachine::defer

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

    We might put in a timer to detect this
  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
  38. If you build it...

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

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

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

  42. Weather station

  43. Infrared Sender/Receiver

  44. Improve your  „green thumb“

  45. CAN-bus

  46. Gong-o-bot

  47. Gong-o-bot

  48. None
  49. fritzing.org/projects/

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

  51. Find a hackerspace!

  52. delicious.com/bitboxer/arduino

  53. Don‘t be evil Source: Wikipedia

  54. Be good! Source: Wikipedia

  55. Thank you!