Description: I'm facing a significant problem with my current project involving the integration of a SIMCOM A7670 module with an ESP32 to establish an MQTT connection. Despite carefully following the code provided at SimCom - mqtt example and other resources like Other example, I'm still unable to get the device to connect to the MQTT server.
Problem Details:
Hardware Used: ESP32 and SIMCOM A7670.
Libraries Used: I'm using the TinyGsmClient.h and PubSubClient.h libraries.
**Problem Description: **The main issue lies in the device not connecting to the MQTT server, preventing it from subscribing to the desired topic and receiving or sending MQTT messages.
Steps Taken: I've meticulously reviewed my code, ensuring it's properly tailored to my hardware and network configuration. Additionally, I've verified the physical hardware connections and the network setup on the MQTT server.
Error Messages: When i use mqtt.state()
I obtain -2.
Code
#define TINY_GSM_MODEM_SIM7600
#define TINY_GSM_RX_BUFFER 1024
#define SerialAT Serial1
#define TINY_GSM_USE_GPRS true
#define TINY_GSM_USE_WIFI false
#include "Arduino.h"
#include <TinyGsmClient.h>
#include <PubSubClient.h>
TinyGsm modem(SerialAT);
TinyGsmClient client(modem);
PubSubClient mqtt(client);
// const char apn[] = "internet.movistar.com.co";
// const char gprsUser[] = "MOVISTAR";
// const char gprsPass[] = "MOVISTAR";
const char apn[] = "internet.comcel.com.co";
const char gprsUser[] = "";
const char gprsPass[] = "";
const char* mqtt_server = "mi ip server***";
const int mqtt_port = "port";//for example 1884
const char* mqtt_topic = "topic";
void reconnect() {
while (!mqtt.connected()) {
Serial.println("Conectando al broker MQTT...");
if (mqtt.connect("ESP32Client")) {
Serial.println("Conectado al broker MQTT");
mqtt.subscribe(mqtt_topic);
} else {
Serial.print("Error de conexión al broker MQTT, rc=");
Serial.print(mqtt.state());
Serial.println(" Intentando de nuevo en 5 segundos");
delay(5000);
}
}
}
void setup() {
Serial.begin(115200);
SerialAT.begin(115200, SERIAL_8N1, 16, 17); // RX y TX
delay(3000); // Espera para que el módem se inicialice correctamente
// Enciende el módem
pinMode(4, OUTPUT);
digitalWrite(4, HIGH);
delay(1000);
String name = modem.getModemName();
Serial.print("Modem Name: "); // Change println to print
Serial.println(name); // Add println for newline
// Obtiene la información del módem
String ccid = modem.getSimCCID();
Serial.println("CCID: " + ccid);
String imei = modem.getIMEI();
Serial.println("IMEI: " + imei);
String cop = modem.getOperator();
Serial.println("Operator: " + cop);
IPAddress local = modem.localIP();
Serial.println("Local IP: " + String(local));
int csq = modem.getSignalQuality();
Serial.println("Signal quality: " + String(csq));
// Consulta información sobre la conexión
SerialAT.println("AT+CPSI?");
modem.getModemInfo();
// Conexion a VPN
Serial.print("Esperando a la red...");
if (!modem.waitForNetwork()) {
Serial.println(" fallo");
delay(10000);
return;
}
Serial.println(" éxito");
Serial.print(F("Conectando a "));
Serial.print(apn);
if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
Serial.println(" fallo");
delay(10000);
return;
}
// Config mqtt
mqtt.setServer(mqtt_server, mqtt_port);
}
void loop() {
if (!modem.isNetworkConnected()) {
Serial.println("Red desconectada");
if (!modem.waitForNetwork(180000L, true)) {
Serial.println(" fallo");
delay(10000);
return;
}
if (modem.isNetworkConnected()) {
Serial.println("Red reconectada");
}
if (!modem.isGprsConnected()) {
Serial.println("GPRS desconectado!");
Serial.print(F("Conectando a "));
Serial.print(apn);
if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
Serial.println(" fallo");
delay(10000);
return;
}
if (modem.isGprsConnected()) { Serial.println("GPRS reconectado"); }
}
}
if (!mqtt.connected()) {
reconnect();
}
mqtt.loop();
}
**
Purpose of the Post: My primary goal is to seek the community's assistance in identifying the root cause of this MQTT connection problem. I appreciate any guidance, advice, or solutions that can be offered to overcome this obstacle.
Module SIMCOM:
SimCom Module
ESP32
DEVKIT ESP32
They are connected serially, the Apn and others work correctly but it does not connect to the mqtt server