Skip to content

Commit

Permalink
Add debugging support
Browse files Browse the repository at this point in the history
  • Loading branch information
sebromero committed Apr 9, 2024
1 parent 557c7a8 commit cec9e96
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 21 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ This library provides a toolkit for interacting with the official Arduino 4G Mod
## Examples
* [examples/HTTPClient]() - Example of using this library together with [HttpClient]() to connect to a web server
* [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
* [examples/SMSReceive]() - Example for the SMS sending and receiving functionality
* [examples/ReceiveSMS]() - Example for the SMS sending and receiving functionality
* [examples/TimeAndLocation]() - Use GPS, or Cellular to acquire the location and time of the device.
* [examples/ModemTerminal]() - A handy example for debugging and Testing AT commands

Expand Down
13 changes: 7 additions & 6 deletions examples/ReceiveSMS/ReceiveSMS.ino
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
constexpr int NEW_SMS_INTERRUPT_PIN = A0;

ArduinoCellular cellular = ArduinoCellular();
volatile boolean newSMSavailable = false;
volatile boolean smsReceived = false;

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

void setup(){
Serial.begin(115200);
while (!Serial);
cellular.setDebugStream(Serial);

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

// Register interrupt based callback for new SMS
attachInterrupt(digitalPinToInterrupt(NEW_SMS_INTERRUPT_PIN), newSMSCallback, RISING);
attachInterrupt(digitalPinToInterrupt(NEW_SMS_INTERRUPT_PIN), onSMSReceived, RISING);

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

void loop(){
if(newSMSavailable){
newSMSavailable = false;
if(smsReceived){
smsReceived = false;
std::vector<SMS> unreadSMS = cellular.getUnreadSMS();
if (unreadSMS.size() > 0){
printMessages(unreadSMS);
Expand Down
56 changes: 43 additions & 13 deletions src/ArduinoCellular.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,23 @@ void ArduinoCellular::begin() {

bool ArduinoCellular::connect(String apn, String gprsUser, String gprsPass, String pin){
SimStatus simStatus = getSimStatus();
if(simStatus == SimStatus::SIM_LOCKED) {
if(simStatus == SimStatus::SIM_LOCKED && pin.length() > 0){
unlockSIM(pin.c_str());
}

simStatus = getSimStatus();
if(simStatus == SimStatus::SIM_READY) {
if(awaitNetworkRegistration()){
if(connectToGPRS(apn.c_str(), gprsUser.c_str(), gprsPass.c_str())){
Serial.println("Setting DNS...");
Serial.println(this->sendATCommand("+QIDNSCFG=1,\"8.8.8.8\",\"8.8.4.4\""));
if(this->debugStream != nullptr){
this->debugStream->println("Setting DNS...");
}

auto response = this->sendATCommand("+QIDNSCFG=1,\"8.8.8.8\",\"8.8.4.4\"");

if(this->debugStream != nullptr){
this->debugStream->println(response);
}
return true;
}
} else {
Expand Down Expand Up @@ -83,7 +90,9 @@ Location ArduinoCellular::getGPSLocation(unsigned long timeout){

return loc;
} else {
Serial.println("Unsupported modem model");
if(this->debugStream != nullptr){
this->debugStream->println("Unsupported modem model");
}
return Location();
}
}
Expand All @@ -108,7 +117,11 @@ void ArduinoCellular::sendSMS(String number, String message){
modem.stream->print(message); // Actually send the message
modem.stream->write(static_cast<char>(0x1A)); // Terminate the message
modem.stream->flush();
Serial.println(modem.waitResponse(10000L));
auto response = modem.waitResponse(10000L);

if(this->debugStream != nullptr){
this->debugStream->println("Response: " + String(response));
}
}


Expand Down Expand Up @@ -141,9 +154,13 @@ bool ArduinoCellular::isConnectedToOperator(){
}

bool ArduinoCellular::connectToGPRS(const char * apn, const char * gprsUser, const char * gprsPass){
Serial.print(F("Connecting to 4G network..."));
if(this->debugStream != nullptr){
this->debugStream->println("Connecting to 4G network...");
}
while(!modem.gprsConnect(apn, gprsUser, gprsPass)) {
Serial.print(".");
if(this->debugStream != nullptr){
this->debugStream->print(".");
}
delay(2000);
}
return true;
Expand All @@ -155,7 +172,9 @@ bool ArduinoCellular::isConnectedToInternet(){

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

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

bool ArduinoCellular::unlockSIM(const char * pin){
Serial.println("Unlocking SIM...");
if(this->debugStream != nullptr){
this->debugStream->println("Unlocking SIM...");
}
modem.simUnlock(pin);
}

bool ArduinoCellular::awaitNetworkRegistration(){
Serial.print("Waiting for network registration...");
if(this->debugStream != nullptr){
this->debugStream->println("Waiting for network registration...");
}
while (!modem.waitForNetwork()) {
Serial.println(".");
if(this->debugStream != nullptr){
this->debugStream->print(".");
}
delay(2000);
}
return true;
}

bool ArduinoCellular::enableGPS(bool assisted){
Serial.println("Enabling GPS...");
if(this->debugStream != nullptr){
this->debugStream->println("Enabling GPS...");
}

if(assisted){
sendATCommand("AT+QGPSCFG=\"agpsposmode\",33488767");
} else {
Expand Down Expand Up @@ -304,4 +332,6 @@ std::vector<SMS> ArduinoCellular::getUnreadSMS(){
}
}


void ArduinoCellular::setDebugStream(Stream &stream){
this->debugStream = &stream;
}
14 changes: 13 additions & 1 deletion src/ArduinoCellular.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class ArduinoCellular {
* @param pin The SIM card PIN.
* @return True if the connection is successful, false otherwise.
*/
bool connect(String apn, String gprsUser, String gprsPass, String pin);
bool connect(String apn, String gprsUser, String gprsPass, String pin = "");

/**
* @brief Checks if the modem is registered on the network.
Expand Down Expand Up @@ -214,6 +214,16 @@ class ArduinoCellular {
*/
int getSignalQuality();

/**
* @brief Sets the debug stream for ArduinoCellular.
*
* This function allows you to set the debug stream for ArduinoCellular.
* The debug stream is used to output debug messages and information.
*
* @param stream A pointer to the Stream object that will be used as the debug stream.
*/
void setDebugStream(Stream& stream);

private:
bool connectToGPRS(const char * apn, const char * gprsUser, const char * gprsPass);

Expand Down Expand Up @@ -249,6 +259,8 @@ class ArduinoCellular {

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

Stream* debugStream = nullptr; /**< The stream to be used for printing debugging messages. */

static unsigned long getTime(); /** Callback for getting the current time as an unix timestamp. */
};

Expand Down

0 comments on commit cec9e96

Please sign in to comment.