Skip to content

Instantly share code, notes, and snippets.

@vickash
Last active December 21, 2015 13:30
Show Gist options
  • Save vickash/6313443 to your computer and use it in GitHub Desktop.
Save vickash/6313443 to your computer and use it in GitHub Desktop.
Simple message queue setup with dino.

Install rabbitmq first: http://www.rabbitmq.com/download.html Or brew install rabbitmq on Mac

Start it up with rabbitmq-server

Install gems to talk to the message queue:

gem install bunny
gem install amqp

Start the daemon, which doesn't actually daemonize itself....

ruby daemon.rb

Run the test:

ruby test.rb
#
# Hook up to the board.
#
require 'dino'
connection = Dino::TxRx::TCP.new("192.168.0.149")
$board = Dino::Board.new(connection)
#
# Set up the RGB led and my desk lamp.
#
$lamp = Dino::Components::Relay.new(board: $board, pin: 8)
$lamp.off
$rgbled = Dino::Components::RgbLed.new(board: $board, pins: {red: '9', green: '10', blue: '11'})
$rgbled.off
#
# Handle incoming messages.
#
def handle_incoming(message)
component, data = message.split("::")
if component == "rgbled"
r, g, b = data.split(",")
$rgbled.color = [r,g,b]
end
if component == "lamp"
$lamp.on if data == "on"
$lamp.off if data == "off"
end
end
#
# Set up EventMachine to listen for incoming message.
#
$incoming_queue_name = "dino.demo.incoming"
require 'amqp'
EventMachine.run do
conn = AMQP.connect(host: "127.0.0.1")
channel = AMQP::Channel.new(conn)
queue = channel.queue($incoming_queue_name)
exchange = channel.direct("")
puts "Connected to AMQP broker. Running amqp version #{AMQP::VERSION}."
# Look for messages on the queue and handle them.
queue.subscribe do |message|
handle_incoming message
end
puts "Listening for messages on queue: #{$incoming_queue_name} and processing..."
end
sleep
require 'bunny'
bunny = Bunny.new.start
channel = bunny.create_channel
dino_queue = channel.queue "dino.demo.incoming"
# make the rgbled white
dino_queue.publish "rgbled::255,255,255"
# turn my lamp on
dino_queue.publish "lamp::on"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment