Processing

This manual will help you to get started with Processing and the processing-mqtt library.

Get Started

You first need to make sure that you are running the latest 3.x version of Processing. If you have an old version or haven't Processing installed at all, get it from processing.org.

Afterwards, install the library using the built-in library manager by searching for MQTT. Alternatively, download the processing-mqtt library from the GitHub repository. When the download has finished, move the library to your sketch folders libraries directory. More information about installing a library can be found here.

To verify that everything worked as expected, go to Files > Examples and open the MQTT > PublishSubscribe example. This example sketch will connect to the public instance and send a hello world message on every keystroke. Run the example and check the console for the following line: [MQTT] connected to: broker.shiftr.io. If you see the connected client, and the messages flowing in the real-time graph, you are ready to go!

In Detail

In the following section, we will look at the following example line by line.

import mqtt.*;

MQTTClient client;

void setup() {
  client = new MQTTClient(this);
  client.connect("mqtt://public:public@public.cloud.shiftr.io", "processing");
  client.subscribe("hello");
}

void draw() {}

void keyPressed() {
  client.publish("hello", "world");
}

void messageReceived(String topic, byte[] payload) {
  println( topic + ": " + new String(payload));
}

A detailed description of the libraries API can also be found in repository on GitHub.

To use the library you first need to import it:

import mqtt.*;

To allocate a new client add the following line to the top of your sketch:

MQTTClient client;

In your setup function, initialize the client by passing a reference to your current sketch:

client = new MQTTClient(this);
  • Among other things, this call will look for the messageReceived callback and internally store a reference.

Finally, you can connect to shiftr.io:

client.connect("mqtt://public:public@public.cloud.shiftr.io", "processing");
  • The first argument is the MQTT URI which you can get from your instance on shiftr.io.
  • The second argument is the client ID, which will be displayed as the connections name in the real-time graph.

Make subscriptions to receive messages from other clients:

client.subscribe("hello");
  • The first argument is the name of the topic to subscribe.

Of course, you can always cancel a subscription by unsubscribing it:

client.unsubscribe("hello");
  • The first argument is the name of the topic to unsubscribe.

Most importantly, publish messages to shiftr.io:

client.publish("hello", "world");
  • The first argument is the topic to publish the message to.
  • The second argument is the payload of the message.

And last but not least, receive messages from other clients by implementing the callback:

void messageReceived(String topic, byte[] payload) {
  println(topic + ": " + new String(payload));
}
  • The first parameter is the topic of the message.
  • The second parameter is the payload in the form of a byte array. As in the example you can transform the byte array to a string using the following: new String(payload).

Congratulations, now you are able to work with shiftr.io in your Processing projects!