StackMQ is an Arduino library that simplifies MQTT communication with a custom MQTT broker. It supports JWT-based authentication, secure connections, and automated topic subscription, making it ideal for IoT applications.
- JWT Authentication: Handles authentication with JWT tokens for your custom MQTT broker.
- Secure Connections: Supports TLS/SSL for secure communication.
- Lightweight and Fast: Minimal overhead for embedded systems.
- Easy to Use: Simple API for connecting, subscribing, and receiving messages.
- Open the Arduino IDE.
- Go to Tools > Manage Libraries....
- Search for
StackMQ
and click Install.
StackMQ relies on the following libraries, which will be installed automatically when you install StackMQ via the Arduino Library Manager:
-
- MQTT client library for Arduino.
- Used for connecting to the MQTT broker and managing subscriptions.
-
- For parsing JSON data, including JWT payloads.
-
- Used for decoding Base64-encoded JWT tokens.
#include "stackmq.h"
// Create an instance of Stackmq
Stackmq stackmq;
// Callback function to handle received MQTT messages
void callback(String message) {
Serial.println("Received message: " + message);
}
void setup() {
Serial.begin(115200);
// WiFi credentials
const char* ssid = "YOUR_WIFI_SSID";
const char* password = "YOUR_WIFI_PASSWORD";
// JWT token as String
String jwtToken = "YOUR_JWT_TOKEN";
// Initialize the Stackmq library
stackmq.begin(ssid, password, jwtToken, callback);
}
void loop() {
stackmq.loop(); // Continuously handle MQTT events
}