MQTT Client library for Arduino based PLC's

January 24, 2019 by
MQTT Client library for Arduino based PLC's
Bernat Garcia

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 from these 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 Github to know further information. 

Enjoy the library! 


​Search in our Blog

MQTT Client library for Arduino based PLC's
Bernat Garcia January 24, 2019

Looking for your ideal Programmable Logic Controller?

Take a look at this product comparison with other industrial controllers Arduino-based. 

We are comparing inputs, outputs, communications and other features with the ones of the relevant brands.


Industrial PLC comparison >>>