recursive.codes


The Personal Blog of Todd Sharp

Realtime GPIO On Raspberry Pi With Spark Java And WebSockets

Posted By: Todd Sharp on 4/10/2017 4:25 GMT
Tagged: Groovy, Java, Raspberry Pi, Spark Java, WebSockets

One of the cooler things about the Spark Java framework is built in websocket support thanks to the embedded Jetty server.  I've long been fascinated with websockets since they can push data in realtime to a subscribed client without the need for client side polling. 

The first (and only) demo often seen is the good old "chat" demo.  Don't get me wrong, chat is still a useful (and often implemented) feature, but I've always wanted to find a more interesting use case for them.  I think I've finally found a pretty cool use case so I wanted to share it here.  

I've blogged in the past about using Spark Java to get a simple website running on the Raspberry Pi.  In this demo I'll do just that and in addition I'll implement a simple GPIO handler to listen for a button press event.  When the event handler fires, I'll turn on an LED and broadcast a message to subscribed websocket clients to tell them about the message.  The majority of the websocket code is altered from this demo.  

Let's get started by looking at our build.gradle file as it has a few dependencies we'll need for this demo.

Next up - the Bootstrap.groovy class:

A few notes on the Bootstrap class:

  1. Line 22 - we create our websocket endpoint and tell Spark to use the ChatWebSocketHandler class.
  2. Line 23 - call the static init() method on Spark since we have no other routes defined.
  3. GPIO is initialized next, using a singleton GpioHandler (code below).  We attach a listener starting on line 36 that ultimately calls a broadcastMessage() method when it handles the button press event. 
  4. broadcastMessage() notifies all connected clients of the event, passing a message to them.
Here's the GpioHandler - nothing fancy here:

And the ChatWebSocketHandler - mostly the same as the demo we're copying:

The view (unchanged from the demo):

And the JavaScript (mildly changed from the demo):

After pushing the code to the Pi and running the app, here's how it responds:

There are tons of possibilities for GPIO with websockets.  Real-time temperature charts, sensors providing immediate feedback to the connected web client, etc.  

Image by bichnguyenvo from Pixabay



Related Posts

Running Database Queries With REST via a Database Tools Connection (IE: The Super Easy Way)

Running Database Queries With REST via a Database Tools Connection (IE: The Super Easy Way)

I recently blogged about a brand-new service in the Oracle Cloud - Database Tools. It's a safe, secure way to store your database credentials in the...

Easy and Secure Oracle Database Connections With Database Tools

Easy and Secure Oracle Database Connections With Database Tools

There's a brand new tool in town, and it's ready to make your life a whole lot easier if you work with Oracle DB in the cloud. It's called "Database Tools...

Quick Pub/Sub Testing for Oracle Advanced Queuing With a Command Line Interface (CLI)

Quick Pub/Sub Testing for Oracle Advanced Queuing With a Command Line Interface (CLI)

I have been spending a lot of time playing around with Oracle Advanced Queuing (AQ) lately and one of the things that has bothered me is the lack of a ...

Note: Comments are currently closed on this blog. Disqus is simply too bloated to justify its use with the low volume of comments on this blog. Please visit my contact page if you have something to say!