Hi All,
My project is to operate a motor using SMS send from my mobile to GSM module (with Arduino Uno). I am using the following code. However, the code works sometimes and not other times. Not sure what is happening exactly. Could anyone please shed some light on this? Here comes the code I am using
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11);
int LED = 8;
int motor = 9;
int temp=0;
int i=0;
char str[15];
void setup()
{
mySerial.begin(9600); // Setting the baud rate of GSM Module
Serial.begin(9600); // Setting the baud rate of Serial Monitor (Arduino)
pinMode(motor,OUTPUT);
pinMode(LED,OUTPUT);
digitalWrite(motor,LOW);
digitalWrite(LED,LOW);
delay(50);
SendMessage();
}
void loop()
{
serialEvent();
RecieveMessage();
if (mySerial.available()>0)
Serial.write(mySerial.read());
//Serial.write(str, i);
}
void SendMessage()
{
mySerial.println("AT+CMGF=1"); //Sets the GSM Module in Text Mode
delay(1000); // Delay of 1000 milli seconds or 1 second
mySerial.println("AT+CMGS="xxxxxxxxx"\r"); // Replace x with mobile number
delay(1000);
mySerial.println("System is up and ready");// The SMS text you want to send
delay(1000);
mySerial.println((char)26);// ASCII code of CTRL+Z
delay(1000);
}
void serialEvent()
{
ClearStrArray();
while (mySerial.available())
{
if(mySerial.find("/"))
{
while(mySerial.available())
{
char inChar=mySerial.read();
str[i++] = inChar;
if (inChar=='/') break;
}
i = i-1;
//Serial.write(str, i);
}
}
}
void ClearStrArray() // function to clear buffer array
{
// for (int j = 0; j < i; j++)
// {
memset(0, str, sizeof(str));
// str[i]= '\0';
// } // clear all index of an array with command NULL
i = 0;
}
void RecieveMessage()
{
mySerial.println("AT+CNMI=2,2,0,0,0"); // AT Command to recieve a live SMS
//delay(100);
Serial.write(str, i);
if(!(strncmp(str,"motor on",8)))
{
digitalWrite(motor,HIGH);
digitalWrite(LED,HIGH);
delay(50);
mySerial.println("AT+CMGF=1"); //Sets the GSM Module in Text Mode
delay(50); // Delay of 1000 milli seconds or 1 second
mySerial.println("AT+CMGS="xxxxxxxxxx"\r"); // Replace x with mobile number
delay(50);
mySerial.println("Motor Activated");// The SMS text you want to send
delay(50);
mySerial.println((char)26);// ASCII code of CTRL+Z
delay(50);
}
else if(!(strncmp(str,"motor off",9)))
{
digitalWrite(motor,LOW);
digitalWrite(LED,LOW);
delay(50);
mySerial.println("AT+CMGF=1"); //Sets the GSM Module in Text Mode
delay(50); // Delay of 1000 milli seconds or 1 second
mySerial.println("AT+CMGS="xxxxxxxxxx"\r"); // Replace x with mobile number
delay(50);
mySerial.println("Motor Deactivated");// The SMS text you want to send
delay(50);
mySerial.println((char)26);// ASCII code of CTRL+Z
delay(50);
}
else if(!(strncmp(str,"test",4)))
{
mySerial.println("AT+CMGF=1"); //Sets the GSM Module in Text Mode
delay(50);
mySerial.println("AT+CMGS="xxxxxxxxxx"\r"); // Replace x with mobile number
delay(50);
mySerial.println("The System is Working fine");// The SMS text you want to send
delay(50);
mySerial.println((char)26);// ASCII code of CTRL+Z
delay(50);
}
}
Thanks much in anticipation