Phprpc + Ruby + Arduino = remote control

Source: Internet
Author: User
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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.