I am about to get time and date from the GSM and send as an SMS but I have a problem.
Can you tell me where did I go wrong? or do I miss something...
this is my code:
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11);
String datetime;
int inByte = 0;
volatile byte var = 0;
int state = 0;
const int pin0 = 2;
void setup() {
Serial.begin(9600);
mySerial.begin(9600);
attachInterrupt(digitalPinToInterrupt(pin0), ON, CHANGE);
pinMode(13, OUTPUT);
}
void loop() {
inByte = digitalRead(pin0);
if (inByte == HIGH) {
if ( var == 0 ) {
state = 1;
var = 1;
}
}
else if (inByte == LOW) {
if (var == 0) {
state = 2;
var = 1;
}
}
switch (state) {
case 0: //for high
digitalWrite(13, HIGH);
break;
case 1: //for rising
sendSMS();
state = 0;
break;
case 2: //for falling
sendSMS();
state = 3;
break;
case 3: //for low
digitalWrite(13, HIGH);
delay(750);
digitalWrite(13, LOW);
delay(750);
break;
default:
break;
}
}
void getTime() {
mySerial.listen();
String mystr = "";
int i = 0;
mySerial.println("AT+CCLK?");
delay(500);
while (mySerial.available() > 0) {
mystr += char(mySerial.read());
}
int x = mystr.indexOf(String('"')) + 1;
int y = mystr.lastIndexOf(String('"'));
datetime = mystr.substring(x, y);
Serial.println(datetime);
delay(500);
}
void sendSMS() {
getTime();
mySerial.listen();
delay(1500);
mySerial.println("AT+CMGF=1"); //because we want to send the sms in text mode
delay(1500);
mySerial.println("AT+CMGS=\"+6491293912\"\r"); //change to the phone number using
delay(1500);
mySerial.println("xXxXXxxX"); //content of the message
mySerial.println("Date/Time:");
delay(100);
mySerial.println(datetime);
delay(200);
mySerial.println((char)26); //the ASCII code of the ctrl+z is 26
delay(1000);
}
void ON() {
var = 0;
}
my code send SMS everytime the pin 2 changes state.
if I turn on the system for the first time it prints the right time.
18/03/14,13:06:51-22
but when the state change, it makes this kind of output:
18/03/14,13:06:51-22
OK
>
>