DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Data Engineering
  3. IoT
  4. Control Remote Peripherals With MQTT

Control Remote Peripherals With MQTT

Building an LED app with a dashboard just takes a bit of code and a few clicks. Let's build something using Ubidots and MQTT for communication.

Francesco Azzola user avatar by
Francesco Azzola
CORE ·
Mar. 21, 17 · Tutorial
Like (8)
Save
Tweet
Share
10.70K Views

Join the DZone community and get the full member experience.

Join For Free

i recently covered the mqtt protocol and how it works , and we already know the iot mqtt is used to send data from remote sensors. just to recap briefly, mqtt is a lightweight protocol used in the telemetry. several iot platforms support mqtt to exchange data with remote iot boards and sensors. usually, iot boards use mqtt to connect to the iot platforms that acquire information.

project description

in this project, we use mqtt in a different way. we want to control an iot board (like the mkr1000) using mqtt. the target of this project is controlling an rgb led matrix remotely using mqtt. to build this project, we will use:

  • arduino mkr1000
  • 4×4 rgb led matrix

the mkr1000 will connect to the ubidots cloud platform using mqtt. this project is divided into two steps:

  1. configure ubidots to handle the rgb color components using the web interface
  2. develop an mqtt client to connect to ubidots and manage the led matrix

at the end of this project, we will control the rgb led matrix remotely using the ubidots web interface.

configure the iot board

in this first step, we will configure the ubidots interface to handle three color components. we will use sliders to control them. let us begin.

before continuing, you have to create a free account using this link . log into the application and click on the device to create a new device. mqtt tutorial config

we used rgb controller as the device name. a device acts as a data container where we can group variables that hold the exchanged data. once the device is ready, we can start adding a new variable:

image title

we will create three variables called:

  • redcolor
  • greencolor
  • bluecolor

now it is time to create the dashboard that we will use to control these variable values. go to dashboard and click on "add". give a name to your dashboard and start adding controls. in this project, we add sliders, so click on "add new widget" and then on "slider":

image title

select the device (aka the data source) previously configured:

image title

select the rgb controller and you will see three variables:

image title

select one of them and repeat the process for all three variables. at the end, you will have a dashboard that looks like this:

image title


implementing a client to handle mqtt

once the iot platform is configured, we can focus our attention on the iot mqtt client. this client receives the data from the platform and manages the rgb led matrix. to create the client, we use the following libraries:

  • adafruit neomatrix
  • mqtt client (you can download it from the arduino library manager)

the first step is initialized the neomatrix, providing a set of information like the matrix width and height and the pin number. in our case, the rgb led matrix is connected using mkr pin 5.

adafruit_neomatrix matrix = adafruit_neomatrix(4, 4, 5,
                           neo_matrix_top     + neo_matrix_right +
                           neo_matrix_columns + neo_matrix_progressive,
                           neo_grb + neo_khz800);


notice the first three parameters represent the number of leds in a row and in a column. using the matrix instance, we can color the matrix.

let us implement the mqtt client. this project uses an mkr1000, so we'll use a wi-fi connection:

wificlient net;
mqttclient mqttclient;
 
void setup() {
      serial.begin(115000);  
      serial.println("configuring the net...");
      wifi.begin("your wifi sid", "your wifi password");
 
  while (wifi.status() != wl_connected) {
        serial.print(".");
        delay(1000);
    }
    serial.println("begin...");
    mqttclient.begin("mqtt://things.ubidots.com", net);
    matrix.begin();
    connect();
}


in the setup() method, we simply initialize the wi-fi connection and the mqttclient . to initialize the mqtt client, we have to set the mqtt server (ubidots server address) and the net .
the connect() method handles the connection and the mqtt topic subscription . according to the ubidots docs , each variable defined in the data source (aka device) has its own topic:

/v1.6/devices/_device_name/_variable_name/lv


notice that the lv at the end of the topic stands for last value . in other words, we are interested only in the last variable value to control the rgb led matrix. in this project, the application has to subscribe to the following topics:

/v1.6/devices/rgbcontroller/redcolor/lv
/v1.6/devices/rgbcontroller/greencolor/lv
/v1.6/devices/rgbcontroller/bluecolor/lv


so the connect() method is:

void connect() {
    serial.println("connecting to mqtt....");
    if (mqttclient.connect("your_unique_id", "your_ubidots_token", "")) {
        serial.println("mqtt client connected");
        int result = mqttclient.
        subscribe("/v1.6/devices/rgbcontroller/redcolor/lv");
        serial.println(result);
        serial.println("subscribed red topic.");
        int result1 = mqttclient.
        subscribe("/v1.6/devices/rgbcontroller/greencolor/lv");
        serial.println(result1);
        serial.println("subscribed green topic.");
        int result2 = mqttclient.
        subscribe("/v1.6/devices/rgbcontroller/bluecolor/lv");
        serial.println(result1);
        serial.println("subscribed blue topic.");
    } else {
        serial.println("######### client not connected ########");
    }
}


once the application is connected to the mqtt server and subscribed to the variable topics, we have to implement the method that listens to the values coming from the mqtt server. to this purpose, we have to implement a method named:

messagereceived(string topic, string payload, char * bytes, unsigned int length)

in this method, the application has to do the following tasks:

  • identify the topic that holds the message.
  • convert the message to the color component value.
  • control the rgb led matrix, setting the new color.

this is the method implementation:

void messagereceived(string topic, string payload,
    char * bytes, unsigned int length) {

    if (topic.indexof("redcolor") >= 0) {
        red = atoi(bytes);
    } else if (topic.indexof("greencolor") >= 0) {
        green = atoi(bytes);
    } else if (topic.indexof("bluecolor") >= 0) {
        blue = atoi(bytes);
    }
    matrix.fillscreen(matrix.color(red, green, blue));
    matrix.show();
}


that’s all. in the last two lines of the method, we use the instance of matrix to fill the rgb led matrix with the color identified by the red, green, or blue component, and we show it.

now you can try to use the three sliders in the ubidots iot console to change the rgb led matrix color.

conclusion

at the end of this post, hopefully, you have gained knowledge about the mqtt protocol and how to use it to control remote peripherals.

MQTT workplace Matrix (protocol) LEd IoT

Published at DZone with permission of Francesco Azzola, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Spring Boot Docker Best Practices
  • What Java Version Are You Running? Let’s Take a Look Under the Hood of the JDK!
  • Last Chance To Take the DZone 2023 DevOps Survey and Win $250! [Closes on 1/25 at 8 AM]
  • Exploring the Benefits of Cloud Computing: From IaaS, PaaS, SaaS to Google Cloud, AWS, and Microsoft

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: