Dear All,
Hope you are doing good,
I'm in need of a guidance of securely connecting a ESP32 to a MQTT Broker with SSL Certificate with a Static IP
Here are the steps i done:
have set an static ip to my ESP32 successfully,
have copied my cert into arduino,
trouble i'm facing is with:
can't connect to MQTTS Broker with port 8883 and username,password
here's the link that i follwed as guidance
Pls anyone can guide me in this,...
here's the code i used:
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <PubSubClient.h>const char* ssid = "xxxxx";
const char* password = "xxxxxxx";IPAddress local_IP(xx,xx,xx,xx);
IPAddress gateway(xx,xx,xx,xx);
IPAddress subnet(xx,xx,xx,xx);const char* mqttServer = "xx.xx.xx.xx";
const int mqttPort = 8883;
const char* mqttUser = "xxxxx";
const char* mqttPassword = "xxxxxxxxxx";const char* ca_cert = \
"-----BEGIN CERTIFICATE-----\n"
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
"-----END CERTIFICATE-----\n";WiFiClientSecure client;
PubSubClient mqttclient();void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived in topic: ");
Serial.println(topic);Serial.print("Message:");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}Serial.println();
Serial.println("-----------------------");}
void setup() {
Serial.begin(115200);
if (!WiFi.config(local_IP, gateway, subnet)) {
Serial.println("STA Failed to configure");WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.println("Connecting to WiFi..");
}
Serial.println("Connected to the WiFi network");client.setCACert(ca_cert);
mqttclient.setServer(mqttServer, mqttPort);
mqttclient.setCallback(callback);while (!mqttclient.connected()) {
Serial.println("Connecting to MQTT...");if (mqttclient.connect("ESP32Client"(mqttUser,mqttPassword))) {
Serial.println("connected");
} else {
Serial.print("failed with state ");
Serial.print(mqttclient.state());
delay(2000);}
}
mqttclient.publish("Sri","HelloWorld");
mqttclient.subscribe("Sri");}
void loop() {
client.loop();
}
Pls help me in troubleshooting this code to work fine
Kind Regards,
Sri Haran