1 | /*
|
---|
2 | SMS receiver
|
---|
3 |
|
---|
4 | This sketch, for the Arduino GSM shield, waits for a SMS message
|
---|
5 | and displays it through the Serial port.
|
---|
6 |
|
---|
7 | Circuit:
|
---|
8 | * GSM shield attached to and Arduino
|
---|
9 | * SIM card that can receive SMS messages
|
---|
10 |
|
---|
11 | created 25 Feb 2012
|
---|
12 | by Javier Zorzano / TD
|
---|
13 |
|
---|
14 | This example is in the public domain.
|
---|
15 |
|
---|
16 | http://arduino.cc/en/Tutorial/GSMExamplesReceiveSMS
|
---|
17 |
|
---|
18 | */
|
---|
19 |
|
---|
20 | // include the GSM library
|
---|
21 | #include <GSM.h>
|
---|
22 |
|
---|
23 | // PIN Number for the SIM
|
---|
24 | #define PINNUMBER ""
|
---|
25 |
|
---|
26 | // initialize the library instances
|
---|
27 | GSM gsmAccess;
|
---|
28 | GSM_SMS sms;
|
---|
29 |
|
---|
30 | // Array to hold the number a SMS is retreived from
|
---|
31 | char senderNumber[20];
|
---|
32 |
|
---|
33 | void setup()
|
---|
34 | {
|
---|
35 | // initialize serial communications and wait for port to open:
|
---|
36 | Serial.begin(9600);
|
---|
37 | while (!Serial) {
|
---|
38 | ; // wait for serial port to connect. Needed for Leonardo only
|
---|
39 | }
|
---|
40 |
|
---|
41 | Serial.println("SMS Messages Receiver");
|
---|
42 |
|
---|
43 | // connection state
|
---|
44 | boolean notConnected = true;
|
---|
45 |
|
---|
46 | // Start GSM connection
|
---|
47 | while (notConnected)
|
---|
48 | {
|
---|
49 | if (gsmAccess.begin(PINNUMBER) == GSM_READY)
|
---|
50 | notConnected = false;
|
---|
51 | else
|
---|
52 | {
|
---|
53 | Serial.println("Not connected");
|
---|
54 | delay(1000);
|
---|
55 | }
|
---|
56 | }
|
---|
57 |
|
---|
58 | Serial.println("GSM initialized");
|
---|
59 | Serial.println("Waiting for messages");
|
---|
60 | }
|
---|
61 |
|
---|
62 | void loop()
|
---|
63 | {
|
---|
64 | char c;
|
---|
65 |
|
---|
66 | // If there are any SMSs available()
|
---|
67 | if (sms.available())
|
---|
68 | {
|
---|
69 | Serial.println("Message received from:");
|
---|
70 |
|
---|
71 | // Get remote number
|
---|
72 | sms.remoteNumber(senderNumber, 20);
|
---|
73 | Serial.println(senderNumber);
|
---|
74 |
|
---|
75 | // An example of message disposal
|
---|
76 | // Any messages starting with # should be discarded
|
---|
77 | if (sms.peek() == '#')
|
---|
78 | {
|
---|
79 | Serial.println("Discarded SMS");
|
---|
80 | sms.flush();
|
---|
81 | }
|
---|
82 |
|
---|
83 | // Read message bytes and print them
|
---|
84 | while (c = sms.read())
|
---|
85 | Serial.print(c);
|
---|
86 |
|
---|
87 | Serial.println("\nEND OF MESSAGE");
|
---|
88 |
|
---|
89 | // Delete message from modem memory
|
---|
90 | sms.flush();
|
---|
91 | Serial.println("MESSAGE DELETED");
|
---|
92 | }
|
---|
93 |
|
---|
94 | delay(1000);
|
---|
95 |
|
---|
96 | }
|
---|
97 |
|
---|
98 |
|
---|