Hi,
I can t get a connection via TinyGsm plus SSLClientESP32 from an ESP32 with a SIM800L GSM modem. I tried all sorts of certificates related to HiveMQ. mqttClient.connect() returns -2. I have no clue how to solve this. I basically will give up and try another MQTT broker. But, by chance, if anyone has tried that with success, I would be curious.
#define RX_GPS 34 // → connect to TX on GPS board, GPIOs 34 & 35
#define TX_GPS 35 //pins for SoftwareSerial connection to the GPS module
#define MQTT_MESSAGE_SIZE 300
// Configure TinyGSM library
#define TINY_GSM_MODEM_SIM800 // Modem is SIM800
#define TINY_GSM_RX_BUFFER 1024 // Set RX buffer to 1Kb
#define SIM800L_IP5306_VERSION_20200811
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h> //I2C
#include <WiFi.h>
#include <PubSubClient.h> //MQTT
#include <ArduinoJson.h> //build a dynamic or static JSON buffer in memory
#include <time.h>
#include <TinyGPSPlus.h> // Tiny GPS Plus Library
#include <TinyGsmClient.h>
#include “utilities.h” // belongs to TinyGsmClient.h
#include <SoftwareSerial.h> // uses actually ESPSoftwareSerial
#include “hivemq_realroot_ca.h”
#include <SSLClientESP32.h> //GitHub - alkonosst/SSLClientESP32: SSLClient - generic secure client Arduino library using mbedtls
char buffer[MQTT_MESSAGE_SIZE]; //MQTT sending buffer
// WiFi
const char *ssid = “fastnetap”; // Enter your WiFi name
const char *password = “1rede2RAP!”; // Enter WiFi password
// MQTT Broker
const int mqtt_port = 8883;
const char *mqtt_username = “blabla”;
const char *mqtt_password = “blabla”;
//this is the TLS URI
const char *mqtt_broker = “blabla.s2.eu.hivemq.cloud”;
const char *topic = “something”;
const char *client_id = “something”;
unsigned int count = 0;
// Your GPRS credentials (leave empty, if missing)
const char apn = “xxx”; // Your APN
const char gprsUser = “”; // User
const char gprsPass = “”; // Password
const char simPIN = “”; // SIM card PIN code, if any
//ToDo dont know what this WiFiClient is
//WiFiClient wifiClient;
TinyGsm modem(Serial1);
TinyGsmClient gsmClient(modem);
SSLClientESP32 sslClient(&gsmClient);
PubSubClient mqttClient(sslClient);
Adafruit_MPU6050 mpu;
TinyGPSPlus gps;
SoftwareSerial gpsSerial(RX_GPS, TX_GPS); //RX GPIO34 and TX…GPIO35
void setup() {
//SerialMonitor via Serial0 - USB. Also used for programming/flashing the ESP32
Serial.begin(115200);
delay(10);
// GSM
sslClient.setCACert(lastTry);
connectGSMModemToMobileNetwork();
delay(1000);
\
//time sync from GSM network time
setDateTime();
Serial.print("[INFO] local clock synched. The time is: ");
Serial.println(getLocalTimestampAsString());
//MQTT
//ToDo MQTT_MAX_PACKET_SIZE:256 Maximum packet size. Override with setBufferSize()
mqttClient.setServer(mqtt_broker, mqtt_port);
delay(100);
if (!mqttClient.setBufferSize(MQTT_MESSAGE_SIZE)){
Serial.println(“[WARNING] could not resize MQTT pubsub buffer”);
} //try to resize buffer from default value of 256, returns True or False
//mqttClient.setCallback(callback);
while (!mqttClient.connected()) {
//String client_id = “radlone”;
//client_id += String(WiFi.macAddress());
Serial.printf(“[INFO] The client %s connects to the public MQTT broker\n”, client_id);
//if (mqttClient.connect(client_id.c_str(), mqtt_username, mqtt_password)) {
if (mqttClient.connect(client_id, mqtt_username, mqtt_password)) {
Serial.println(“[INFO] Hive MQTT broker connected”);
} else {
Serial.print("[INFO] failed connecting Hive MQTT. State: ");
Serial.println(mqttClient.state());
delay(200);
}
}
//END MQTT
}