ESP8266 MQTT Client Mosquitto Node-RED

For some time now the IoT Internet of things has been booming, opening up a possibility of interconnecting an Internet device, one of the most well-known protocols implemented is the MQTT protocol, this TCP/IP lightweight messaging protocol is excellent for sending Bidirectional data (sensors, actuators, etc.). An MQTT Broker installer is required in case of local tests as in this case, although there is a large number of IoT Platforms that have already implemented it.

Subscribe

To define a subscription requires the function “client_MQTT.subscribe (” output “);” and the message will return messages in the function callback performs the reception of all subscriptions, with an if the required topics are filtered and assigned to local variables .

1

client_MQTT.subscribe("output");/// activate Led - activa led

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

voidcallback(char*topic,byte*payload,unsignedintlength){

Stringstring;

Serial.print("Message arrived [");

Serial.print(topic);

Serial.print("] ");

for(inti=0;i<length;i++){

// Serial.print((char)payload[i]);

string+=((char)payload[i]);

}

Serial.println(string);

if(topic="output"){/// select topic "output"

Serial.print(" ");

intresultado=string.toInt();

digitalWrite(14,resultado);

}

}

Publish

The function to publish “client_MQTT.publish (” input “, buf);” Use the parameters “topic”, “message” in this case the value of the input is converted from int to String, the function does not accept Strings, uses char array in this case and we have implemented this conversion function.