|
1 | 1 | #include "ArduinoCellular.h"
|
2 | 2 | #include "arduino_secrets.h"
|
3 | 3 |
|
4 |
| -// #define TINY_GSM_DEBUG Serial |
5 |
| -// #define ARDUINO_CELLULAR_DEBUG |
6 | 4 |
|
7 | 5 | constexpr int NEW_SMS_INTERRUPT_PIN = A0;
|
8 | 6 |
|
9 | 7 | ArduinoCellular cellular = ArduinoCellular();
|
10 | 8 | volatile boolean smsReceived = false;
|
| 9 | +constexpr int POLLING_INTERVAL_MS = 1 * 60 * 1000; // 1 minute |
11 | 10 |
|
12 | 11 | void printMessages(std::vector<SMS> msg){
|
13 | 12 | for(int i = 0; i < msg.size(); i++){
|
14 | 13 | Serial.println("SMS:");
|
15 |
| - Serial.print("\t * From: "); Serial.println(msg[i].number); |
16 |
| - Serial.print("\t * Message: "); Serial.println(msg[i].message); |
17 |
| - Serial.print("\t * Timestamp: "); Serial.println(msg[i].timestamp.getISO8601()); |
| 14 | + Serial.print("* Index: "); Serial.println(msg[i].index); |
| 15 | + Serial.print("* From: "); Serial.println(msg[i].sender); |
| 16 | + Serial.print("* Timestamp: "); Serial.println(msg[i].timestamp.getISO8601()); |
| 17 | + Serial.println("* Message: "); Serial.println(msg[i].message); |
| 18 | + Serial.println("--------------------\n"); |
18 | 19 | }
|
19 | 20 | }
|
20 | 21 | void onSMSReceived(){
|
21 |
| - Serial.println("New SMS received!"); |
22 | 22 | smsReceived = true;
|
23 | 23 | }
|
24 | 24 |
|
25 | 25 | void setup(){
|
26 | 26 | Serial.begin(115200);
|
27 | 27 | while (!Serial);
|
28 | 28 | cellular.setDebugStream(Serial);
|
29 |
| - |
30 | 29 | cellular.begin();
|
| 30 | + |
31 | 31 | Serial.println("Connecting...");
|
32 | 32 | cellular.connect(SECRET_GPRS_APN, SECRET_GPRS_LOGIN, SECRET_GPRS_PASSWORD, SECRET_PINNUMBER);
|
33 |
| - |
| 33 | + |
34 | 34 | // Register interrupt based callback for new SMS
|
35 | 35 | attachInterrupt(digitalPinToInterrupt(NEW_SMS_INTERRUPT_PIN), onSMSReceived, RISING);
|
36 | 36 |
|
37 |
| - Serial.println("Read SMS:"); |
38 | 37 | std::vector<SMS> readSMS = cellular.getReadSMS();
|
39 |
| - printMessages(readSMS); |
| 38 | + if(readSMS.size() > 0){ |
| 39 | + Serial.println("Read SMS:"); |
| 40 | + printMessages(readSMS); |
| 41 | + } |
40 | 42 |
|
41 |
| - Serial.println("Unread SMS:"); |
42 | 43 | std::vector<SMS> unreadSMS = cellular.getUnreadSMS();
|
43 |
| - printMessages(unreadSMS); |
| 44 | + if(unreadSMS.size() > 0){ |
| 45 | + Serial.println("Unread SMS:"); |
| 46 | + printMessages(unreadSMS); |
| 47 | + } |
44 | 48 | }
|
45 | 49 |
|
46 | 50 | void loop(){
|
47 |
| - if(smsReceived){ |
| 51 | + static unsigned long lastPoll = 0; |
| 52 | + static bool checkForNewSMS = false; |
| 53 | + |
| 54 | + if(millis() - lastPoll > POLLING_INTERVAL_MS){ |
| 55 | + checkForNewSMS = true; |
| 56 | + lastPoll = millis(); |
| 57 | + } |
| 58 | + |
| 59 | + if(smsReceived || checkForNewSMS){ |
48 | 60 | smsReceived = false;
|
| 61 | + checkForNewSMS = false; |
49 | 62 | std::vector<SMS> unreadSMS = cellular.getUnreadSMS();
|
| 63 | + |
50 | 64 | if (unreadSMS.size() > 0){
|
| 65 | + Serial.println("New SMS received!"); |
51 | 66 | printMessages(unreadSMS);
|
52 | 67 | }
|
53 | 68 | }
|
|
0 commit comments