Index of all the blog posts related to the Wifi Interactive Christmas Tree project is here.
I am planning to use my PC and Mac for all the music and sound generation. They will be MQTT subscribers. So first i decided to setup MQTT client 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
C:\processing-2.2.1\modes\java\libraries
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.
- Created a folder named “mqtt"
- Within this folder created another folder named “library"
- Copied the downloaded mqtt-client-0.4.0.jar file to this folder.
- 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)
- 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 ; myClient.connect(); for(int i=0;i<10;i++) { message = new MqttMessage((payload+ ", Test Msg " + i).getBytes()); myClient.publish(topic, message); } myClient.disconnect(); } catch(MqttException mex) { System.out.println("Error: "+mex.getMessage()); mex.printStackTrace(); } }
Results from mosquitto_sub
MQTT Subscriber
In Part 2 of this post, i will show how i used the processing sketch as a subscriber and sent OSC messages to Chuck to make the minions speak.