Look for in our Blog. You will find multiple applications, solutions, code examples. Navigate using the tag cloud or search using specific criteria

MQTT Client library for Arduino based PLC's

In this post is showed the MQTT library recommended for Industrial Shields. This library has been tested and used in several installations without any issue.  So we can assure that is stable and reliable. 

Download and additional information: MQTT Library


To install the library we just have to download the .zip file and go to our Arduino IDE. Go to --> Sketch --> Include library --> Add ZIP library. Then select the downloaded file and click to OK button to install this library into the Arduino IDE.

Now we are ready to use it. 


HOW TO USE IT

There are several functions to use in this library. 

First of all we must include the library and set up our network connection properly (Wi-Fi, Ethernet, GSM...).

#include <PubSubClient.h>

Then we must indicate the server IP address, initialize a client for our network and for our MQTT connection

IPAddress server(xxx, xxx, xxx, xxx);
EthernetClient ethClient;
PubSubClient client(ethClient);

Initialize client and callback function (optional).

client.setServer(server, port);
client.setCallback(callback);


After the initialization of the client we have different functions to check, connect, receive and send information.

Use client.connected to verify if the client is connected and use client.connect() to establish communication.

if (!client.connected()){
    client.connect();
}

To subscribe and publish

client.publish(topic, payload);
client.subscribe(topic);

Apart of this functions you must place the function client.loop() in the loop function to maintain the communication. 

void loop(){
    client.loop();
}


These are the bases to use this MQTT library. Check the Github to know further information. 

Enjoy the library!