how to connect esp32 to mqtt broker with ssl/tls encryption using sim800l network

71 Views Asked by At

i am using lilygo module which has sim800l and esp32. i am using tinygsm and pubsub libraries. i am usable to connect to mqtt broker with ssl/tls encryption. how to do it?

#include "config.h"
#define TINY_GSM_MODEM_SIM800
#define TINY_GSM_RX_BUFFER   1024  // Set RX buffer to 1Kb
#include <TinyGsmClient.h>
#include <PubSubClient.h>
#ifdef DUMP_AT_COMMANDS
  #include <StreamDebugger.h>
  StreamDebugger debugger(SerialAT, SerialMon);
  TinyGsm modem(debugger);
#else
  TinyGsm modem(SerialAT);
#endif
// TinyGSM Client for Internet connection

TinyGsmClientSecure client(modem);
PubSubClient  mqtt(client);

void setup() 
{
  pinMode(MODEM_PWKEY, OUTPUT);
  pinMode(MODEM_RST, OUTPUT);
  pinMode(MODEM_POWER_ON, OUTPUT);
  digitalWrite(MODEM_PWKEY, LOW);
  digitalWrite(MODEM_RST, HIGH);
  digitalWrite(MODEM_POWER_ON, HIGH);
  SerialMon.begin(115200);
  SerialAT.begin(115200, SERIAL_8N1, MODEM_RX, MODEM_TX);
  SerialMon.println("Initializing modem...");
  modem.restart();
  if(connectToMQTT())
  {
    publishData();
    SerialMon.println("DATA HAS BEEN PUBLISHED");
  }
  else
  {
    SerialMon.println("FAILED TO PUBLISH");
  }
}

void loop() 
{
  while(SerialAT.available())
  {
    //SerialMon.println(SerialAT.readStringUntil('\n'));
    SerialMon.println(SerialAT.readString());
  }
  while(SerialMon.available())
  {
    SerialAT.write(SerialMon.read());
  }
}

so in the above code i used TinyGsmClientSecure client(modem); PubSubClient mqtt(client);

for ssl mqtt connection.

bool connectToMQTT()
{
  modem.gprsConnect("CMNET");
  const char *ca_cert = NULL;
  //client.setCertificate(ca_cert);
  // Set the certificate for SSL/TLS
    SerialMon.print(F("Setting SSL certificate... "));
modem.sendAT(GF("+SSLSETCERT=\"" CERT_FILE "\""));
int response = modem.waitResponse(5000);  // Adjust timeout as needed

if (response == 1) {
    SerialMon.println(F("OK"));
} else {
    SerialMon.println(F("Failed"));
    SerialMon.print(F("AT response code: "));
    SerialMon.println(response);
    while (1);
}
  mqtt.setServer("pag.tayrix.com", 30883);
  mqtt.setCallback(mqttCallback);
  SerialMon.println("Connecting to MQTT broker...");
  // Connect to MQTT Broker
  //boolean status = mqtt.connect("GsmClientTest");
   boolean status = mqtt.connect("GsmClientName", "[email protected]", "a2k1jkd");
  if (status == false) 
  {
    SerialMon.println(" fail");
    return false;
  }
  else 
  {
    SerialMon.println(" success");
    return true;
  }
  //mqtt.subscribe(topicLed);
}
void publishData()
{
  // Replace "your_topic" with the desired MQTT topic
  const char *topic = "telemetry";
  const char *message = "Hello, MQTT!";  // Replace with your actual data
  String kkk=send_json_data();
  mqtt.publish(topic, kkk.c_str());
  
}
void mqttCallback(char* topic, byte* payload, unsigned int length) 
{
  
}

when i use

const char* ca_cert = "-----BEGIN CERTIFICATE-----\n..."; client.setCACert(ca_cert);

here it is giving setCACert is not a function of TinyGsmClientSecure. so help me how connect broker with ssl mqtt with sim800l?

0

There are 0 best solutions below