> For the complete documentation index, see [llms.txt](https://docs.consentiumiot.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.consentiumiot.com/readme/using-the-consentium-iot-public-mqtt-broker.md).

# Using the Consentium IoT Public MQTT Broker

This guide provides developers with the necessary information to connect to the Consentium IoT public MQTT broker. Our broker is designed to facilitate lightweight, real-time data exchange for your IoT projects, including those utilizing our proprietary hardware such as the Edge Dalton board.

### Connection Details

* Broker Address: `broker.consentiumiot.com`
* Port: `1883` (Non-secure TCP)
* Protocol: MQTT (Message Queuing Telemetry Transport)

### Python Implementation

To interact with the broker in Python, we recommend using the `paho-mqtt` library.

#### Prerequisites

Install the library via terminal:

Bash

```
pip install paho-mqtt
```

#### Example Code

Python

```python
import paho.mqtt.client as mqtt
import time

# Broker settings
BROKER = "broker.consentiumiot.com"
PORT = 1883
TOPIC = "consentium/test/topic"

def on_connect(client, userdata, flags, rc):
    if rc == 0:
        print("Connected to Consentium Broker")
        client.subscribe(TOPIC)
    else:
        print(f"Failed to connect, return code {rc}")

def on_message(client, userdata, msg):
    print(f"Message received on {msg.topic}: {msg.payload.decode()}")

# Setup client
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message

# Connect and loop
client.connect(BROKER, PORT, 60)
client.loop_start()

# Publish a test message
client.publish(TOPIC, "Hello from Consentium IoT!")

try:
    while True:
        time.sleep(1)
except KeyboardInterrupt:
    client.loop_stop()
```

### ESP32 Implementation

For ESP32 development, the `PubSubClient` library is the standard choice for handling MQTT communication.

#### Prerequisites

1. Ensure the ESP32 Board Support is installed in your Arduino IDE.
2. Install the PubSubClient library via the Library Manager.

#### Example Code

C++

```cpp
#include <WiFi.h>
#include <PubSubClient.h>

const char* ssid = "YOUR_WIFI_SSID";
const char* password = "YOUR_WIFI_PASSWORD";
const char* mqtt_server = "broker.consentiumiot.com";

WiFiClient espClient;
PubSubClient client(espClient);

void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  
  client.setServer(mqtt_server, 1883);
}

void loop() {
  if (!client.connected()) {
    // Unique ID based on MAC address
    String clientId = "ESP32Client-" + String(WiFi.macAddress());
    if (client.connect(clientId.c_str())) {
      Serial.println("Connected to Broker");
    }
  }
  
  client.loop();
  
  // Publish example data
  client.publish("consentium/edge/dalton", "{\"status\": true, \"value\": 42}");
  delay(5000);
}
```

### Best Practices

* Client IDs: Always use unique Client IDs for each device. Using `WiFi.macAddress()` is a reliable way to ensure uniqueness for ESP32 devices.
* Payloads: While our broker supports raw strings, we recommend using JSON format for structured data, ensuring you follow schema requirements (e.g., using `true`/`false` for boolean feature states).
* QoS: Use QoS 0 for telemetry data to maintain low latency, and QoS 1 if message delivery guarantees are critical for your application.
* Non-blocking: Keep your `loop()` functions non-blocking to ensure the MQTT client can maintain its connection heartbeats.
