[175] | 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 | ATTENTION: to work correctly with M0/M0 pro you have to connect pin 2 to pin 4 of the shield
|
---|
| 16 |
|
---|
| 17 | created 25 Feb 2012
|
---|
| 18 | by Tom Igoe
|
---|
| 19 |
|
---|
| 20 | This example is in the public domain.
|
---|
| 21 |
|
---|
| 22 | http://arduino.cc/en/Tutorial/GSMExamplesSendSMS
|
---|
| 23 |
|
---|
| 24 |
|
---|
| 25 | */
|
---|
| 26 |
|
---|
| 27 | // Include the GSM library
|
---|
| 28 | #include <GSM.h>
|
---|
| 29 |
|
---|
| 30 | #define PINNUMBER ""
|
---|
| 31 |
|
---|
| 32 | // initialize the library instance
|
---|
| 33 | GSM gsmAccess;
|
---|
| 34 | GSM_SMS sms;
|
---|
| 35 |
|
---|
| 36 | void setup()
|
---|
| 37 | {
|
---|
| 38 | // initialize serial communications and wait for port to open:
|
---|
| 39 | SerialUSB.begin(9600);
|
---|
| 40 | while (!SerialUSB) {
|
---|
| 41 | ; // wait for serial port to connect. Needed for Leonardo only
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | SerialUSB.println("SMS Messages Sender");
|
---|
| 45 |
|
---|
| 46 | // connection state
|
---|
| 47 | boolean notConnected = true;
|
---|
| 48 |
|
---|
| 49 | // Start GSM shield
|
---|
| 50 | // If your SIM has PIN, pass it as a parameter of begin() in quotes
|
---|
| 51 | while (notConnected)
|
---|
| 52 | {
|
---|
| 53 | if (gsmAccess.begin(PINNUMBER) == GSM_READY)
|
---|
| 54 | notConnected = false;
|
---|
| 55 | else
|
---|
| 56 | {
|
---|
| 57 | SerialUSB.println("Not connected");
|
---|
| 58 | delay(1000);
|
---|
| 59 | }
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | SerialUSB.println("GSM initialized");
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | void loop()
|
---|
| 66 | {
|
---|
| 67 |
|
---|
| 68 | SerialUSB.print("Enter a mobile number: ");
|
---|
| 69 | char remoteNum[20]; // telephone number to send sms
|
---|
| 70 | readSerial(remoteNum);
|
---|
| 71 | SerialUSB.println(remoteNum);
|
---|
| 72 |
|
---|
| 73 | // sms text
|
---|
| 74 | SerialUSB.print("Now, enter SMS content: ");
|
---|
| 75 | char txtMsg[200];
|
---|
| 76 | readSerial(txtMsg);
|
---|
| 77 | SerialUSB.println("SENDING");
|
---|
| 78 | SerialUSB.println();
|
---|
| 79 | SerialUSB.println("Message:");
|
---|
| 80 | SerialUSB.println(txtMsg);
|
---|
| 81 |
|
---|
| 82 | // send the message
|
---|
| 83 | sms.beginSMS(remoteNum);
|
---|
| 84 | sms.print(txtMsg);
|
---|
| 85 | sms.endSMS();
|
---|
| 86 | SerialUSB.println("\nCOMPLETE!\n");
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | /*
|
---|
| 90 | Read input serial
|
---|
| 91 | */
|
---|
| 92 | int readSerial(char result[])
|
---|
| 93 | {
|
---|
| 94 | int i = 0;
|
---|
| 95 | while (1)
|
---|
| 96 | {
|
---|
| 97 | while (SerialUSB.available() > 0)
|
---|
| 98 | {
|
---|
| 99 | char inChar = SerialUSB.read();
|
---|
| 100 | if (inChar == '\n')
|
---|
| 101 | {
|
---|
| 102 | result[i] = '\0';
|
---|
| 103 | SerialUSB.flush();
|
---|
| 104 | return 0;
|
---|
| 105 | }
|
---|
| 106 | if (inChar != '\r')
|
---|
| 107 | {
|
---|
| 108 | result[i] = inChar;
|
---|
| 109 | i++;
|
---|
| 110 | }
|
---|
| 111 | }
|
---|
| 112 | }
|
---|
| 113 | }
|
---|