0

In my example, Arduino UNO (ATMEGA328p) connects using the SoftwareSerial library with the ESP8266-01 module. Then I used the PubSubClient library to connect to the MQTT broker. The connection is to port 1883 and is not encrypted. Everything works fine.

How can this be done to make the connection encrypted (TLS) to port 8883 with the setting of my own CA certificate.

Example:

#include <WiFiEsp.h>
#include <WiFiEspClient.h>
#include <SoftwareSerial.h>
#include <PubSubClient.h>

...

static const char ca_cert[] PROGMEM = R"EOF(
-----BEGIN CERTIFICATE-----
...
-----END CERTIFICATE-----
)EOF";

...

SoftwareSerial wifiSerial(WIFI_RX, WIFI_TX);
WiFiEspClient WIFI_CLIENT;
PubSubClient MQTT_CLIENT;

...


void setup() {
  wifiSerial.begin(9600);
  WiFi.init(&wifiSerial);
  if (WiFi.status() == WL_NO_SHIELD) {
    printLog("WiFi shield not present");
    // don't continue
    while (true);
  }
  printLog("Connecting to AP ...");
  WiFi.begin(WIFI_SSID, WIFI_PASS);
  ???   WIFI_CLIENT.setCACert(ca_cert);   ???
  printLog("Connected to AP");  
}

  • if your certificate is signed by well known CA then the AT firmware accepts it. the WiFiEsp library has connectSSL on WiFiEspClient, but the PubSubClient will not use it. create WiFiEspSSLClient client which calls connectSSL() in connect(). – Juraj May 11 at 16:07
  • if it is a client side private certificate, then you would have to upload it to esp for AT firmware to use it, if the AT fw version you use supports it or build the AT firmware with the key included – Juraj May 11 at 16:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.