1
0
mirror of https://github.com/arduino/Arduino.git synced 2024-12-11 22:24:13 +01:00
Arduino/libraries/GSM/examples/ReceiveSMS/ReceiveSMS.ino

94 lines
1.8 KiB
Arduino
Raw Normal View History

2013-03-11 12:17:08 +01:00
/*
SMS receiver
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-03-11 12:17:08 +01:00
created 25 Feb 2012
by Javier Zorzano / TD
2013-03-11 12:17:08 +01:00
This example is in the public domain.
http://www.arduino.cc/en/Tutorial/GSMExamplesReceiveSMS
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
char senderNumber[20];
2013-03-11 12:17:08 +01: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) {
; // wait for serial port to connect. Needed for native USB port only
}
2013-03-11 12:17:08 +01:00
Serial.println("SMS Messages Receiver");
2013-03-11 12:17:08 +01:00
// connection state
boolean notConnected = true;
2013-03-11 12:17:08 +01:00
// Start GSM connection
while (notConnected) {
if (gsmAccess.begin(PINNUMBER) == GSM_READY) {
2013-03-11 12:17:08 +01:00
notConnected = false;
} else {
2013-03-11 12:17:08 +01:00
Serial.println("Not connected");
delay(1000);
}
}
2013-03-11 12:17:08 +01:00
Serial.println("GSM initialized");
Serial.println("Waiting for messages");
}
void loop() {
2013-03-11 12:17:08 +01:00
char c;
// If there are any SMSs available()
if (sms.available()) {
2013-03-11 12:17:08 +01:00
Serial.println("Message received from:");
2013-03-11 12:17:08 +01:00
// Get remote number
sms.remoteNumber(senderNumber, 20);
Serial.println(senderNumber);
// An example of message disposal
2013-03-11 12:17:08 +01:00
// Any messages starting with # should be discarded
if (sms.peek() == '#') {
2013-03-11 12:17:08 +01:00
Serial.println("Discarded SMS");
sms.flush();
}
2013-03-11 12:17:08 +01:00
// Read message bytes and print them
while (c = sms.read()) {
2013-03-11 12:17:08 +01:00
Serial.print(c);
}
2013-03-11 12:17:08 +01:00
Serial.println("\nEND OF MESSAGE");
2013-03-11 12:17:08 +01:00
// Delete message from modem memory
sms.flush();
Serial.println("MESSAGE DELETED");
}
delay(1000);
}