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

Adding Tracing to Your Distributed Cloud Native Microservices

Adding Tracing to Your Distributed Cloud Native Microservices

When adopting cloud-native technologies and certain architectures such as the microservice pattern, observability and monitoring become a huge need and...

Project GreenThumb Part 5 - The Front-End, Build Pipeline, Push Notifications and Overall Progress

Project GreenThumb Part 5 - The Front-End, Build Pipeline, Push Notifications and Overall Progress

In this short blog series, I introduced you to Project GreenThumb, a project that I created to automate and monitor the process of growing seedlings with...

Collect and Analyze Application and Server Metric Data with Micronaut's Support for OCI Monitoring

Collect and Analyze Application and Server Metric Data with Micronaut's Support for OCI Monitoring

The Micronaut team over in Oracle Labs have been hard at work on a number of impressive features and framework improvements and they’re moving so fast ...

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!