fabianoriccardi / dimmable-light

Arduino library to manage dimmers compatible with AVR, ESP8266, ESP32, SAMD, and RP2040 platforms.
GNU Lesser General Public License v2.1
97 stars 29 forks source link

MQTT lib doesn't compile with this library #42

Closed Prithvikd closed 12 months ago

Prithvikd commented 1 year ago

I am trying to implement mqtt and i'm are facing compilation errors for the MQTTClient.h. I'm using the 256dpi/MQTT@^2.5.0. The code runs fine separately for MQTT and dimmable-light but only when i combine the both it throws up compilation errors.

This is the compilation terminal that I get.

`Compiling .pio\build\esp32doit-devkit-v1\lib7c4\ArduinoSTL\new_opnt.cpp.o Compiling .pio\build\esp32doit-devkit-v1\lib7c4\ArduinoSTL\new_opv.cpp.o In file included from include/awsconnection.h:2:0, from src/main.cpp:1: C:/Users/Administrator/.platformio/packages/framework-arduinoespressif32/cores/esp32/Arduino.h:158:12: error: 'std::isinf' has not been de clared using std::isinf; ^ C:/Users/Administrator/.platformio/packages/framework-arduinoespressif32/cores/esp32/Arduino.h:159:12: error: 'std::isnan' has not been de clared using std::isnan; ^ In file included from C:/Users/Administrator/.platformio/packages/framework-arduinoespressif32/libraries/WiFi/src/WiFiSTA.h:28:0, from C:/Users/Administrator/.platformio/packages/framework-arduinoespressif32/libraries/WiFi/src/WiFi.h:32, from include/awsconnection.h:3, from src/main.cpp:1: C:/Users/Administrator/.platformio/packages/framework-arduinoespressif32/libraries/WiFi/src/WiFiGeneric.h:46:14: error: 'function' in name space 'std' does not name a template type typedef std::function<void(system_event_id_t event, system_event_info_t info)> WiFiEventFuncCb; ^ C:/Users/Administrator/.platformio/packages/framework-arduinoespressif32/libraries/WiFi/src/WiFiGeneric.h:89:29: error: 'WiFiEventFuncCb' has not been declared wifi_event_id_t onEvent(WiFiEventFuncCb cbEvent, system_event_id_t event = SYSTEM_EVENT_MAX); ^ In file included from C:/Users/Administrator/.platformio/packages/framework-arduinoespressif32/libraries/WiFi/src/WiFi.h:37:0, from include/awsconnection.h:3, from src/main.cpp:1: C:/Users/Administrator/.platformio/packages/framework-arduinoespressif32/libraries/WiFi/src/WiFiClient.h:42:10: error: 'shared_ptr' in nam espace 'std' does not name a template type std::shared_ptr clientSocketHandle; ^ C:/Users/Administrator/.platformio/packages/framework-arduinoespressif32/libraries/WiFi/src/WiFiClient.h:43:10: error: 'shared_ptr' in nam espace 'std' does not name a template type std::shared_ptr _rxBuffer; ^ Compiling .pio\build\esp32doit-devkit-v1\lib7c4\ArduinoSTL\new_opvnt.cpp.o Compiling .pio\build\esp32doit-devkit-v1\lib7c4\ArduinoSTL\numeric.cpp.o Compiling .pio\build\esp32doit-devkit-v1\lib7c4\ArduinoSTL\ostream.cpp.o .pio\libdeps\esp32doit-devkit-v1\ArduinoSTL\src\ArduinoSTL.cpp:79:2: warning: #warning "printf() will not be functional on this platform." [-Wcpp]

warning "printf() will not be functional on this platform."

^ Compiling .pio\build\esp32doit-devkit-v1\lib7c4\ArduinoSTL\ostream_helpers.cpp.o Compiling .pio\build\esp32doit-devkit-v1\lib7c4\ArduinoSTL\queue.cpp.o Compiling .pio\build\esp32doit-devkit-v1\lib7c4\ArduinoSTL\set.cpp.o In file included from .pio\libdeps\esp32doit-devkit-v1\ArduinoSTL\src\ArduinoSTL.h:12:0, from .pio\libdeps\esp32doit-devkit-v1\ArduinoSTL\src\ArduinoSTL.cpp:1: C:\Users\Administrator.platformio\packages\framework-arduinoespressif32\cores\esp32/Arduino.h:158:12: error: 'std::isinf' has not been de clared using std::isinf; ^ C:\Users\Administrator.platformio\packages\framework-arduinoespressif32\cores\esp32/Arduino.h:159:12: error: 'std::isnan' has not been de clared using std::isnan; ^ In file included from include/awsconnection.h:6:0, from src/main.cpp:1: .pio/libdeps/esp32doit-devkit-v1/MQTT/src/MQTTClient.h:51:14: error: 'function' in namespace 'std' does not name a template type typedef std::function<void(String &topic, String &payload)> MQTTClientCallbackSimpleFunction; ^ .pio/libdeps/esp32doit-devkit-v1/MQTT/src/MQTTClient.h:52:14: error: 'function' in namespace 'std' does not name a template type typedef std::function<void(MQTTClient *client, char topic[], char bytes[], int length)> ^ .pio/libdeps/esp32doit-devkit-v1/MQTT/src/MQTTClient.h:61:3: error: 'MQTTClientCallbackSimpleFunction' does not name a type MQTTClientCallbackSimpleFunction functionSimple = nullptr; Compiling .pio\build\esp32doit-devkit-v1\lib7c4\ArduinoSTL\sstream.cpp.o ^ .pio/libdeps/esp32doit-devkit-v1/MQTT/src/MQTTClient.h:62:3: error: 'MQTTClientCallbackAdvancedFunction' does not name a type MQTTClientCallbackAdvancedFunction functionAdvanced = nullptr; ^ .pio/libdeps/esp32doit-devkit-v1/MQTT/src/MQTTClient.h:114:18: error: 'MQTTClientCallbackSimpleFunction' has not been declared void onMessage(MQTTClientCallbackSimpleFunction cb); ^ .pio/libdeps/esp32doit-devkit-v1/MQTT/src/MQTTClient.h:115:26: error: 'MQTTClientCallbackAdvancedFunction' has not been declared void onMessageAdvanced(MQTTClientCallbackAdvancedFunction cb); ^ Compiling .pio\build\esp32doit-devkit-v1\lib7c4\ArduinoSTL\stack.cpp.o Compiling .pio\build\esp32doit-devkit-v1\lib7c4\ArduinoSTL\stdexcept.cpp.o Compiling .pio\build\esp32doit-devkit-v1\lib7c4\ArduinoSTL\streambuf.cpp.o Compiling .pio\build\esp32doit-devkit-v1\lib7c4\ArduinoSTL\string.cpp.o [.pio\build\esp32doit-devkit-v1\lib7c4\ArduinoSTL\ArduinoSTL.cpp.o] Error 1 [.pio\build\esp32doit-devkit-v1\src\main.cpp.o] Error 1 ======================================================= [FAILED] Took 5.85 seconds =======================================================`

fabianoriccardi commented 1 year ago

Which version of ESP32 Core are you using?

Prithvikd commented 1 year ago

Which version of ESP32 Core are you using?

ESP 32 WROOM 32D Xtensa dual core

Platformio: espressif32 3.5.0

fabianoriccardi commented 1 year ago

Espressif32 3.5.0 corresponds to Arduino Esp32 v1.0.6.

I have tested with Arduino IDE 2 the following sketch and it compiles:

#include <WiFi.h>
#include <MQTT.h>

#include <dimmable_light.h>

DimmableLight light(3);

WiFiClient net;
MQTTClient client;

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

void loop() {
  // put your main code here, to run repeatedly:
}

Can you post the minimal sketch that throws the error?

Prithvikd commented 1 year ago

Espressif32 3.5.0 corresponds to Arduino Esp32 v1.0.6.

I have tested with Arduino IDE 2 the following sketch and it compiles:

#include <WiFi.h>
#include <MQTT.h>

#include <dimmable_light.h>

DimmableLight light(3);

WiFiClient net;
MQTTClient client;

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

void loop() {
  // put your main code here, to run repeatedly:
}

Can you post the minimal sketch that throws the error?

include

include

include

DimmableLight light(3);

WiFiClient net; MQTTClient client;

void incomingMessageHandler(String &topic, String &payload){

//connect to AWS }

void setup() { Serial.begin(115200); //wifi connection with mqtt client connection to aws with the certificates mqtt_client_incommingmessagehandler (); }

void loop() {

mqtt_client . loop(); }

fabianoriccardi commented 1 year ago

Please post a correct sketch. For sure 'mqtt_client_incommingmessagehandler' is not declared function

Prithvikd commented 1 year ago

Please post a correct sketch. For sure 'mqtt_client_incommingmessagehandler' is not declared function

include

include

include

include

include

DimmableLight light(3);

WiFiClient wifi_client; MQTTClient mqtt_client; const char *AWS_ENDPOINT = "xxxxxxxxxxxxxxx-ats.iot.us-east-1.amazonaws.com";

void incomingMessageHandler(String &topic, String &payload) { Serial.println("incoming: " + topic + " - " + payload); DynamicJsonDocument doc(1024); deserializeJson(doc, payload); if (doc.containsKey("brightness")) { light.setBrightness(doc["brightness"]); } } connectWifi() { //connect to wifi } AWSconnect() { //connect to AWS mqtt_client.begin(AWS_ENDPOINT, wifi_client); mqtt_client.onMessage(incomingMessageHandler); } void setup() { Serial.begin(115200); connectWifi(); AWSconnect(); Serial.print("Initializing DimmableLight library... "); DimmableLight::setSyncPin(syncPin); DimmableLight::begin(); }

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

fabianoriccardi commented 1 year ago

It seems you don't want the help: the code doesn't compile! I had solved 3 trivial issues that prevents compilation (missing return values of functions and missing variable declaration) and it works seamlessly.

Post the MCVE. You say that you are using platformio, so you should also post the platformio.ini file.

reddy9698 commented 1 year ago

Hi, I encountered the same problem.

This is the basic code:

`

include

include

include

include

include

include

define THINGNAME "dimlight"

const charSSID = "SSID"; const charpassword = "PASS"; const char endpoint[] = "xxxxxxxxxx-ats.iot.ap-south-1.amazonaws.com";

const int syncPin = 5; // const int thyristorPin = 19;

DimmableLight light1(12); DimmableLight light2(14); DimmableLight light3(27); DimmableLight light4(26); DimmableLight light5(13);

// Delay between brightness increments, in milliseconds const int period = 50;

define sub_topic "$aws/things/dimlight/shadow/update/delta"

define pub_topic "$aws/things/dimlight/shadow/update"

WiFiClientSecure wifi_client=WiFiClientSecure(); MQTTClient mqtt_client=MQTTClient(2048);

static const char ROOT_CA[] PROGMEM= R"EOF( -----BEGIN CERTIFICATE----- MIIDQTCCAimgAwIBAgITBmyfz5m/jAo54vB4ikPmljZbyjANBgkqhkiG9w0BAQsF ADA5MQswCQYDVQQGEwJVUzEPMA0GA1UEChMGQW1hem9uMRkwFwYDVQQDExBBbWF6 b24gUm9vdCBDQSAxMB4XDTE1MDUyNjAwMDAwMFoXDTM4MDExNzAwMDAwMFowOTEL MAkGA1UEBhMCVVMxDzANBgNVBAoTBkFtYXpvbjEZMBcGA1UEAxMQQW1hem9uIFJv b3QgQ0EgMTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBALJ4gHHKeNXj ca9HgFB0fW7Y14h29Jlo91ghYPl0hAEvrAIthtOgQ3pOsqTQNroBvo3bSMgHFzZM 9O6II8c+6zf1tRn4SWiw3te5djgdYZ6k/oI2peVKVuRF4fn9tBb6dNqcmzU5L/qw IFAGbHrQgLKm+a/sRxmPUDgH3KKHOVj4utWp+UhnMJbulHheb4mjUcAwhmahRWa6 VOujw5H5SNz/0egwLX0tdHA114gk957EWW67c4cX8jJGKLhD+rcdqsq08p8kDi1L 93FcXmn/6pUCyziKrlA4b9v7LWIbxcceVOF34GfID5yHI9Y/QCB/IIDEgEw+OyQm jgSubJrIqg0CAwEAAaNCMEAwDwYDVR0TAQH/BAUwAwEB/zAOBgNVHQ8BAf8EBAMC AYYwHQYDVR0OBBYEFIQYzIU07LwMlJQuCFmcx7IQTgoIMA0GCSqGSIb3DQEBCwUA A4IBAQCY8jdaQZChGsV2USggNiMOruYou6r4lK5IpDB/G/wkjUu0yKGX9rbxenDI U5PMCCjjmCXPI6T53iHTfIUJrU6adTrCC2qJeHZERxhlbI1Bjjt/msv0tadQ1wUs N+gDS63pYaACbvXy8MWy7Vu33PqUXHeeE6V/Uq2V8viTO96LXFvKWlJbYK8U90vv o/ufQJVtMVT8QtPHRh8jrdkPSHCa2XV4cdFyQzR1bldZwgJcJmApzyMZFo6IQ6XU 5MsI+yMRQ+hDKXJioaldXgjUkK642M4UwtBV8ob2xJNDd2ZhwLnoQdeXeGADbkpy rqXRfboQnoZsG4q5WTP468SQvvG5 -----END CERTIFICATE----- )EOF";

static const char PRIVATE_KEY[] PROGMEM = R"KEY( -----BEGIN RSA PRIVATE KEY----- // your key here -----END RSA PRIVATE KEY-----

)KEY";

static const char CERTFICATE_KEY [] PROGMEM = R"KEY( -----BEGIN CERTIFICATE----- // your key here -----END CERTIFICATE-----

)KEY";

void IncomingMessageHandler (String &topic, String&payload) { Serial.println("Message received"); Serial.println("Topic" +String(topic)); Serial.println("payload" + String(payload)); }

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

while (!Serial) ; Serial.println(); WiFi.mode(WIFI_STA); WiFi.begin (SSID,password); while(WiFi.status()!=WL_CONNECTED) { Serial.print("."); delay(50);

} Serial.println("WiFi Connected"); Serial.println(WiFi.localIP());
DimmableLight::setSyncPin(syncPin); DimmableLight::begin(); // Serial.println("Done!"); wifi_client.setCACert(ROOT_CA); wifi_client.setCertificate(CERTFICATE_KEY); wifi_client.setPrivateKey(PRIVATE_KEY); mqtt_client.begin(endpoint,8883,wifi_client); mqtt_client.onMessage(IncomingMessageHandler); Serial.println("connecting to AWS"); mqtt_client.connect(THINGNAME); while(!mqtt_client.connect(THINGNAME)); { Serial.print("*"); delay(50); } Serial.print("connected To AWS"); if(!mqtt_client.connected()) { Serial.println("Connection Timeout"); } mqtt_client.subscribe(sub_topic); }

void loop() { mqtt_client.loop();

} `

and this is the platformio.ini file: [env:esp32doit-devkit-v1] platform = espressif32 board = esp32doit-devkit-v1 framework = arduino lib_deps = bblanchon/ArduinoJson@^6.21.3 256dpi/MQTT@^2.5.0 fabianoriccardi/Dimmable Light for Arduino@^1.5.0 monitor_speed = 115200

The libraries MQTT and dimmable light work very good separately. The problem arises when I include the MQTT part with dimmable light.

fabianoriccardi commented 1 year ago

I post here the version that really gives the issue(😅):

#include <WiFi.h>
#include <MQTTClient.h>
#include <WiFiClientSecure.h>
#include <dimmable_light.h>

#define THINGNAME "dimlight"

const char SSID[] = "SSID";
const char password[] = "PASS";
const char endpoint[] = "xxxxxxxxxx-ats.iot.ap-south-1.amazonaws.com";

const int syncPin = 5;
// const int thyristorPin = 19;

DimmableLight light1(12);

// Delay between brightness increments, in milliseconds
const int period = 50;

#define sub_topic "$aws/things/dimlight/shadow/update/delta"
#define pub_topic "$aws/things/dimlight/shadow/update"

WiFiClientSecure wifi_client = WiFiClientSecure();
MQTTClient mqtt_client = MQTTClient(2048);

static const char ROOT_CA[] PROGMEM = R"EOF(
-----BEGIN CERTIFICATE-----
MIIDQTCCAimgAwIBAgITBmyfz5m/jAo54vB4ikPmljZbyjANBgkqhkiG9w0BAQsF
ADA5MQswCQYDVQQGEwJVUzEPMA0GA1UEChMGQW1hem9uMRkwFwYDVQQDExBBbWF6
b24gUm9vdCBDQSAxMB4XDTE1MDUyNjAwMDAwMFoXDTM4MDExNzAwMDAwMFowOTEL
MAkGA1UEBhMCVVMxDzANBgNVBAoTBkFtYXpvbjEZMBcGA1UEAxMQQW1hem9uIFJv
b3QgQ0EgMTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBALJ4gHHKeNXj
ca9HgFB0fW7Y14h29Jlo91ghYPl0hAEvrAIthtOgQ3pOsqTQNroBvo3bSMgHFzZM
9O6II8c+6zf1tRn4SWiw3te5djgdYZ6k/oI2peVKVuRF4fn9tBb6dNqcmzU5L/qw
IFAGbHrQgLKm+a/sRxmPUDgH3KKHOVj4utWp+UhnMJbulHheb4mjUcAwhmahRWa6
VOujw5H5SNz/0egwLX0tdHA114gk957EWW67c4cX8jJGKLhD+rcdqsq08p8kDi1L
93FcXmn/6pUCyziKrlA4b9v7LWIbxcceVOF34GfID5yHI9Y/QCB/IIDEgEw+OyQm
jgSubJrIqg0CAwEAAaNCMEAwDwYDVR0TAQH/BAUwAwEB/zAOBgNVHQ8BAf8EBAMC
AYYwHQYDVR0OBBYEFIQYzIU07LwMlJQuCFmcx7IQTgoIMA0GCSqGSIb3DQEBCwUA
A4IBAQCY8jdaQZChGsV2USggNiMOruYou6r4lK5IpDB/G/wkjUu0yKGX9rbxenDI
U5PMCCjjmCXPI6T53iHTfIUJrU6adTrCC2qJeHZERxhlbI1Bjjt/msv0tadQ1wUs
N+gDS63pYaACbvXy8MWy7Vu33PqUXHeeE6V/Uq2V8viTO96LXFvKWlJbYK8U90vv
o/ufQJVtMVT8QtPHRh8jrdkPSHCa2XV4cdFyQzR1bldZwgJcJmApzyMZFo6IQ6XU
5MsI+yMRQ+hDKXJioaldXgjUkK642M4UwtBV8ob2xJNDd2ZhwLnoQdeXeGADbkpy
rqXRfboQnoZsG4q5WTP468SQvvG5
-----END CERTIFICATE-----
)EOF";

static const char PRIVATE_KEY[] PROGMEM = R"KEY(
-----BEGIN RSA PRIVATE KEY-----
// your key here
-----END RSA PRIVATE KEY-----

)KEY";

static const char CERTFICATE_KEY[] PROGMEM = R"KEY(
-----BEGIN CERTIFICATE-----
// your key here
-----END CERTIFICATE-----

)KEY";

void IncomingMessageHandler(String &topic, String &payload)
{
  Serial.println("Message received");
  Serial.println("Topic" + String(topic));
  Serial.println("payload" + String(payload));
}

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

  while (!Serial)
    ;
  Serial.println();
  WiFi.mode(WIFI_STA);
  WiFi.begin(SSID, password);
  while (WiFi.status() != WL_CONNECTED)
  {
    Serial.print(".");
    delay(50);
  }
  Serial.println("WiFi Connected");
  Serial.println(WiFi.localIP());
  DimmableLight::setSyncPin(syncPin);
  DimmableLight::begin();
  // Serial.println("Done!");
  wifi_client.setCACert(ROOT_CA);
  wifi_client.setCertificate(CERTFICATE_KEY);
  wifi_client.setPrivateKey(PRIVATE_KEY);
  mqtt_client.begin(endpoint, 8883, wifi_client);
  mqtt_client.onMessage(IncomingMessageHandler);
  Serial.println("connecting to AWS");
  mqtt_client.connect(THINGNAME);
  while (!mqtt_client.connect(THINGNAME))
    ;
  {
    Serial.print("*");
    delay(50);
  }
  Serial.print("connected To AWS");
  if (!mqtt_client.connected())
  {
    Serial.println("Connection Timeout");
  }
  mqtt_client.subscribe(sub_topic);
}

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

Platformio.ini

[env:lolin_d32-devkit-v1]
platform = espressif32 
board = lolin_d32
framework = arduino 
lib_deps =  
    256dpi/MQTT@^2.5.0 
    fabianoriccardi/Dimmable Light for Arduino@^1.5.1
monitor_speed = 115200
lib_compat_mode = strict
fabianoriccardi commented 1 year ago

The problem is simple: PIO includes ArduinoSTL library even if not necessary on esp32 platform. In library.json there is the configuration that explicitly tells that ArduinoSTL is only needed for Arduino AVR, but PIO, by default behavior, includes it in every platformio.

To induce PIO to consider the specific platform, you have to add the following line in platformio.ini:

lib_compat_mode = strict

docs

fabianoriccardi commented 12 months ago

Add a note in readme in https://github.com/fabianoriccardi/dimmable-light/commit/b40c7c4565c2a3aaabc35ca450019fc4cc25d052.

I'm closing since I didn't find any better solution. If you still have problems, let me know.

reddy9698 commented 12 months ago

Hi, your solution works fine for now. Thank you for the fast response.