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

Automatic reconnection #481

Open Jens1985 opened 3 years ago

Jens1985 commented 3 years ago

Hi there, after a loss of signal, the modem does not automatically reconnect to the network.

I have an Arduino MKR NB 1500 with a UBLOX SARA R410M modem. For testing I use the (TinyGSM) MQTTclient example.

Does anyone have a solution for this? That must be a common problem. Especially with portable devices, the signal breaks down regularly.

Here ist the debug output:

=== MQTT NOT CONNECTED === AT+USORD=0,0 AT+USOCTL=0,10 === MQTT NOT CONNECTED === AT+USORD=0,0 AT+USOCTL=0,10 === MQTT NOT CONNECTED === Connecting to XXX.XXX.XXX.133AT+USORD=0,0 AT+USOCTL=0,10 AT+USORD=0,0 AT+USOCTL=0,10 AT+USOCR=6 fail AT+USORD=0,0 AT+USOCTL=0,10 === MQTT NOT CONNECTED === AT+USORD=0,0 AT+USOCTL=0,10 === MQTT NOT CONNECTED === AT+USORD=0,0 AT+USOCTL=0,10

After the change suggested here(), there is this debug output:

=== MQTT NOT CONNECTED === Connecting to 157.90.26.133AT+USORD=0,0 AT+USOCTL=0,10 AT+USORD=0,0 AT+USOCTL=0,10 AT+USOCR=6 fail AT+CGATT=1

Here is the Source: TinyGSM_MQTT_Github.zip

adrianca88 commented 3 years ago

You must check your GPRS connection first (and reconnect it if isGprsConnected() == false) and, after that, reconnect your MQTT client. For example:

void loop() {
  if (!modem.isGprsConnected() {
    modem.gprsConnect(apn, gprsUser, gprsPass);  // TODO process return val
  }
  if (!mqtt.connected()) {
    mqttConnect();  // TODO process return val
  }
}