From c20e9b2dcd0d7f95f86f97aa91147651bebe5e7f Mon Sep 17 00:00:00 2001 From: maidnl Date: Thu, 23 Jan 2025 10:26:10 +0100 Subject: [PATCH 1/2] make getSimStatus public --- src/ArduinoCellular.h | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/ArduinoCellular.h b/src/ArduinoCellular.h index a04707e..642aad2 100644 --- a/src/ArduinoCellular.h +++ b/src/ArduinoCellular.h @@ -259,15 +259,16 @@ class ArduinoCellular { */ void setDebugStream(Stream& stream); - private: - bool connectToGPRS(const char * apn, const char * gprsUser, const char * gprsPass); - - /** + /** * @brief Gets the SIM card status. * @return The SIM card status. */ SimStatus getSimStatus(); + private: + bool connectToGPRS(const char * apn, const char * gprsUser, const char * gprsPass); + + /** * @brief Waits for network registration. (Blocking call) * @return True if the network registration is successful, false otherwise. From 4606dbfb862e5681e63273313af04b6a2dbd540a Mon Sep 17 00:00:00 2001 From: maidnl Date: Thu, 23 Jan 2025 10:27:17 +0100 Subject: [PATCH 2/2] avoid connect to loop forever if connection to APN is not established --- src/ArduinoCellular.cpp | 16 +++++++++++++--- src/ArduinoCellular.h | 11 +++++++++-- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/ArduinoCellular.cpp b/src/ArduinoCellular.cpp index 1c9c3b6..6326abc 100644 --- a/src/ArduinoCellular.cpp +++ b/src/ArduinoCellular.cpp @@ -44,7 +44,12 @@ void ArduinoCellular::begin() { } -bool ArduinoCellular::connect(String apn, String username, String password){ +bool ArduinoCellular::connect(String apn, bool waitForever) { + connect(apn,String(""),String(""), waitForever); +} + + +bool ArduinoCellular::connect(String apn, String username, String password, bool waitForever){ SimStatus simStatus = getSimStatus(); if(simStatus == SimStatus::SIM_LOCKED){ @@ -62,7 +67,7 @@ bool ArduinoCellular::connect(String apn, String username, String password){ return false; } - if(!awaitNetworkRegistration()){ + if(!awaitNetworkRegistration(waitForever)){ return false; } @@ -225,11 +230,16 @@ bool ArduinoCellular::unlockSIM(String pin){ return modem.simUnlock(pin.c_str()); } -bool ArduinoCellular::awaitNetworkRegistration(){ +bool ArduinoCellular::awaitNetworkRegistration(bool waitForever){ if(this->debugStream != nullptr){ this->debugStream->println("Waiting for network registration..."); } while (!modem.waitForNetwork(waitForNetworkTimeout)) { + + if(!waitForever) { + return false; + } + if(this->debugStream != nullptr){ this->debugStream->print("."); } diff --git a/src/ArduinoCellular.h b/src/ArduinoCellular.h index 642aad2..1978cd3 100644 --- a/src/ArduinoCellular.h +++ b/src/ArduinoCellular.h @@ -119,9 +119,15 @@ class ArduinoCellular { * @param apn The Access Point Name. * @param username The APN username. * @param password The APN password. + * @param waitForever The function does not return unless a connection has been established * @return True if the connection is successful, false otherwise. */ - bool connect(String apn = "", String username = "", String password = ""); + bool connect(String apn = "", String username = "", String password = "", bool waitForever = true); + + /** + * @brief same as previous, username and password are empty + */ + bool connect(String apn, bool waitForever); /** * @brief Checks if the modem is registered on the network. @@ -271,9 +277,10 @@ class ArduinoCellular { /** * @brief Waits for network registration. (Blocking call) + * @param waitForever if true the function does not return until a connection has been established * @return True if the network registration is successful, false otherwise. */ - bool awaitNetworkRegistration(); + bool awaitNetworkRegistration(bool waitForever); /** * @brief Gets the GPS location. (Blocking call)