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

MKR1400 can't reconnect after client disconnects #485

Open tuloski opened 3 years ago

tuloski commented 3 years ago

Board: MKR1400 (I followed the special setup https://github.com/vshymanskyy/TinyGSM/wiki/Board-configuration) Modem: SARA-U201

Trying to send http request (GET, POST) with ArduinoHttpClient passing TinyGsmClientSecure as client. The first requests go through, but when the client disconnects it's not able to reconnect again. I don't know if it's modem fault or client fault Below the AT dump AFTER the disconnection with some extra to understand what it was doing:

making GET request AT+USORD=3,0 +USORD: 3,0 OK AT+USOCTL=3,10 +USOCTL: 3,10,7 OK AT+USOCR=6 +USOCR: 4 OK AT+USOSEC=4,1 OK AT+USOCO=4,"httpbin.org",443 ERROR GET error: -1 Made GET request +UUSOCL: 4 AT+USORD=4,0 ERROR AT+USOCTL=4,10 ERROR AT+USORD=4,0 ERROR AT+USOCTL=4,10 ERROR AT+USORD=4,0 ERROR AT+USOCTL=4,10 ERROR AT+USORD=4,0 ERROR AT+USOCTL=4,10 ERROR AT+USORD=4,0 ERROR AT+USOCTL=4,10 ERROR AT+USORD=4,0 ERROR AT+USOCTL=4,10 ERROR AT+USORD=4,0 ERROR AT+USOCTL=4,10 ERROR AT+USORD=4,0 ERROR AT+USOCTL=4,10 ERROR Check connections Client gsm not connected AT+CGATT? +CGATT: 1 OK AT+UPSND=0,0 +UPSND: 0,0,"10.175.113.24" OK AT+CGREG? +CGREG: 0,1 OK CHECKED connections AT+USORD=4,0 ERROR AT+USOCTL=4,10 ERROR AT+USORD=4,0 ERROR AT+USOCTL=4,10 ERROR AT+USORD=4,0 ERROR AT+USOCTL=4,10 ERROR AT+USORD=4,0 ERROR AT+USOCTL=4,10

SRGDamia1 commented 3 years ago

Huh. It looks like the new socket is being closed as soon as it's opened. (The +UUSOCL: 4 is the modem telling you the socket has been closed.) Once the socket is closed, you can't do anything with that socket; you have to intentionally reopen a new one. You're getting all those ERROR messages because you're trying to ask the state of a closed socket. Try adjusting your code to check the socket state and re-open a new socket if needed before sending or checking for data.

If the socket is always being closed immediately after being opened, you can try enabling "keepAlive" in your local copy of the library to see if that helps. There's commented-out code for it on line 631 of TinyGsmClientUBLOX.h.

tuloski commented 3 years ago

But in the periodical check modem.isGprsConnected() and modem.isNetworkConnected() both return true. Only client.connected() returns false, so it seems related to the client, not to socket or modem.

SRGDamia1 commented 3 years ago

@tuloski - Sorry, confusing terminology. In this library client = socket. If client.connected() is false and you try to send data (ie, client.print()) the modem will explode with errors because there's no where for the data to go.

To send data to a specific website/server/anything all three of the above functions must return true. If any of them return false, you have to step back and fix the lowest level of what's broken and then re-open every higher level.

tuloski commented 3 years ago

@SRGDamia1 I see, but since I pass the client to httpClient, I have no power to reconnect (I can manually do it but it's wrong). IMO it should be done by the client itself or the httpClient. Actually I think it is trying to reconnect but it's failing somewhere, but I'm no good at interpreting AT messages.

SRGDamia1 commented 3 years ago

In the AT commands you posted, there's only one attempt to connect at the very beginning.

It can be very convention, but I'm not a fan of the Arduino httpClient library. I would recommend you write out the HTTP yourself.