I am trying to connect using MQTT over TLS. I have a working code ready for non-TLS version, which uses only 2 libraries: WiFi.h and PubSubClient.h. As I try to do the same, but with TLS, I changed library from WiFi.h to WiFiClientSecure.h, thus having PubSubClient.h and WiFiClientSecure.h now. I wanted to modify the earlier code by addng certs and I have succesfully managed to connect to the broker. The problem is, I did that without using publish or subscribe commands from MQTT, so basically I am connected to the broker but can neither subscribe nor publish. Also I had to comment out
//PubSubClient client();
to make it compile.
#include <WiFiClientSecure.h>
#include <PubSubClient.h>
const char* ssid = "";
const char* password = "";;
const char* mqtt_broker = "";
const char* test_root_ca= \
"-----BEGIN CERTIFICATE-----\n" \
"-----END CERTIFICATE-----\n";
// You can use x.509 client certificates if you want
const char* test_client_key = //to verify the client
"-----BEGIN PRIVATE KEY-----\n" \
"-----END PRIVATE KEY-----\n" ;
const char* test_client_cert = //to verify the client
"-----BEGIN CERTIFICATE-----\n" \
"-----END CERTIFICATE-----\n" ;
WiFiClientSecure client;
//PubSubClient client();
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print("Connecting to ");
Serial.println(ssid);
}
Serial.print("Connected with IP: ");
Serial.println(WiFi.localIP());
client.setCACert(test_root_ca);
client.setCertificate(test_client_cert); // for client verification
client.setPrivateKey(test_client_key); // for client verification
while (!client.connected())
{
Serial.println("Connecting to the broker...");
if (client.connect(mqtt_broker,8883))
{
Serial.println("Connected");
}
else
{
Serial.print("Failed with state ");
delay(2000);
}
}
for(int i=0; i<=1000; i++)
{
char payload[10];
itoa(i, payload, 10);
//client.publish("DemoTopic",payload);
}
}
void loop() {
// do nothing
}
Any idea how to work it out? Any other libraries that will help me with a setup? The error I get is:
'class WiFiClientSecure' has no member named 'publish'