How to connect a sim800l to a broker with SSL?

Viewed 2813

I am trying to connect to an a mosquito broker hosted on AWS, on port 8883, but so far I have not been successful. I am using the TinyGSM and PubSubClient libraries.

The GSM module is connected to the internet and works perfectly in a broker without SSL. But when I try to switch to SSL, he can't connect and the MQTT client returns -2. But this error is not very clear, as you can see here in the PubSub documentation.

#define MQTT_CONNECT_FAILED -2

These are the AT commands that it performs to try to connect to the broker

AT+CIPRXGET=4,0

+CIPRXGET: 4,0,0

OK
AT+CIPSTATUS=0

+CIPSTATUS: 0,0,"TCP","3.13.162.221","8883","CLOSED"

OK
* Trying to connect to the MQTT Broker: <broker_url>
AT+CIPCLOSE=0,1

ERROR
AT+CIPSSL=1

OK
AT+CIPSTART=0,"TCP",<broker_url>,8883

OK
Failed to reconnect to the broker.
Status: -2

Some useful parts of the code

void setupGSM() {
  SerialMon.println("Setup GSM...");
  
  modem.sendAT("+SSLOPT=1,1");
  if (modem.waitResponse() != 1) {
    SerialMon.printf("modem +SSLOPT=1,1 failed");
  }
  while (!modem.gprsConnect(APN, APN_USER, APN_KEY)) {
    SerialMon.println("GPRS Connection Failed");
    modem.restart();
    delay(1000);
  }
  SerialMon.println("GPRS Connection Success");
}

void reconnectMQTT() {
  while (!MQTT.connected()) {
    SerialMon.print("* Trying to connect to the MQTT Broker: ");
    SerialMon.println(BROKER_MQTT);
    initMQTT();

    if (MQTT.connect(ID_MQTT, MQTT_USERNAME, MQTT_KEY)) {
      SerialMon.println("Successfully connected to the MQTT broker!");
      MQTT.subscribe(RECEIVED_CREDITS);
    }
    else {
      SerialMon.println("Failed to reconnect to the broker.");
      SerialMon.print("Status: ");
      SerialMon.println(MQTT.state());
      delay(2000);
    }
  }
}

The question is, how can I connect to an MQTT broker with SSL using the sim800l module?

Info:
Modem: SIMCOM_SIM800L R14.18
Main processor board: TTGO-T-Call ESP32
TinyGSM Version: 0.10.5

1 Answers

The AWS IoT Broker requires a SSL client certificate to be used.

Related