Skip to content

Commit cec9e96

Browse files
committed
Add debugging support
1 parent 557c7a8 commit cec9e96

File tree

4 files changed

+64
-21
lines changed

4 files changed

+64
-21
lines changed

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ This library provides a toolkit for interacting with the official Arduino 4G Mod
88
## Examples
99
* [examples/HTTPClient]() - Example of using this library together with [HttpClient]() to connect to a web server
1010
* [examples/HTTPClient]() - Example of using this library together with [HttpClient]() that uses [BearSSL]() under the hood to create a secure connection to a web server
11-
* [examples/SMSReceive]() - Example for the SMS sending and receiving functionality
11+
* [examples/ReceiveSMS]() - Example for the SMS sending and receiving functionality
1212
* [examples/TimeAndLocation]() - Use GPS, or Cellular to acquire the location and time of the device.
1313
* [examples/ModemTerminal]() - A handy example for debugging and Testing AT commands
1414

Diff for: examples/ReceiveSMS/ReceiveSMS.ino

+7-6
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
constexpr int NEW_SMS_INTERRUPT_PIN = A0;
88

99
ArduinoCellular cellular = ArduinoCellular();
10-
volatile boolean newSMSavailable = false;
10+
volatile boolean smsReceived = false;
1111

1212
void printMessages(std::vector<SMS> msg){
1313
for(int i = 0; i < msg.size(); i++){
@@ -17,21 +17,22 @@ void printMessages(std::vector<SMS> msg){
1717
Serial.print("\t * Timestamp: "); Serial.println(msg[i].timestamp.getISO8601());
1818
}
1919
}
20-
void newSMSCallback(){
20+
void onSMSReceived(){
2121
Serial.println("New SMS received!");
22-
newSMSavailable = true;
22+
smsReceived = true;
2323
}
2424

2525
void setup(){
2626
Serial.begin(115200);
2727
while (!Serial);
28+
cellular.setDebugStream(Serial);
2829

2930
cellular.begin();
3031
Serial.println("Connecting...");
3132
cellular.connect(SECRET_GPRS_APN, SECRET_GPRS_LOGIN, SECRET_GPRS_PASSWORD, SECRET_PINNUMBER);
3233

3334
// Register interrupt based callback for new SMS
34-
attachInterrupt(digitalPinToInterrupt(NEW_SMS_INTERRUPT_PIN), newSMSCallback, RISING);
35+
attachInterrupt(digitalPinToInterrupt(NEW_SMS_INTERRUPT_PIN), onSMSReceived, RISING);
3536

3637
Serial.println("Read SMS:");
3738
std::vector<SMS> readSMS = cellular.getReadSMS();
@@ -43,8 +44,8 @@ void setup(){
4344
}
4445

4546
void loop(){
46-
if(newSMSavailable){
47-
newSMSavailable = false;
47+
if(smsReceived){
48+
smsReceived = false;
4849
std::vector<SMS> unreadSMS = cellular.getUnreadSMS();
4950
if (unreadSMS.size() > 0){
5051
printMessages(unreadSMS);

Diff for: src/ArduinoCellular.cpp

+43-13
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,23 @@ void ArduinoCellular::begin() {
4545

4646
bool ArduinoCellular::connect(String apn, String gprsUser, String gprsPass, String pin){
4747
SimStatus simStatus = getSimStatus();
48-
if(simStatus == SimStatus::SIM_LOCKED) {
48+
if(simStatus == SimStatus::SIM_LOCKED && pin.length() > 0){
4949
unlockSIM(pin.c_str());
5050
}
5151

5252
simStatus = getSimStatus();
5353
if(simStatus == SimStatus::SIM_READY) {
5454
if(awaitNetworkRegistration()){
5555
if(connectToGPRS(apn.c_str(), gprsUser.c_str(), gprsPass.c_str())){
56-
Serial.println("Setting DNS...");
57-
Serial.println(this->sendATCommand("+QIDNSCFG=1,\"8.8.8.8\",\"8.8.4.4\""));
56+
if(this->debugStream != nullptr){
57+
this->debugStream->println("Setting DNS...");
58+
}
59+
60+
auto response = this->sendATCommand("+QIDNSCFG=1,\"8.8.8.8\",\"8.8.4.4\"");
61+
62+
if(this->debugStream != nullptr){
63+
this->debugStream->println(response);
64+
}
5865
return true;
5966
}
6067
} else {
@@ -83,7 +90,9 @@ Location ArduinoCellular::getGPSLocation(unsigned long timeout){
8390

8491
return loc;
8592
} else {
86-
Serial.println("Unsupported modem model");
93+
if(this->debugStream != nullptr){
94+
this->debugStream->println("Unsupported modem model");
95+
}
8796
return Location();
8897
}
8998
}
@@ -108,7 +117,11 @@ void ArduinoCellular::sendSMS(String number, String message){
108117
modem.stream->print(message); // Actually send the message
109118
modem.stream->write(static_cast<char>(0x1A)); // Terminate the message
110119
modem.stream->flush();
111-
Serial.println(modem.waitResponse(10000L));
120+
auto response = modem.waitResponse(10000L);
121+
122+
if(this->debugStream != nullptr){
123+
this->debugStream->println("Response: " + String(response));
124+
}
112125
}
113126

114127

@@ -141,9 +154,13 @@ bool ArduinoCellular::isConnectedToOperator(){
141154
}
142155

143156
bool ArduinoCellular::connectToGPRS(const char * apn, const char * gprsUser, const char * gprsPass){
144-
Serial.print(F("Connecting to 4G network..."));
157+
if(this->debugStream != nullptr){
158+
this->debugStream->println("Connecting to 4G network...");
159+
}
145160
while(!modem.gprsConnect(apn, gprsUser, gprsPass)) {
146-
Serial.print(".");
161+
if(this->debugStream != nullptr){
162+
this->debugStream->print(".");
163+
}
147164
delay(2000);
148165
}
149166
return true;
@@ -155,7 +172,9 @@ bool ArduinoCellular::isConnectedToInternet(){
155172

156173
SimStatus ArduinoCellular::getSimStatus(){
157174
int simStatus = modem.getSimStatus();
158-
Serial.println("SIM Status: " + String(simStatus));
175+
if(this->debugStream != nullptr){
176+
this->debugStream->println("SIM Status: " + String(simStatus));
177+
}
159178

160179
if (modem.getSimStatus() == 0) {
161180
return SimStatus::SIM_ERROR;
@@ -171,21 +190,30 @@ SimStatus ArduinoCellular::getSimStatus(){
171190
}
172191

173192
bool ArduinoCellular::unlockSIM(const char * pin){
174-
Serial.println("Unlocking SIM...");
193+
if(this->debugStream != nullptr){
194+
this->debugStream->println("Unlocking SIM...");
195+
}
175196
modem.simUnlock(pin);
176197
}
177198

178199
bool ArduinoCellular::awaitNetworkRegistration(){
179-
Serial.print("Waiting for network registration...");
200+
if(this->debugStream != nullptr){
201+
this->debugStream->println("Waiting for network registration...");
202+
}
180203
while (!modem.waitForNetwork()) {
181-
Serial.println(".");
204+
if(this->debugStream != nullptr){
205+
this->debugStream->print(".");
206+
}
182207
delay(2000);
183208
}
184209
return true;
185210
}
186211

187212
bool ArduinoCellular::enableGPS(bool assisted){
188-
Serial.println("Enabling GPS...");
213+
if(this->debugStream != nullptr){
214+
this->debugStream->println("Enabling GPS...");
215+
}
216+
189217
if(assisted){
190218
sendATCommand("AT+QGPSCFG=\"agpsposmode\",33488767");
191219
} else {
@@ -304,4 +332,6 @@ std::vector<SMS> ArduinoCellular::getUnreadSMS(){
304332
}
305333
}
306334

307-
335+
void ArduinoCellular::setDebugStream(Stream &stream){
336+
this->debugStream = &stream;
337+
}

Diff for: src/ArduinoCellular.h

+13-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ class ArduinoCellular {
9999
* @param pin The SIM card PIN.
100100
* @return True if the connection is successful, false otherwise.
101101
*/
102-
bool connect(String apn, String gprsUser, String gprsPass, String pin);
102+
bool connect(String apn, String gprsUser, String gprsPass, String pin = "");
103103

104104
/**
105105
* @brief Checks if the modem is registered on the network.
@@ -214,6 +214,16 @@ class ArduinoCellular {
214214
*/
215215
int getSignalQuality();
216216

217+
/**
218+
* @brief Sets the debug stream for ArduinoCellular.
219+
*
220+
* This function allows you to set the debug stream for ArduinoCellular.
221+
* The debug stream is used to output debug messages and information.
222+
*
223+
* @param stream A pointer to the Stream object that will be used as the debug stream.
224+
*/
225+
void setDebugStream(Stream& stream);
226+
217227
private:
218228
bool connectToGPRS(const char * apn, const char * gprsUser, const char * gprsPass);
219229

@@ -249,6 +259,8 @@ class ArduinoCellular {
249259

250260
ModemModel model; /**< The modem model. */
251261

262+
Stream* debugStream = nullptr; /**< The stream to be used for printing debugging messages. */
263+
252264
static unsigned long getTime(); /** Callback for getting the current time as an unix timestamp. */
253265
};
254266

0 commit comments

Comments
 (0)