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 a04707e..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. @@ -259,20 +265,22 @@ 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) + * @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)