2013-03-11 12:17:08 +01:00
|
|
|
/*
|
|
|
|
SMS receiver
|
2013-10-21 09:58:40 +02:00
|
|
|
|
|
|
|
This sketch, for the Arduino GSM shield, waits for a SMS message
|
|
|
|
and displays it through the Serial port.
|
|
|
|
|
2013-03-11 12:17:08 +01:00
|
|
|
Circuit:
|
|
|
|
* GSM shield attached to and Arduino
|
|
|
|
* SIM card that can receive SMS messages
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2013-03-11 12:17:08 +01:00
|
|
|
created 25 Feb 2012
|
|
|
|
by Javier Zorzano / TD
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2013-03-11 12:17:08 +01:00
|
|
|
This example is in the public domain.
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2015-05-20 17:10:06 +02:00
|
|
|
http://www.arduino.cc/en/Tutorial/GSMExamplesReceiveSMS
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2013-03-11 12:17:08 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
// include the GSM library
|
|
|
|
#include <GSM.h>
|
|
|
|
|
|
|
|
// PIN Number for the SIM
|
|
|
|
#define PINNUMBER ""
|
|
|
|
|
|
|
|
// initialize the library instances
|
|
|
|
GSM gsmAccess;
|
|
|
|
GSM_SMS sms;
|
|
|
|
|
|
|
|
// Array to hold the number a SMS is retreived from
|
2013-10-21 09:58:40 +02:00
|
|
|
char senderNumber[20];
|
2013-03-11 12:17:08 +01:00
|
|
|
|
2015-07-06 15:18:33 +02:00
|
|
|
void setup() {
|
2013-03-11 12:17:08 +01:00
|
|
|
// initialize serial communications and wait for port to open:
|
|
|
|
Serial.begin(9600);
|
|
|
|
while (!Serial) {
|
2015-09-21 14:44:19 +02:00
|
|
|
; // wait for serial port to connect. Needed for native USB port only
|
2013-10-21 09:58:40 +02:00
|
|
|
}
|
2013-03-11 12:17:08 +01:00
|
|
|
|
|
|
|
Serial.println("SMS Messages Receiver");
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2013-03-11 12:17:08 +01:00
|
|
|
// connection state
|
|
|
|
boolean notConnected = true;
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2013-03-11 12:17:08 +01:00
|
|
|
// Start GSM connection
|
2015-07-06 15:18:33 +02:00
|
|
|
while (notConnected) {
|
|
|
|
if (gsmAccess.begin(PINNUMBER) == GSM_READY) {
|
2013-03-11 12:17:08 +01:00
|
|
|
notConnected = false;
|
2015-07-06 15:18:33 +02:00
|
|
|
} else {
|
2013-03-11 12:17:08 +01:00
|
|
|
Serial.println("Not connected");
|
|
|
|
delay(1000);
|
|
|
|
}
|
|
|
|
}
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2013-03-11 12:17:08 +01:00
|
|
|
Serial.println("GSM initialized");
|
|
|
|
Serial.println("Waiting for messages");
|
|
|
|
}
|
|
|
|
|
2015-07-06 15:18:33 +02:00
|
|
|
void loop() {
|
2013-03-11 12:17:08 +01:00
|
|
|
char c;
|
2013-10-21 09:58:40 +02:00
|
|
|
|
|
|
|
// If there are any SMSs available()
|
2015-07-06 15:18:33 +02:00
|
|
|
if (sms.available()) {
|
2013-03-11 12:17:08 +01:00
|
|
|
Serial.println("Message received from:");
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2013-03-11 12:17:08 +01:00
|
|
|
// Get remote number
|
|
|
|
sms.remoteNumber(senderNumber, 20);
|
|
|
|
Serial.println(senderNumber);
|
|
|
|
|
2013-10-21 09:58:40 +02:00
|
|
|
// An example of message disposal
|
2013-03-11 12:17:08 +01:00
|
|
|
// Any messages starting with # should be discarded
|
2015-07-06 15:18:33 +02:00
|
|
|
if (sms.peek() == '#') {
|
2013-03-11 12:17:08 +01:00
|
|
|
Serial.println("Discarded SMS");
|
|
|
|
sms.flush();
|
|
|
|
}
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2013-03-11 12:17:08 +01:00
|
|
|
// Read message bytes and print them
|
2015-07-06 15:18:33 +02:00
|
|
|
while (c = sms.read()) {
|
2013-03-11 12:17:08 +01:00
|
|
|
Serial.print(c);
|
2015-07-06 15:18:33 +02:00
|
|
|
}
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2013-03-11 12:17:08 +01:00
|
|
|
Serial.println("\nEND OF MESSAGE");
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2013-03-11 12:17:08 +01:00
|
|
|
// Delete message from modem memory
|
|
|
|
sms.flush();
|
|
|
|
Serial.println("MESSAGE DELETED");
|
|
|
|
}
|
|
|
|
|
|
|
|
delay(1000);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|