mqtt - dl3ebb/OpenIot GitHub Wiki

MQTT

All my IoT devices communicate with each other via an MQTT broker. In my example, it's a local Eclipse Mosquitto server. The installation is very simple and is described on the Server Setup page.

To monitor the MQTT server, I use the tool MQTT Explorer.

The following example demonstrates the use of the MQTT connector. Please adjust the configuration data to match your environment.

/**
 * Demo of the MQTT Usage
 */

#define SWITCH_PIN 25
#define LED_PIN    32
#define WLAN_SSID   "your_ssid"
#define WLAN_PASSWD "your_passwd"
#define MQTT_SERVER "your.mqtt.server"
#define MQTT_PREFIX "/Labs/HelloMqqt"


#include <OpenIot.h>
#include <Connector/DigitalInConnector.h>
#include <Connector/DigitalOutConnector.h>
#include <Connector/MqttConnector.h>
#include <Logger/SerialLogger.h>

#include <Modules/Wifi.h>

void setup() {
    serialLogger->setBaudrate(115200);
    openIot.setLogger(serialLogger);
    
    wifi->setEnablePrimary(true);
    wifi->setSsid1(WLAN_SSID);
    wifi->setPassword1(WLAN_PASSWD);

    mqttConnector->setEnable(true);
    mqttConnector->setServer (MQTT_SERVER);
    mqttConnector->setPrefix (MQTT_PREFIX);

    /**
     * Create the Elements and register them to the connectors
     */
    BoolElement *switch1 = new BoolElement("Switch1");
    digitalInConnector->registerElement(switch1, SWITCH_PIN, true);
    mqttConnector->registerPublish(switch1, "/app/Switch1/status");

    BoolElement *led1 = new BoolElement("Led1");
    digitalOutConnector->registerElement(led1, LED_PIN);
    mqttConnector->registerSubscribe(led1, "/app/Led1/command");
    mqttConnector->registerPublish(led1, "/app/Led1/status");

    eventManager.addListener<NewBoolValueEvent>(NewBoolValue, "Switch1", [led1](NewBoolValueEvent *event) {
        led1->setValue(event->newValue);
    });

    openIot.setup("OpenIotMqtt", 1);
}

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

In addition to configuring the WLAN, the MQTT server is now also configured. This is done with the following lines:

mqttConnector->setEnable(true);
mqttConnector->setServer (MQTT_SERVER);
mqttConnector->setPrefix (MQTT_PREFIX);

MQTT_SERVER is the IP address or hostname of the IoT server. MQTT_PREFIX is the prefix for the device.

Next, we need to register the elements of the application with the MQTT connector to ensure they are transmitted to the MQTT broker.

There is a distinction between registering for publish and subscribe. The switch is registered as a publish:

mqttConnector->registerPublish(switch1, "/app/Switch1/status");

The LED is registered as both publish and subscribe:

mqttConnector->registerSubscribe(led1, "/app/Led1/command");
mqttConnector->registerPublish(led1, "/app/Led1/status");

When the device restarts, you can observe the MQTT status in the console, and the device will connect to the server:

INFO  - ---- Mqtt settings : ------
INFO  - Enabled               : true
INFO  - Server                : your.mqtt.server
INFO  - Port                  : 1883
INFO  - Prefix                : /Labs/HelloMqtt
INFO  - ClientId              :
INFO  - Username              :
INFO  - Password              :
INFO  - Connect to MQTT Server
INFO  - Mqtt Connection established.

When the switch is pressed, you can observe in the MQTT Explorer that the value for the topic /Labs/HelloMqtt/app/Switch1/status changes from false to true.

For the LED, the value of /Labs/HelloMqtt/app/Led1/status will also change, as it is turned on when the switch is pressed.

Additionally, the LED has a subscribe topic that binds it to the value of the topic /Labs/HelloMqtt/app/Led1/command. If you publish true to the topic /Labs/HelloMqtt/app/Led1/command in MQTT Explorer, the LED will turn on.

The MQTT connector accepts true, on, or 1 for logical true, and any other values will be evaluated as logical false.


| ← Previous Page (Wi-Fi) | ↑ Tutorial Main Page | Next Page (REST) → |

⚠️ **GitHub.com Fallback** ⚠️