From 6e10a3a009d121b0b5ad2f04b9ecbea379d10170 Mon Sep 17 00:00:00 2001 From: Sara Damiano Date: Mon, 17 May 2021 12:23:29 -0400 Subject: [PATCH] Fix crash on SIM7000SSL Signed-off-by: Sara Damiano --- examples/AllFunctions/AllFunctions.ino | 8 ++--- examples/MqttClient/MqttClient.ino | 47 +++++++++++++++++++------- src/TinyGsmClientSIM7000SSL.h | 6 ++++ src/TinyGsmClientSIM7080.h | 2 ++ 4 files changed, 47 insertions(+), 16 deletions(-) diff --git a/examples/AllFunctions/AllFunctions.ino b/examples/AllFunctions/AllFunctions.ino index 96e0e55..b3e6ff6 100644 --- a/examples/AllFunctions/AllFunctions.ino +++ b/examples/AllFunctions/AllFunctions.ino @@ -52,16 +52,16 @@ SoftwareSerial SerialAT(2, 3); // RX, TX // Define the serial console for debug prints, if needed #define TINY_GSM_DEBUG SerialMon -// Add a reception delay, if needed. -// This may be needed for a fast processor at a slow baud rate. -// #define TINY_GSM_YIELD() { delay(2); } - // Range to attempt to autobaud // NOTE: DO NOT AUTOBAUD in production code. Once you've established // communication, set a fixed baud rate using modem.setBaud(#). #define GSM_AUTOBAUD_MIN 9600 #define GSM_AUTOBAUD_MAX 57600 +// Add a reception delay, if needed. +// This may be needed for a fast processor at a slow baud rate. +// #define TINY_GSM_YIELD() { delay(2); } + /* * Tests enabled */ diff --git a/examples/MqttClient/MqttClient.ino b/examples/MqttClient/MqttClient.ino index be02f9e..77a8500 100644 --- a/examples/MqttClient/MqttClient.ino +++ b/examples/MqttClient/MqttClient.ino @@ -10,20 +10,15 @@ * For more MQTT examples, see PubSubClient library * ************************************************************** - * Use Mosquitto client tools to work with MQTT - * Ubuntu/Linux: sudo apt-get install mosquitto-clients - * Windows: https://mosquitto.org/download/ + * This example connects to HiveMQ's showcase broker. * - * Subscribe for messages: - * mosquitto_sub -h test.mosquitto.org -t GsmClientTest/init -t GsmClientTest/ledStatus -q 1 - * Toggle led: - * mosquitto_pub -h test.mosquitto.org -t GsmClientTest/led -q 1 -m "toggle" + * You can quickly test sending and receiving messages from the HiveMQ webclient + * available at http://www.hivemq.com/demos/websocket-client/. * - * You can use Node-RED for wiring together MQTT-enabled devices - * https://nodered.org/ - * Also, take a look at these additional Node-RED modules: - * node-red-contrib-blynk-ws - * node-red-dashboard + * Subscribe to the topic GsmClientTest/ledStatus + * Publish "toggle" to the topic GsmClientTest/led and the LED on your board + * should toggle and you should see a new message published to + * GsmClientTest/ledStatus with the newest LED status. * **************************************************************/ @@ -251,6 +246,34 @@ void setup() { } void loop() { + // Make sure we're still registered on the network + if (!modem.isNetworkConnected()) { + SerialMon.println("Network disconnected"); + if (!modem.waitForNetwork(180000L, true)) { + SerialMon.println(" fail"); + delay(10000); + return; + } + if (modem.isNetworkConnected()) { + SerialMon.println("Network re-connected"); + } + +#if TINY_GSM_USE_GPRS + // and make sure GPRS/EPS is still connected + if (!modem.isGprsConnected()) { + SerialMon.println("GPRS disconnected!"); + SerialMon.print(F("Connecting to ")); + SerialMon.print(apn); + if (!modem.gprsConnect(apn, gprsUser, gprsPass)) { + SerialMon.println(" fail"); + delay(10000); + return; + } + if (modem.isGprsConnected()) { SerialMon.println("GPRS reconnected"); } + } +#endif + } + if (!mqtt.connected()) { SerialMon.println("=== MQTT NOT CONNECTED ==="); // Reconnect every 10 seconds diff --git a/src/TinyGsmClientSIM7000SSL.h b/src/TinyGsmClientSIM7000SSL.h index 357a98e..2f88e89 100644 --- a/src/TinyGsmClientSIM7000SSL.h +++ b/src/TinyGsmClientSIM7000SSL.h @@ -485,6 +485,12 @@ class TinyGsmSim7000SSL } size_t modemGetAvailable(uint8_t mux) { + // If the socket doesn't exist, just return + if (!sockets[mux]) { return 0; } + // We need to check if there are any connections open *before* checking for + // available characters. The SIM7000 *will crash* if you ask about data + // when there are no open connections. + if (!modemGetConnected(mux)) { return 0; } // NOTE: This gets how many characters are available on all connections that // have data. It does not return all the connections, just those with data. sendAT(GF("+CARECV?")); diff --git a/src/TinyGsmClientSIM7080.h b/src/TinyGsmClientSIM7080.h index b54db22..8d7aae0 100644 --- a/src/TinyGsmClientSIM7080.h +++ b/src/TinyGsmClientSIM7080.h @@ -488,6 +488,8 @@ class TinyGsmSim7080 : public TinyGsmSim70xx, } size_t modemGetAvailable(uint8_t mux) { + // If the socket doesn't exist, just return + if (!sockets[mux]) { return 0; } // NOTE: This gets how many characters are available on all connections that // have data. It does not return all the connections, just those with data. sendAT(GF("+CARECV?"));