Files
MeshCore-mqtt-observer/lib/PsychicMqttClient/examples/Simple/main.cpp
T
agessaman 498566e6c9 MQTT bridge: fix memory use, improve status reporting
- Main broker: only allocate _mqtt_client when custom broker configured
  (analyzer-only saves one PsychicMqttClient). Reconnect main broker after
  forced disconnect with 30s throttle; set last_attempt on disconnect so
  throttle applies and avoids reconnect storms on flaky WiFi.
- Analyzer clients: call disconnect() when WiFi transitions to disconnected
  so ESP-IDF frees MQTT buffers (it does not free on WiFi drop). Reduces
  fragmentation and Max drop after disconnect/reconnect cycles.
- get wifi.status: report WiFi uptime (Xd Xh Xm Xs) when WITH_MQTT_BRIDGE.
  Track connect time in bridge; backfill when already connected at first check.
- get mqtt.status: show msgs on/off, broker (connected/disconnected/n/a),
  analyzer US/EU (connected/disconnected/off), and queue count.

Note: PsychicMqttClient change (register event only on first client creation)
belongs in the library repo if committed separately.
2026-02-02 19:09:31 -08:00

104 lines
3.2 KiB
C++

/**
* PsychicMqttClient
*
* Simple Example for a minimal MQTT client using the PsychicMqttClient library.
*
* Please change the ssid and pass to your actual WiFi credentials.
*
* This example uses the public MQTT broker broker.hivemq.com,
* registers a onTopic callback function subscribed to a unique topic
* and publishes a message to that topic. The echoed message payload will be
* printed to the serial monitor.
*
*/
#include <Arduino.h>
#include <WiFi.h>
#include <PsychicMqttClient.h>
const char ssid[] = "ssid"; // your network SSID (name)
const char pass[] = "pass"; // your network password
/**
* Create a PsychicMqttClient object
*/
PsychicMqttClient mqttClient;
void setup()
{
Serial.begin(115200);
/**
* Connect to the WiFi network with the given ssid and pass.
*/
WiFi.begin(ssid, pass);
Serial.printf("Connecting to WiFi %s .", ssid);
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
delay(500);
}
Serial.printf("\r\nConnected, IP address: %s \r\n", WiFi.localIP().toString().c_str());
/**
* Connect to the open MQTT broker broker.hivemq.com.
*/
mqttClient.setServer("mqtt://broker.hivemq.com");
/**
* Lambda callback function for onTopic Event Handler
*
* Subscribes to the topic "{MAC-Address}/simple" with QoS 0.
* The lambda callback function will be called when a message is received.
*/
String topic = String(ESP.getEfuseMac()) + "/simple";
mqttClient.onTopic(topic.c_str(), 0, [&](const char *topic, const char *payload, int retain, int qos, bool dup)
{
/**
* Using a lambda callback function is a very convenient way to handle the
* received message. The function will be called when a message is received.
*
* The parameters are:
* - topic: The topic of the received message
* - payload: The payload of the received message
* - retain: The retain flag of the received message
* - qos: The qos of the received message
* - dup: The duplicate flag of the received message
*
* It is important not to do any heavy calculations, hardware access, delays or
* blocking code in the callback function.
*/
Serial.printf("Received Topic: %s\r\n", topic);
Serial.printf("Received Payload: %s\r\n", payload); });
/**
* Connect to the MQTT broker
*/
mqttClient.connect();
/**
* Wait blocking until the connection is established
*/
while (!mqttClient.connected())
{
delay(500);
}
/**
* Publish a message to the topic "{MAC-Address}/simple" with QoS 0 and retain flag 0.
*
* You can only publish messages after the connection is established.
*/
mqttClient.publish(topic.c_str(), 0, 0, "Hello World!");
}
void loop()
{
/**
* Nothing to do here, the onTopic callback function will be called when a message is received.
*/
}