ESP32-MQTT was not connected using Azure SSL Certificates

#include <WiFiClientSecure.h>

const char* ssid = “xxx”; // your network SSID (name of wifi network)
const char* password = “xxx”; // your network password
const char* server = “xxx.azure-devices.net”; // Server URL

const char* test_root_ca=
“-----BEGIN CERTIFICATE-----\n”
“-----END CERTIFICATE-----\n”;

// You can use x.509 client certificates if you want
const char* test_client_key =
“-----BEGIN RSA PRIVATE KEY-----\n”
“-----END RSA PRIVATE KEY-----\n”; //to verify the client

const char* test_client_cert =
“-----BEGIN CERTIFICATE-----\n”
“-----END CERTIFICATE-----\n”; //to verify the client

WiFiClientSecure client;

void setup() {
//Initialize serial and wait for port to open:
Serial.begin(115200);
delay(100);

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);

client.setCACert(test_root_ca);
client.setCertificate(test_client_cert); // for client verification
client.setPrivateKey(test_client_key); // for client verification

Serial.println("\nStarting connection to server…");
if (!client.connect(server, 8883)){
Serial.println(“Connection failed!”);
}
else {
Serial.println(“Connected to server!”);
}

}

void loop() {
// do nothing
}

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.