vshymanskyy / TinyGSM

A small Arduino library for GSM modules, that just works
GNU Lesser General Public License v3.0
1.91k stars 708 forks source link

Can't connect to MQTT broker from time to time #777

Open Veccoy opened 4 months ago

Veccoy commented 4 months ago

[X ] I have read the Troubleshooting section of the ReadMe

What type of issues is this?

[ ] Request to support a new module

[ ] Bug or problem compiling the library [X] Bug or issue with library functionality (ie, sending data over TCP/IP) [X] Question or request for help

What are you working with?

Modem: SIM7000E Waveshare Main processor board: ESP32 TinyGSM version: 0.11.7

Hi, I'm working with the hardware above in France with an NB-IoT SIM Card from Objenious (Bouygues Telecom). I'm trying to connect to the online MQTT broker from Adafruit for testing purposes. I'm running the second excerpt of my code in the setup function, the details of the functions called are in the third excerpt and the define are in the first excerpt. I'm using the Arduino IDE.

Although I already manage to connect to the MQTT broker from Adafruit and publish some data a few times, the module fail to connect to the broker since yesterday. It has already hapenned in the past and I don't know why sometimes it works. I tried several solutions, but all failed :

Can you please help me?

Excerpts of my code:

Defines:

#define TINY_GSM_MODEM_SIM7000
#include <TinyGsmClient.h> // https://github.com/vshymanskyy/TinyGSM

#define MODEM_RX                32 // ESP32 hardware serial GPIO32 to module RX 
#define MODEM_TX                33 // ESP32 hardware serial GPIO33 to module TX

#define SerialAT Serial1

#define DUMP_AT_COMMANDS

#define TINY_GSM_YIELD() { delay(2); }

#define USE_LTE       true
#define USE_GPRS      true

#define SIM_PIN ""
#if USE_GPRS
#define APN               "www.ido.net"
#define GPRS_USER         ""
#define GPRS_PASSWORD     ""
#endif

#if USE_GPRS && not defined TINY_GSM_MODEM_HAS_GPRS
#undef USE_GPRS
#undef USE_WIFI
#define USE_GPRS false
#define USE_WIFI true
#endif
#if USE_WIFI && not defined TINY_GSM_MODEM_HAS_WIFI
#undef USE_GPRS
#undef USE_WIFI
#define USE_GPRS true
#define USE_WIFI false
#endif
#if USE_GPS && not defined TINY_GSM_MODEM_HAS_GSM_LOCATION
#undef USE_GPS
#define USE_GPS false
#endif

#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);

In setup function:

initializeModem();
initializeGPS();
setNetworkMode();
connectCellNetwork();
mqtt.setServer(MQTT_BROKER, MQTT_PORT);
connectMQTTBroker(true);

Functions:

void initializeModem()
{
  Serial.print("Initializing modem...");

  if (!modem.init()) {
    Serial.print(" fail! Can't initialize the Modem. Restarting the modem...");
    if (!modem.restart()) {
      Serial.println(" fail! Even after restart. Restarting ESP...");
      ESP.restart();
    }
  }

  #if USE_GPRS
    if (SIM_PIN && modem.getSimStatus() != 3) {
      Serial.println("Unlocking SIM...");
      modem.simUnlock(SIM_PIN);
    }
  #endif

  Serial.println(" Modem initialized.");
}

void setNetworkMode()
{
  bool set = false;

  // Network Mode : 2-Automatic, 13-GSM(2G), 38-LTE(CAT-M1), 51-GSMandLTEonly
  // Prefered Mode : 1 - CAT-M, 2 - NB-IOT, 3 - auto NB-IOT and CAT-M
  while (!set) {
    #ifdef USE_LTE
      #if USE_LTE
        set = modem.setNetworkMode(38);
        Serial.printf("Setting network MODE to LTE: %s", set ? "ok\n" : "fail! Retrying...\n");
        delay(10000);
      #else
        set = modem.setNetworkMode(13);
        Serial.printf("Setting network MODE to GSM (2G): %s", set ? "ok\n" : "fail! Retrying...\n");
        delay(10000);
      #endif
    #else
      set = modem.setNetworkMode(2);
    #endif

    if (set) {
      delay(50);
      set = modem.setPreferredMode(2);
      Serial.printf("Setting band to NB IoT: %s", set ? "ok\n" : "fail! Retrying...\n");
      delay(10000);
    }
  }
}

void connectCellNetwork()
{
  while (!modem.isNetworkConnected()) {
    Serial.print(F("Network disconnected. Connecting..."));
    if (!modem.waitForNetwork(180000L, true)) {
      Serial.println(F("Failed to connect to the network or SIM not registered, retrying..."));
      delay(10000);
    }
  }
  Serial.println(F("Connected to the network."));

  int sigQuality = modem.getSignalQuality();
  Serial.printf("Signal Quality: %d/30\n", sigQuality);

  #if USE_GPRS
    while (!modem.isGprsConnected()) {
      Serial.print(F("GPRS disconnected. Connecting to "));
      Serial.print(APN);
      Serial.println(F("..."));
      if (!modem.gprsConnect(APN, GPRS_USER, GPRS_PASSWORD)) {
        Serial.println(F("Failed to enable GPRS connection, retrying..."));
        delay(10000);
      }
    }
    Serial.println(F("GPRS connected!"));
  #endif
}

void connectMQTTBroker(bool setup)
{
  while (!mqtt.connected()) {
    mqtt.disconnect();
    Serial.printf("MQTT disconnected. Connecting to %s...", MQTT_BROKER);

    if (!mqtt.connect("TestClient", MQTT_USERNAME, MQTT_PASSWORD)) {
      Serial.println(F("Failed to connect to the MQTT broker, retrying..."));
      delay(10000);
    }
  }

  Serial.println(F("Connected to the MQTT broker!"));

  if (setup) {
    mqtt.publish(INIT_TOPIC, "Connecté.");
  }
}

void initializeGPS()
{
  Serial.print("Initializing GPS...");
  while (!modem.enableGPS()){
    Serial.println(" fail! Retrying...");
    delay(10000);
  }

  Serial.print(" GPS enabled: ");
  Serial.println(modem.getGPSraw());
}

Expected result

0, CONNECT OK

Actual result

17:49:29.005 -> Initializing modem...AT
17:49:29.005 -> 
17:49:29.005 -> OK
17:49:29.005 -> ATE0
17:49:29.005 -> 
17:49:29.005 -> OK
17:49:29.037 -> AT+CMEE=0
17:49:29.037 -> 
17:49:29.037 -> OK
17:49:29.037 -> AT+CLTS=1
17:49:29.072 -> 
17:49:29.072 -> OK
17:49:29.072 -> AT+CBATCHK=1
17:49:29.072 -> 
17:49:29.104 -> OK
17:49:29.104 -> AT+CPIN?
17:49:29.104 -> 
17:49:29.104 -> +CPIN: READY
17:49:29.136 -> 
17:49:29.136 -> OK
17:49:29.136 -> AT+CPIN?
17:49:29.136 -> 
17:49:29.136 -> +CPIN: READY
17:49:29.173 -> 
17:49:29.173 -> OK
17:49:29.173 -> Unlocking SIM...
17:49:29.173 ->  Modem initialized.
17:49:29.173 -> Initializing GPS...AT+CGNSPWR=1
17:49:29.173 -> 
17:49:29.205 -> OK
17:49:29.205 ->  GPS enabled: AT+CGNSINF
17:49:29.205 -> 
17:49:29.205 -> +CGNSINF: 1,0,,,,,,,0,,,,,,,,,,,,,
17:49:29.238 -> 
17:49:29.238 -> OK
17:49:29.284 -> 1,0,,,,,,,0,,,,,,,,,,,,,
17:49:29.284 -> AT+CNMP=38
17:49:29.284 -> 
17:49:29.284 -> OK
17:49:29.284 -> Setting network MODE to LTE: ok
17:49:39.313 -> AT+CMNB=2
17:49:39.358 -> 
17:49:39.358 -> OK
17:49:39.358 -> Setting band to NB IoT: ok
17:49:49.352 -> AT+CEREG?
17:49:49.352 -> 
17:49:49.387 -> +CEREG: 0,1
17:49:49.387 -> 
17:49:49.387 -> OK
17:49:49.387 -> Connected to the network.
17:49:49.387 -> AT+CSQ
17:49:49.387 -> 
17:49:49.425 -> +CSQ: 19,99
17:49:49.425 -> 
17:49:49.425 -> OK
17:49:49.425 -> Signal Quality: 19/30
17:49:49.425 -> AT+CGATT?
17:49:49.425 -> 
17:49:49.463 -> +CGATT: 1
17:49:49.463 -> 
17:49:49.463 -> OK
17:49:49.463 -> AT+CIFSR;E0
17:49:49.463 -> 
17:49:49.502 -> 10.108.92.30
17:49:49.502 -> 
17:49:49.502 -> OK
17:49:49.502 -> GPRS connected!
17:49:49.502 -> AT+CIPRXGET=4,0
17:49:49.533 -> 
17:49:49.533 -> +CIPRXGET: 4,0,0
17:49:49.533 -> 
17:49:49.533 -> OK
17:49:49.565 -> AT+CIPSTATUS=0
17:49:49.565 -> 
17:49:49.565 -> +CIPSTATUS: 0,0,"TCP","","1883","CONNECTING"
17:49:49.641 -> 
17:49:49.641 -> OK
17:49:49.924 -> 
17:49:49.924 -> 0, CONNECT FAIL
17:49:51.567 -> AT+CIPSEND=0,2
17:49:51.599 -> 
17:49:51.599 -> ERROR
17:49:51.599 -> AT+CIPCLOSE=0
17:49:51.631 -> 
17:49:51.631 -> ERROR
17:49:51.631 -> MQTT disconnected. Connecting to io.adafruit.com...AT+CIPRXGET=4,0
17:49:51.663 -> 
17:49:51.663 -> +CIPRXGET: 4,0,0
17:49:51.698 -> 
17:49:51.698 -> OK
17:49:51.698 -> AT+CIPSTATUS=0
17:49:51.698 -> 
17:49:51.730 -> +CIPSTATUS: 0,,"","","","INITIAL"
17:49:51.763 -> 
17:49:51.763 -> OK
17:49:51.763 -> AT+CIPCLOSE=0
17:49:51.763 -> 
17:49:51.800 -> ERROR
17:49:51.800 -> AT+CIPSTART=0,"TCP","io.adafruit.com",1883
17:49:51.847 -> 
17:49:51.847 -> OK
17:50:41.941 -> 
17:50:41.976 -> 0, CONNECT FAIL
17:50:41.976 -> Failed to connect to the MQTT broker, retrying...
17:50:51.982 -> AT+CIPRXGET=4,0
17:50:51.982 -> 
17:50:52.021 -> +CIPRXGET: 4,0,0
17:50:52.021 -> 
17:50:52.021 -> OK
17:50:52.021 -> AT+CIPSTATUS=0
17:50:52.052 -> 
17:50:52.052 -> +CIPSTATUS: 0,,"","","","INITIAL"
17:50:52.097 -> 
17:50:52.097 -> OK
17:50:52.097 -> AT+CIPSEND=0,2
17:50:52.097 -> 
17:50:52.128 -> ERROR
17:50:52.128 -> AT+CIPCLOSE=0
17:50:52.128 -> 
17:50:52.128 -> ERROR
17:50:52.161 -> MQTT disconnected. Connecting to io.adafruit.com...AT+CIPCLOSE=0
17:50:52.161 -> 
17:50:52.161 -> ERROR
[...]
Veccoy commented 4 months ago

Now, it works again without changing anything to the code. It might come from the Adafruit MQTT broker that could limit the connexion