#include <SoftwareSerial.h>
/***** Define *****/
SoftwareSerial gsmSerial(11, 12); // RX = 11, TX = 12
const char GSMSYNC = A0;
const char GSMIGT = 3;
const char RX = 11;
/Output*/
const char LED = 5;
/***** Global variables *****/
char gsmResponse[20];
char dataSMS[10];
char hpNoRx[12];
unsigned int i, counter = 0;
/***** Main function *****/
void setup()
{
pinMode(GSMSYNC, INPUT_PULLUP); // Set as input with internal pull up
pinMode(GSMIGT, OUTPUT); // Set as output
pinMode(RX, INPUT_PULLUP); // Set as input with internal pull up
pinMode(LED, OUTPUT); // Set as output
Serial.begin(9600); // Set hardware serial baudrate at 9600bps
gsmSerial.begin(9600); // Set software serial baudrate at 9600bps
Serial.print("Welcome!\r\n");
Serial.print("My Final Project\r\n");
gsmInit(); // GSM Initialize
Serial.print("GSM Ready\r\n\n");
}
void loop()
{
if (gsmSerial.available())
{
gsmReadLine(gsmResponse, '\n'); // Receive enter data
gsmReadLine(gsmResponse, '\n'); // Receive SMS indicator
if (!strncmp("+CMTI: "SM",1", gsmResponse, 12))
{
Serial.print("SMS Receive from ");
for (i = 0; i < 10; i++) dataSMS = ' ';
- gsmSendCmd("AT+CMGR=1"); // Read SMS at index 1*
- gsmReadLine(gsmResponse, '"');*
- gsmReadLine(gsmResponse, '"');*
- gsmReadLine(gsmResponse, '"');*
- gsmReadLine(hpNoRx, '"'); // Receive SMS sender no*
- gsmReadLine(gsmResponse, '\n');*
- gsmReadLine(dataSMS, '\n'); // Receive SMS content*
- gsmReadLine(gsmResponse, '\n');*
- gsmReadLine(gsmResponse, '\n');*
_ for (i = 0; i < 12; i++) Serial.print(hpNoRx*); // Print sender no*_
* Serial.print("\r\n> ");*
* Serial.print(dataSMS); // Print SMS content at monitor*
* if (!strncmp("LED ON", dataSMS, 6)) // Compare string "LED ON",6 alphabet*
* digitalWrite(LED, HIGH); // If match, LED turn ON*
* else if (!strncmp("LED OFF", dataSMS, 7)) // Compare string "LED OFF"*
* digitalWrite(LED, LOW); // If match, LED turn OFF*
* delay(1000);*
* gsmSendCmd("AT+CMGD=1"); // Delete SMS at index 1*
* gsmReadLine(gsmResponse, '\n'); // Receive OK*
* Serial.print("\r\n\n");*
* Serial.print("GSM Ready\r\n\n");*
* }*
* }*
* if (Serial.available())*
* {*
* Serial.print("Send SMS to +60133075890\r\n");*
* Serial.print("> GSM TC35 & My Final Project :)\r\n");*
* gsmSendCmd("AT+CMGS="+60133075890""); // Send SMS to corresponding no*
* while (!gsmSerial.available()); // Wait till receive data*
* if (gsmSerial.read() != '>') // If data not equal to '>'*
* {*
* Serial.print("Error"); // Print Error*
* while (1); // Wait until reset*
* }*
* gsmSerial.print("GSM TC35 & My Final Project :)"); // SMS content*
* gsmSerial.write(0x1A); // Send SMS*
* delay(5000); // Delay 5 seconds*
* Serial.print("\r\n");*
* Serial.print("GSM Ready\r\n\n");*
* }*
}
/***** Sub function *****/
void gsmInit()
{
* // Trigger GSM IGT pin*
* digitalWrite(GSMIGT, HIGH);*
* delay(1000);*
* digitalWrite(GSMIGT, LOW);*
* delay(1000);*
* digitalWrite(GSMIGT, HIGH);*
* // Wait GSM to detect SIM card*
* Serial.print("Initialize GSM:\r\n");*
* Serial.print("Read SIM Card: ");*
* while (1)*
* {*
* while (digitalRead(GSMSYNC) == HIGH);*
* while (digitalRead(GSMSYNC) == LOW);*
* delay(100);*
* if (digitalRead(GSMSYNC) == LOW)*
* {*
* Serial.print("OK\r\n");*
* break;*
* }*
* }*
* // Sent AT command*
* Serial.print("AT ");*
* gsmSendCmd("AT");*
* gsmReadLine(gsmResponse, '\n');*
* if (!strncmp("OK", gsmResponse, 2))*
* Serial.print("OK\r\n");*
* else {*
* Serial.print("Error");*
* while (1) ;*
* }*
* delay(200);*
* // Set text mode*
* Serial.print("Set Text Mode ");*
* gsmSendCmd("AT+CMGF=1");*
* gsmReadLine(gsmResponse, '\n');*
* if (!strncmp("OK", gsmResponse, 2))*
* Serial.print("OK\r\n");*
* else {*
* Serial.print("Error");*
* while (1);*
* }*
* delay(200);*
* // SMS indicator*
* Serial.print("Set Display SMS Index ");*
* gsmSendCmd("AT+CNMI=1,1");*
* gsmReadLine(gsmResponse, '\n');*
* if (!strncmp("OK", gsmResponse, 2))*
* Serial.print("OK\r\n");*
* else {*
* Serial.print("Error");*
* while (1);*
* }*
* delay(200);*
* // Delete 1st SMS*
* Serial.print("Delete SMS Index 1 ");*
* gsmSendCmd("AT+CMGD=1");*
* gsmReadLine(gsmResponse, '\n');*
* if (!strncmp("OK", gsmResponse, 2))*
* Serial.print("OK\r\n\n");*
* else {*
* Serial.print("Error");*
* while (1);*
* }*
* delay(200);*
}
void gsmSendCmd(const char *cmd)
{
* unsigned char inByte;*
_ while (cmd) gsmSerial.write(cmd++); // Send command data_
_ gsmSerial.print("\r\n");_
* do { // Wait until receive enter*
* while (!gsmSerial.available());*
* inByte = gsmSerial.read();*
* }*
* while (inByte != '\n');*
}
void gsmReadLine(char *buffer, char dataEnd)
{
* unsigned char inByte;*
* do { // Read data, until receive end data*
* while (!gsmSerial.available());*
* inByte = gsmSerial.read();*
_ buffer++ = inByte;_
_ }_
_ while (inByte != dataEnd);_
_}*_