vshymanskyy / TinyGSM

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

esp32 + sim800c: always reconnects #229

Closed spa-sam closed 5 years ago

spa-sam commented 5 years ago

How to fix permanent reconnections?

#define` TINY_GSM_MODEM_SIM800
#include <TinyGsmClient.h>
#include <PubSubClient.h>

#define SerialMon Serial
HardwareSerial SerialAT(1);

const char apn[]  = "internet";
const char user[] = "";
const char pass[] = "";

// MQTT details
const char* broker = "m15.cloudmqtt.com";

const char* topicLed = "GsmClientTest/led";
const char* topicInit = "GsmClientTest/init";
const char* topicLedStatus = "GsmClientTest/ledStatus";

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

#define LED_PIN 13
int ledStatus = LOW;

long lastReconnectAttempt = 0;

void setup() {
  SerialMon.begin(115200);
  delay(10);

 SerialAT.begin(115200, SERIAL_8N1, 16, 17, false);
  delay(3000);

  SerialMon.println("Initializing modem...");
  modem.restart();

  String modemInfo = modem.getModemInfo();
  SerialMon.print("Modem: ");
  SerialMon.println(modemInfo);

  SerialMon.print("Waiting for network...");
  if (!modem.waitForNetwork()) {
    SerialMon.println(" fail");
    while (true);
  }
  SerialMon.println(" OK");

  SerialMon.print("Connecting to ");
  SerialMon.print(apn);
  if (!modem.gprsConnect(apn, user, pass)) {
    SerialMon.println(" fail");
    while (true);
  }
  SerialMon.println(" OK");

  // MQTT Broker setup
  mqtt.setServer(broker, 14115);
  mqtt.setCallback(mqttCallback);
}

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

  boolean status = mqtt.connect("GsmClientName", "user", "psw");

  if (status == false) {
    SerialMon.println(" fail");
    return false;
  }
  SerialMon.println(" OK");
  mqtt.publish(topicInit, "GsmClientTest started");
  mqtt.subscribe(topicLed);
  return mqtt.connected();
}

void loop() {

  if (!mqtt.connected()) {
    SerialMon.println("=== MQTT NOT CONNECTED ===");
    // Reconnect every 10 seconds
    unsigned long t = millis();
    if (t - lastReconnectAttempt > 10000L) {
      lastReconnectAttempt = t;
      if (mqttConnect()) {
        lastReconnectAttempt = 0;
      }
    }
    delay(100);
    return;
  }

  mqtt.loop();
}

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

  // Only proceed if incoming message's topic matches
  if (String(topic) == topicLed) {
    ledStatus = !ledStatus;
    mqtt.publish(topicLedStatus, ledStatus ? "1" : "0");
  }
}

Initializing modem... Modem: SIM800 R14.18 Waiting for network... OK Connecting to internet OK === MQTT NOT CONNECTED === Connecting to m15.cloudmqtt.com OK [29749] ### Unhandled: +CIPSTATUS: 1,0,"TCP","54.227.205.125","14115","REMOTE CLOSING"

OK === MQTT NOT CONNECTED === Connecting to m15.cloudmqtt.com OK [34377] ### Unhandled: +CIPSTATUS: 1,0,"TCP","54.227.205.125","14115","REMOTE CLOSING"

OK === MQTT NOT CONNECTED === Connecting to m15.cloudmqtt.com OK [39128] ### Unhandled: +CIPSTATUS: 1,0,"TCP","54.227.205.125","14115","REMOTE CLOSING"

etc...

spa-sam commented 5 years ago

Tests with the operator showed a problem on the side of vodafone.ua, changing the SIM card to lifecell.ua, the problem with the disconnection disappeared. I will experiment with the operators and write down the result here.

spa-sam commented 5 years ago

The problem turned out to be in a certain tariff plan vodafone, changing it to another tariff of the same operator, the problem disappeared. The topic can be closed.