Skip to content

Commit a97dec0

Browse files
Merge pull request #25 from maidnl/opta_cellular_dev
Avoid connect to APN to loop forever in case of missing connection.
2 parents d46664c + 2f549ab commit a97dec0

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

src/ArduinoCellular.cpp

Lines changed: 13 additions & 3 deletions
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

@@ -233,11 +238,16 @@ bool ArduinoCellular::unlockSIM(String pin){
233238
return true;
234239
}
235240

236-
bool ArduinoCellular::awaitNetworkRegistration(){
241+
bool ArduinoCellular::awaitNetworkRegistration(bool waitForever){
237242
if(this->debugStream != nullptr){
238243
this->debugStream->println("Waiting for network registration...");
239244
}
240245
while (!modem.waitForNetwork(waitForNetworkTimeout)) {
246+
247+
if(!waitForever) {
248+
return false;
249+
}
250+
241251
if(this->debugStream != nullptr){
242252
this->debugStream->print(".");
243253
}

src/ArduinoCellular.h

Lines changed: 14 additions & 6 deletions
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 = true);
125131

126132
/**
127133
* @brief Checks if the modem is registered on the network.
@@ -259,20 +265,22 @@ class ArduinoCellular {
259265
*/
260266
void setDebugStream(Stream& stream);
261267

262-
private:
263-
bool connectToGPRS(const char * apn, const char * gprsUser, const char * gprsPass);
264-
265-
/**
268+
/**
266269
* @brief Gets the SIM card status.
267270
* @return The SIM card status.
268271
*/
269272
SimStatus getSimStatus();
270273

274+
private:
275+
bool connectToGPRS(const char * apn, const char * gprsUser, const char * gprsPass);
276+
277+
271278
/**
272279
* @brief Waits for network registration. (Blocking call)
280+
* @param waitForever if true the function does not return until a connection has been established
273281
* @return True if the network registration is successful, false otherwise.
274282
*/
275-
bool awaitNetworkRegistration();
283+
bool awaitNetworkRegistration(bool waitForever);
276284

277285
/**
278286
* @brief Gets the GPS location. (Blocking call)

0 commit comments

Comments
 (0)