mqttstatus - dl3ebb/OpenIot GitHub Wiki

MQTT Status

If you're already using an MQTT server, the MqttStatus module can help you monitor your devices.

The following code shows you how to use it:

/**
 * Demo of the MQTT Status
 */

#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 <Modules/MqttStatus.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);

    mqttStatus->enable();

    /**
     * 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("OpenIotMqttStatus", 1);
}

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

In addition to the already known MQTT server configuration, you need to add the following line:

mqttStatus->enable();

This will enable the MQTT status. You can now view the status of your application in the MQTT Explorer :

image

Loops per Second This value shows how often your application has run through the main loop. The higher the value, the faster the application can react to events.

Localtime This is the local time of the module. Since no NTP server is configured in our example, it will show January 1, 1970 (Unix epoch).

Uptime

This shows how long the module has been running. You could implement monitoring here to issue a warning if the uptime is less than 10 minutes, indicating a recent restart.

IpAdress

This is the IP address under which the module can be accessed.

Wifi-SSID

The SSID of the Wi-Fi network the module is connected to.

Wifi-Rssi

The strength of the Wi-Fi signal.

Heapsize

This value should stabilize after a while. If it fluctuates, there might be a memory leak.


| ← Previous Page (Web Update) | ↑ Tutorial Main Page | Next Page (Syslog Logger) → |

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