256dpi / arduino-mqtt

MQTT library for Arduino
MIT License
1.01k stars 232 forks source link

ESP32-Mqtt was not connected using Azure SSL Certificate #241

Closed selvamani1991 closed 3 years ago

selvamani1991 commented 3 years ago

Hi, ESP32-Mqtt was not connected using Azure SSL Certificate I am using the below code for connecting with ssl certificate was not working.

include

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 }

256dpi commented 3 years ago

I'm closing this for now as TLS is handled by Arduino anyway.