Hi,
I'm trying to connect to one of my MQTT server using TLS certificate
can anyone help me in troubleshooting the code,
Though i tried all the way i can't do it
Please help me the same,
This is the Error i'm getting
#include <WiFi.h>
#include "WiFiClientSecure.h"
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"/************************* WiFi Access Point *********************************/
#define WLAN_SSID "XXXXXXX"
#define WLAN_PASS "XXXXXXX"
IPAddress local_IP(XX,XX,XXX,XXX);
IPAddress gateway(XX,XX,XXX,XXX);
IPAddress subnet(XXX,XXX,XXX,XXX);#define AIO_SERVER "mqtt.com"
// Using port 8883 for MQTTS
#define AIO_SERVERPORT 8883// Adafruit IO Account Configuration
// (to obtain these values, visit https://io.adafruit.com and click on Active Key)
#define AIO_USERNAME "XXXXXX"
#define AIO_KEY "XXXXXXXXXXX"/************ Global State (you don't need to change this!) ******************/
// WiFiFlientSecure for SSL/TLS support
WiFiClientSecure client;// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);// io.adafruit.com root CA
const char* adafruitio_root_ca =
"-----BEGIN CERTIFICATE-----\n"
"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
"-----END CERTIFICATE-----\n";/****************************** Feeds ***************************************/
// Setup a feed called 'test' for publishing.
// Notice MQTT paths for AIO follow the form: /feeds/
Adafruit_MQTT_Publish test = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/test");/*************************** Sketch Code ************************************/
void setup() {
Serial.begin(115200);
delay(10);
if (!WiFi.config(local_IP, gateway, subnet)) {
Serial.println("STA Failed to configure");
}
Serial.println(F("Adafruit IO MQTTS (SSL/TLS) Example"));// Connect to WiFi access point.
Serial.println(); Serial.println();
Serial.print("Connecting to ");
Serial.println(WLAN_SSID);delay(1000);
WiFi.begin(WLAN_SSID, WLAN_PASS);
delay(2000);while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();Serial.println("WiFi connected");
Serial.println("IP address: "); Serial.println(WiFi.localIP());// Set Adafruit IO's root CA
client.setCACert(adafruitio_root_ca);
}uint32_t x=0;
void loop() {
// Ensure the connection to the MQTT server is alive (this will make the first
// connection and automatically reconnect when disconnected). See the MQTT_connect
// function definition further below.
MQTT_connect();// Now we can publish stuff!
Serial.print(F("\nSending val "));
Serial.print(x);
Serial.print(F(" to test feed..."));
if (! test.publish(x++)) {
Serial.println(F("Failed"));
} else {
Serial.println(F("OK!"));
}// wait a couple seconds to avoid rate limit
delay(2000);}
// Function to connect and reconnect as necessary to the MQTT server.
// Should be called in the loop function and it will take care if connecting.
void MQTT_connect() {
int8_t ret;// Stop if already connected.
if (mqtt.connected()) {
return;
}Serial.print("Connecting to MQTT... ");
uint8_t retries = 3;
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
Serial.println(mqtt.connectErrorString(ret));
Serial.println("Retrying MQTT connection in 5 seconds...");
mqtt.disconnect();
delay(5000); // wait 5 seconds
retries--;
if (retries == 0) {
// basically die and wait for WDT to reset me
while (1);
}
}Serial.println("MQTT Connected!");
}