Read this series and see how to do it:
https://medium.com/@alonfliess/the-internet-of-things-the-maturity-phase-d7e8d9a06fdd
https://medium.com/@alonfliess/article-4-of-5-the-cloud-service-code-6fb589fa4ee5
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Previously I had created an MQTT Broker in Azure Event Grid. I've followed the documentation at
https://learn.microsoft.com/en-us/azure/event-grid/mqtt-publish-and-subscribe-portal
there used and successfully published subscribe on MQTTx with 2 clients.
Is there any documentation on how I can connect my ESP32 either hardware or online such as using Wokwi ESP32 to use the DHT22 sensor to send temperature and humidity data.
Simply put I just want to use the MQTT Broker Azure event grid namespace to use it on my ESP32 as a broker with a DHT22 sensor
here's the code below :
but always get eror :
Failed, rc=-2 Retrying in 5 seconds...
Connecting to MQTT broker...
#include <WiFi.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>
#include <WiFiClientSecure.h>
// Wi-Fi settings
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// Azure Event Grid settings
const char* mqttBroker = "eventgrid1.southeastasia-1.ts.eventgrid.azure.net";
const int mqttPort = 8883;
const char* eventGridTopic = "Topics/topic1";
const char* username = "client1-authnID";
const char* deviceID = "client1-sessionID1";
// TLS/SSL client setup
WiFiClientSecure wifiClient;
PubSubClient mqttClient(wifiClient);
// Certificates
const char* caCert = "";
const char* clientCert = R"KEY(
-----BEGIN CERTIFICATE-----
MIIB7jCCAZSgAwIBAgIRAM86q1LIwWpvhSCeqIxrTmowCgYIKoZIzj0EAwIwRjEZ
MBcGA1UEChMQTXF0dEFwcFNhbXBsZXNDQTEpMCcGA1UEAxMgTXF0dEFwcFNhbXBs
ZXNDQSBJbnRlcm1lZGlhdGUgQ0EwHhcNMjMwNjE2MDYyNjMzWhcNMjMwOTI0MDYy
NjMwWjAaMRgwFgYDVQQDEw9jbGllbnQxLWF1dGhuSUQwWTATBgcqhkjOPQIBBggq
hkjOPQMBBwNCAASa8R2ZS4wE2c5GU6MApWM/u9F7/Qo1DRhS6VPsr2za0OTXuO9C
hOE3tpxu8G3/rsyM4bPj9I6DAZMebHXbbX/To4GOMIGLMA4GA1UdDwEB/wQEAwIH
gDAdBgNVHSUEFjAUBggrBgEFBQcDAQYIKwYBBQUHAwIwHQYDVR0OBBYEFKkhXgJe
Id1zDD5sESQi3ABs33WIMB8GA1UdIwQYMBaAFIeMEpJCsKRfq+HjhXmE0uVfubMP
MBoGA1UdEQQTMBGCD2NsaWVudDEtYXV0aG5JRDAKBggqhkjOPQQDAgNIADBFAiAq
sZYheFfVrlgNk6De060blPZkrim13yrJRmRo1WUMiAIhAPZ6R+5Hyl5sGB4m8HYh
9mHEl+qVZauee8HGu9IWyKYq
-----END CERTIFICATE-----
)KEY";
const char* clientKey = R"KEY(
-----BEGIN EC PRIVATE KEY-----
MHcCAQEEIAb/hcl7cXX8gHH8gjlhXjcaj8pTonHP+MzqcS2rHY13oAoGCCqGSM49
AwEHoUQDQgAEmvEdmUuMBNnORlOjAKVjP7vRe/0KNQ0YUulT7K9s2tDk17jvQoTh
N7acbvBt/67MjOGz4/SOgwGTHmx1221/0w==
-----END EC PRIVATE KEY-----
)KEY";
void setup() {
Serial.begin(115200);
delay(2000);
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
// Load CA certificate, client certificate, and client key
wifiClient.setCACert(caCert);
wifiClient.setCertificate(clientCert);
wifiClient.setPrivateKey(clientKey);
// Set MQTT server and port
mqttClient.setServer(mqttBroker, mqttPort);
// Set MQTT callback function
mqttClient.setCallback(callback);
// Connect to MQTT broker
connectToMqttBroker();
}
void loop() {
if (!mqttClient.connected()) {
// Reconnect to MQTT broker if the connection is lost
connectToMqttBroker();
}
mqttClient.loop();
// Publish message to Event Grid
publishEvent();
// Delay before publishing the next event
delay(5000);
}
void connectToMqttBroker() {
while (!mqttClient.connected()) {
Serial.println("Connecting to MQTT broker...");
if (mqttClient.connect(username, deviceID,"")) {
Serial.println("Connected to MQTT broker");
} else {
Serial.print("Failed, rc=");
Serial.print(mqttClient.state());
Serial.println(" Retrying in 5 seconds...");
delay(5000);
}
}
}
void callback(char* topic, byte* payload, unsigned int length) {
// Handle incoming MQTT messages here if needed
}
void publishEvent() {
// Create the event message
DynamicJsonDocument eventMessage(256);
eventMessage["id"] = "1";
eventMessage["subject"] = "Example Event";
eventMessage["eventType"] = "example.event";
JsonObject eventData = eventMessage.createNestedObject("data");
eventData["key"] = "value";
// Serialize the event message to a JSON string
String jsonMessage;
serializeJson(eventMessage, jsonMessage);
// Publish the event message to Event Grid
mqttClient.publish(eventGridTopic, jsonMessage.c_str());
}
please helpme