I am trying to make a project using Arduino which sends an sms when electricty is off and sends another when it is on again. Also, when I send an SMS to Arduino, it replies back with "Electricty is on" or "Electricity is off".
I am using SeeedStudio GPRS Shield with Arduino. I have connected an 5.7V adapter to the grid and Arduino is checking if there is voltage coming from that adapter by using analogRead.
Everything works fine when Arduino is powered by the computer via USB. I thought I have accomplished the task. However, when Arduino is powered via 12V wall plug or 5V USB adapter connected to the grid, code does not work properly. This is what I mean by properly:
- When electricity is off, Arduino sends an SMS and calls me. Good!
- When electricity is on again, Arduino sends another SMS and calls me. Again this is what I want.
- However, when I send an SMS, it does not reply back with "Electricty is on or off" which is something Arduino does when it is powered by USB from computer.
- If I reset the Arduino by pressing the reset button, again everything works properly.
I do not understand why some parts of the code always work and some parts of it work when Arduino is connected to computer. Also, resetting the Arduino seems to solve the problem but I want the code to work properly when I power the Arduino for the first time without it needing a reset.
I am a total newbie with this kind of stuff and any help is much appreciated.
The code can be seen below.
#include <SoftwareSerial.h>
SoftwareSerial gprsSerial(7,8);
int check = 0;
int control = 500;
String myString = "";
char character;
///////////////////////////////////////////////////////////////////////////////////////////////////////////
void setup()
{
gprsSerial.begin(19200); // GPRS shield baud rate
Serial.begin(19200);
delay(100);
pinMode(9, OUTPUT);
pinMode(A0, INPUT);
delay(100);
digitalWrite(9, HIGH);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////
void loop()
{
if (Serial.available()) // if there is incoming serial data
switch(Serial.read()) // read the character
{
case 't': // if the character is 't'
SendTextMessage(); // send the text message
break;
case 'd': // if the character is 'd'
DialVoiceCall(); // dial a number
break;
case 'r': // if the character is 'r'
ReadSMS(); // read the text messages
break;
case 's': // if the character is 's'
DeleteSMS(); // delete text messages
break;
case 'a': // if the character is 'a'
RecieveCall(); // answer incoming call
break;
}
while(gprsSerial.available()) {
character = gprsSerial.read();
delay(10);
myString.concat(character);
}
if (myString.indexOf("CMTI: ") > 0) // Incoming SMS. Read the SMS and if it is from me, reply.
{
gprsSerial.println("AT+CMGL=\"ALL\"");
delay(200);
while (myString.indexOf("OK") < 0)
{
myString = "";
Oku();
if (myString.indexOf("(SOME PART OF MY MOBILE NUMBER") > 0)
{
if (control > 200)
{
ElectricityOn();
}
else
{
ElectricityOff();
}
delay(3000);
DeleteSMS();
}
}
}
if (myString != "")
{
Serial.print(myString);
myString = "";
}
if (millis() > 40000)
{
control = analogRead(A0);
if (control == 0 && check == 0)
{
Serial.print("control: ");
Serial.println(control);
ElectricityOff();
delay(5000);
DialVoiceCall();
delay(15000);
gprsSerial.println("AT+CHUP");
check = 1;
}
if (control > 200 && check == 1)
{
Serial.print("control: ");
Serial.println(control);
ElectricityBackOn();
delay(5000);
DialVoiceCall();
delay(15000);
gprsSerial.println("AT+CHUP");
check = 0;
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////
void Oku()
{
while(gprsSerial.available())
{
character = gprsSerial.read();
delay(10);
myString.concat(character);
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////
void SendTextMessage()
{
Serial.println("Sending text...");
gprsSerial.println("AT+CMGF=1\r"); // Set the shield to SMS mode
delay(100);
gprsSerial.println("AT+CMGS = \"MY PHONE NUMBER\"");
delay(100);
gprsSerial.println("myString"); //the content of the message
delay(100);
gprsSerial.print((char)26);//the ASCII code of the ctrl+z is 26 (required according to the datasheet)
delay(100);
gprsSerial.println();
Serial.println("SMS sent.");
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////
void DialVoiceCall()
{
gprsSerial.println("ATD+MY PHONE NUMBER;");//dial the number, must include country code
delay(100);
gprsSerial.println();
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////
void ReadSMS()
{
gprsSerial.println("AT+CMGL=\"ALL\"");
delay(100);
gprsSerial.println();
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////
void DeleteSMS()
{
gprsSerial.println("AT+CMGD=1,4\r");
delay(100);
gprsSerial.println();
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////
void RecieveCall()
{
gprsSerial.println("ata");
delay(100);
gprsSerial.println();
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////
void ElectricityOff()
{
Serial.println("Electricity is off. Sending SMS.");
gprsSerial.println("AT+CMGF=1\r"); // Set the shield to SMS mode
delay(100);
gprsSerial.println("AT+CMGS = \"MY PHONE NUMBER\"");
delay(100);
gprsSerial.println("Elektricity is off."); //the content of the message
delay(100);
gprsSerial.print((char)26);//the ASCII code of the ctrl+z is 26 (required according to the datasheet)
delay(100);
gprsSerial.println();
Serial.println("SMS sent.");
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////
void ElectricityBackOn()
{
Serial.println("Electricity is back on. Sending SMS.");
gprsSerial.println("AT+CMGF=1\r"); // Set the shield to SMS mode
delay(100);
gprsSerial.println("AT+CMGS = \"MY PHONE NUMBER\"");
delay(100);
gprsSerial.println("Elektrik is back on."); //the content of the message
delay(100);
gprsSerial.print((char)26);//the ASCII code of the ctrl+z is 26 (required according to the datasheet)
delay(100);
gprsSerial.println();
Serial.println("SMS sent.");
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////
void ElectricityOn()
{
Serial.println("Electricity is on. Sending SMS.");
gprsSerial.println("AT+CMGF=1\r"); // Set the shield to SMS mode
delay(100);
gprsSerial.println("AT+CMGS = \"MY PHONE NUMBER\"");
delay(100);
gprsSerial.println("Elektricity is on :)"); //the content of the message
delay(100);
gprsSerial.print((char)26);//the ASCII code of the ctrl+z is 26 (required according to the datasheet)
delay(100);
gprsSerial.println();
Serial.println("SMS sent.");
}