Hi guys,
Some developer ask to me “How to connect to the Azure IoT Hub with MQTT-TLS”, because of Google IoT core ended.
So I we will guide How do we connect to the Azure IoT Hub on this topic.
enviroment
Particle Argon(I tested firmware version 5.1.0).
MQTT-TLS 0.2.25
3 Likes
sample source code is here
“demomqttparticle” is my test Azure IoT Hub(already deleted from my Azure), use your own Azure IoT Hub.
#include "MQTT-TLS.h"
#define AZURE_IOT_HUB_ROOT_CA_PEM \
"------BEGIN CERTIFICATE----- \r\n" \
"MIIDdzCCAl+gAwIBAgIEAgAAuTANBgkqhkiG9w0BAQUFADBaMQswCQYDVQQGEwJJ\r\n" \
"RTESMBAGA1UEChMJQmFsdGltb3JlMRMwEQYDVQQLEwpDeWJlclRydXN0MSIwIAYD\r\n" \
"VQQDExlCYWx0aW1vcmUgQ3liZXJUcnVzdCBSb290MB4XDTAwMDUxMjE4NDYwMFoX\r\n" \
"DTI1MDUxMjIzNTkwMFowWjELMAkGA1UEBhMCSUUxEjAQBgNVBAoTCUJhbHRpbW9y\r\n" \
"ZTETMBEGA1UECxMKQ3liZXJUcnVzdDEiMCAGA1UEAxMZQmFsdGltb3JlIEN5YmVy\r\n" \
"VHJ1c3QgUm9vdDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAKMEuyKr\r\n" \
"mD1X6CZymrV51Cni4eiVgLGw41uOKymaZN+hXe2wCQVt2yguzmKiYv60iNoS6zjr\r\n" \
"IZ3AQSsBUnuId9Mcj8e6uYi1agnnc+gRQKfRzMpijS3ljwumUNKoUMMo6vWrJYeK\r\n" \
"mpYcqWe4PwzV9/lSEy/CG9VwcPCPwBLKBsua4dnKM3p31vjsufFoREJIE9LAwqSu\r\n" \
"XmD+tqYF/LTdB1kC1FkYmGP1pWPgkAx9XbIGevOF6uvUA65ehD5f/xXtabz5OTZy\r\n" \
"dc93Uk3zyZAsuT3lySNTPx8kmCFcB5kpvcY67Oduhjprl3RjM71oGDHweI12v/ye\r\n" \
"jl0qhqdNkNwnGjkCAwEAAaNFMEMwHQYDVR0OBBYEFOWdWTCCR1jMrPoIVDaGezq1\r\n" \
"BE3wMBIGA1UdEwEB/wQIMAYBAf8CAQMwDgYDVR0PAQH/BAQDAgEGMA0GCSqGSIb3\r\n" \
"DQEBBQUAA4IBAQCFDF2O5G9RaEIFoN27TyclhAO992T9Ldcw46QQF+vaKSm2eT92\r\n" \
"9hkTI7gQCvlYpNRhcL0EYWoSihfVCr3FvDB81ukMJY2GQE/szKN+OMY3EU/t3Wgx\r\n" \
"jkzSswF07r51XgdIGn9w/xZchMB5hbgF/X++ZRGjD8ACtPhSNzkE1akxehi/oCr0\r\n" \
"Epn3o0WC4zxe9Z2etciefC7IpJ5OCBRLbf1wbWsaY71k5h+3zvDyny67G7fyUIhz\r\n" \
"ksLi4xaNmjICq44Y3ekQEe5+NauQrz4wlHrQMz2nZQ/1/I6eYs9HRCwBXbsdtTLS\r\n" \
"R9I4LtD+gdwyah617jzV/OeBHRnDJELqYzmp\r\n" \
"-----END CERTIFICATE-----\r\n"
const char azureIoTHubRootCaPem[] = AZURE_IOT_HUB_ROOT_CA_PEM;
MQTT client("demomqttparticle.azure-devices.net", 8883, callback);
// recieve message
void callback(char* topic, byte* payload, unsigned int length) {
Serial.write(payload, length);
Serial.print(":");
Serial.println(length);
if (strncmp("RED", (const char*)payload, strlen("RED")) == 0)
RGB.color(255, 0, 0);
else if (strncmp("GREEN", (const char*)payload, strlen("GREEN")) == 0)
RGB.color(0, 255, 0);
else if (strncmp("BLUE", (const char*)payload, strlen("BLUE")) == 0)
RGB.color(0, 0, 255);
else {
RGB.color(255, 255, 255);
}
delay(1000);
}
#define ONE_DAY_MILLIS (24 * 60 * 60 * 1000)
unsigned long lastSync = millis();
void setup() {
if (millis() - lastSync > ONE_DAY_MILLIS) {
Particle.syncTime();
lastSync = millis();
}
RGB.control(true);
// enable tls. set Root CA pem, private key file.
client.enableTls(azureIoTHubRootCaPem, sizeof(azureIoTHubRootCaPem));
Serial.println("tls enable");
// connect to the server
client.connect("argondevice", "demomqttparticle.azure-devices.net/argondevice", "SharedAccessSignature sr=demomqttparticle.azure-devices.net%2Fdevices%2Fargondevice&sig=Y23brx8y%2B11QIgz2KpoEsmz0mEQgTjFTW8T7gypfTLM%3D&se=1668184617");
// publish/subscribe
if (client.isConnected()) {
Serial.println("client connected");
client.publish("devices/argondevice/messages/events/", "hello world");
client.subscribe("devices/argondevice/messages/events/");
}
}
int counter = 0;
void loop() {
if (client.isConnected()) {
client.loop();
}
delay(1000);
counter += 1;
client.publish("devices/argondevice/messages/events/", "hello world : " + String(counter));
}
1 Like
Colleen
November 10, 2022, 6:50pm
5
Wow, this is a great tutorial. Thanks @hirotakaster !
1 Like
ian.c
November 15, 2022, 5:12pm
6
This is an excellent resource and very clearly laid out @hirotakaster . I’m presently updating our code to migrate from Google to Azure and have some small variations from the example framework (E-series and MQTT-TLS config changes). I’ll follow this topic as I work through the changes and update this thread with any nuances I find on the E-series. Hope to get this working quickly!
3 Likes