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

99 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.
2013-03-11 12:17:08 +01:00
http://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 Leonardo 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)
2013-03-11 12:17:08 +01:00
{
if (gsmAccess.begin(PINNUMBER) == GSM_READY)
2013-03-11 12:17:08 +01:00
notConnected = false;
else
{
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()
2013-03-11 12:17:08 +01:00
if (sms.available())
{
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);
}