#include <Ticker.h>
#include <WiFi.h>
#include <PubSubClient.h>
Ticker aiTimer; // Timer
const char *mqttServer1 = "broker.hivemq.com"; // Wokwi only allow this or
const char *mqttServer2 = "mqtt-dashboard.com"; // this one only
const int mqttPort = 1883;
const char* topicStatus = "np/std1/sensor/status";
const char* topicAnalog = "np/std1/sensor/analog";
const char* topicLWT = "np/std1/device/lwt";
const char *topicLed1 = "np/std1/led1";
const char *topicLed2 = "np/std1/led2";
const char *topicAll = "np/std1/all";
WiFiClient wifiClient;
PubSubClient mqttClient(wifiClient);
void reconnect() {
static const char* servers[] = {mqttServer1, mqttServer2};
static int currentServerIndex = 0;
while (!mqttClient.connected()) {
const char* tryServer = servers[currentServerIndex];
// Set the current broker
mqttClient.setServer(tryServer, mqttPort);
String clientID = "np25" + String(random(0xffff), HEX);
Serial.printf("[%s] Attempting MQTT connect to %s ...",
clientID.c_str(), tryServer);
if (mqttClient.connect(clientID.c_str(),
topicLWT, 1, true, "offline")) {
Serial.printf("connected\n");
mqttClient.publish(topicLWT, "online", true);
mqttClient.subscribe(topicLed1);
mqttClient.subscribe(topicLed2);
mqttClient.subscribe(topicAll);
Serial.println("Subscribed to LED control topics");
break; // Exit loop on success
} else {
Serial.printf("failed, rc=%d. ", mqttClient.state());
currentServerIndex = (currentServerIndex + 1) % 2; // Switch to next server
Serial.printf("Trying next broker in 5s...\n");
delay(5000);
}
}
}
void callback(char *topic, byte *payload, unsigned int length) {
// Debug
Serial.print("Received: [");
Serial.print(topic);
Serial.print("]: ");
Serial.write(payload, length);
Serial.println();
if (strcmp(topic, topicLed1) == 0) {
if (length == 2 && memcmp(payload, "ON", 2) == 0) {
digitalWrite(13, HIGH);
}
else if (length == 3 && memcmp(payload, "OFF", 3) == 0) {
digitalWrite(13, LOW);
}
}
else if (strcmp(topic, topicLed2) == 0) {
if (length == 2 && memcmp(payload, "ON", 2) == 0) {
digitalWrite(12, HIGH);
}
else if (length == 3 && memcmp(payload, "OFF", 3) == 0) {
digitalWrite(12, LOW);
}
}
else if (strcmp(topic, topicAll) == 0) {
if (length == 2 && memcmp(payload, "ON", 2) == 0) {
digitalWrite(13, HIGH);
digitalWrite(12, HIGH);
}
else if (length == 3 && memcmp(payload, "OFF", 3) == 0) {
digitalWrite(13, LOW);
digitalWrite(12, LOW);
}
}
}
void scanDI() {
static bool lastButtonState = LOW;
bool buttonState = digitalRead(4);
if (buttonState != lastButtonState) {
String status = buttonState ? "Pressed" : "Released";
mqttClient.publish(topicStatus, status.c_str());
lastButtonState = buttonState;
}
}
void scanAI() {
int potValue = analogRead(34);
String analogMsg = String(potValue);
mqttClient.publish(topicAnalog, analogMsg.c_str());
}
void setup() {
Serial.begin(115200);
Serial.println("7.4 Complete MQTT Client");
pinMode(4, INPUT_PULLDOWN);
pinMode(13, OUTPUT); // LED1
pinMode(12, OUTPUT); // LED2
digitalWrite(13, LOW); // Preset LOW
digitalWrite(12, LOW); // Preset LOW
WiFi.mode(WIFI_STA);
WiFi.begin("Wokwi-GUEST", "");
WiFi.setAutoReconnect(true);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.printf("\nConnected! IP: %s\n",
WiFi.localIP().toString().c_str());
mqttClient.setCallback(callback);
aiTimer.attach(15, scanAI);
}
void loop() {
if (!mqttClient.connected()) {
reconnect();
}
mqttClient.loop(); // Needed for keep-alive
scanDI();
delay(10);
}