From d7c7e2b7d2b735c67a0502bfc50201910c3d8b96 Mon Sep 17 00:00:00 2001 From: Sebastian Romero Date: Tue, 9 Apr 2024 16:23:38 +0200 Subject: [PATCH 1/2] Clean up receive SMS example --- .../ReceiveSMS.ino} | 28 +++++++++---------- .../arduino_secrets.h | 0 2 files changed, 13 insertions(+), 15 deletions(-) rename examples/{SMSReceive/SMSReceive.ino => ReceiveSMS/ReceiveSMS.ino} (70%) rename examples/{SMSReceive => ReceiveSMS}/arduino_secrets.h (100%) diff --git a/examples/SMSReceive/SMSReceive.ino b/examples/ReceiveSMS/ReceiveSMS.ino similarity index 70% rename from examples/SMSReceive/SMSReceive.ino rename to examples/ReceiveSMS/ReceiveSMS.ino index fc72329..4d1720b 100644 --- a/examples/SMSReceive/SMSReceive.ino +++ b/examples/ReceiveSMS/ReceiveSMS.ino @@ -1,15 +1,13 @@ #include "ArduinoCellular.h" #include "arduino_secrets.h" +// #define TINY_GSM_DEBUG Serial +// #define ARDUINO_CELLULAR_DEBUG - -#define TINY_GSM_DEBUG Serial -#define ARDUINO_CELLULAR_DEBUG +constexpr int NEW_SMS_INTERRUPT_PIN = A0; ArduinoCellular cellular = ArduinoCellular(); - -boolean newSMS = false; - +volatile boolean newSMSavailable = false; void printMessages(std::vector msg){ for(int i = 0; i < msg.size(); i++){ @@ -20,18 +18,20 @@ void printMessages(std::vector msg){ } } void newSMSCallback(){ - Serial.println("new sms received"); - newSMS = true; + Serial.println("New SMS received!"); + newSMSavailable = true; } void setup(){ Serial.begin(115200); while (!Serial); + cellular.begin(); + Serial.println("Connecting..."); cellular.connect(SECRET_GPRS_APN, SECRET_GPRS_LOGIN, SECRET_GPRS_PASSWORD, SECRET_PINNUMBER); - - attachInterrupt(digitalPinToInterrupt(A0), newSMSCallback, RISING); + // Register interrupt based callback for new SMS + attachInterrupt(digitalPinToInterrupt(NEW_SMS_INTERRUPT_PIN), newSMSCallback, RISING); Serial.println("Read SMS:"); std::vector readSMS = cellular.getReadSMS(); @@ -40,16 +40,14 @@ void setup(){ Serial.println("Unread SMS:"); std::vector unreadSMS = cellular.getUnreadSMS(); printMessages(unreadSMS); - - cellular.sendSMS("+40788494946", "bleep bleep"); } void loop(){ - if(newSMS){ - newSMS = false; + if(newSMSavailable){ + newSMSavailable = false; std::vector unreadSMS = cellular.getUnreadSMS(); if (unreadSMS.size() > 0){ printMessages(unreadSMS); } } -} \ No newline at end of file +} diff --git a/examples/SMSReceive/arduino_secrets.h b/examples/ReceiveSMS/arduino_secrets.h similarity index 100% rename from examples/SMSReceive/arduino_secrets.h rename to examples/ReceiveSMS/arduino_secrets.h From 78092839d02769dbb7aa3d25f998a3cedd2c58cf Mon Sep 17 00:00:00 2001 From: Sebastian Romero Date: Tue, 9 Apr 2024 16:27:06 +0200 Subject: [PATCH 2/2] Clean up HTTP examples --- examples/HTTPClient/HTTPClient.ino | 15 ++++++--------- examples/HTTPSClient/HTTPSClient.ino | 19 ++++++++----------- 2 files changed, 14 insertions(+), 20 deletions(-) diff --git a/examples/HTTPClient/HTTPClient.ino b/examples/HTTPClient/HTTPClient.ino index bc8cb31..5525676 100644 --- a/examples/HTTPClient/HTTPClient.ino +++ b/examples/HTTPClient/HTTPClient.ino @@ -3,14 +3,12 @@ #include "ArduinoCellular.h" #include "arduino_secrets.h" - const char server[] = "vsh.pp.ua"; const char resource[] = "/TinyGSM/logo.txt"; const int port = 80; ArduinoCellular cellular = ArduinoCellular(); -HttpClient http = cellular.getHTTPClient(server, port); - +HttpClient client = cellular.getHTTPClient(server, port); void setup(){ Serial.begin(115200); @@ -23,19 +21,18 @@ void loop(){ Serial.println("Making GET request..."); - http.get(resource); + client.get(resource); - int status_code = http.responseStatusCode(); - String response = http.responseBody(); + int statusCode = client.responseStatusCode(); + String response = client.responseBody(); Serial.print("Status code: "); - Serial.println(status_code); + Serial.println(statusCode); Serial.print("Response: "); Serial.println(response); - http.stop(); + client.stop(); delay(5000); } - diff --git a/examples/HTTPSClient/HTTPSClient.ino b/examples/HTTPSClient/HTTPSClient.ino index 3ac5e72..f4b45d7 100644 --- a/examples/HTTPSClient/HTTPSClient.ino +++ b/examples/HTTPSClient/HTTPSClient.ino @@ -4,13 +4,12 @@ #include #include "arduino_secrets.h" - const char server[] = "example.com"; const char resource[] = "/"; const int port = 443; ArduinoCellular cellular = ArduinoCellular(); -HttpClient http = cellular.getHTTPSClient(server, port); +HttpClient client = cellular.getHTTPSClient(server, port); void setup(){ Serial.begin(115200); @@ -20,21 +19,19 @@ void setup(){ cellular.connect(SECRET_GPRS_APN, SECRET_GPRS_LOGIN, SECRET_GPRS_PASSWORD, SECRET_PINNUMBER); } -void loop() -{ +void loop(){ Serial.println("Making GET request..."); - http.get(resource); + client.get(resource); - int status_code = http.responseStatusCode(); - String response = http.responseBody(); + int statusCode = client.responseStatusCode(); + String response = client.responseBody(); Serial.print("Status code: "); - Serial.println(status_code); + Serial.println(statusCode); Serial.print("Response: "); Serial.println(response); - http.stop(); - + client.stop(); delay(5000); -} \ No newline at end of file +}