<< Summary

Use the Adafruit PCA9685 with a Raspberry PI Raspberry PI
in Java, to drive a servo over the Internet

We will be more or less do what's described in this document, but over the Internet.
From wherever you are on the web, you can drive the servo(s) connected to the Raspberry PI, just like if you were next to it.
We will be using node.js as a server, and WebSocket as the protocol.
This document explains how to install all we need on the Raspberry PI.

The node.js server can run on the Raspberry PI, but it is not mandatory. You can very well have the node.js server installed on its own box, and the Raspberry PI connect to it, just like the other web clients that are going to connect to it too to drive the servo(s).
In any case, we will be running a Java program that uses some Java library to connect to the node.js server, using some WebSocket API. This connection from the Java program to the node.js server can be made locally or remotely, the only difference would be the URL of the server.
Having the node.js server running on a separate box would actually relieve the Raspberry PI of the load of the server.


We want to use a web user interface to make the standard servo move.
Three major components are involved here:

The node server

Implemented in server.js.
The application logic is very simple. When a WebSocket message is received, it is re-broadcasted to all the connected clients.

The web client

Implemented by display.html and client.js.

The Java class

Implemented in adafruiti2c.samples.ws.WebSocketListener.java.
You will notice that it uses the adafruiti2c.AdafruitPCA9685 class to drive the servo, and the tools provided by the orasocket-client-12.1.3.jar library to listen to the WebSocket server.

Run it

Start the server

 Prompt> node server.js

Start the Java program

Use the file named ws.servo

 sudo java -cp $CP adafruiti2c.samples.ws.WebSocketListener
If the node server does not run on the Raspberry PI (aka localhost), use a system variable named ws.uri to provide its name in the script above:

 sudo java -cp $CP -Dws.uri=ws://othermachine:9876/ adafruiti2c.samples.ws.WebSocketListener

 Prompt> ./ws.servo

Start the web client(s)

Its URL would be http://[machine]:9876/data/display.html.
You need to run it in a browser that supports HTML5 and WebSocket (Chrome, Firefox, Safari, ... Not Internet Explorer 9).
The web user interface will look like this:

The slider values go from -90 to 90. They represent the angle the (standard) servo can take.
When the user moves the cursor of the slider, the display is updated, and the servo will follow.
All the connected clients are also updated, so the UI reflects the position of the servo.

Bonus: an Android client

Runs on an Android device. Uses the gravity sensors to detect the way the device is tilted, and sends this tilt to the WebSocket server... The Java program on the Raspberry PI is listening to the WebSocket server, and drives the servo accordingly.
The following snapshots have been taken on a smartphone.
The application was written using the Eclipse based Android Developer Tools (ADT).

Click to enlarge
The Android application interface. Before connection.

Click to enlarge

Click to enlarge
Tilted left.

The sources of the Android application are available here.

Runs on Android tablets and smarphones...
Note: If you are using both the web client, and the Android client, when the Android client makes the servo move, the web client is updated as well.

Oliv did it