Chat freely about anything...

User avatar
By UniqueIdentifier
#47341 Leveraging the PubSubClient.h - Thanks to knolleary

I'm able to get temperature readings from my device to a cloud service.
I'm also able to get subscription work (a hello from PC makes it onto the IoT Device)

My questions are, if someone could help.


1) Does client.loop() call the callback function?
If I don’t have the client.loop() then my sketch doesn’t work, I expect it does, but I would have thought with previous subscribe and connect messages it would work without.
client.subscribe(TopicSwitch);
client.loop();// This will check the callback function to see if there is a message - Breaks if not here…..

2)
If I issue the following command, will it stay subscribed to this topic as long as my MQTT connection is alive
client.subscribe(TopicSwitch);

3)
If I wanted to subscribe and print messages from two topics, would I have to subscribe AND run the client.loop for this?
client.subscribe(TopicSwitch1);
client.loop()
client.subscribe(TopicSwitch2);
client.loop()

I’ve seen people use
client.subscribe("2177800/1206/arduinoin");

In their Setup() but I would like to subscribe to a few topics

and then client.loop() with separate topics

for example

client.loop() — Check Topic 1 for Alert
client.loop() - Check Topic 2 for Alert

* Note, this does seem to work if I subscribe to a topic just before running client.loop() but wonder if there is a better way

4)
How can I get the value of the cstring out of the callback function into my loop()
I would like to use the value to turn off/on a switch?
Tried to “return cstring” but that didn’t work.

Code: Select allvoid callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  Serial.println();
  Serial.println("Attempting Print entire Payload");
  char *cstring = (char *) payload;
  cstring[length] = '\0';    // Adds a terminate terminate to end of string based on length of current payload
  Serial.println(cstring);   
  Serial.println();
//  return cstring;
}
User avatar
By UniqueIdentifier
#47358 Noting for the benefit of others, thanks to PaulS. His response below

1)
No. client.loop() does not check the callback function. It checks to see if an event has happened. If one has happened, and there is a callback registered for that event, it calls the callback.

3)
No. You'd have two subscribe() method calls (I'd expect to see them in setup()) and one loop() method call (in loop())

4)
Think of a callback like this. You are home cooking dinner. The doorbell rings. You do something about that (whatever the callback function says to do). How you get information from the person at the door to the meal you are cooking is up to you. Copying the data to a global variable/array is one way.

The callback is where you are supposed to deal with the event. For instance, when you press the Post button, a callback happens. You would expect that everything that was supposed to happen would happen in that function. You would not, for instance, expect that the data in the text field was copied to the clipboard and you needed to paste that somewhere else.
User avatar
By UniqueIdentifier
#47379 Again, for the benefit of others.

This is how I used a single callback function for multiple topics, each topic is string compared to the names I want, then an action is taken

Code: Select allint strcomparison = strcmp(topic, TopicSwitch);
  if (strcomparison == 0) {
    Serial.println("Matched Switch1 Topic");
                             } //end If
strcomparison = strcmp(topic, TopicSwitch2);
  if (strcomparison == 0) {
    Serial.println("Matched Switch2 Topic");
                             } //end If


Thanks to PaulS for all the help.