### Basic sMQTTBroker Setup Source: https://context7.com/terrorsl/smqttbroker/llms.txt A minimal setup for sMQTTBroker without subclassing, suitable for quickstarts where event callbacks are not needed. Initializes the broker on the specified port after WiFi connection. ```cpp #include sMQTTBrokerWithoutEvent broker; void setup() { WiFi.begin("MySSID", "MyPassword"); while (WiFi.status() != WL_CONNECTED) delay(500); broker.init(1883); } void loop() { broker.update(); } // Any MQTT client can now connect, subscribe, and publish freely // with no authentication or event filtering applied ``` -------------------------------- ### Initialize sMQTTBroker Source: https://github.com/terrorsl/smqttbroker/blob/main/readme.md Instantiate the sMQTTBroker and initialize it with a specified port. This is a basic setup for the broker. ```c++ sMQTTBrokerWithoutEvent broker; #define PORT 1883 ``` ```c++ void setup(){ broker.init(PORT); }; ``` -------------------------------- ### Initialize sMQTTBroker Source: https://github.com/terrorsl/smqttbroker/blob/main/doc/html/md_readme.html Include the sMQTTBroker library and initialize the broker in the setup function. The update function must be called in the loop. ```c++ #include sMQTTBroker broker; #define PORT 1883 void setup(){ broker.init(PORT); }; void loop(){ broker.update(); }; ``` -------------------------------- ### Include sMQTTBroker Library Source: https://github.com/terrorsl/smqttbroker/blob/main/readme.md Include the sMQTTBroker library header file to start using the broker. ```c++ #include ``` -------------------------------- ### Initialize and Start sMQTTBroker Source: https://context7.com/terrorsl/smqttbroker/llms.txt Sets up the TCP server on the specified port and begins listening for incoming MQTT client connections. Pass `checkWifiConnection=true` to enable automatic WiFi reconnection handling inside the `update()` loop. Returns `true` on successful initialization. ```cpp #include #include sMQTTBroker broker; #define MQTT_PORT 1883 void setup() { Serial.begin(115200); WiFi.begin("MySSID", "MyPassword"); while (WiFi.status() != WL_CONNECTED) delay(500); Serial.print("IP: "); Serial.println(WiFi.localIP()); // Initialize broker on port 1883, with WiFi connection checking enabled if (broker.init(MQTT_PORT, true)) { Serial.println("MQTT broker started"); } else { Serial.println("Failed to start broker"); } } void loop() { broker.update(); } ``` -------------------------------- ### sMQTTBroker::init Source: https://context7.com/terrorsl/smqttbroker/llms.txt Initializes and starts the MQTT broker on a specified TCP port. It can optionally enable automatic WiFi connection checking for reconnection handling. Returns true if initialization is successful. ```APIDOC ## sMQTTBroker::init — Initialize and start the broker Sets up the TCP server on the specified port and begins listening for incoming MQTT client connections. Pass `checkWifiConnection=true` to enable automatic WiFi reconnection handling inside the `update()` loop. Returns `true` on successful initialization. ### Method ```cpp bool init(uint16_t port, bool checkWifiConnection = false) ``` ### Parameters #### Path Parameters - **port** (uint16_t) - Required - The TCP port to listen on for MQTT connections. - **checkWifiConnection** (bool) - Optional - If true, enables automatic WiFi reconnection handling. ### Request Example ```cpp #include #include sMQTTBroker broker; #define MQTT_PORT 1883 void setup() { Serial.begin(115200); WiFi.begin("MySSID", "MyPassword"); while (WiFi.status() != WL_CONNECTED) delay(500); Serial.print("IP: "); Serial.println(WiFi.localIP()); // Initialize broker on port 1883, with WiFi connection checking enabled if (broker.init(MQTT_PORT, true)) { Serial.println("MQTT broker started"); } else { Serial.println("Failed to start broker"); } } void loop() { broker.update(); } ``` ``` -------------------------------- ### Custom Authentication Logic Source: https://context7.com/terrorsl/smqttbroker/llms.txt Implement custom authentication by returning false from `onEvent(NewClient_sMQTTEventType)` to trigger `sMQTTConnReturnBadUsernameOrPassword`. This example checks a hardcoded username and password. ```cpp // Return false from onEvent(NewClient_sMQTTEventType) to trigger // sMQTTConnReturnBadUsernameOrPassword automatically. bool onEvent(sMQTTEvent *event) override { if (event->Type() == NewClient_sMQTTEventType) { auto *e = static_cast(event); return (e->Login() == "user" && e->Password() == "pass"); } return true; } ``` -------------------------------- ### Get variable header start pointer Source: https://github.com/terrorsl/smqttbroker/blob/main/doc/html/s_m_q_t_t_message_8h_source.html Returns a pointer to the beginning of the variable header section within the message buffer. Assumes the message state is advanced enough to have a variable header. ```cpp const char* getVHeader() const { return &buffer[vheader]; } ``` -------------------------------- ### Get QoS level Source: https://github.com/terrorsl/smqttbroker/blob/main/doc/html/s_m_q_t_t_message_8h_source.html Extracts the Quality of Service (QoS) level from the fixed header of a PUBLISH message. Returns 0, 1, or 2. ```cpp unsigned char QoS() { return (buffer[0] & 0x6)>>1; } ``` -------------------------------- ### Get message type Source: https://github.com/terrorsl/smqttbroker/blob/main/doc/html/s_m_q_t_t_message_8h_source.html Returns the type of the MQTT message. This is determined from the first byte of the buffer if the message state is 'Complete'. ```cpp Type type() const { return state == Complete ? static_cast(buffer[0] & 0XF0) : Unknown; } ``` -------------------------------- ### init() Source: https://github.com/terrorsl/smqttbroker/blob/main/doc/html/functions_func.html Initializes the sMQTTBroker. ```APIDOC ## init() ### Description Initializes the sMQTTBroker. ### Method (Implicitly called or part of constructor) ### Parameters None explicitly documented. ### Response None explicitly documented. ``` -------------------------------- ### init() Source: https://github.com/terrorsl/smqttbroker/blob/main/doc/html/functions.html Initializes the sMQTTBroker. This is a class method. ```APIDOC ## init() ### Description Initializes the sMQTTBroker. ### Method (Not specified, likely a constructor or initialization function) ### Endpoint N/A ### Parameters None explicitly documented. ### Request Example N/A ### Response N/A ``` -------------------------------- ### init Source: https://github.com/terrorsl/smqttbroker/blob/main/doc/html/classs_m_q_t_t_broker.html Initializes the MQTT broker on the specified port. Optionally checks for Wi-Fi connection. ```APIDOC ## init ### Description Initializes the MQTT broker. ### Method bool init(unsigned short port, bool checkWifiConnection = false) ### Parameters * **port** (unsigned short) - The port number to initialize the broker on. * **checkWifiConnection** (bool) - Optional. If true, checks for Wi-Fi connection before initializing. ### Return Value Returns true if initialization is successful, false otherwise. ``` -------------------------------- ### Get end pointer of message buffer Source: https://github.com/terrorsl/smqttbroker/blob/main/doc/html/s_m_q_t_t_message_8h_source.html Returns a pointer to the end of the internal message buffer. Useful for iterating or calculating remaining data. ```cpp const char* end() const { return &buffer[0] + buffer.size(); } ``` -------------------------------- ### sMQTTBroker::init Source: https://github.com/terrorsl/smqttbroker/blob/main/doc/html/s_m_q_t_t_broker_8h_source.html Initializes the MQTT broker on the specified port. Optionally checks for Wi-Fi connection. ```APIDOC ## init ### Description Initializes the MQTT broker on the specified port. Optionally checks for Wi-Fi connection. ### Method bool ### Parameters - **port** (unsigned short) - The port number to run the broker on. - **checkWifiConnection** (bool) - Optional. If true, checks for Wi-Fi connection before starting. Defaults to false. ### Definition sMQTTBroker.h:16 ``` -------------------------------- ### Get string from buffer Source: https://github.com/terrorsl/smqttbroker/blob/main/doc/html/s_m_q_t_t_message_8h_source.html A static utility function to extract a length-prefixed string from a buffer. It updates the buffer pointer and provides the string's length. ```cpp static void getString(const char* &buff, unsigned short &len); ``` -------------------------------- ### sMQTTBroker Constructor Source: https://github.com/terrorsl/smqttbroker/blob/main/doc/html/classs_m_q_t_t_broker-members.html Initializes the sMQTTBroker with a specified port and an optional Wi-Fi connection check. ```APIDOC ## init ### Description Initializes the sMQTTBroker with a specified port and an optional Wi-Fi connection check. ### Method Signature `init(unsigned short port, bool checkWifiConnection=false)` ``` -------------------------------- ### sMQTTClient Constructor Source: https://github.com/terrorsl/smqttbroker/blob/main/doc/html/classs_m_q_t_t_client-members.html Constructs a new sMQTTClient instance. ```APIDOC ## sMQTTClient(sMQTTBroker *parent, TCPClient &client) ### Description Initializes a new instance of the sMQTTClient class, associating it with a parent sMQTTBroker and a TCPClient. ### Method N/A (This is a C++ constructor) ### Parameters - **parent** (*sMQTTBroker* *) - A pointer to the parent sMQTTBroker. - **client** (*TCPClient* &) - A reference to the TCPClient used for communication. ``` -------------------------------- ### Filter Publish Events with sMQTTEventType Source: https://context7.com/terrorsl/smqttbroker/llms.txt Use the `sMQTTEventType` enum to identify specific event types within the `onEvent` handler. This example specifically checks for and processes publish events. ```cpp bool onEvent(sMQTTEvent *event) override { if (event->Type() == Public_sMQTTEventType) { auto *pub = static_cast(event); // Forward to another system, log, or react Serial.println(pub->Payload().c_str()); } return true; } ``` -------------------------------- ### Handle New Client Connections and Credentials Source: https://context7.com/terrorsl/smqttbroker/llms.txt Process new client connections using `sMQTTNewClientEvent`. You can access the client ID, login, and password. Return `false` to reject the connection if credentials are not valid. ```cpp case NewClient_sMQTTEventType: { auto *e = static_cast(event); std::string id = e->Client()->getClientId(); std::string usr = e->Login(); std::string pwd = e->Password(); // Allow only known clients if (usr == "sensor_node" && pwd == "pass123") { Serial.printf("Accepted: %s\n", id.c_str()); return true; } Serial.printf("Rejected: %s (bad credentials)\n", id.c_str()); return false; } ``` -------------------------------- ### Handle Client Publish Events and Actions Source: https://context7.com/terrorsl/smqttbroker/llms.txt React to client publish events using `sMQTTPublicClientEvent`. This snippet demonstrates how to check the topic for specific commands like 'home/command/reboot' or sensor data. ```cpp case Public_sMQTTEventType: { auto *e = static_cast(event); if (e->Topic() == "home/command/reboot") { Serial.println("Reboot command received — restarting ESP"); ESP.restart(); } if (e->Topic().rfind("home/sensor/", 0) == 0) { Serial.printf("[%s] %s = %s\n", e->Client()->getClientId().c_str(), e->Topic().c_str(), e->Payload().c_str()); } break; } ``` -------------------------------- ### Client Source: https://github.com/terrorsl/smqttbroker/blob/main/doc/html/classs_m_q_t_t_public_client_event.html Returns the associated sMQTTClient object. ```APIDOC ## Client ### Description Returns a pointer to the associated sMQTTClient object. ### Returns - [sMQTTClient](classs_m_q_t_t_client.html) * ### Method [sMQTTClient](classs_m_q_t_t_client.html) * ``` -------------------------------- ### Client() Source: https://github.com/terrorsl/smqttbroker/blob/main/doc/html/classs_m_q_t_t_new_client_event.html Returns a pointer to the sMQTTClient associated with this event. ```APIDOC ## Client() ### Description Retrieves the sMQTTClient object associated with this new client event. ### Signature sMQTTClient* Client() ### Returns A pointer to the sMQTTClient. ``` -------------------------------- ### Login() Source: https://github.com/terrorsl/smqttbroker/blob/main/doc/html/functions.html Handles client login events. This method is associated with sMQTTNewClientEvent. ```APIDOC ## Login() ### Description Handles client login events. This method is part of the sMQTTNewClientEvent class. ### Method (Not specified) ### Endpoint N/A ### Parameters None explicitly documented. ### Request Example N/A ### Response N/A ``` -------------------------------- ### Login() Source: https://github.com/terrorsl/smqttbroker/blob/main/doc/html/classs_m_q_t_t_new_client_event.html Retrieves the login string for the new client. ```APIDOC ## Login() ### Description Gets the login username for the newly connected client. ### Signature std::string Login() ### Returns The client's login username. ``` -------------------------------- ### restart Source: https://github.com/terrorsl/smqttbroker/blob/main/doc/html/classs_m_q_t_t_broker.html Restarts the Wi-Fi server for the MQTT broker. ```APIDOC ## restart ### Description Restarts the underlying Wi-Fi server associated with the MQTT broker. ### Method void restart() ### Parameters None ### Return Value None ``` -------------------------------- ### sMQTTTopic Constructors Source: https://github.com/terrorsl/smqttbroker/blob/main/doc/html/classs_m_q_t_t_topic.html Constructors for creating an sMQTTTopic object. ```APIDOC ## sMQTTTopic Constructors ### Description Constructors for creating an sMQTTTopic object with different initializations. ### Methods - `sMQTTTopic(const char *name, char QoS=0)` - `sMQTTTopic(const char *name, unsigned short nameLen, const char *payload, unsigned short payloadLen)` - `sMQTTTopic(std::string &name, std::string &payload, char QoS=0)` - `sMQTTTopic(sMQTTTopic *other)` ``` -------------------------------- ### restart() Source: https://github.com/terrorsl/smqttbroker/blob/main/doc/html/functions.html Restarts the sMQTTBroker. This is a class method. ```APIDOC ## restart() ### Description Restarts the sMQTTBroker. ### Method (Not specified) ### Endpoint N/A ### Parameters None explicitly documented. ### Request Example N/A ### Response N/A ``` -------------------------------- ### Login() Source: https://github.com/terrorsl/smqttbroker/blob/main/doc/html/functions_func.html Handles client login events. ```APIDOC ## Login() ### Description Handles client login events, likely returning information about a new client connection. ### Method (Likely an event handler or callback) ### Parameters None explicitly documented. ### Response Returns a [sMQTTNewClientEvent](classs_m_q_t_t_new_client_event.html#a86b87f9a291abbda4a4d73e3c7192b76) object. ``` -------------------------------- ### restart Source: https://github.com/terrorsl/smqttbroker/blob/main/doc/html/classs_m_q_t_t_broker-members.html Restarts the sMQTTBroker. ```APIDOC ## restart ### Description Restarts the sMQTTBroker. ### Method Signature `restart()` ``` -------------------------------- ### sMQTTClient Constructor Source: https://github.com/terrorsl/smqttbroker/blob/main/doc/html/classs_m_q_t_t_client.html Constructs an sMQTTClient object, associating it with a parent sMQTTBroker and a TCPClient instance. ```APIDOC ## sMQTTClient Constructor ### Description Initializes a new instance of the sMQTTClient class. ### Parameters * **parent** (*sMQTTBroker **) - A pointer to the parent sMQTTBroker. * **client** (*TCPClient &&*) - A reference to the TCPClient instance used for communication. ``` -------------------------------- ### sMQTTTopic Constructors Source: https://github.com/terrorsl/smqttbroker/blob/main/doc/html/classs_m_q_t_t_topic-members.html Constructors for the sMQTTTopic class. ```APIDOC ## sMQTTTopic(const char *name, char QoS=0) ### Description Constructor for sMQTTTopic. ### Parameters - **name** (const char *) - The topic name. - **QoS** (char) - The Quality of Service level, defaults to 0. ``` ```APIDOC ## sMQTTTopic(const char *name, unsigned short nameLen, const char *payload, unsigned short payloadLen) ### Description Constructor for sMQTTTopic with payload. ### Parameters - **name** (const char *) - The topic name. - **nameLen** (unsigned short) - The length of the topic name. - **payload** (const char *) - The topic payload. - **payloadLen** (unsigned short) - The length of the payload. ``` ```APIDOC ## sMQTTTopic(std::string &name, std::string &payload, char QoS=0) ### Description Constructor for sMQTTTopic using std::string. ### Parameters - **name** (std::string &) - The topic name. - **payload** (std::string &) - The topic payload. - **QoS** (char) - The Quality of Service level, defaults to 0. ``` ```APIDOC ## sMQTTTopic(sMQTTTopic *other) ### Description Copy constructor for sMQTTTopic. ``` -------------------------------- ### Process Broker Events with `update()` Source: https://context7.com/terrorsl/smqttbroker/llms.txt Must be called repeatedly from `loop()`. Handles accepting new TCP connections, reading incoming MQTT packets, processing control messages, and maintaining keep-alive timers. If `checkWifiConnection` was set to `true` at init, it also monitors WiFi status and calls `restart()` automatically on reconnection. ```cpp void loop() { broker.update(); // Call as frequently as possible for responsive message handling } ``` -------------------------------- ### Client Member Function Source: https://github.com/terrorsl/smqttbroker/blob/main/doc/html/classs_m_q_t_t_remove_client_event.html Returns a pointer to the associated sMQTTClient object. ```APIDOC ## Client() ### Description Returns a pointer to the sMQTTClient object associated with this event. ### Returns - *sMQTTClient* * - A pointer to the sMQTTClient object. ``` -------------------------------- ### sMQTTNewClientEvent Constructor Source: https://github.com/terrorsl/smqttbroker/blob/main/doc/html/classs_m_q_t_t_new_client_event.html Constructs a new sMQTTNewClientEvent object with the provided client, login, and password. ```APIDOC ## sMQTTNewClientEvent Constructor ### Description Initializes a new client event with associated client, login, and password details. ### Signature sMQTTNewClientEvent(sMQTTClient *client, std::string &login, std::string &password) ``` -------------------------------- ### Platform Macros for ESP8266/ESP32/WT32-ETH01 Source: https://context7.com/terrorsl/smqttbroker/llms.txt Automatically maps TCPClient and TCPServer to platform-specific WiFi types. Define `SMQTT_WT32_ETH01` before including for WT32-ETH01 boards. Includes notes on debug logging for ESP8266 and ESP32. ```cpp // For WT32-ETH01 (Ethernet ESP32 board), define before including: #define SMQTT_WT32_ETH01 #include // For ESP8266: TCPClient = WiFiClient, TCPServer = WiFiServer (ESP8266WiFi.h) // For ESP32: TCPClient = WiFiClient, TCPServer = WiFiServer (WiFi.h) // Debug logging on ESP8266 (outputs via DEBUG_ESP_PORT if defined): // #define DEBUG_ESP_PORT Serial // Debug logging on ESP32 uses ESP_LOGD with tag "sMQTTBroker" ``` -------------------------------- ### sMQTTTopic Class Definition Source: https://github.com/terrorsl/smqttbroker/blob/main/doc/html/s_m_q_t_t_topic_8h_source.html Defines the sMQTTTopic class, its constructors, and methods for managing topic data, matching, and subscriptions. Includes members for topic name, payload, QoS, and a list of subscribed clients. ```cpp #define SINGLE_LEVEL_WILDCARD "+" #define TOPIC_LEVEL_SEPARATOR "/" #define MULTI_LEVEL_WILDCARD "#" #include class sMQTTTopic { public: sMQTTTopic(const char *name,char QoS=0); sMQTTTopic(const char *name, unsigned short nameLen, const char *payload, unsigned short payloadLen); sMQTTTopic(std::string &name,std::string &payload, char QoS=0); sMQTTTopic(sMQTTTopic *other); virtual ~sMQTTTopic(); void update(sMQTTTopic *other) { if (_payload) delete[] _payload; _name = other->Name(); if (other->Payload()) { _payload = new char[strlen(other->Payload()) + 1]; strcpy(_payload, other->Payload()); } else _payload = 0; qos = other->QoS(); } const char *Name() { return _name.c_str(); } const char *Payload() { return _payload; } const unsigned char QoS() { return qos; } bool match(sMQTTTopic *other); bool match(const std::string &other); void subscribe(sMQTTClient *client); bool unsubscribe(sMQTTClient *client) { sMQTTClientList::iterator cli; for (cli = clients.begin(); cli != clients.end(); cli++)if (*cli == client) { clients.erase(cli); break; } return clients.empty(); } const sMQTTClientList getSubscribeList() { return clients; } protected: std::string _name; char *_payload; unsigned char qos; unsigned short _payloadLen; // subscribe to topic sMQTTClientList clients; }; typedef std::vector sMQTTTopicList; ``` -------------------------------- ### sMQTTMessage Class Overview Source: https://github.com/terrorsl/smqttbroker/blob/main/doc/html/s_m_q_t_t_message_8h_source.html This section details the sMQTTMessage class, its constructors, and public methods for managing MQTT messages. ```APIDOC ## Class sMQTTMessage ### Description Represents an MQTT message, handling its construction, parsing, and transmission. ### Methods #### Constructor - `sMQTTMessage()`: Default constructor. - `sMQTTMessage(Type t, unsigned char bits_d3_d0 = 0)`: Constructor to initialize with a specific message type and flags. #### Public Methods - `void incoming(char byte)`: Processes a single incoming byte of the message. - `void add(char byte)`: Alias for `incoming(char byte)`. - `void add(const char* p, size_t len, bool addLength = true)`: Adds a block of data to the message buffer. Optionally prepends length information. - `void add(const std::string &str)`: Adds a string to the message buffer. - `const char* end() const`: Returns a pointer to the end of the message buffer. - `const char* getVHeader() const`: Returns a pointer to the variable header section of the message. - `void reset()`: Resets the message object to an initial state. - `Type type() const`: Returns the type of the message if it's complete, otherwise `Unknown`. - `unsigned char QoS()`: Returns the Quality of Service level of the message. - `bool isRetained()`: Checks if the message is retained. - `sMQTTError sendTo(sMQTTClient *client, bool needRecalc = true)`: Sends the message to the specified client. #### Static Methods - `static void getString(const char* &buff, unsigned short &len)`: Extracts a length-prefixed string from a buffer. ### Enums #### Type Represents the different types of MQTT messages. - `Unknown = 0` - `Connect = 0x10` - `ConnAck = 0x20` - `Publish = 0x30` - `PubAck = 0x40` - `PubRec = 0x50` - `PubRel = 0x60` - `PubComp = 0x70` - `Subscribe = 0x80` - `SubAck = 0x90` - `UnSubscribe = 0xA0` - `UnSuback = 0xB0` - `PingReq = 0xC0` - `PingResp = 0xD0` - `Disconnect = 0xE0` #### State Represents the current parsing or construction state of the message. - `FixedHeader = 0` - `Length = 1` - `VariableHeader = 2` - `PayLoad = 3` - `Complete = 4` - `Error = 5` - `Create = 6 ``` -------------------------------- ### sMQTTLostConnectionEvent() Source: https://github.com/terrorsl/smqttbroker/blob/main/doc/html/classs_m_q_t_t_lost_connection_event-members.html Constructor for sMQTTLostConnectionEvent. Initializes a new instance of the sMQTTLostConnectionEvent class. ```APIDOC ## sMQTTLostConnectionEvent() ### Description Constructor for sMQTTLostConnectionEvent. Initializes a new instance of the sMQTTLostConnectionEvent class. ### Method `sMQTTLostConnectionEvent()` ### Parameters None ### Returns An instance of sMQTTLostConnectionEvent. ``` -------------------------------- ### Windows Platform Definitions Source: https://github.com/terrorsl/smqttbroker/blob/main/doc/html/s_m_q_t_tplatform_8h_source.html Defines dummy TCPClient and TCPServer classes for Windows when no specific network library is included. Use this when targeting Windows without specific network dependencies. ```cpp #define SMQTT_DEPRECATED(msg) [[deprecated(msg)]] #if defined(WIN32) class TCPClient { public: bool available() { return false; }; char read() { return 0; } bool connected() { return false; } void stop() {} void write(const char *, int) {} }; class TCPServer { public: TCPServer(short) {} void begin() {} }; #define SMQTT_LOGD ``` -------------------------------- ### Handle Subscribe/Unsubscribe Events Source: https://context7.com/terrorsl/smqttbroker/llms.txt Fired for both subscribe and unsubscribe actions. Provides the client ID and the topic filter, which may include wildcards. ```cpp case Subscribe_sMQTTEventType: { auto *e = static_cast(event); Serial.printf("SUB client=%s filter=%s\n", e->Client()->getClientId().c_str(), e->Topic().c_str()); break; } ``` ```cpp case UnSubscribe_sMQTTEventType: { auto *e = static_cast(event); Serial.printf("UNSUB client=%s filter=%s\n", e->Client()->getClientId().c_str(), e->Topic().c_str()); break; } ``` -------------------------------- ### Internal message creation helper Source: https://github.com/terrorsl/smqttbroker/blob/main/doc/html/s_m_q_t_t_message_8h_source.html Private helper function to initialize the message buffer with the message type and reserve space for the length. Sets the initial state to 'Create'. ```cpp void create(Type type) { buffer.push_back((char)type); buffer.push_back('\0'); // reserved for msg length vheader = 2; size = 0; state = Create; } ``` -------------------------------- ### sMQTTBroker::restart Source: https://github.com/terrorsl/smqttbroker/blob/main/doc/html/s_m_q_t_t_broker_8h_source.html Restarts the MQTT broker's Wi-Fi server. ```APIDOC ## restart ### Description Restarts the MQTT broker's Wi-Fi server. ### Method void ### Definition sMQTTBroker.h:22 ``` -------------------------------- ### Publish Messages with `sMQTTBroker::publish` Source: https://context7.com/terrorsl/smqttbroker/llms.txt Publishes a message from the broker itself to all clients subscribed to the given topic. Supports QoS 0 and QoS 1. The `retain` flag causes the message to be stored and immediately delivered to new subscribers. ```cpp #include sMQTTBroker broker; void setup() { WiFi.begin("MySSID", "MyPassword"); while (WiFi.status() != WL_CONNECTED) delay(500); broker.init(1883); // Publish a retained status message at QoS 1 broker.publish("home/status", "online", 1, true); } void loop() { broker.update(); // Publish sensor reading every 5 seconds at QoS 0 static unsigned long last = 0; if (millis() - last > 5000) { last = millis(); float temp = 22.5; // read from sensor broker.publish("home/sensor/temperature", String(temp).c_str(), 0, false); } } ``` -------------------------------- ### sMQTTMessage Public Member Functions Source: https://github.com/terrorsl/smqttbroker/blob/main/doc/html/classs_m_q_t_t_message.html Provides methods for constructing, parsing, and managing MQTT messages. ```APIDOC ## sMQTTMessage Public Member Functions ### Constructor ```cpp sMQTTMessage(Type t, unsigned char bits_d3_d0 = 0) ``` Constructs an sMQTTMessage object with a specified type and initial bits. ### `incoming` ```cpp void incoming(char byte) ``` Processes a single incoming byte for message parsing. ### `add` (overloaded) ```cpp void add(char byte) void add(const char *p, size_t len, bool addLength = true) void add(const std::string &str) ``` Adds data to the message. Overloaded versions handle single bytes, character arrays with optional length prefix, and std::string objects. ### `end` ```cpp const char *end() const ``` Returns a pointer to the end of the message data. ### `getVHeader` ```cpp const char *getVHeader() const ``` Returns a pointer to the variable header of the MQTT message. ### `reset` ```cpp void reset() ``` Resets the message object to its initial state. ### `type` ```cpp Type type() const ``` Returns the type of the MQTT message. ### `QoS` ```cpp unsigned char QoS() ``` Returns the Quality of Service level of the message. ### `isRetained` ```cpp bool isRetained() const ``` Checks if the message is retained. ### `sendTo` ```cpp sMQTTError sendTo(sMQTTClient *client, bool needRecalc = true) ``` Sends the MQTT message to the specified client. `needRecalc` indicates if the length needs recalculation. ``` -------------------------------- ### publish() Source: https://github.com/terrorsl/smqttbroker/blob/main/doc/html/functions_func.html Publishes a message through the sMQTTBroker. ```APIDOC ## publish() ### Description Publishes a message through the sMQTTBroker. ### Method (Likely a core messaging function) ### Parameters None explicitly documented. ### Response None explicitly documented. ``` -------------------------------- ### publish() Source: https://github.com/terrorsl/smqttbroker/blob/main/doc/html/functions.html Publishes a message using the sMQTTBroker. This is a class method. ```APIDOC ## publish() ### Description Publishes a message using the sMQTTBroker. ### Method (Not specified) ### Endpoint N/A ### Parameters None explicitly documented. ### Request Example N/A ### Response N/A ``` -------------------------------- ### sMQTTNewClientEvent Class Source: https://github.com/terrorsl/smqttbroker/blob/main/doc/html/s_m_q_t_t_event_8h_source.html Represents an event when a new client connects to the MQTT broker. ```APIDOC ## sMQTTNewClientEvent ### Description Represents an event when a new client connects to the MQTT broker. It stores information about the connected client, login, and password. ### Methods - **sMQTTNewClientEvent([sMQTTClient](classs_m_q_t_t_client.html) *client, std::string &login, std::string &password)**: Constructor. - **Client()**: Returns a pointer to the connected client. - **Login()**: Returns the login username. - **Password()**: Returns the password. ``` -------------------------------- ### sMQTTMessage Constructor with Type Source: https://github.com/terrorsl/smqttbroker/blob/main/doc/html/s_m_q_t_t_message_8h_source.html Constructs an sMQTTMessage with a specified type and optional initial bits for the fixed header. Used for creating outgoing messages. ```cpp sMQTTMessage(Type t, unsigned char bits_d3_d0 = 0); ``` -------------------------------- ### ESP32 Platform Definitions Source: https://github.com/terrorsl/smqttbroker/blob/main/doc/html/s_m_q_t_tplatform_8h_source.html Includes WiFi.h for ESP32 and defines TCPClient and TCPServer using WiFiClient and WiFiServer. It also defines a logging macro SMQTT_LOGD using ESP_LOGD for ESP32. ```cpp #elif defined(ESP32) #include #ifdef SMQTT_WT32_ETH01 #include #endif #define TCPClient WiFiClient #define TCPServer WiFiServer static const char *SMQTTTAG = "sMQTTBroker"; #define SMQTT_LOGD(...) ESP_LOGD(SMQTTTAG,__VA_ARGS__) #else #error "unknown platform" ``` -------------------------------- ### sMQTTTopic Methods Source: https://github.com/terrorsl/smqttbroker/blob/main/doc/html/classs_m_q_t_t_topic-members.html Methods available for the sMQTTTopic class. ```APIDOC ## getSubscribeList() ### Description Retrieves the list of subscribers for the topic. ### Returns (List of subscribers) ``` ```APIDOC ## match(sMQTTTopic *other) ### Description Checks if this topic matches another sMQTTTopic. ### Parameters - **other** (sMQTTTopic *) - The other sMQTTTopic to compare with. ### Returns (Boolean indicating if topics match) ``` ```APIDOC ## match(const std::string &other) ### Description Checks if this topic matches a given topic name string. ### Parameters - **other** (const std::string &) - The topic name string to compare with. ### Returns (Boolean indicating if topics match) ``` ```APIDOC ## Name() ### Description Gets the name of the topic. ### Returns (Topic name) ``` ```APIDOC ## Payload() ### Description Gets the payload of the topic. ### Returns (Topic payload) ``` ```APIDOC ## QoS() ### Description Gets the Quality of Service level of the topic. ### Returns (QoS level) ``` ```APIDOC ## subscribe(sMQTTClient *client) ### Description Subscribes a client to this topic. ### Parameters - **client** (sMQTTClient *) - The client to subscribe. ``` ```APIDOC ## unsubscribe(sMQTTClient *client) ### Description Unsubscribes a client from this topic. ### Parameters - **client** (sMQTTClient *) - The client to unsubscribe. ``` ```APIDOC ## update(sMQTTTopic *other) ### Description Updates this topic with the content of another sMQTTTopic. ### Parameters - **other** (sMQTTTopic *) - The other sMQTTTopic to update from. ``` -------------------------------- ### Password() Source: https://github.com/terrorsl/smqttbroker/blob/main/doc/html/classs_m_q_t_t_new_client_event.html Retrieves the password string for the new client. ```APIDOC ## Password() ### Description Gets the password for the newly connected client. ### Signature std::string Password() ### Returns The client's password. ``` -------------------------------- ### sMQTTTopicRetain Constructor Source: https://github.com/terrorsl/smqttbroker/blob/main/doc/html/classs_m_q_t_t_topic_retain-members.html Constructor for the sMQTTTopicRetain class. ```APIDOC ## sMQTTTopicRetain(const char *name, const char *payload) ### Description Constructor for the sMQTTTopicRetain class. ### Parameters #### Path Parameters - **name** (const char *) - Description not available - **payload** (const char *) - Description not available ``` -------------------------------- ### update() Source: https://github.com/terrorsl/smqttbroker/blob/main/doc/html/functions_func.html Updates the sMQTTBroker configuration or state. ```APIDOC ## update() ### Description Updates the sMQTTBroker configuration or state. ### Method (Likely a configuration or control function) ### Parameters None explicitly documented. ### Response None explicitly documented. ``` -------------------------------- ### sMQTTMessage Constructors Source: https://github.com/terrorsl/smqttbroker/blob/main/doc/html/classs_m_q_t_t_message-members.html Constructors for creating sMQTTMessage objects. ```APIDOC ## sMQTTMessage Constructors ### Description Initializes a new instance of the sMQTTMessage class. ### Constructors - `sMQTTMessage()` - `sMQTTMessage(Type t, unsigned char bits_d3_d0 = 0)` ``` -------------------------------- ### sMQTTMessage Static Public Member Functions Source: https://github.com/terrorsl/smqttbroker/blob/main/doc/html/classs_m_q_t_t_message.html Provides static utility functions for string manipulation within MQTT messages. ```APIDOC ## sMQTTMessage Static Public Member Functions ### `getString` ```cpp static void getString(const char *&buff, unsigned short &len) ``` Extracts a string from a buffer, updating the buffer pointer and length. This is likely used for parsing UTF-8 encoded strings in MQTT headers. ``` -------------------------------- ### write Source: https://github.com/terrorsl/smqttbroker/blob/main/doc/html/classs_m_q_t_t_client.html Writes data to the MQTT client's output stream. ```APIDOC ## write ### Description Sends a buffer of data to the connected MQTT broker. ### Method `void write(const char *buf, size_t length)` ### Parameters * **buf** (*const char **) - A pointer to the buffer containing the data to be sent. * **length** (*size_t*) - The number of bytes to send from the buffer. ``` -------------------------------- ### sMQTTNewClientEvent Constructors Source: https://github.com/terrorsl/smqttbroker/blob/main/doc/html/classs_m_q_t_t_new_client_event-members.html Constructors for the sMQTTNewClientEvent class. ```APIDOC ## sMQTTNewClientEvent(sMQTTClient *, std::string &, std::string &) ### Description Constructor for sMQTTNewClientEvent. ### Parameters - **sMQTTClient ofoam** (sMQTTClient *) - Pointer to sMQTTClient. - **std::string &** - First string parameter. - **std::string &** - Second string parameter. ``` -------------------------------- ### sMQTTTopicRetain Constructor Source: https://github.com/terrorsl/smqttbroker/blob/main/doc/html/classs_m_q_t_t_topic_retain.html Constructs a new sMQTTTopicRetain object with a given name and payload. ```APIDOC ## sMQTTTopicRetain(const char *name, const char *payload) ### Description Constructs a new sMQTTTopicRetain object. ### Parameters * **name** (const char *) - The name of the topic. * **payload** (const char *) - The payload of the retained message. ``` -------------------------------- ### QoS Source: https://github.com/terrorsl/smqttbroker/blob/main/doc/html/classs_m_q_t_t_topic.html Retrieves the Quality of Service level for the topic. ```APIDOC ## QoS ### Description Returns the Quality of Service (QoS) level configured for this topic. ### Method `const unsigned char QoS()` ### Returns - `const unsigned char` - The QoS level (0, 1, or 2). ``` -------------------------------- ### sMQTTNewClientEvent Methods Source: https://github.com/terrorsl/smqttbroker/blob/main/doc/html/classs_m_q_t_t_new_client_event-members.html Public methods available for sMQTTNewClientEvent. ```APIDOC ## Client() ### Description Represents a client. ### Method (Implicitly public, as it's a constructor-like name in a member list) ### Endpoint N/A (Class method) ``` ```APIDOC ## Login() ### Description Handles client login. ### Method (Implicitly public, as it's a method name in a member list) ### Endpoint N/A (Class method) ``` ```APIDOC ## Password() ### Description Handles client password. ### Method (Implicitly public, as it's a method name in a member list) ### Endpoint N/A (Class method) ``` -------------------------------- ### Topic Source: https://github.com/terrorsl/smqttbroker/blob/main/doc/html/classs_m_q_t_t_public_client_event.html Returns the topic of the client event. ```APIDOC ## Topic ### Description Returns the topic associated with this client event. ### Returns - std::string - The topic of the event. ### Method std::string ``` -------------------------------- ### sMQTTMessage default constructor Source: https://github.com/terrorsl/smqttbroker/blob/main/doc/html/s_m_q_t_t_message_8h_source.html Default constructor for sMQTTMessage. Initializes an empty message object. ```cpp sMQTTMessage(); ``` -------------------------------- ### sMQTTPublicClientEvent Constructor Source: https://github.com/terrorsl/smqttbroker/blob/main/doc/html/classs_m_q_t_t_public_client_event.html Constructs a new sMQTTPublicClientEvent object. ```APIDOC ## sMQTTPublicClientEvent ### Description Constructs a new sMQTTPublicClientEvent object with the specified client and topic. ### Parameters #### Path Parameters - **client** (*sMQTTClient**) - **topic** (const std::string &) ### Method Constructor ``` -------------------------------- ### sMQTTEvent Constructor Source: https://github.com/terrorsl/smqttbroker/blob/main/doc/html/classs_m_q_t_t_event-members.html Constructs an sMQTTEvent object with a specified type. ```APIDOC ## sMQTTEvent(unsigned char type) ### Description Constructor for the sMQTTEvent class. ### Parameters #### Path Parameters - **type** (unsigned char) - Description of the type parameter. ``` -------------------------------- ### sMQTTEvent Constructor Source: https://github.com/terrorsl/smqttbroker/blob/main/doc/html/classs_m_q_t_t_event.html Constructs an sMQTTEvent object with a specified type. ```APIDOC ## sMQTTEvent ### Description Constructor for the sMQTTEvent class. ### Parameters #### Path Parameters - **type** (unsigned char) - Description of the event type. ``` -------------------------------- ### sMQTTSubUnSubClientEvent Constructors Source: https://github.com/terrorsl/smqttbroker/blob/main/doc/html/classs_m_q_t_t_sub_un_sub_client_event-members.html Constructors for the sMQTTSubUnSubClientEvent class. ```APIDOC ## Client() ### Description Constructor for sMQTTSubUnSubClientEvent. ### Method `Client()` ### Parameters None ``` ```APIDOC ## sMQTTSubUnSubClientEvent(unsigned char type, sMQTTClient *client, const std::string &topic) ### Description Constructor for sMQTTSubUnSubClientEvent. ### Method `sMQTTSubUnSubClientEvent(unsigned char type, sMQTTClient *client, const std::string &topic)` ### Parameters - **type** (unsigned char) - The type of the event. - **client** (sMQTTClient *) - The client associated with the event. - **topic** (const std::string &) - The topic associated with the event. ``` -------------------------------- ### Handle All Broker Events with sMQTTBroker::onEvent Source: https://context7.com/terrorsl/smqttbroker/llms.txt Override this virtual method to process all significant broker events. Cast the `sMQTTEvent*` to the appropriate subclass. Return `false` from `NewClient_sMQTTEventType` to reject a connection. ```cpp #include class MyBroker : public sMQTTBroker { public: bool onEvent(sMQTTEvent *event) override { switch (event->Type()) { case NewClient_sMQTTEventType: { auto *e = static_cast(event); Serial.printf("Client connected: id=%s login=%s\n", e->Client()->getClientId().c_str(), e->Login().c_str()); // Reject clients with wrong credentials if (e->Login() != "admin" || e->Password() != "secret") { return false; // deny connection } return true; // allow connection } case RemoveClient_sMQTTEventType: { auto *e = static_cast(event); Serial.printf("Client disconnected: id=%s\n", e->Client()->getClientId().c_str()); break; } case Public_sMQTTEventType: { auto *e = static_cast(event); Serial.printf("Publish from %s topic=%s payload=%s\n", e->Client()->getClientId().c_str(), e->Topic().c_str(), e->Payload().c_str()); break; } case Subscribe_sMQTTEventType: { auto *e = static_cast(event); Serial.printf("Subscribe: client=%s topic=%s\n", e->Client()->getClientId().c_str(), e->Topic().c_str()); break; } case UnSubscribe_sMQTTEventType: { auto *e = static_cast(event); Serial.printf("Unsubscribe: client=%s topic=%s\n", e->Client()->getClientId().c_str(), e->Topic().c_str()); break; } case LostConnect_sMQTTEventType: { Serial.println("Broker lost WiFi connection"); break; } } return true; } }; MyBroker broker; void setup() { Serial.begin(115200); WiFi.begin("MySSID", "MyPassword"); while (WiFi.status() != WL_CONNECTED) delay(500); broker.init(1883, true); } void loop() { broker.update(); } ``` -------------------------------- ### update() Source: https://github.com/terrorsl/smqttbroker/blob/main/doc/html/functions.html Updates the sMQTTBroker. This is a class method. ```APIDOC ## update() ### Description Updates the sMQTTBroker. ### Method (Not specified) ### Endpoint N/A ### Parameters None explicitly documented. ### Request Example N/A ### Response N/A ``` -------------------------------- ### ESP8266 Platform Definitions Source: https://github.com/terrorsl/smqttbroker/blob/main/doc/html/s_m_q_t_tplatform_8h_source.html Includes ESP8266 WiFi library and defines TCPClient and TCPServer to use WiFiClient and WiFiServer respectively. Conditional logging is enabled if DEBUG_ESP_PORT is defined. ```cpp #elif defined(ESP8266) #include #define TCPClient WiFiClient #define TCPServer WiFiServer #if defined(DEBUG_ESP_PORT) #define SMQTT_LOGD(...) DEBUG_ESP_PORT.printf(__VA_ARGS__) #else #define SMQTT_LOGD(...) #endif ``` -------------------------------- ### sMQTTMessage::QoS Source: https://github.com/terrorsl/smqttbroker/blob/main/doc/html/classs_m_q_t_t_message-members.html Returns the Quality of Service level of the message. ```APIDOC ## sMQTTMessage::QoS ### Description Gets the Quality of Service level of the message. ### Method `unsigned char QoS()` ``` -------------------------------- ### sMQTTMessage::getString Source: https://github.com/terrorsl/smqttbroker/blob/main/doc/html/classs_m_q_t_t_message-members.html Retrieves a string from a buffer. ```APIDOC ## sMQTTMessage::getString ### Description Extracts a string from a given buffer. ### Method `static const char* getString(const char *&buff, unsigned short &len)` ``` -------------------------------- ### sMQTTEvent Class Source: https://github.com/terrorsl/smqttbroker/blob/main/doc/html/s_m_q_t_t_event_8h_source.html Base class for all MQTT events. It holds the type of the event. ```APIDOC ## sMQTTEvent ### Description Base class for all MQTT events. It holds the type of the event. ### Methods - **sMQTTEvent(unsigned char type)**: Constructor. - **Type()**: Returns the type of the event. ``` -------------------------------- ### sMQTTTopic Class Source: https://github.com/terrorsl/smqttbroker/blob/main/doc/html/s_m_q_t_t_topic_8h_source.html The sMQTTTopic class manages MQTT topics, including their names, payloads, QoS levels, and subscriptions. ```APIDOC ## Class sMQTTTopic ### Description Represents a topic in the sMQTT broker, handling topic name, payload, Quality of Service (QoS), and managing subscribed clients. ### Methods * **sMQTTTopic(const char *name, char QoS=0)**: Constructor for creating a topic with a name and QoS. * **sMQTTTopic(const char *name, unsigned short nameLen, const char *payload, unsigned short payloadLen)**: Constructor for creating a topic with name, payload, and their lengths. * **sMQTTTopic(std::string &name, std::string &payload, char QoS=0)**: Constructor using std::string for name and payload. * **sMQTTTopic(sMQTTTopic *other)**: Copy constructor. * **~sMQTTTopic()**: Virtual destructor. * **update(sMQTTTopic *other)**: Updates the topic's payload and QoS from another topic. * **Name()**: Returns the topic's name. * **Payload()**: Returns the topic's payload. * **QoS()**: Returns the topic's QoS level. * **match(sMQTTTopic *other)**: Checks if this topic matches another topic. * **match(const std::string &other)**: Checks if this topic matches a given string. * **subscribe(sMQTTClient *client)**: Subscribes a client to this topic. * **unsubscribe(sMQTTClient *client)**: Unsubscribes a client from this topic. Returns true if the topic has no more subscribers. * **getSubscribeList()**: Returns a list of subscribed clients. ### Protected Members * **std::string _name**: The name of the topic. * **char *_payload**: Pointer to the topic's payload. * **unsigned char qos**: The Quality of Service level. * **unsigned short _payloadLen**: The length of the payload. * **sMQTTClientList clients**: A list of clients subscribed to this topic. ``` -------------------------------- ### sMQTTMessage::add Source: https://github.com/terrorsl/smqttbroker/blob/main/doc/html/classs_m_q_t_t_message-members.html Adds data to the message. Overloaded for different data types like char, const char*, and std::string. ```APIDOC ## sMQTTMessage::add ### Description Adds data to the message payload. ### Methods - `void add(char byte)` - `void add(const char *p, size_t len, bool addLength = true)` - `void add(const std::string &str)` ``` -------------------------------- ### onEvent() Source: https://github.com/terrorsl/smqttbroker/blob/main/doc/html/functions_func.html Processes incoming events for the sMQTTBroker. ```APIDOC ## onEvent() ### Description Processes incoming events for the sMQTTBroker. ### Method (Likely an event handler or callback) ### Parameters None explicitly documented. ### Response None explicitly documented. ```