1 | /*
|
---|
2 | SMS sender
|
---|
3 |
|
---|
4 | This sketch, for the Arduino GSM shield,sends an SMS message
|
---|
5 | you enter in the serial monitor. Connect your Arduino with the
|
---|
6 | GSM shield and SIM card, open the serial monitor, and wait for
|
---|
7 | the "READY" message to appear in the monitor. Next, type a
|
---|
8 | message to send and press "return". Make sure the serial
|
---|
9 | monitor is set to send a newline when you press return.
|
---|
10 |
|
---|
11 | Circuit:
|
---|
12 | * GSM shield
|
---|
13 | * SIM card that can send SMS
|
---|
14 |
|
---|
15 | created 25 Feb 2012
|
---|
16 | by Tom Igoe
|
---|
17 |
|
---|
18 | This example is in the public domain.
|
---|
19 |
|
---|
20 | http://arduino.cc/en/Tutorial/GSMExamplesSendSMS
|
---|
21 |
|
---|
22 | */
|
---|
23 |
|
---|
24 | // Include the GSM library
|
---|
25 | #include <GSM.h>
|
---|
26 |
|
---|
27 | #define PINNUMBER ""
|
---|
28 |
|
---|
29 | // initialize the library instance
|
---|
30 | GSM gsmAccess;
|
---|
31 | GSM_SMS sms;
|
---|
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 Sender");
|
---|
42 |
|
---|
43 | // connection state
|
---|
44 | boolean notConnected = true;
|
---|
45 |
|
---|
46 | // Start GSM shield
|
---|
47 | // If your SIM has PIN, pass it as a parameter of begin() in quotes
|
---|
48 | while (notConnected)
|
---|
49 | {
|
---|
50 | if (gsmAccess.begin(PINNUMBER) == GSM_READY)
|
---|
51 | notConnected = false;
|
---|
52 | else
|
---|
53 | {
|
---|
54 | Serial.println("Not connected");
|
---|
55 | delay(1000);
|
---|
56 | }
|
---|
57 | }
|
---|
58 |
|
---|
59 | Serial.println("GSM initialized");
|
---|
60 | }
|
---|
61 |
|
---|
62 | void loop()
|
---|
63 | {
|
---|
64 |
|
---|
65 | Serial.print("Enter a mobile number: ");
|
---|
66 | char remoteNum[20]; // telephone number to send sms
|
---|
67 | readSerial(remoteNum);
|
---|
68 | Serial.println(remoteNum);
|
---|
69 |
|
---|
70 | // sms text
|
---|
71 | Serial.print("Now, enter SMS content: ");
|
---|
72 | char txtMsg[200];
|
---|
73 | readSerial(txtMsg);
|
---|
74 | Serial.println("SENDING");
|
---|
75 | Serial.println();
|
---|
76 | Serial.println("Message:");
|
---|
77 | Serial.println(txtMsg);
|
---|
78 |
|
---|
79 | // send the message
|
---|
80 | sms.beginSMS(remoteNum);
|
---|
81 | sms.print(txtMsg);
|
---|
82 | sms.endSMS();
|
---|
83 | Serial.println("\nCOMPLETE!\n");
|
---|
84 | }
|
---|
85 |
|
---|
86 | /*
|
---|
87 | Read input serial
|
---|
88 | */
|
---|
89 | int readSerial(char result[])
|
---|
90 | {
|
---|
91 | int i = 0;
|
---|
92 | while (1)
|
---|
93 | {
|
---|
94 | while (Serial.available() > 0)
|
---|
95 | {
|
---|
96 | char inChar = Serial.read();
|
---|
97 | if (inChar == '\n')
|
---|
98 | {
|
---|
99 | result[i] = '\0';
|
---|
100 | Serial.flush();
|
---|
101 | return 0;
|
---|
102 | }
|
---|
103 | if (inChar != '\r')
|
---|
104 | {
|
---|
105 | result[i] = inChar;
|
---|
106 | i++;
|
---|
107 | }
|
---|
108 | }
|
---|
109 | }
|
---|
110 | }
|
---|