Hi Everyone,
I have a project that involves sending MQTT messages to the Things MQTT broker on the domain mqttcomm.xyz.com. I am using an ESP32S3 and a Quectel M66 modem to provide GPRS connectivity, utilizing the TinyGsm Library to handle the GPRS connection setup and other functions of the GSM module.
Before continuing, I want to note that I am defining the board used by TinyGSM as the M95 with #define TINY_GSM_MODEM_M95 and not the M66, as it is not specifically supported. However, the AT Command set for both boards is identical, as shown here under the Connected Products heading https://www.quectel.com/download/quectel_gsm_atc_application_note_v1-2/.
For the past four days, I have been attempting to connect to the Thingsboard server without success. The device connects to the GPRS network and has a data connection, and it can ping IP addresses such as 1.1.1.1 and 8.8.8.8 using the AT+QIPING command. It can also resolve hostnames and retrieve their IP addresses using AT+QIDNSGIP. I have tried multiple methods to resolve the issue, including editing the AT commands sent by the library to set up the GPRS connection. The commands I edited are as:
- Changed
sendAT(GF("+QODE=0"));tosendAT(GF("+QIMODE=1"));(changes TCPIP Transfer Mode from Normal mode Transparent mode) - Changed
sendAT(GF("+QIMUX=1"));tosendAT(GF("+QIMUX=0"));(disables multiple TCPIP sessions at the same time)
I attempted to connect to the server manually using AT+QIOPEN, but it returned an ERROR. I also tried defining the PDP context manually using AT+CGDCONT and then activating the PDP context using AT+CGACT=1,1, but the issue persisted. I manually sent various combinations of AT commands used by the library to set the GPRS connection, thinking the library might be causing the problem, but to none of the above solutoins worked.
Later I tried with a SIMCom 800L module and didn't change a bit in the code apart from defining the new board using #define TINY_GSM_MODEM_SIM800and it just connected to Thingsboard server without any issues. I looked at the source file for the SIMCom 800 in the TinyGsm library TinyGsmClientSIM800.h and tried to replicate the commands that handles the GPRS connection sequence in the bool gprsConnectImpl() (from line 314 to line 386) function for the M66 but some AT commands didn't have direct equivalents but still I tried but still it couldn't connect.
I was hoping someone could guide me and tell me what I could be missing out that it is causing it to not connect.
Thanks
Attatching the AT command manual for M66
Quectel_GSM_ATC_Application_Note_V1.2.pdf (1.1 MB)
Attaching the code being used to connect to the server
#include <ThingsBoard.h>
#include <Arduino_MQTT_Client.h>
// Define the GSM modem model
#define TINY_GSM_MODEM_M95
#define GSM_AUTOBAUD_MIN 9600
#define GSM_AUTOBAUD_MAX 115200
// GPRS credentials
const char apn[] = "net"; // APN for
#include <TinyGsmClient.h>
constexpr char TOKEN[] = "XXYYZZAABB";
constexpr char THINGSBOARD_SERVER[] = "mqttcomm.xyz.com";
constexpr uint16_t THINGSBOARD_PORT = 1883U; // MQTT port
constexpr uint16_t MAX_MESSAGE_SIZE = 256U;
// Layers stack
TinyGsm modem(Serial1);
TinyGsmClient gsm_transpor_layer(modem);
Arduino_MQTT_Client mqttClient(gsm_transpor_layer);
ThingsBoard tb(mqttClient, MAX_MESSAGE_SIZE);
void setup() {
// Start Serial monitor
Serial.begin(115200);
delay(10);
// Start Serial1 for GSM communication
Serial1.begin(115200, SERIAL_8N1, RX, TX); // RX and TX are the predefined pins on Xiao ESP32S3
delay(1000);
// Restart the modem
Serial.println("Initializing modem...");
modem.restart();
// Wait for network registration
if (!modem.waitForNetwork()) {
Serial.println("Failed to connect to the network");
while (true)
;
}
Serial.println("Network connected");
// Connect to GPRS
if (!modem.gprsConnect(apn)) {
Serial.println("Failed to connect to GPRS");
while (true)
;
}
Serial.println("GPRS connected");
}
void loop() {
constexpr char CONNECTING_MSG[] = "Connecting to: (%s) with token (%s)";
// Add any functionality you want to run after connecting to GPRS
updateSerial();
if (!tb.connected()) {
// Reconnect to the ThingsBoard server,
// if a connection was disrupted or has not yet been established
char message[Helper::detectSize(CONNECTING_MSG, THINGSBOARD_SERVER, TOKEN)];
snprintf(message, sizeof(message), CONNECTING_MSG, THINGSBOARD_SERVER, TOKEN);
Serial.println(message);
if (!tb.connect(THINGSBOARD_SERVER, TOKEN, THINGSBOARD_PORT)) {
Serial.println("Failed to connect");
//Serial.println(mqttClient.connectError());
}
}
delay(1000);
tb.sendTelemetryData("TEMPERATURE_KEY", random(10, 31));
tb.sendTelemetryData("HUMIDITY_KEY", random(40, 90));
tb.loop();
}
void updateSerial() {
delay(500);
while (Serial.available()) {
Serial1.write(Serial.read()); // Forward what Serial received to Hardware Serial Port
}
while (Serial1.available()) {
Serial.write(Serial1.read()); // Forward what Hardware Serial received to Serial Port
}
}