MQTT Arduino client.publish with Generated Topic

Just want to hang out with other Makers and chat about stuff? This is the place to do it.
Joined:Sun Jan 07, 2018 7:39 pm
MQTT Arduino client.publish with Generated Topic

Post by kolbep » Sun Jan 07, 2018 7:52 pm

Hi All.
I am working on a similar system to SuperHouse's Alarm IO Shield, as a proof of concept,

I know you can do the following in Arduino :

Code: Select all

client.publish("topic/1", "ALARM");
client.publish("topic/2", "ALARM"); 
But I am trying to shave my code down to make it more modular.
I want to generate the topic number automatically based on the iteration i am in in a loop, so I am trying the following :

Code: Select all

     for (int channelno = 0; channelno < 8; channelno++) {
          --- Additional code ---
          char pubstring = "alarm/"+channelno;
          client.publish(pubstring, "ALARM");
What I expect it to do is the same, for each iteration of the loop, publish to a different topic,
eg : First Iteration : Topic = topic/1 Message = ALARM
Second Iteration : Topic = topic/2 Message = ALARM

but I am getting stuck, it looks like the client.publish is looking for a const char for the topic and message,
it gives me the following error :

Code: Select all

In file included from C:\Users\User\Documents\Arduino\AlarmMonster1\AlarmMonster1.ino:7:0:

C:\Users\User\Documents\Arduino\libraries\PubSubClient\src/PubSubClient.h:130:12: error:   initializing argument 1 of 'boolean PubSubClient::publish(const char*, const char*)' [-fpermissive]

    boolean publish(const char* topic, const char* payload);


exit status 1
invalid conversion from 'const char*' to 'char' [-fpermissive]
Any ideas of the workaround
I am sure it is something simple, but I am stumped

Post Reply