Using MQTT-TLS library to connect to AWS IoT Core

First I used the install library command from the workbench, but now I tried to start everything from scratch, used the github code as library, reformatted the certificates, had no other library or anything in my code and still… no connection.

Thank you so much for your help!

EDIT: it works now, I did reformat the certificates to a “one-line” const char* (see the code below) - I don’t know why that changed anything as the size of the string stays the same and I had to use sizeof() instead of strlen(). Either way I’m really happy it finally works.
Again thank you for your help!!!

#include "cert.h"
#include "MQTT-TLS.h"

const char awsRootCert[] = "-----BEGIN CERTIFICATE-----\r\nMIIDQTCCAimgAwIBAgITBmyfz5m/jAo54vB4ikPmljZbyjANBgkqhkiG9w0BAQsF\r\nADA5MQswC-----END CERTIFICATE-----";
const char awsClientCert[] = "-----BEGIN CERTIFICATE-----\r\nMIIDWTCCAkGgAwIBAgIUN94dGYvsTJoeQvwxz6JgJcdOWjUwDQYJKoZIhvcNAQEL\r\nBQAwTT-----END CERTIFICATE-----";
const char awsClientKey[] = "-----BEGIN RSA PRIVATE KEY-----\r\nMIIEowIBAAKCAQEA2dS+/HsIGCN6d0tz+BZQ8eNj18OiyjNBzevQEdJ4tRKcfXgk\r\nGkoDy/PPEjV----END RSA PRIVATE KEY-----";

void callback(char* topic, byte* payload, unsigned int length);

MQTT client("a17c5omik6yjsa-ats.iot.eu-central-1.amazonaws.com",8883, callback);

void callback(char* topic, byte* payload, unsigned int length) {
    char p[length + 1];
    memcpy(p, payload, length);
    p[length] = NULL;
    String message(p);
}


// setup() runs once, when the device is first turned on.
void setup() {
  // Put initialization like pinMode and begin functions here.

client.enableTls(awsRootCert, sizeof(awsRootCert),awsClientCert, sizeof(awsClientCert), awsClientKey, sizeof(awsClientKey));
client.connect("Particle_1");
client.publish("sensor/test", "temp");


}

// loop() runs over and over again, as quickly as it can execute.
void loop() {
  if(client.isConnected()){
    client.loop();
    client.publish("sensor/test", "80");
    Particle.publish("connection", "connected", PRIVATE);
  } else
  {
    Particle.publish("connection", "not connected", PRIVATE);
  }
  

  delay(10000);
  // The core of your code will likely live here.

}
3 Likes