Phprpc + Ruby + Arduino = led turn-on (?) Well, I know this is a very boring sample.
For phprpc and Arduino, Google
Before reading this demo, please install the SerialPort mentioned in another article.
With another gem
KIT: phprpc
In this example, I established a server through phprpc and called some methods through rpc to control the USB serial port.
In this demo, we must establish the server and client.
The following is the source code:
Transfer internal content to the clipboard
Generation:
# Server
#!/usr/bin/env ruby
require 'rubygems'
require 'serialport'
require 'phprpc'
class Cloud
def initialize
@@sp ||= SerialPort.new("/dev/cu.usb
serial-A600bMiv", {:baudrate => 9600})
end
def write(color)
@@sp.write("##{color}")
end
def close
@@sp.close
end
end
def randomColor
r = rand(256).to_s(16)
g = rand(256).to_s(16)
b = rand(256).to_s(16)
r = "0" + r if r.size == 1
g = "0" + g if g.size == 1
b = "0" + b if b.size == 1
color = r+b+g
cloud = Cloud.new
cloud.write(color)
"Color #{color} has been set."
end
def setColor(color)
cloud = Cloud.new
cloud.write(color)
"Color #{color} has been set."
end
def setRGBColor(r,g,b)
cloud = Cloud.new
cloud.write((r+g+b).to_s)
"Color #{(r+g+b)} has been set."
end
methods = %w"randomColor setColor setRGBColor"
server = PHPRPC::Server.new
server.debug = true
server.add(methods)
server.start
at_exit{
cloud.close
}
Transfer internal content to the clipboard
Generation:
# Client
#!/usr/bin/env ruby
require 'rubygems'
require 'phprpc'
rpc_url = "http://localhost:3000/"
client = PHPRPC::Client.new(rpc_url)
cmd = nil
while cmd = gets.chomp
case cmd.downcase
when "on"
client.setColor("FFFFFF")
puts "LEDs On"
when "off"
client.setColor("000000")
puts "LEDs Off"
when "rand", "random"
puts client.randomColor
when "setcolor"
print "Please give me a color: "
color = gets.chomp
puts client.setColor(color)
when "setrgbcolor"
puts "Please give me the color red: "
red = gets.chomp
puts "Please give me the color green: "
green = gets.chomp
puts "Please give me the color blue: "
blue = gets.chomp
puts client.setRGBColor(red, green, blue)
when "exit"
exit
end
end
Then, the method of running the server:
Reference:
Ruby server. RB mongrel
You can install mongrel or thin, but I think everyone can use mongrel.
If mongrel generates a shard, create a resource called log under the same project.