MQTT Client Libraries in a Processing Sketch

While waiting for my Arduino Yun to arrive for my WiFi Christmas Tree project, I decided to complete as many software components as possible. I had already setup a local MQTT broker and bridge. Now I am planning to use my PC and Mac for all the music and sound generation. They will act as MQTT subscribers. I will be running Processing sketches on the PC. So I decided to setup MQTT Client Libraries on Processing.

MQTT Java Client Libraries

Since Processing uses Java runtime, I got the Java client libraries from Eclipse foundation web site. I needed just the jar file.

MQTT Library Setup in Processing

I wanted to set up the MQTT library for future use as well. So I installed it in the main Library folder so that it appears in the processing import library menu. Even if you are installing it in the contributed libraries folder, you will still have to follow the naming conventions described below. Otherwise processing won’t recognize it.

On a Windows PC

Find the installation directory of processing in your machine and navigate down to the Libraries folder.  On my machine its at:

On a Mac

Open Finder. Go to Applications. Find Processing. Right click and choose “Show Package Contents”
Contents > Java > modes > java > libraries
This is important. Processing doesn’t recognize the libraries if they are not setup in a particular way and the names do not match. So i had to be precise in the following steps to get it recognized by processing.

  1. Created a folder named “mqtt”
  2. Within this folder created another folder named “library”
  3. Copied the downloaded mqtt-client-0.4.0.jar file to this folder.
  4. Renamed the jar file to “mqtt.jar” (the jar file cannot have hyphens or it cannot be different from the folder name. Hence the renaming)
  5. Closed all Processing IDE windows and reopened

Now i could see the mqtt library appearing in the Sketch>Import Library Menu

Testing the MQTT Library setup

I created a new sketch and ran this code. I could see my MQTT messages from processing are retrieved by the mosquitto_sub client.

import org.eclipse.paho.client.mqttv3.*;

void setup()
    String broker   = "tcp://localhost:1883";
    String topic    = "WiFiTree/FromProcessing/RGBTest";
    String payload  = "255,232,233";

        try {
            MqttClient myClient = new MqttClient(broker, MqttClient.generateClientId());
            MqttMessage message ;
            for(int i=0;i<10;i++)
                message = new MqttMessage((payload+ ", Test Msg " + i).getBytes());
                myClient.publish(topic, message);

        } catch(MqttException mex) {

            System.out.println("Error: "+mex.getMessage());

Results from mosquitto_sub

mqtt subscriber messages
mqtt subscriber messages

2 thoughts on “MQTT Client Libraries in a Processing Sketch”

Leave a Reply

Your email address will not be published. Required fields are marked *