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