Controlling Arduino with Mosquitto and JavaFX on Raspberry Pi

One of the example applications in my book “Getting started with Java on the Raspberry Pi” combines a JavaFX application with Mosquitto on the Raspberry Pi to control a LED strip with an Arduino. All wireless and independent of each other as the Mosquitto-queue is dealing with exchange commands between all applications.

Between all applications a predefined list of commands can be exchanged as listed in table below.

See it in action:

Java application

The sources are available on GitHub. The color picker is a component of Gerrit Grunwald and combined with other JavaFX components two UI’s are available. The first one is used to control the LED strip.

The second UI is also receiving the commands from Mosquitto and shows them in a table.

The Java application also exposes a web interface with Undertow as the server, so anyone within the network can control the LED strip. As a proof-of-concept, only a few effects are available.

Arduino application

These sources are also available on GitHub. The Arduino code has functions per type of LED effect to generate the requested effect and is split into multiple easy to understand parts.

void setRunningLight() {  
  if (currentAction >= NUMBER_OF_LEDS) {    
    currentAction = 0;    
  }

  // Show color 1
  strip.setPixelColor(currentAction, rgb1);
  strip.show(); 

  // Reset to color 2 for next loop
  strip.setPixelColor(currentAction, rgb2);

  currentAction++;
}

Full description

This is just a very short description of what is described in more than 30 pages in the book in “Chapter 12: Message Queues”, including how to install and test Mosquitto on the Pi. Feel free to take a look in the code which is freely available on GitHub. I would love to hear your feedback!