[HELP GETTING STARTED] simple if statement issue in MQTT code

Hi there,

I'm working with the EspMQTTClient GitHub - plapointe6/EspMQTTClient: Wifi and MQTT handling for ESP8266 and ESP32
because of the interesting encapsulated functions (from PuSubClient lib.)

However I'm not able to convert the "const String &" type to something working in a simple if statement (in order to blink a simple led).

So, I would like to have

if (payload == '0')
digitalWrite(ledPin, HIGH);
if (payload == '1')
digitalWrite(ledPin, LOW);

in this :

void onConnectionEstablished()
{
// Subscribe to "mytopic/test" and display received message to Serial
client.subscribe("mytopic/test", [](const String &payload) {
Serial.println(payload);
});

Until here I only get errors in my attemps
Any help would be appreciated.

--

  client.subscribe("mytopic/test", [](const String &payload) {

The second argument is the function to call when the broker publishes something related to that topic. That stuff does not look like a callable function.

Mmmm ... ok that's clearer.
Are those brackets at the begining of the "const String &" stand for a function ?

kyred:
Mmmm ... ok that's clearer.
Are those brackets at the begining of the "const String &" stand for a function ?

No. Square brackets indicate an array.

PaulS:
Square brackets indicate an array.

Or a Lambda, which is most definitely not a programming construct recommended for newbies.

Lambda ... Wow!
... in my little knowledge brackets for an array are put at the end ..

Okay .. tell me just if is there any way to extract a variable from the "[](const String &payload)" in order to blink my led or should I investigate the ESPmqttCLient lib for that portion of code ?

I don't have/use that library, but I looked on the GitHub. Here's the signature for the subscribe method:

typedef void(*MessageReceivedCallback) (const String &message);

void subscribe(const String &topic, MessageReceivedCallback messageReceivedCallback);

So, Like PaulS said, it's expecting a function pointer as the second argument. Looks like your code is supplying a pointer to anonymous function (aka a Lambda). That's allowable. But, if you don't know what a Lambda is, why are you doing it?

Just create a regular function (with a signature as specified by the typedef above) and put your ledPin stuff inside that. Then, supply THAT function as the second argument to subscribe().

Okay Yes, thanks for the tip @gfvalvo.
But it looks like I'm going to dive into another MQTT library, I'm not able to do what you offer (really good explanation however :slight_smile:
This is a little sad because this EspMQTTClient is really simplier than other Clients and it works perfect with the mosquitto broker I've installed on my ubuntu.

kyred:
I'm not able to do what you offer

Why not?

I'm not going to "hack" or to fork the code. Indeed I don't know how to. If I understand the EspMQTTClient project is built on top of PubSubCLient. it seems the guy omitted features allowing basic arduino coding like simply retrieve a variable from the MQTT Broker in order to do something with it. I have more chance to investigate, learn and get closer to my goal from Arduino Client for MQTT which is well documented than with a project that encapsulate it, even if it worked "plug and play" with my MQTT server at first attempt. Now I can't go further with that, so I prefr backtrack. Thanks for your help however, this forum seems nice :slight_smile:

kyred:
I'm not going to "hack" or to fork the code.

You're making this out to be much harder than it is. You don't need to hack or fork anything. Simply provide 'client.subscribe()' with a pointer to a function having the correct signature. Here's the code right from the library's example with that modification:

#include "EspMQTTClient.h"

void onConnectionEstablished();
void myCallback(const String &msg);

EspMQTTClient client(
  "ssid",             // Wifi ssid
  "pass",             // Wifi password
  "192.168.1.101",    // MQTT broker ip
  1883,               // MQTT broker port
  "usr",              // MQTT username
  "mqttpass",         // MQTT password
  "test1",            // Client name
  onConnectionEstablished, // Connection established callback
  true,               // Enable web updater
  true                // Enable debug messages
);

void setup() {
  Serial.begin(115200);
}

void onConnectionEstablished() {
  // Subscribe to "mytopic/test" and display received message to Serial
  client.subscribe("mytopic/test", myCallback);

  // Publish a message to "mytopic/test"
  client.publish("mytopic/test", "This is a message");

  // Execute delayed instructions
  client.executeDelayed(5 * 1000, []() {
    client.publish("mytopic/test", "This is a message sent 5 seconds later");
  });
}

void loop() {
  client.loop();
}

void myCallback(const String &msg) {
  // Put Your LED Stuff Here
}

bffiou I realize now as that was so near. Many thanks, I've learned right from common sense too. Is it rare to get some generosity on forum ...? I don't think so :wink:

1 Like