-
Notifications
You must be signed in to change notification settings - Fork 1
/
sketch_sms.ino
executable file
·112 lines (93 loc) · 2.55 KB
/
sketch_sms.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/**
* Arduino SMS
*
* This sketch, for the Arduino GSM shield, sends a preset
* message upon connecting to the GSM network and then
* listens (forever) for incoming SMS messages.
*
* Circuit:
* GSM shield attached to and Arduino
* SIM card that can place/receive phone calls and
* send/receive SMS messages
*/
#include <GSM.h>
/**
* Your SIM's PIN number, if it requires one
* @var string
*/
#define PINNUMBER ""
/**
* Library initialization
*/
GSM gsmAccess; // include a 'true' parameter for debug enabled
GSM_SMS sms;
/**
* Telephone number to send the initial "we're connected" SMS to.
* Prefix with the country-code, such as `1` for the U.S.
* Sample: `18005551212`
* @var string
*/
char notificationNumber[20] = "";
/**
* Holds the phone number for the sender of the received SMS.
* @var string
*/
char smsFromNumber[20];
/**
* The message to send when we've connected to the GSM network.
* @var string
*/
char connectionEstablishedMsg[200] = ":wave: ... we're connected (^_^)";
/**
* Initializes the serial comms for the Arduino and monitors the GSM
* initialization and connection status.
*/
void setup() {
// initialize serial comms on port 9600
Serial.begin(9600);
while (!Serial) {
; // wait for the port to connect
}
Serial.println("Arduino SMS");
Serial.println("-----------");
// wait for the GSM connection
Serial.print("Connecting to GSM Network");
while (gsmAccess.begin(PINNUMBER) != GSM_READY) {
Serial.print(".");
delay(1000);
}
Serial.println("\nConnected!");
// send connection-established message
sendConnectionEstablishedMsg();
}
/**
* Sends the preset connection-established message to the preset number.
*/
void sendConnectionEstablishedMsg() {
Serial.println("Sending connection established message to:");
Serial.println(notificationNumber);
sms.beginSMS(notificationNumber);
sms.print(connectionEstablishedMsg);
sms.endSMS();
Serial.println("\nSENT!\n");
}
/**
* Waits until we have received an SMS and prints it to the serial connection.
*/
void loop() {
char c;
if (sms.available()) {
Serial.println("Message received from:");
// fetch the sender's phone number
sms.remoteNumber(smsFromNumber, 20);
Serial.println(smsFromNumber);
// read and print the received message, byte-by-byte
while (c = sms.read()) {
Serial.print(c);
}
Serial.println("\n-");
// remove the message from the modem's memory
sms.flush();
}
delay(1000);
}