Need of a guidance of securely connecting a ESP32 to a MQTT Broker with SSL Certificate with a Static IP

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

Edit your post and insert code tags!

Shouldn't you have a setClient call to tell the mqttclient about the wifi client?

Sorry @wildbill can't understand i'm a newbie to embedded programming

Look at the documentation for the pubsubclient. Somehow, it needs to know which wifi client to use to talk to the MQTT broker and although you have created one, you didn't specify that it should be used.

Can you pls say that i can't find it out where to identify sorry...
Regards,
Sri

Hi @pylon,
Can you pls troubleshoot this..
Regards,
SRI

You're apparently trying to run before you can crawl. I suggest that you find a simpler example, preferably without HTTPS, probably available with the library you loaded. Once you can get that to work, adapt it to your real needs.

Or read here about how to initialize/configure your client.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.