Skip to content

Commit 4606dbf

Browse files
committed
avoid connect to loop forever if connection to APN is not established
1 parent c20e9b2 commit 4606dbf

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

Diff for: src/ArduinoCellular.cpp

+13-3
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,12 @@ void ArduinoCellular::begin() {
4444

4545
}
4646

47-
bool ArduinoCellular::connect(String apn, String username, String password){
47+
bool ArduinoCellular::connect(String apn, bool waitForever) {
48+
connect(apn,String(""),String(""), waitForever);
49+
}
50+
51+
52+
bool ArduinoCellular::connect(String apn, String username, String password, bool waitForever){
4853
SimStatus simStatus = getSimStatus();
4954

5055
if(simStatus == SimStatus::SIM_LOCKED){
@@ -62,7 +67,7 @@ bool ArduinoCellular::connect(String apn, String username, String password){
6267
return false;
6368
}
6469

65-
if(!awaitNetworkRegistration()){
70+
if(!awaitNetworkRegistration(waitForever)){
6671
return false;
6772
}
6873

@@ -225,11 +230,16 @@ bool ArduinoCellular::unlockSIM(String pin){
225230
return modem.simUnlock(pin.c_str());
226231
}
227232

228-
bool ArduinoCellular::awaitNetworkRegistration(){
233+
bool ArduinoCellular::awaitNetworkRegistration(bool waitForever){
229234
if(this->debugStream != nullptr){
230235
this->debugStream->println("Waiting for network registration...");
231236
}
232237
while (!modem.waitForNetwork(waitForNetworkTimeout)) {
238+
239+
if(!waitForever) {
240+
return false;
241+
}
242+
233243
if(this->debugStream != nullptr){
234244
this->debugStream->print(".");
235245
}

Diff for: src/ArduinoCellular.h

+9-2
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,15 @@ class ArduinoCellular {
119119
* @param apn The Access Point Name.
120120
* @param username The APN username.
121121
* @param password The APN password.
122+
* @param waitForever The function does not return unless a connection has been established
122123
* @return True if the connection is successful, false otherwise.
123124
*/
124-
bool connect(String apn = "", String username = "", String password = "");
125+
bool connect(String apn = "", String username = "", String password = "", bool waitForever = true);
126+
127+
/**
128+
* @brief same as previous, username and password are empty
129+
*/
130+
bool connect(String apn, bool waitForever);
125131

126132
/**
127133
* @brief Checks if the modem is registered on the network.
@@ -271,9 +277,10 @@ class ArduinoCellular {
271277

272278
/**
273279
* @brief Waits for network registration. (Blocking call)
280+
* @param waitForever if true the function does not return until a connection has been established
274281
* @return True if the network registration is successful, false otherwise.
275282
*/
276-
bool awaitNetworkRegistration();
283+
bool awaitNetworkRegistration(bool waitForever);
277284

278285
/**
279286
* @brief Gets the GPS location. (Blocking call)

0 commit comments

Comments
 (0)