How to connect Emqx using cellular Netwrok #162
Unanswered
Nikitanagar
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I am trying to connect my ESP32 with EMQX over a cellular network using a SIM7600 module. The same code works fine over WiFi, but it fails when using GPRS/LTE.
Successfully connected the SIM7600 to the network (I can get an IP address).
Here is my code:
`#define TINY_GSM_MODEM_SIM7600 // SIM7600 AT instruction is compatible with A7670
#define TINY_GSM_USE_GPRS true
#include <PubSubClient.h>
#include "credentials.h"
#include <ArduinoJson.h>
#include <TinyGsmClient.h>
#include <TinyGPSPlus.h>
#define RXD2 27
#define TXD2 26
#define SerialAT Serial1
#define SerialGPS Serial2
#define GNSS_enable 14 //Set as LOW
#define GSM_Power_Sequence 33
const char apn[] = "";
const char gprsUser[] = "";
const char gprsPass[] = "";
#ifdef DUMP_AT_COMMANDS
#include <StreamDebugger.h>
StreamDebugger debugger(SerialAT, Serial);
TinyGsm modem(debugger);
#else
TinyGsm modem(SerialAT);
#endif
TinyGsmClient client(modem);
PubSubClient mqtt(client);
// Function Declarations
void connectToMQTT();
void mqttCallback(char *topic, byte *payload, unsigned int length);
void setup()
{
Serial.begin(115200);
SerialAT.begin(115200, SERIAL_8N1, RXD2, TXD2);
SerialGPS.begin(115200, SERIAL_8N1, 17, 16);
delay(5000);
pinMode(GNSS_enable, OUTPUT);
pinMode(GSM_Power_Sequence, OUTPUT);
if (!modem.init()) {
DBG("Failed to restart modem, delaying 10s and retrying");
return;
}
digitalWrite(GNSS_enable, LOW);
digitalWrite(GSM_Power_Sequence, HIGH); //GSM Power Up Sequence
delay(1500);
digitalWrite(GSM_Power_Sequence, LOW);
delay(500);
String name = modem.getModemName();
DBG("Modem Name:", name);
String modemInfo = modem.getModemInfo();
DBG("Modem Info:", modemInfo);
Serial.println("Waiting for network...");
if (!modem.waitForNetwork()) {
Serial.println(" fail");
delay(10000);
return;
}
Serial.println(" success");
if (modem.isNetworkConnected()) {
Serial.println("Network connected");
}
// GPRS connection parameters are usually set after network registration
Serial.print(F("Connecting to "));
Serial.print(apn);
if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
Serial.println(" fail");
delay(10000);
return;
}
Serial.println(" success");
if (modem.isGprsConnected()) {
Serial.println("LTE module connected");
}
mqtt.setServer(mqtt_broker, mqtt_port);
mqtt.setCallback(mqttCallback);
}
void connectToMQTT()
{
while (!mqtt.connected()){
Serial.print("Attempting MQTT connection...");
}
}
void mqttCallback(char *topic, byte *payload, unsigned int length)
{
Serial.print("Message received on topic: ");
Serial.println(topic);
Serial.print("Message: ");
for (unsigned int i = 0; i < length; i++)
{
Serial.print((char)payload[i]);
}
Serial.println("\n-----------------------");
}
void loop()
{
if (!mqtt.connected())
{
connectToMQTT();
}
mqtt.loop();
delay(100);
}`
Error: The MQTT connection fails with rc=-2 or rc=-4.
Problem:
Works fine over WiFi.
Over GSM, the connection fails.
The device gets an IP address, but the MQTT handshake does not complete.
Questions:
Does EMQX require any special settings for cellular MQTT connections?
Is there a way to debug the handshake process?
Any recommended APN settings or additional AT commands for SIM7600?
Any help would be appreciated. Thanks!
Beta Was this translation helpful? Give feedback.
All reactions