Hello,
I'm using MQTT to make a remote for my home automation and at the beginning I had:
TOPIC1= /remote/control/parking
TOPIC2= /remote/control/visiteurs
TOPIC3= /remote/control/portail
which were working fine, I tried to replace with an array:
char* command_topics[5];
generated with:
//Channels
const char* Channels[] = {"parking","visiteurs","portail","immeuble","lumiere"}; //name channels
const int numChannels = sizeof(Channels) / sizeof(Channels[0]); //amount of Channels
//MQTT
const char* COMMAND_TOPIC_PATH = "remote/command/"; //Commands Topic Path
const char* STATE_TOPIC_PATH = "remote/state/"; //State of Command Topic Path
using the following code called during the setup and in case of a reconnection to the MQTT broker:
void mqtt_topics_initialization() {
for (int i = 0; i < numChannels; i++) {
//Command Topic Generation & subscribe to MQTT
char buf[50];
strcpy(buf, COMMAND_TOPIC_PATH);
strcat(buf, Channels[i]);
if (!client.subscribe(buf)) {
Serial.print("Subscribe to ");
Serial.print(buf);
Serial.println(" failed");
client.disconnect();
return;
}
Serial.print("Subscribed to ");
Serial.println(buf);
command_topics[i] = buf;
Serial.println(command_topics[i]); //check command topic stored
//State Topic Generation & subscribe to MQTT
char buf2[50];
strcpy(buf2, STATE_TOPIC_PATH);
strcat(buf2, Channels[i]);
if (!client.subscribe(buf2)) {
Serial.print("Subscribe to ");
Serial.print(buf2);
Serial.println(" failed");
client.disconnect();
return;
}
Serial.print("Subscribed to ");
Serial.println(buf2);
state_topics[i] = buf2;
Serial.println(state_topics[i]); //check state topic stored
client.publish(state_topics[i], "OFF");
}
//Debug check size of array
numCommandTopics = sizeof(command_topics) / sizeof(command_topics[0]);
numStateTopics = sizeof(state_topics) / sizeof(state_topics[0]);
Serial.println(numCommandTopics); //Debug check size of array
Serial.println(numStateTopics);
}
In the terminal it shows the right topics when it gets initialized and subscribed in the void_setup:
Attempting MQTT connection...
Subscribed to /remote/command/parking
remote/command/parking
Subscribed to /remote/state/parking
remote/state/parking
but it seems something is wrong as when it is in the loop it, printing the content of the array shows:
L-�?�'�?�
L-�?�'�?�
L-�?�'�?�
L-�?�'�?�
L-�?�'�?�
and it also makes a second problem, when it receives an MQTT message it is supposed to compare the topic of the message to the topics contained in the array
but all the values of the array gets replaced by the content of the message received. (if I send HELLO with MQTT the values of the array will show HELLO for all)
// Handle incoming messages from the broker
void callback(char* topic, byte* payload, unsigned int length) {
for (int i = 0; i < numCommandTopics; i++) {
Serial.println(command_topics[i]);
}
String response;
for (int i = 0; i < length; i++) {
response += (char)payload[i];
}
Serial.println(topic);
Serial.println(response);
Serial.println("--test begin--");
for (int i = 0; i < numCommandTopics; i++) {
Serial.println(command_topics[i]);
}
Serial.println("--test end--");
if (strcmp(topic, command_topics[0]) == 0) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
Serial.println(response);
if (response == "ON") // Turn the light on
{
mqtt_lora_channel_1();
}
else if (response == "OFF") // Turn the light off
{
client.publish(state_topics[0], "OFF");
}
}
I guess this is not what it is supposed to do as there is nothing in the code to put the response in the command_topics and at some point with the code generating the topics URL it was causing reboots.
How could I solve this problem? Is there possible improvements also?
Thank you