element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Blog Part 2.1: Using Mqtt Client Libraries in a Processing Sketch
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: pmohan
  • Date Created: 13 Dec 2014 6:14 AM Date Created
  • Views 2014 views
  • Likes 1 like
  • Comments 3 comments
  • RoadTest
  • mqtt
  • processing
  • wifi_christmas_tree
  • iot_holidaylights
  • arduino
  • java
Related
Recommended

Part 2.1: Using Mqtt Client Libraries in a Processing Sketch

pmohan
pmohan
13 Dec 2014

Index of all the blog posts related to the Wifi Interactive Christmas Tree project is here.

 

While waiting for my Arduino Yun to arrive for my WiFi Christmas Tree hardware build, i decided to complete as many software components as possible.

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.

http://repo.eclipse.org/content/repositories/paho-releases/org/eclipse/paho/mqtt-client/0.4.0/mqtt-client-0.4.0.jar

 

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.

  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 ;
         
            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

image

 

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.

  • Sign in to reply
  • DAB
    DAB over 10 years ago

    I like how simple everything goes together.

     

    DAB

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • fvan
    fvan over 10 years ago

    Nice work, Mohan!

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • clem57
    clem57 over 10 years ago

    Good job with this MQTT!

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube