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.




Recommendations for local tests of the Mosquitto and Node-RED corridor, For the next test of use Mosquitto MQTT (Recommended Tutorial Installation of Mosquitto Broker MQTT in Linux (Ubuntu) linux), Although it can be installed in Windows and MAC, we have decided to use Lubuntu Or create an MV in Virtualbox and on the installation Mosquitto and Node-RED.

This compilation of Tutorial videos about Node-RED will be a good introduction to this platform: Node-RED IoT.

trialcommand.com

Test

We will perform a small test of ESP8266 12E NodeMCU Lolinfde as an MQTT client, in this way our module (Client MQTT) through Mosquitto (Broker) will connect with Node-RED (Client MQTT).

We have connected a led diode (GPIO D15) and a pushbutton (GPIO D0) to module ESP8266, from Node-RED Dashboard activate and deactivate the led diode and display the status of the pushbutton.

Two MQTT topics have been created:

  1. client_MQTT.subscribe(“output”);    /// activate Led – activa led
  2. client_MQTT.publish(“input”,buf);  //// Array char mensage MQTT /// state input – estado entrada.

The following tutorial will explain the main points of the application.

ESP8266 MQTT Client Node-RED Mosquitto

Note: In this case the ESP8266 is constantly sending the value of the Input button and reading Output status, it is recommended to make periodical requests using delay’s to avoid conflicts.

IDE Arduino Code





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 .

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.

Conclusions

The MQTT protocol has become one of the standard protocols for Internet applications of IoT things. Comparing with other protocols we will mention some advantages:

  • It allows the sending of Strings without limiting to Numbers.
  • Send messages without requiring specific names of records.
  • Simple grouping and organization of records can be performed.
  • We consider that it is very fast, although in our example we abuse to have a constant sending, it is recommended to send data in fixed intervals.

Node-RED

Node-RED is a great tool for applications of these characteristics and has a Dashboard that allows a large number of applications.





Referencias

Downloads

PubSubClient  MQTT Client library for ESP8266

Created by knolleary Thank for contributions to community IoT!

Leave a Reply