<< Summary

Read analog data, in Java, part II Raspberry PI

ADC & WebSocket

Reading a potentiometer is not something new.
Reading it from the Raspberry PI is interesting, but there is no reason to climb the curtains!

But wait..., with the Raspberty PI connected on the Internet, you can broadcast the value read from the potentiometer to whoever is interested, for example via the WebSocket protocol.

This is the beauty of this little thingies of the Internet...
They're connected on one side to the Internet, and on the other to some sensor(s), like the Raspberry PI with its GPIO interface.

We provide here a full running example, running on top of node.js.
Find here an article about the way to install node on the Raspberry PI.

We need:

To install the WebSocket module on the Node server:

  Prompt> npm install websocket        
And this is pretty much all you will need. The server version presented here does not use Express. This would be an option, and would make the code a bit simpler.
But for now, you do not need to install anything else. Period.
The node server can run on the Raspberry PI, or not. This is not important for this demo. In the real world, it would probably run on a machine that could take some load.
The Java program running on the Raspberry PI feeds the WebSocket server, wherever it is.


The class that reads the potentiometer will not display its value, it will send a message containing it to the WebSocket server.
The WebSocket server application sends the message it receives to all its connected clients.
A web page is also provided with this demo, that will render the value emitted by the Java program reading the potentiometer. The browser will connect to the WebSocket server, to listen to those data.
Obviously, the browser running this page needs to be WebSocket enabled.


As a reminder, here is what it looks like from the command line. You turn the knob, and the program running on the Raspberry (here visualized through VNC) renders the value.

Display it if full screen for more details

And here is what it can look like when you have a WebSocket server in the picture.
See the breadborad, the server console running on the Raspberry PI, and the rendering in a browser running on a tablet (Android in this case).

Display it if full screen for more details

Again, the WebSocket server could be running anywhere, not necessarily on the Raspberry PI.
And as many browsers as you want can simultaneously render the data emitted by the Raspberry PI.

The code

The class that reads the ADC and emits the data message to the WebSocket server is adc.sample.WebSocketFeeder.
It reads the ADC just like the classes we've seen so far, and establishes a connection with the server, so data can be sent.

The server

The server is implemented by the file named server.js.
The logic is extremely simple: when a message is received, it is rebroadcasted to all the connected clients.

The client

The client subscribes to the same WebSocket channel as the one the adc.sample.WebSocketFeeder feeds, hosted by the server defined by server.js.
When a message is received - everytime the knob is moved - the value is displayed, as below.

This is just an example. Drag the slider to set the value on the display.
But this is what you would see when manipulating the knob of the potentiometer.

Run it

  1. Start the server
      Prompt> cd node
      Prompt> node server.js          
  2. Start the client
      Prompt> ./run.ws 0          
    You will notice in the script run.ws that you can use a System variable named ws.uri to set the uri of the WebSocket to feed, in case it is not just ws://localhost:9876/.
  3. In a browser, reach the following URL
  4. Turn the knob of the potentiometer, back and forth...

Oliv did it