/**
* Simple MQTT Test for Wokwi
* Tests authentication and MQTT subscription
*/
#include <Arduino.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include <PubSubClient.h>
// ============================================================================
// CONFIGURATION
// ============================================================================
// WiFi credentials (use Wokwi WiFi)
const char* WIFI_SSID = "Wokwi-GUEST";
const char* WIFI_PASSWORD = "";
// Server configuration
const char* SERVER_URL = "http://103.136.236.16";
const char* MQTT_BROKER = "103.136.236.16";
const int MQTT_PORT = 1883;
// Device credentials (your actual credentials)
const char* DEVICE_ID = "wt001-DEV-02-690e6a9d092433c0acfb9178";
const char* HARDWARE_ID = "ESP32";
const char* USERNAME = "[email protected]";
const char* PASSWORD = "{Password}";
// ============================================================================
// GLOBALS
// ============================================================================
WiFiClient wifiClient;
PubSubClient mqttClient(wifiClient);
HTTPClient http;
char jwtToken[512] = "";
bool authenticated = false;
// ============================================================================
// MQTT CALLBACK
// ============================================================================
void mqttCallback(char* topic, byte* payload, unsigned int length) {
Serial.println("\n============================================================");
Serial.printf("šØ Message received on topic: %s\n", topic);
Serial.print("Payload: ");
// Print payload
for (unsigned int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
Serial.println("============================================================\n");
}
// ============================================================================
// WIFI CONNECTION
// ============================================================================
bool connectWiFi() {
Serial.println("\nš” Connecting to WiFi...");
Serial.printf("SSID: %s\n", WIFI_SSID);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
int attempts = 0;
while (WiFi.status() != WL_CONNECTED && attempts < 20) {
delay(500);
Serial.print(".");
attempts++;
}
if (WiFi.status() == WL_CONNECTED) {
Serial.println("\nā
WiFi connected!");
Serial.printf("IP Address: %s\n", WiFi.localIP().toString().c_str());
return true;
} else {
Serial.println("\nā WiFi connection failed");
return false;
}
}
// ============================================================================
// HTTP AUTHENTICATION
// ============================================================================
bool authenticate() {
Serial.println("\nš Authenticating with server...");
Serial.printf("Username: %s\n", USERNAME);
String url = String(SERVER_URL) + "/api/auth/login";
http.begin(url);
http.addHeader("Content-Type", "application/json");
// Create JSON payload
JsonDocument doc;
doc["username"] = USERNAME;
doc["password"] = PASSWORD;
String payload;
serializeJson(doc, payload);
Serial.printf("POST %s\n", url.c_str());
Serial.printf("Body: %s\n", payload.c_str());
int httpCode = http.POST(payload);
if (httpCode > 0) {
Serial.printf("HTTP Response: %d\n", httpCode);
if (httpCode == 200) {
String response = http.getString();
Serial.printf("Response: %s\n", response.c_str());
// Parse response
JsonDocument responseDoc;
DeserializationError error = deserializeJson(responseDoc, response);
if (!error) {
if (responseDoc.containsKey("token")) {
String token = responseDoc["token"].as<String>();
strncpy(jwtToken, token.c_str(), sizeof(jwtToken) - 1);
Serial.println("ā
Authentication successful!");
Serial.printf("JWT Token: %s\n", jwtToken);
http.end();
return true;
} else {
Serial.println("ā No token in response");
}
} else {
Serial.printf("ā JSON parse error: %s\n", error.c_str());
}
} else {
String response = http.getString();
Serial.printf("ā Authentication failed: %s\n", response.c_str());
}
} else {
Serial.printf("ā HTTP request failed: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
return false;
}
// ============================================================================
// MQTT CONNECTION
// ============================================================================
bool connectMQTT() {
Serial.println("\nš” Connecting to MQTT broker...");
Serial.printf("Broker: %s:%d\n", MQTT_BROKER, MQTT_PORT);
Serial.printf("Client ID: esp32-%s\n", HARDWARE_ID);
Serial.printf("Username: %s\n", DEVICE_ID);
Serial.printf("Password (JWT): %s\n", jwtToken);
// Create client ID
String clientId = "esp32-" + String(HARDWARE_ID);
// Create Last Will and Testament
String willTopic = "devices/" + String(DEVICE_ID) + "/status";
JsonDocument willDoc;
willDoc["status"] = "offline";
willDoc["timestamp"] = millis();
String willPayload;
serializeJson(willDoc, willPayload);
Serial.printf("Will Topic: %s\n", willTopic.c_str());
Serial.printf("Will Payload: %s\n", willPayload.c_str());
// Connect to MQTT
bool connected = mqttClient.connect(
clientId.c_str(), // Client ID
DEVICE_ID, // Username (deviceId)
jwtToken, // Password (JWT token)
willTopic.c_str(), // Will topic
1, // Will QoS
true, // Will retain
willPayload.c_str() // Will message
);
if (connected) {
Serial.println("ā
MQTT connected!");
// Publish online status
String statusTopic = "devices/" + String(DEVICE_ID) + "/status";
JsonDocument statusDoc;
statusDoc["status"] = "online";
statusDoc["timestamp"] = millis();
String statusPayload;
serializeJson(statusDoc, statusPayload);
mqttClient.publish(statusTopic.c_str(), statusPayload.c_str(), true);
Serial.printf("š¤ Published to %s: %s\n", statusTopic.c_str(), statusPayload.c_str());
// Subscribe to topics
String controlTopic = "devices/" + String(DEVICE_ID) + "/control";
String configTopic = "devices/" + String(DEVICE_ID) + "/config";
String telemetryTopic = "devices/" + String(DEVICE_ID) + "/telemetry";
mqttClient.subscribe(controlTopic.c_str(), 1);
Serial.printf("š” Subscribed to: %s\n", controlTopic.c_str());
mqttClient.subscribe(configTopic.c_str(), 1);
Serial.printf("š” Subscribed to: %s\n", configTopic.c_str());
mqttClient.subscribe(telemetryTopic.c_str(), 1);
Serial.printf("š” Subscribed to: %s\n", telemetryTopic.c_str());
return true;
} else {
int state = mqttClient.state();
Serial.printf("ā MQTT connection failed, rc=%d\n", state);
Serial.println("MQTT Error Codes:");
Serial.println(" -4: MQTT_CONNECTION_TIMEOUT - server didn't respond within keepalive time");
Serial.println(" -3: MQTT_CONNECTION_LOST - network connection broken");
Serial.println(" -2: MQTT_CONNECT_FAILED - network connection failed");
Serial.println(" -1: MQTT_DISCONNECTED - client disconnected cleanly");
Serial.println(" 0: MQTT_CONNECTED - client connected");
Serial.println(" 1: MQTT_CONNECT_BAD_PROTOCOL - server doesn't support requested MQTT version");
Serial.println(" 2: MQTT_CONNECT_BAD_CLIENT_ID - server rejected client identifier");
Serial.println(" 3: MQTT_CONNECT_UNAVAILABLE - server unavailable");
Serial.println(" 4: MQTT_CONNECT_BAD_CREDENTIALS - username/password rejected");
Serial.println(" 5: MQTT_CONNECT_UNAUTHORIZED - client not authorized");
return false;
}
}
// ============================================================================
// SETUP
// ============================================================================
void setup() {
Serial.begin(115200);
delay(1000);
Serial.println("\nāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā");
Serial.println("ā MQTT Authentication Test ā");
Serial.println("āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā\n");
// Connect to WiFi
if (!connectWiFi()) {
Serial.println("ā WiFi connection failed. Halting.");
while (1) delay(1000);
}
// Authenticate
if (!authenticate()) {
Serial.println("ā Authentication failed. Halting.");
while (1) delay(1000);
}
authenticated = true;
// Setup MQTT
mqttClient.setServer(MQTT_BROKER, MQTT_PORT);
mqttClient.setCallback(mqttCallback);
mqttClient.setKeepAlive(60);
mqttClient.setSocketTimeout(30);
// Connect to MQTT
if (!connectMQTT()) {
Serial.println("ā MQTT connection failed. Will retry in loop...");
}
Serial.println("\nā
Setup complete!");
Serial.println("ā³ Waiting for MQTT messages...\n");
}
// ============================================================================
// LOOP
// ============================================================================
void loop() {
// Reconnect if needed
if (!mqttClient.connected()) {
unsigned long now = millis();
static unsigned long lastReconnect = 0;
if (now - lastReconnect > 5000) {
lastReconnect = now;
Serial.println("š Reconnecting to MQTT...");
if (!authenticated) {
if (authenticate()) {
connectMQTT();
}
} else {
connectMQTT();
}
}
}
// Process MQTT
mqttClient.loop();
// Keep alive
delay(10);
}