Xinyuan-LilyGO / LilyGO-T-SIM7000G

LilyGO T-SIM7000G
https://pt.aliexpress.com/item/4000542688096.html
293 stars 128 forks source link

Constantly disconnects #163

Open kedzior-io opened 2 years ago

kedzior-io commented 2 years ago

I'm connecting to my own mqtt broker and testing the how stable the connection is and I'm getting a lot of modem disconnects.

I'm wondering of this is something related to the code or the network. I wouldn't say network coverage is a problem as I have tower nearby (also in city center). The device is powered with a proper 5V power supply.

This my log of when device is getting disconnected:

[19/02/2022 00:25]
Connected

[19/02/2022 00:38]
Connected

[19/02/2022 02:45]
Connected

[19/02/2022 05:49]
Connected

[19/02/2022 06:35]
Connected

[19/02/2022 08:33]
Connected

[19/02/2022 09:06]
Connected

[19/02/2022 09:21]
Connected

[19/02/2022 09:26]
Connected

[19/02/2022 09:28]
Connected

[19/02/2022 09:30]
Connected

[19/02/2022 09:32]
Connected

[19/02/2022 09:37]
Connected

[19/02/2022 09:48]
Connected

[19/02/2022 09:53]
Connected

[19/02/2022 09:55]
Connected

[19/02/2022 10:32]
Connected

[19/02/2022 10:34]
Connected

[19/02/2022 10:43]
Connected

[19/02/2022 10:49]
Connected

[19/02/2022 11:08]
Connected

[19/02/2022 11:43]
Connected

[19/02/2022 12:38]
Connected

[19/02/2022 12:53]
Connected

[19/02/2022 12:58]
Connected

[19/02/2022 13:01]
Connected

[19/02/2022 13:04]
Connected

[19/02/2022 13:05]
Connected

[19/02/2022 13:14]
Connected

[19/02/2022 13:17]
Connected

[19/02/2022 13:21]
Connected

[19/02/2022 13:22]
Connected

[19/02/2022 13:30]
Connected

[19/02/2022 13:31]
Connected

[19/02/2022 13:39]
Connected

[19/02/2022 13:55]
Connected

[19/02/2022 14:16]
Connected

[19/02/2022 14:20]
Connected

[19/02/2022 14:26]
Connected

[19/02/2022 14:56]
Connected

[19/02/2022 15:18]
Connected

[19/02/2022 15:22]
Connected

[19/02/2022 15:27]
Connected

[19/02/2022 15:32]
Connected

[19/02/2022 15:40]
Connected

[19/02/2022 15:53]
Connected

[19/02/2022 16:47]
Connected

[19/02/2022 17:33]
Connected

[19/02/2022 17:45]
Connected

[19/02/2022 18:28]
Connected

[19/02/2022 18:34]
Connected

[19/02/2022 21:03]
Connected

[19/02/2022 21:15]
Connected

[19/02/2022 21:27]
Connected

[19/02/2022 21:46]
Connected

[19/02/2022 22:28]
Connected

[19/02/2022 23:19]
Connected

[19/02/2022 23:48]
Connected

[19/02/2022 23:52]
Connected

[20/02/2022 06:27]
Connected

[20/02/2022 06:36]
Connected

[20/02/2022 09:20]
Connected

[20/02/2022 09:21]
Connected

[20/02/2022 10:18]
Connected

[20/02/2022 10:22]
Connected

[20/02/2022 11:18]
Connected

[20/02/2022 11:20]
Connected

[20/02/2022 11:21]
Connected

[20/02/2022 11:26]
Connected

[20/02/2022 11:39]
Connected

[20/02/2022 11:59]
Connected

[20/02/2022 12:08]
Connected

[20/02/2022 12:10]
Connected

[20/02/2022 12:28]
Connected

[20/02/2022 13:04]
Connected

[20/02/2022 14:04]
Connected

[20/02/2022 14:21]
Connected

[20/02/2022 14:51]
Connected

[20/02/2022 15:13]
Connected

[20/02/2022 15:16]
Connected

[20/02/2022 15:39]
Connected

[20/02/2022 15:42]
Connected

[20/02/2022 15:50]
Connected

[20/02/2022 15:53]
Connected

The code:

#define TINY_GSM_MODEM_SIM7000

#define TINY_GSM_RX_BUFFER 1024 // Set RX buffer to 1Kb

#include <TinyGsmClient.h>
#include <PubSubClient.h>

RTC_DATA_ATTR int bootCount = 0;   
HardwareSerial SerialAT(1);

#define SerialMon Serial
#define SerialAT Serial1

// rogers
const char apn[] = "orangeworld";
const char user[] = "";
const char pass[] = "";

#define LED_PIN 25

// MQTT details
const char* broker            = "mqtt.example.com";
const char* topicOpen         = "home/open";
const char* topicInit         = "home/init";
const char* topicBootCount    = "home/boot-count";

uint32_t lastReconnectAttempt = 0;

// TTGO T-SIM pin definitions
#define MODEM_RST 5
#define MODEM_PWKEY 4
#define MODEM_DTR 25
#define MODEM_TX 26
#define MODEM_RX 27
//#define I2C_SDA 21
//#define I2C_SCL 22
#define reading_samles 100

#ifdef DUMP_AT_COMMANDS
  #include <StreamDebugger.h>
  StreamDebugger debugger(SerialAT, SerialMon);
  TinyGsm modem(debugger);
#else
TinyGsm modem(SerialAT);
#endif

TinyGsmClient client(modem);
PubSubClient  mqtt(client);

// Set to true, if modem is connected
bool modemConnected = false;

void shutdown();

void wait_till_ready();

void modem_off();

void print_wakeup_reason()
{
  esp_sleep_wakeup_cause_t wakeup_reason;

  wakeup_reason = esp_sleep_get_wakeup_cause();

  switch (wakeup_reason)
  {
    case ESP_SLEEP_WAKEUP_EXT0:
      Serial.println("Wakeup caused by external signal using RTC_IO");
      break;
    case ESP_SLEEP_WAKEUP_EXT1:
      Serial.println("Wakeup caused by external signal using RTC_CNTL");
      break;
    case ESP_SLEEP_WAKEUP_TIMER:
      Serial.println("Wakeup caused by timer");
      break;
    case ESP_SLEEP_WAKEUP_TOUCHPAD:
      Serial.println("Wakeup caused by touchpad");
      break;
    case ESP_SLEEP_WAKEUP_ULP:
      Serial.println("Wakeup caused by ULP program");
      break;
    default:
      Serial.printf("Wakeup was not caused by deep sleep: %d\n", wakeup_reason);
      break;
  }
}

void modem_reset()
{
  Serial.println("Modem hardware reset");
  pinMode(MODEM_RST, OUTPUT);
  digitalWrite(MODEM_RST, LOW);
  delay(260); //Treset 252ms
  digitalWrite(MODEM_RST, HIGH);
  delay(4000); //Modem takes longer to get ready and reply after this kind of reset vs power on
  //modem.factoryDefault();
  //modem.restart(); //this results in +CGREG: 0,0
}

void modem_on()
{
  // Set-up modem  power pin
  pinMode(MODEM_PWKEY, OUTPUT);
  digitalWrite(MODEM_PWKEY, HIGH);
  delay(10);
  digitalWrite(MODEM_PWKEY, LOW);
  delay(1010); //Ton 1sec
  digitalWrite(MODEM_PWKEY, HIGH);

  //wait_till_ready();
  SerialMon.println(F("************************************************************************"));  
  Serial.println("Step 1: Waiting till modem ready...");
  delay(4510); //Ton uart 4.5sec but seems to need ~7sec after hard (button) reset
  //On soft-reset serial replies immediately.
  SerialAT.begin(115200, SERIAL_8N1, MODEM_TX, MODEM_RX);
  modem.setBaud(115200);
  modem.begin();
  delay(10000);

  if (!modem.restart()) {
    SerialMon.println(F(" [fail]"));
    SerialMon.println(F("************************"));
    SerialMon.println(F(" Is your modem connected properly?"));
    SerialMon.println(F(" Is your serial speed (baud rate) correct?"));
    SerialMon.println(F(" Is your modem powered on?"));
    SerialMon.println(F(" Do you use a good, stable power source?"));
    SerialMon.println(F("************************"));
    delay(10000);
    return;
  }

  SerialMon.println(F("************************************************************************"));
  SerialMon.println(F("Step 2: [OK] was able to open modem"));
  String modemInfo = modem.getModemInfo();
  SerialMon.println("Step 3: Modem details: ");
  SerialMon.println(modemInfo);

  SerialMon.println(F("************************************************************************"));
  SerialMon.println("Step 4: Waiting for network...");
  if (!modem.waitForNetwork()) {
    SerialMon.println(" fail");
    delay(10000);
    return;
  }
  SerialMon.println(" success");
  SerialMon.println(F("************************************************************************"));
  if (modem.isNetworkConnected()) {
    SerialMon.println("Network connected");
  }
  SerialMon.println(F("************************************************************************"));

  SerialMon.print("Step 5: Waiting for network...");
  if (!modem.waitForNetwork(1200000L)) {
    SerialMon.println(F(" [fail] while waiting for network"));
    SerialMon.println(F("************************"));
    SerialMon.println(F(" Is your sim card locked?"));
    SerialMon.println(F(" Do you have a good signal?"));
    SerialMon.println(F(" Is antenna attached?"));
    SerialMon.println(F(" Does the SIM card work with your phone?"));
    SerialMon.println(F("************************"));
    delay(10000);
    return;
  }
  SerialMon.println(F("Found network: [OK]"));
  SerialMon.println(F("************************************************************************"));

  SerialMon.print("Step 6: About to set network mode to LTE Only 38: ");
  // Might not be needed for your carrier 
  modem.setNetworkMode(38);

  delay(3000);

  SerialMon.print("Step 6: About to set network mode: to CAT=M");
  // Might not be needed for your carrier 
  modem.setPreferredMode(3);
  SerialMon.println(F("************************************************************************"));  
  delay(500);
  Serial.print(F("Waiting for network..."));

  if (!modem.waitForNetwork(60000L))
  {
      Serial.println(" fail");
      modem_reset();
      shutdown();
  }
      Serial.println(" OK");

      Serial.print("Signal quality:");
      Serial.println(modem.getSignalQuality());
  SerialMon.println(F("************************************************************************"));      
  delay(3000);  
  // GPRS connection parameters are usually set after network registration
  SerialMon.println("Step 7: Connecting to APN at LTE Mode Only (channel--> 38): ");
  SerialMon.println(apn);
  if (!modem.gprsConnect(apn, user, pass)) {
    SerialMon.println(F(" [fail]"));
    SerialMon.println(F("************************"));
    SerialMon.println(F(" Is GPRS enabled by network provider?"));
    SerialMon.println(F(" Try checking your card balance."));
    SerialMon.println(F("************************"));
    delay(10000);
    return;
  }

  if (modem.isGprsConnected()) {
    SerialMon.println(F("Step 8: Connected to network: [OK]"));
    IPAddress local = modem.localIP();
    SerialMon.print("Step 9: Local IP: ");
    SerialMon.println(local);
    modem.enableGPS();
    delay(3000);
    String IMEI = modem.getIMEI();
    SerialMon.print("Step 10: IMEI: ");
    SerialMon.println(IMEI);
  } else {
    SerialMon.println(F("Step 8: FAIL NOT Connected to network: "));
  }

   modemConnected = true;
   Serial.println("MODEM CONNECTED!");
   SerialMon.println(F("************************************************************************"));  

}

void modem_off()
{
  SerialMon.println(F("************************************************************************"));    
  //if you turn modem off while activating the fancy sleep modes it takes ~20sec, else its immediate
  Serial.println("Going to sleep now with modem turned off");
  //modem.gprsDisconnect();
  //modem.radioOff();
  modem.sleepEnable(false); // required in case sleep was activated and will apply after reboot
  modem.poweroff();
}

void shutdown()
{
  //modem_sleep();
  modem_off();
  delay(1000);
  Serial.flush();
  esp_deep_sleep_start();
  Serial.println("Going to sleep now");
  delay(1000);
  Serial.flush();
  esp_deep_sleep_start();
}

void wait_till_ready() // NOT WORKING - Attempt to minimize waiting time
{
  for (int8_t i = 0; i < 100; i++) //timeout 100 x 100ms = 10sec
  {
    if (modem.testAT())
    {
      Serial.printf("Wait time: %d\n", i);
      break;
    }
    delay(100);
  }
}

void mqttCallback(char* topic, byte* payload, unsigned int len) {
  SerialMon.print("Message arrived [");
  SerialMon.print(topic);
  SerialMon.print("]: ");
  SerialMon.write(payload, len);
  SerialMon.println();

  digitalWrite(LED_PIN, HIGH);
  delay(1000);
  digitalWrite(LED_PIN, LOW);
}

boolean mqttConnect() {
  SerialMon.print("Connecting to ");
  SerialMon.print(broker);

  // Connect to MQTT Broker
  boolean status = mqtt.connect("home");

  // Or, if you want to authenticate MQTT:
  // boolean status = mqtt.connect("GsmClientName", "mqtt_user", "mqtt_pass");

  if (status == false) {
    SerialMon.println(" fail");
    return false;
  }
  SerialMon.println(" success");
  mqtt.publish(topicInit, "home started");
  mqtt.subscribe(topicOpen);
  return mqtt.connected();
}

void setup()
{

  pinMode (LED_PIN, OUTPUT);
  digitalWrite(LED_PIN, HIGH);

  Serial.begin(115200);

  ++bootCount;
  Serial.println("Boot number: " + String(bootCount));

  modem_on();

  if(modemConnected){
      mqtt.setServer(broker, 1884);
      mqtt.setCallback(mqttCallback);
  }

  randomSeed(analogRead(0));
}

void loop()
{

    if(!modem.isGprsConnected())
    {
      digitalWrite(LED_PIN, HIGH); 
      modem_on();
      digitalWrite(LED_PIN, LOW); 
    }

    if (!mqtt.connected())
    {
      SerialMon.println(F("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"));    

      digitalWrite(LED_PIN, HIGH); 
      SerialMon.println(F("MQTT is not connected"));   

      SerialMon.println(F("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"));    

      uint32_t t = millis();
      if (t - lastReconnectAttempt > 5000L) 
      {
        lastReconnectAttempt = t;
        if (mqttConnect()) 
        { 
          lastReconnectAttempt = 0;
          digitalWrite(LED_PIN, LOW); 
        }
      }

      delay(100);
      return;
    }

  mqtt.loop();
}
emigbur commented 2 years ago

Im running more or less same config, my mqtt broker is configured over raspberry pi, docker mosquito I will log the disconnection to check. I do the check each minute, I mean this function "if (!mqtt.connected())"

kedzior-io commented 2 years ago

@emigbur , have you had a chance to check your logs? I'm wondering if this is related to the network provider or maybe there is a memory leak somewhere and it restarts the board.

jupiter8888 commented 2 years ago

// Connect to MQTT Broker boolean status = mqtt.connect("home");

By any chance do you have another device with the same MQTT Client name? It has to be unique.

kedzior-io commented 2 years ago

@jupiter8888 nope. That's the only device connected to my MQTT broker. I think it´s more of a modem issue.

superpance commented 1 month ago

Hi @kedzior-io Did you solve the issue ? I have similar problem with SIM7000E

Many thanks in advance. Kind regards, Angel

cho0p commented 4 weeks ago

I have a similar issue. Connection is not reliable. Sometimes it works for a few hours without a disconnect and sometimes it disconnects and reconnects every few minutes. Power supply and cellular signal are sufficient. I also ordered another antenna but same behaviour. I use the Tinygsm and pubsubclient libraries. In the debug log I can only see:

00:01:24:155 -> DATA ACCEPT:0,31 00:01:24:215 -> [726630] ### Unhandled: DATA ACCEPT:0,31 00:01:24:215 -> AT+CIPCLOSE=0

but I haven't been able to find the rootcause yet. I can see those disconnects with and without using the SSLClient library.

kedzior-io commented 3 weeks ago

@superpance & @cho0p I gave up on the device. Pivoted to Raspberry Pi Zero with USB LTE modem. Way faster to get it running and way easier to code and debug. 🤷