Skip to content

Avoid connect to APN to loop forever in case of missing connection #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/ArduinoCellular.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand All @@ -62,7 +67,7 @@ bool ArduinoCellular::connect(String apn, String username, String password){
return false;
}

if(!awaitNetworkRegistration()){
if(!awaitNetworkRegistration(waitForever)){
return false;
}

Expand Down Expand Up @@ -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(".");
}
Expand Down
20 changes: 14 additions & 6 deletions src/ArduinoCellular.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
bool connect(String apn, bool waitForever);
bool connect(String apn, bool waitForever = true);

I'd assign the same default value to the version without credentials.


/**
* @brief Checks if the modem is registered on the network.
Expand Down Expand Up @@ -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)
Expand Down
Loading