WiFiClientSecure#connected() issue - blocking when no incoming data?

I am trying to do MQTT overs TLS using PubSubClient and the WiFiSecureClient. Everything works as expected with plain TCP sockets and AtWifi.h's WiFiClient. However, I can’t seem to get it to work over TLS, although the initial connection seems to be working (i.e. CA is correctly setup).

It would appear WiFiClientSecure#connected() is blocking when trying to do ssl_client#data_to_read() - I wonder if something needs to be changed so that the call to mbed_ssl_read actually returns, even when 0 bytes are available, which doesn’t seem to be the case at the moment?

Full example below – if I remove the checks “if client.connected()” in the publish() and subscribe() method of the PubSubClient, it works around the issue.

#include <Arduino.h>
#include <AtWifi.h>
#include <WiFiClientSecure.h>
#include <PubSubClient.h>

#include "iot_config.h"

const char *ssid = IOT_CONFIG_WIFI_SSID;
const char *password = IOT_CONFIG_WIFI_PASSWORD;

//WiFiClient wifiClient;
WiFiClientSecure wifiClient;

PubSubClient mqttClient(wifiClient);

void connectToWifi();
void connectMQTT();

void callback(char *topic, byte *payload, unsigned int length)
{
    Serial.print("Message arrived [");
    Serial.print(topic);
    Serial.print("] ");
    for (int i = 0; i < length; i++)
    {
        Serial.print((char)payload[i]);
    }
    Serial.println();
}

void setup()
{
    //Initialize serial and wait for port to open:
    Serial.begin(115200);
    while (!Serial)
        ; // Wait for Serial to be ready
    delay(1000);

    connectToWifi();

    wifiClient.setCACert("-----BEGIN CERTIFICATE-----\n"
                         "foobar\n"
                         "-----END CERTIFICATE-----");

    mqttClient.setServer("xxx.net", 8883);
    mqttClient.setCallback(callback);

    connectMQTT();
}

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

void connectToWifi()
{
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    WiFi.begin(ssid, password);

    // attempt to connect to Wifi network:
    while (WiFi.status() != WL_CONNECTED)
    {
        Serial.print(".");
        // wait 1 second for re-trying
        delay(1000);
    }
    Serial.print("Connected to ");
    Serial.println(ssid);

    //  wifiClient.setCACert(baltimore_root_ca);
}

void connectMQTT()
{
    while (!mqttClient.connected())
    {
        Serial.print("Attempting MQTT connection...");
        // Attempt to connect
        if (mqttClient.connect("wioTerminal"))
        {
            Serial.println("connected");
            // Once connected, publish an announcement...
            mqttClient.publish("outTopic", "hello world!");
            // ... and resubscribe
            mqttClient.subscribe("inTopic");

            while (mqttClient.connected())
            {
                mqttClient.publish("outTopic", "hello");
                delay(5000);
            }
        }
        else
        {
            Serial.print("failed, rc=");
            Serial.print(mqttClient.state());
            Serial.println(" try again in 5 seconds");
            // Wait 5 seconds before retrying
            delay(5000);
        }
    }
}```

We meet a similar problem to yours. Hopefully, we can work this out together in the near future.

1 Like

@benjamin537 We have just updated the software related to WiFi. Please let me know if you have any problems.

Thanks, it works! I will be publishing a code sample on Github later today.

1 Like

Can I watch your GitHub repo? Can You give me a link? @benjamin537

Sure, here it is:

1 Like