Help With Recieving a value from mqtt and sending a value out over i2c

Hi All, im in the process of writing a peice of code based off my current one to
1: clean it up and,
2: make it more user friendly.

Here is a link to my github repo

So it mostly works. i can send a command to the Mqtt topic and see whats its receiving Either a “1” for ON or a “0 for OFF”
in this case the topic would be house/switchboard/12/relay/+/command with the + being a single level wildcard for the relay number

so in this example if i wanted to turn relay on i would publish a “1” to house/switchboard/12/relay/1/command

the part i get stuck at is how can i take the single-level wildcard to sort of select a relay channel and then turn it on or off.

currently i use this
if (strcmp(topic, “relay1”) == 0) {
if (true) {
if ((char)payload[0] == ‘1’)
Serial.println(“Relay 1 triggered ON”);
client.publish(mqttStatusTopic, ‘1’);

else if ((char)payload[0] == ‘0’)
Serial.println(“Relay 1 triggered OFF”);
client.publish(mqttStatusTopic, ‘0’);

and then i have it listen on 32 MQTT topics which i think is not the best practice.

So this would listen on topic “relay1” for a “1” or a “0” and do the respective action

hopefully that makes sense, i just cant figure out this part of the code


Here is a link to my github repo

Post your code to this forum if you want help from us!

Use code tags to post code!

 if (true) {

This conditional statement is useless.

If you want to parse the topic, program a simple parser. So instead of comparing the complete string, compare the first 5 characters to "relay" and if that matches, convert the rest from the string to an integer. Voila! If that wasn't your question, explain in more detail!