#include "SIM900.h"
#include <SoftwareSerial.h>
#include "sms.h"
#include <string.h>
#include <TinyGPS.h>
TinyGPS gps;
#define ledpin 13
#define pwrkey 27
char
sms_rx[122];
byte type_sms=SMS_ALL;
byte del_sms=1;
char
number_incoming[20];
SMSGSM sms;
int
error;
boolean started=
false
;
bool
newData =
false
;
char
gps_year[8];
char
gps_mon[3];
char
gps_day[3];
char
gps_hour[3];
char
gps_min[3];
char
gps_sec[3];
char
gps_lon[20];
char
gps_lat[20];
char
gps_sms[100];
void
setup()
{
pinMode(pwrkey,OUTPUT);
digitalWrite(pwrkey,HIGH);
delay(600);
digitalWrite(pwrkey,LOW);
Serial.begin(115200);
Serial2.begin(9600);
if
(gsm.begin(9600)) {
Serial.println(
"nstatus=READY"
);
gsm.forceON();
started=
true
;
}
else
Serial.println(
"nstatus=IDLE"
);
if
(started)
{
Serial.println(
"Deleting SMS"
);
char
error = DeleteAllSMS();
if
(error==1)Serial.println(
"All SMS deleted"
);
else
Serial.println(
"SMS not deleted"
);
}
else
{Serial.println(
"SIM900 NOT EXISTED"
);
while
(1);}
delay(10000);
}
void
loop()
{
if
(started)
{
check_gps();
Check_SMS();
}
}
void
Check_SMS()
{
char
pos_sms_rx;
pos_sms_rx=sms.IsSMSPresent(type_sms);
if
(pos_sms_rx!=0)
{
sms.GetSMS(pos_sms_rx,number_incoming,sms_rx,120);
Serial.print(
"Received SMS from "
);
Serial.print(number_incoming);
Serial.print(
"(sim position: "
);
Serial.print(word(pos_sms_rx));
Serial.println(
")"
);
Serial.println(sms_rx);
if
(del_sms==1)
{
error=sms.DeleteSMS(pos_sms_rx);
if
(error==1)Serial.println(
"SMS deleted"
);
else
Serial.println(
"SMS not deleted"
);
}
if
((
strstr
(sms_rx,
"gps"
)!=0)&&(
strlen
(sms_rx)==3))
{
Serial.println(
"nsending SMS"
);
if
(newData)
{
if
(sms.SendSMS(number_incoming, gps_sms))
Serial.println(
"nSMS sent OK"
);
else
Serial.println(
"nSMS sent error"
);
}
else
{
if
(sms.SendSMS(number_incoming,
"gps not ready"
))
Serial.println(
"nSMS sent OK"
);
else
Serial.println(
"nSMS sent error"
);
}
}
Serial2.flush();
}
newData=
false
;
return
;
}
char
check_gps()
{
newData=
false
;
unsigned
long
chars;
unsigned
short
sentences, failed;
for
(unsigned
long
start = millis(); millis() - start < 1000;)
{
while
(Serial2.available())
{
char
c = Serial2.read();
if
(gps.encode(c))
newData =
true
;
}
}
if
(newData)
{
float
flat, flon;
unsigned
long
age;
int
_year;
byte _month, _day,_hour,_minute,_second,_hundredths;
gps.f_get_position(&flat, &flon, &age);
gps.crack_datetime(&_year,&_month,&_day,&_hour,&_minute,&_second,&_hundredths,&age);
flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flat, 6;
flon == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flon, 6;
dtostrf(flat, 11, 6, gps_lat);
dtostrf(flon, 10, 6, gps_lon);
strcpy
(gps_sms,
"lat:"
);
strcat
(gps_sms,gps_lat);
strcat
(gps_sms,
"n"
);
strcat
(gps_sms,
"lon:"
);
strcat
(gps_sms,gps_lon);
strcat
(gps_sms,
"n"
);
strcat
(gps_sms,
"time:"
);
itoa(_year,gps_year,10);
strcat
(gps_sms,gps_year);
itoa(_month,gps_mon,10);
if
(
strlen
(gps_mon)==1)
strcat
(gps_sms,
"0"
);
strcat
(gps_sms,gps_mon);
itoa(_day,gps_day,10);
if
(
strlen
(gps_day)==1)
strcat
(gps_sms,
"0"
);
strcat
(gps_sms,gps_day);
itoa(_hour,gps_hour,10);
if
(
strlen
(gps_hour)==1)
strcat
(gps_sms,
"0"
);
strcat
(gps_sms,gps_hour);
itoa(_minute,gps_min,10);
if
(
strlen
(gps_min)==1)
strcat
(gps_sms,
"0"
);
strcat
(gps_sms,gps_min);
itoa(_second,gps_sec,10);
if
(
strlen
(gps_sec)==1)
strcat
(gps_sms,
"0"
);
strcat
(gps_sms,gps_sec);
Serial.println(gps_sms);
}
}
char
DeleteAllSMS()
{
char
ret_val = -1;
if
(CLS_FREE != gsm.GetCommLineStatus())
return
(ret_val);
gsm.SetCommLineStatus(CLS_ATCMD);
ret_val = 0;
gsm.SimpleWriteln(F(
"AT+CMGDA="DEL ALL""
));
switch
(gsm.WaitResp(8000, 50,
"OK"
)) {
case
RX_TMOUT_ERR:
ret_val = -2;
break
;
case
RX_FINISHED_STR_RECV:
ret_val = 1;
break
;
case
RX_FINISHED_STR_NOT_RECV:
ret_val = 0;
break
;
}
gsm.SetCommLineStatus(CLS_FREE);
return
(ret_val);
}
发表评论 取消回复