<< Just a thought - Beware of the Partner Acceptance Factor
The mystery of the negative byte value in Java: a story of bits, bytes, signed and unsigned >>

Controlling Arduino with Mosquitto and JavaFX on Raspberry Pi

2019-10-27 09:38:41
This is an extract of my book-in-progress Getting started with Java on the Raspberry Pi.
There is a lot more info about this topic and other code examples in the book!
You can already buy it on Leanpub and will receive all future updates!
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:



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 and I would love to hear your feedback.