0

I'm trying to use EspMQTTClient inside a class, called myIOT32.

An error, which I don't find the reason for is generated.

/home/guy/Documents/git/Arduino/libraries/myIOTesp32/myIOTesp32.cpp: In lambda function:
/home/guy/Documents/git/Arduino/libraries/myIOTesp32/myIOTesp32.cpp:25:5: error: 'this' was not captured for this lambda function
 client.publish("mytopic/test", "This is a message sent 5 seconds later");
 ^
/home/guy/Documents/git/Arduino/libraries/myIOTesp32/myIOTesp32.cpp:25:5: error: invalid use of non-static data member 'myIOT32::client'
In file included from /home/guy/Documents/git/Arduino/libraries/myIOTesp32/myIOTesp32.cpp:1:0:
/home/guy/Documents/git/Arduino/libraries/myIOTesp32/myIOTesp32.h:20:19: note: declared here
 EspMQTTClient client;
 ^
Multiple libraries were found for "WiFi.h"
 Used: /home/guy/.arduino15/packages/esp32/hardware/esp32/1.0.4/libraries/WiFi
 Not used: /home/guy/arduino-1.8.12/libraries/WiFi
exit status 1
Error compiling for board LOLIN D32.

.h file:

#ifndef myIOT32_h
#define myIOT32_h
// OTA libraries
#include <WiFi.h>
#include <ESPmDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include "secrets.h"
#include "EspMQTTClient.h"
#include "Arduino.h"
class myIOT32
{
private:
 void onConnectionEstablished(); // <--- this is a must have function ( was taken as is from lib's built-in example 
public:
 EspMQTTClient client;
 myIOT32(char *ssid, char *wifi_p, char *mqtt_broker, char *mqttU, char *mqttP, char *devTopic, int port = 1883);
};
#endif

.cpp file:

#include "myIOTesp32.h"
myIOT32::myIOT32(char *ssid, char *wifi_p, char *mqtt_broker, char *mqttU, char *mqttP, char *devTopic, int port)
 : client(ssid, wifi_p, mqtt_broker, mqttU, mqttP, devTopic, port)
{
}
void myIOT32::onConnectionEstablished()
{
 // Subscribe to "mytopic/test" and display received message to Serial
 client.subscribe("mytopic/test", [](const String & payload) {
 Serial.println(payload);
 });
 // Subscribe to "mytopic/wildcardtest/#" and display received message to Serial
 client.subscribe("mytopic/wildcardtest/#", [](const String & topic, const String & payload) {
 Serial.println(topic + ": " + payload);
 });
 // Publish a message to "mytopic/test"
 client.publish("mytopic/test", "This is a message"); // You can activate the retain flag by setting the third parameter to true
 // Execute delayed instructions
 client.executeDelayed(5 * 1000, []() {
 client.publish("mytopic/test", "This is a message sent 5 seconds later");
 });
}

Appreciate any help in constructing such a class

asked Apr 18, 2020 at 20:10
1
  • reordering Arduino.h did not solve. Commented Apr 18, 2020 at 21:35

1 Answer 1

1

For the fix, just add the reference capture and it should compile fine:

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

I could not test, but you will have to apply to other functions as well.

[] Capture nothing 
[&] Capture any referenced variable by reference 
[=] Capture any referenced variable by making a copy
answered Apr 18, 2020 at 21:57

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.