A web link sending a command to a Ruby daemon that instructs an Arduino to move a servo

Posted by .

I’m working on a remote camera shutter project, which is a Raspberry Pi with a webcam and when it detects motion it clicks the camera shutter button. The button is pressed by a small servo that is connected to an Arduino nano. The Raspberry Pi and Arduino communicate over serial.

As well as automatically taking photos based on motion I would also like to be able to take a photo by clicking a button on my website (which gives me a remote shutter button on my iphone). So I’m just going to go through the software involved in the communication between a web page, a Ruby daemon and the Arduino (over a serial connection).

The flow goes something like this:

HTML link > local Ruby daemon > Arduino over USB/serial to move the servo

When you:

press this button

a bit of jQuery sends a request to http://192.168.1.101:2000. Which is my local machine so the link won’t actually do anything for anyone outside of my local network but hopefully this code will be useful for someone looking to bridge the gap between a webpage which could be hosted online or a web based interface you have running locally where you need to pass commands to a Ruby script running on a Raspberry Pi or just on your local machine. You can change this script to use any web accessible IP address providing the route is there but for my needs on this project the local network is all I need. I can just go to this webpage on my Iphone since it’s on the same wifi network and by clicking on that button activate the remote shutter.

Local Ruby daemon

I have forked the Arduino Ruby gem project over on github to add an addition method for controlling servos. It’s only the development branch that has the servo code. There are two example scripts, one is example_servo.rb which just moves the servo through some of it’s 180 degree range pausing and a few key places, the other is example_servo_daemon.rb which once executed will run until you cancel it (ctrl+c) waiting for a request to move the servo.

The servo daemon uses the Mongrel library for the web/app server element so you will need to install that gem to get the daemon to work. I will quickly walk you through the setup and usage of this Ruby servo daemon:

 # get the arduino gem and example scripts. You can use git or you could just download a zip file from github
 # using git
 git clone https://github.com/WPsites/arduino.git
 cd arduino
 git checkout development
 
 #  Build and install the arduino gem
 gem build arduino.gemspec
 gem install arduino-0.4.gem
 
 # you need the Mongrel gem to run the daemon
 gem install mongrel
 
 # if you have a problem installing the Mongrel gem try this command instead
 gem install mongrel --pre

If you have you’re servo control wire in pin 9 no pins need to be edited, if you have you’re servo plugged into a different output then you will need to edit example_servo_daemon.rb to change the pin numbers.

You will probably need to edit line 18 of example_servo_daemon.rb to reflect your Arduino’s serial connection, “/dev/tty.usbserial-A100MU2V” is what is used in the demo.

Then you will also need to upload the arduino.pde sketch to your Arduino so that it knows to wait for commands and what to do once it receives a command over serial.

Once you execute the example_servo_daemon.rb script it will output a couple of setup messages and then just hang while it awaits commands. The default setup binds to all IP addresses assigned to your machine on port 2000 so to tell your servo to move you could issue a command like this in your terminal:

wget http://127.0.0.1:2000/take_photo

Or you could just go to that address in your browser!

————————————————————-

Now I just need to put the above Ruby code on my Raspberry Pi so that the Pi can be the brains of this system rather than my local machine. I’m going to connect the Arduino to the Raspberry Pi using USB, plug a web cam into the Pi so that when motion is detected the Pi can just issue the take photo command as above.

Comments are closed.