debsahu / ESP-MQTT-AWS-IoT-Core

Arduino examples of connecting ESP8266/ESP32 to AWS IOT Core
MIT License
118 stars 50 forks source link

Compilation issues for esp32dev environment: net.getLastSSLError() #14

Open gitolicious opened 4 years ago

gitolicious commented 4 years ago

When compiling a fresh checkout for ESP32 (esp32dev), I am getting the following error:

Compiling .pio\build\esp32dev\lib694\WiFi\WiFiGeneric.cpp.o .../ESP-MQTT-AWS-IoT-Core/Arduino/MQTT/MQTT.ino: In function 'void connectToMqtt(bool)': .../ESP-MQTT-AWS-IoT-Core/Arduino/MQTT/MQTT.ino:133:26: error: 'class WiFiClientSecure' has no member named 'getLastSSLError' Serial.println(net.getLastSSLError());

The offending line https://github.com/debsahu/ESP-MQTT-AWS-IoT-Core/blob/64817e28b0a7ca295af926a6c352a9b4d51ee15e/Arduino/MQTT/MQTT.ino#L133

is referring to code that is only available in the ESP8266 WiFiClientSecure (BearSSL) implementation:

https://github.com/esp8266/Arduino/blob/c3c61a5f751fcc75440e571b5aa89254dce6e4f7/libraries/ESP8266WiFi/src/WiFiClientSecureBearSSL.h#L115-L116

// Return an error code and possibly a text string in a passed-in buffer with last SSL failure
int getLastSSLError(char *dest = NULL, size_t len = 0);

For ESP32, BearSSL is not used (not available). Would net.lastError() be the correct alternative?

https://github.com/espressif/arduino-esp32/blob/b92c58d74b151c7a3b56db4e78f2d3c90c16446f/libraries/WiFiClientSecure/src/WiFiClientSecure.cpp#L322-L331

int WiFiClientSecure::lastError(char *buf, const size_t size)
{
    if (!_lastError) {
        return 0;
    }
    char error_buf[100];
    mbedtls_strerror(_lastError, error_buf, 100);
    snprintf(buf, size, "%s", error_buf);
    return _lastError;
}

If so, then the implementation could work like this:

#ifdef ESP8266
      Serial.println(net.getLastSSLError());
#else
      char *errorMessage;
      int errorCode = net.lastError(errorMessage, 2048);
      Serial.println(errorCode);
      Serial.println(errorMessage);
#endif