I'm configuring an ESP8266 using Arduino Uno.
I have several MQTT topics I wish it to subscribe, and I do not want to repeat code 3 or 4 times ( in Python I would have created a list - and make a for loop to subscribe all )
These are my topics:
//MQTT topic
const char* deviceName = "Sonoff1";
const char* deviceTopic = "HomePi/Switches/Sonoff1";
const char* msgTopic = "HomePi/Messages";
const char* groupTopic = "HomePi/All";
This is the function registering a single topic:
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect(deviceName,user, passw)) {
Serial.println("connected");
pub_msg("Power On");
client.subscribe(deviceTopic);
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
Appriciate you kind help,
Guy
forloop