1
1
#include " ArduinoCellular.h"
2
2
#include " arduino_secrets.h"
3
3
4
+ // #define TINY_GSM_DEBUG Serial
5
+ // #define ARDUINO_CELLULAR_DEBUG
4
6
5
-
6
- #define TINY_GSM_DEBUG Serial
7
- #define ARDUINO_CELLULAR_DEBUG
7
+ constexpr int NEW_SMS_INTERRUPT_PIN = A0;
8
8
9
9
ArduinoCellular cellular = ArduinoCellular();
10
-
11
- boolean newSMS = false ;
12
-
10
+ volatile boolean newSMSavailable = false ;
13
11
14
12
void printMessages (std::vector<SMS> msg){
15
13
for (int i = 0 ; i < msg.size (); i++){
@@ -20,18 +18,20 @@ void printMessages(std::vector<SMS> msg){
20
18
}
21
19
}
22
20
void newSMSCallback (){
23
- Serial.println (" new sms received" );
24
- newSMS = true ;
21
+ Serial.println (" New SMS received! " );
22
+ newSMSavailable = true ;
25
23
}
26
24
27
25
void setup (){
28
26
Serial.begin (115200 );
29
27
while (!Serial);
28
+
30
29
cellular.begin ();
30
+ Serial.println (" Connecting..." );
31
31
cellular.connect (SECRET_GPRS_APN, SECRET_GPRS_LOGIN, SECRET_GPRS_PASSWORD, SECRET_PINNUMBER);
32
32
33
-
34
- attachInterrupt (digitalPinToInterrupt (A0 ), newSMSCallback, RISING);
33
+ // Register interrupt based callback for new SMS
34
+ attachInterrupt (digitalPinToInterrupt (NEW_SMS_INTERRUPT_PIN ), newSMSCallback, RISING);
35
35
36
36
Serial.println (" Read SMS:" );
37
37
std::vector<SMS> readSMS = cellular.getReadSMS ();
@@ -40,16 +40,14 @@ void setup(){
40
40
Serial.println (" Unread SMS:" );
41
41
std::vector<SMS> unreadSMS = cellular.getUnreadSMS ();
42
42
printMessages (unreadSMS);
43
-
44
- cellular.sendSMS (" +40788494946" , " bleep bleep" );
45
43
}
46
44
47
45
void loop (){
48
- if (newSMS ){
49
- newSMS = false ;
46
+ if (newSMSavailable ){
47
+ newSMSavailable = false ;
50
48
std::vector<SMS> unreadSMS = cellular.getUnreadSMS ();
51
49
if (unreadSMS.size () > 0 ){
52
50
printMessages (unreadSMS);
53
51
}
54
52
}
55
- }
53
+ }
0 commit comments