Share this example!

Share this example with your friends on your favorite profile:

You can easily embed this example to your blog or website by copying the following code:

<iframe style="height: 510px; width: 100%; margin: 10px 0 10px;" allowTransparency="true" src="https://codebender.cc/embed/example/PubSubClient/mqtt_auth" frameborder="0"></iframe>


You can also embed the Serial Monitor section! Just use this HTML code.

<iframe style="height: 510px; width: 100%; margin: 10px 0 10px;" allowTransparency="true" src="https://codebender.cc/embed/serialmonitor" frameborder="0"></iframe>



  • 2021-03-10 selva mani

    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 <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
    }

  • 2015-11-20 codebender

    This example was tested on 2016-06-11 and it compiles on Arduino Uno, Arduino Leonardo, Arduino Mega.