Enthernet Code
DIIDevHeads IoT Integration Server
•Created by Boss lady on 6/11/2024 in #edge-networking
Establish a secure MQTT connection to a broker using TLS on an ESP32
@Boss lady your code looks correct to me and should run perfectly well without any issues once all required parameters are field, just in case this is what it looked like once I tried it out
#include <WiFi.h>
#include <WiFiClientSecure.h>
const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";
const char* mqtt_server = "broker.hivemq.com";
WiFiClientSecure espClient;
PubSubClient client(espClient);
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
espClient.setCACert(test_root_ca);
client.setServer(mqtt_server, 8883);
client.setCallback(callback);
}
void callback(char* topic, byte* message, unsigned int length) {
Serial.print("Message arrived on topic: ");
Serial.print(topic);
Serial.print(". Message: ");
String messageTemp;
for (int i = 0; i < length; i++) {
messageTemp += (char)message[i];
}
Serial.println(messageTemp);
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
}
void reconnect() {
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
if (client.connect("ESP32Client")) {
Serial.println("connected");
client.subscribe("test/topic");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}
static const char test_root_ca[] PROGMEM = R"EOF(
-----BEGIN CERTIFICATE-----
MIIDXTCCAkWgAwIBAgIJAK5tdeuXKHVWMA0GCSqGSIb3DQEBCwUAMEUxCzAJBgNV
...
U9uHgT3UtNymf6O9Dp+u3e5VzXk=
-----END CERTIFICATE-----
)EOF";
#include <WiFi.h>
#include <WiFiClientSecure.h>
const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";
const char* mqtt_server = "broker.hivemq.com";
WiFiClientSecure espClient;
PubSubClient client(espClient);
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
espClient.setCACert(test_root_ca);
client.setServer(mqtt_server, 8883);
client.setCallback(callback);
}
void callback(char* topic, byte* message, unsigned int length) {
Serial.print("Message arrived on topic: ");
Serial.print(topic);
Serial.print(". Message: ");
String messageTemp;
for (int i = 0; i < length; i++) {
messageTemp += (char)message[i];
}
Serial.println(messageTemp);
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
}
void reconnect() {
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
if (client.connect("ESP32Client")) {
Serial.println("connected");
client.subscribe("test/topic");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}
static const char test_root_ca[] PROGMEM = R"EOF(
-----BEGIN CERTIFICATE-----
MIIDXTCCAkWgAwIBAgIJAK5tdeuXKHVWMA0GCSqGSIb3DQEBCwUAMEUxCzAJBgNV
...
U9uHgT3UtNymf6O9Dp+u3e5VzXk=
-----END CERTIFICATE-----
)EOF";
3 replies