Why won't my Arduino MKR Wifi 1010 connect to AWS Iot Core?

I am attempting to connect my Arduino MKR Wifi 1010 board to AWS IoT Core via MQTT. The code is based on the demo code from the Arduino MQTT Client Library and Arduino https://docs.arduino.cc/tutorials/mkr-wifi-1010/securely-connecting-an-arduino-mkr-wifi-1010-to-aws-iot-core/

I created a certificate for a "thing" on AWS IoT core with a policy that accepts everything however when I try to run my code on the Arduino it keeps getting stuck trying to connect to the MQTTClient broker, producing an error code of -2 (which apparantely means connection refused by server). I think my problems lies in either the certificate I am using as the certificate I downloaded came with a few certificates.

Here is the code I'm running:

#include <ArduinoBearSSL.h>
#include <ArduinoECCX08.h>
#include <ArduinoMqttClient.h>
#include <WiFiNINA.h> 
#include "arduino_secrets.h"

/////// Enter your sensitive data in arduino_secrets.h
const char ssid[]        = SECRET_SSID;
const char pass[]        = SECRET_PASS;
const char broker[]      = SECRET_BROKER;
const char clientid[]    = SECRET_CLIENT_ID;
const char* root_cert    = SECRET_ROOT_CERTIFICATE;
const char* certificate  = SECRET_CERTIFICATE;
const char* public_cert  = SECRET_PUBLIC_CERTIFICATE;
const char* private_cert = SECRET_PRIVATE_CERTIFICATE;
const char* aws_cert     = SECRET_AWS_CERTIFICATE;
int port = 8883;
const char topic[] = "arduino/incoming";

WiFiSSLClient    wifiClient;            // Used for the TCP socket connection
//BearSSLClient SslClient(wifiClient); // Used for SSL/TLS connection, integrates with ECCX08
MqttClient    mqttClient(wifiClient); // Used for MQTT protocol usage

unsigned long lastMillis = 0;
int count = 0;

void setup() {
  Serial.begin(9600);
  while (!Serial);

  if (!ECCX08.begin()) {
    Serial.println("No ECCX08 present!");
    while (1);
  }
 
 // attempt to connect to WiFi network:
  Serial.print("Attempting to connect to WPA SSID: ");
  Serial.println(ssid);
  while (WiFi.begin(ssid, pass) != WL_CONNECTED) {
    // failed, retry
    Serial.print(".");
    delay(5000);
  }

  Serial.println("You're connected to the network");
  Serial.println();

  // Set a callback to get the current time
  // used to validate the servers certificate
  //ArduinoBearSSL.onGetTime(getTime);

  // Set the ECCX08 slot to use for the private key
  // and the accompanying public certificate for it
  //SslClient.setEccSlot(0, root_cert);

  //SslClient.setKey(certificate, private_cert);
  // Optional, set the client id used for MQTT,
  // each device that is connected to the broker
  // must have a unique client id. The MQTTClient will generate
  // a client id for you based on the millis() value if not set
  mqttClient.setId(clientid);
  
  // Set the message callback, this function is
  // called when the MQTTClient receives a message
  // mqttClient.onMessage(onMessageReceived);
  Serial.print("Attempting to connect to the MQTT broker: ");
  Serial.println(broker);

  if (!mqttClient.connect(broker, port)) {
    Serial.print("MQTT connection failed! Error code = ");
    Serial.println(mqttClient.connectError());

    while (1);
  }
  Serial.println("You're connected to the MQTT broker!");
  Serial.println();
}

void loop() {
  if (WiFi.status() != WL_CONNECTED) {
    connectWiFi();
  }

  if (!mqttClient.connected()) {
    // MQTT client is disconnected, connect
    connectMQTT();
  }

  // poll for new MQTT messages and send keep alives
  mqttClient.poll();

  // publish a message roughly every 5 seconds.
  if (millis() - lastMillis > 5000) {
    lastMillis = millis();

    publishMessage();
  }
}

unsigned long getTime() {
  // get the current time from the WiFi module  
  return WiFi.getTime();
}

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