#include<SoftwareSerial.h>
#include <DFRobot_SIM808.h>
#define rxPin 11
#define txPin 10
// motor
int motor1pin1 = 2;
int motor1pin2 = 3;
int motor2pin1 = 4;
int motor2pin2 = 5;
SoftwareSerial mySerial(txPin, rxPin);
DFRobot_SIM808 sim808(&mySerial);
char wlat[12];
char wlon[12];
char wwspeed[12];
int port = 80;
char wdatetime[24];
#define MESSAGE_LENGTH 160
char message[MESSAGE_LENGTH];
int messageIndex = 0;
char phone[16];
char datetime[24];
void setup(){
mySerial.begin(9600);
Serial.begin(115200);
Serial.println("Starting...");
while(!sim808.init()) {
delay(1000);
Serial.print("Sim808 init error\r\n");
}
//************* Turn on the GPS power************
if( sim808.attachGPS())
{
Serial.println("Open the GPS power success");
}
else
{
Serial.println("Open the GPS power failure");
}
pinMode(motor1pin1, OUTPUT);
pinMode(motor1pin2, OUTPUT);
pinMode(motor2pin1, OUTPUT);
pinMode(motor2pin2, OUTPUT);
}
void jsonarray()
{
messageIndex = sim808.isSMSunread();
Serial.print("messageIndex: ");
Serial.println(messageIndex);
//*********** At least, there is one UNREAD SMS ***********
if (messageIndex > 0) {
sim808.readSMS(messageIndex, message, MESSAGE_LENGTH, phone, datetime);
//***********In order not to full SIM Memory, is better to delete it**********
sim808.deleteSMS(messageIndex);
Serial.print("From number: ");
Serial.println(phone);
Serial.print("Datetime: ");
Serial.println(datetime);
Serial.print("Recieved Message: ");
Serial.println(message);
if(String(message)=="m"){
digitalWrite(motor1pin1, HIGH);
digitalWrite(motor1pin2, LOW);
digitalWrite(motor2pin1, HIGH);
digitalWrite(motor2pin2, LOW);
delay(1000);
digitalWrite(motor1pin1, LOW);
digitalWrite(motor1pin2, LOW);
digitalWrite(motor2pin1, LOW);
digitalWrite(motor2pin2, LOW);
delay(1000);
}
}
}
void loop(){
if (sim808.getGPS()) {
jsonarray();
delay(1000);
Serial.print(sim808.GPSdata.year);
Serial.print("/");
Serial.print(sim808.GPSdata.month);
Serial.print("/");
Serial.print(sim808.GPSdata.day);
Serial.print(" ");
Serial.print(sim808.GPSdata.hour);
Serial.print(":");
Serial.print(sim808.GPSdata.minute);
Serial.print(":");
Serial.print(sim808.GPSdata.second);
Serial.print(":");
Serial.println(sim808.GPSdata.centisecond);
Serial.print("latitude :");
Serial.println(sim808.GPSdata.lat,6);
sim808.latitudeConverToDMS();
Serial.print("latitude :");
Serial.print(sim808.latDMS.degrees);
// Serial.print("\^");
Serial.print(sim808.latDMS.minutes);
Serial.print("\'");
Serial.print(sim808.latDMS.seconeds,6);
Serial.println("\"");
Serial.print("longitude :");
Serial.println(sim808.GPSdata.lon,6);
sim808.LongitudeConverToDMS();
Serial.print("longitude :");
Serial.print(sim808.longDMS.degrees);
// Serial.print("\^");
Serial.print(sim808.longDMS.minutes);
Serial.print("\'");
Serial.print(sim808.longDMS.seconeds,6);
Serial.println("\"");
Serial.print("speed_kph :");
Serial.println(sim808.GPSdata.speed_kph);
Serial.print("heading :");
Serial.println(sim808.GPSdata.heading);
float la = sim808.GPSdata.lat;
float lo = sim808.GPSdata.lon;
float ws = sim808.GPSdata.speed_kph;
delay(1000);
SubmitHttpRequest(la,lo,ws);
}
//delay(20000);
}
void SubmitHttpRequest(float la,float lo, float ws)
{
dtostrf( la,6, 6, wlat); //put float value of la into char array of lat. 6 = number of digits before decimal sign. 2 = number of digits after the decimal sign.
dtostrf( lo,6, 6, wlon); //put float value of lo into char array of lon
dtostrf( ws,6, 6, wwspeed); //put float value of ws into char array of wspeed
mySerial.println("AT");
delay(100);
ShowSerialData();
mySerial.println("AT+CREG?");
delay(100);
ShowSerialData();
mySerial.println("AT+CSQ");
delay(100);
ShowSerialData();// this code is to show the data from gprs shield, in order to easily see the process of how the gprs shield submit a http request, and the following is for this purpose too.
mySerial.println("AT+CGATT?");
delay(100);
ShowSerialData();
mySerial.println("AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\"");//setting the SAPBR, the connection type is using gprs
delay(1000);
ShowSerialData();
mySerial.println("AT+SAPBR=3,1,\"APN\",\"Mobinil\"");//setting the APN, the second need you fill in your local apn server
delay(4000);
ShowSerialData();
mySerial.println("AT+SAPBR=1,1");//setting the SAPBR, for detail you can refer to the AT command mamual
delay(2000);
ShowSerialData();
mySerial.println("AT+SAPBR=3,1");//setting the SAPBR, for detail you can refer to the AT command mamual
delay(2000);
ShowSerialData();
mySerial.println("AT+SAPBR=2,1");//setting the SAPBR, for detail you can refer to the AT command mamual
delay(2000);
ShowSerialData();
mySerial.println("AT+HTTPINIT"); //init the HTTP request
delay(2000);
ShowSerialData();
mySerial.println("AT+HTTPPARA=\"CID\",1"); //init the HTTP request
delay(2000);
ShowSerialData();
mySerial.println("AT+HTTPSSL=0"); // set SSL for HTTPS
ShowSerialData();
delay(1000);
mySerial.println("AT+HTTPSSL=1"); //init the HTTP request
delay(2000);
ShowSerialData();
//mySerial.println("AT+HTTPPARA=\"URL\",\"gpstest.edafait.com/Default2?lat=36\"");// setting the httppara, the second parameter is the website you want to access
//delay(2000);
//ShowSerialData();
mySerial.print("AT+HTTPPARA=\"URL\",\"gpstest.edafait.com/Default2?");
mySerial.print("&lat="); // This is the value name
mySerial.print(wlat); // value of...
mySerial.print("&longs="); // This is the value name
mySerial.print(wlon); // value of...
mySerial.print("&actions="); // This is the value name
mySerial.print(wwspeed); // value of...
mySerial.print("&dates="); // This is the value name
mySerial.print("AT+CGSN");
mySerial.print("&time="); // This is the value name
mySerial.print("AT+CGMM");
mySerial.println("\"");
delay(2000);
ShowSerialData();
// that's because you have a crapy way of reading the Serial line..
// an attempt to read for a bit longer
mySerial.println("AT+HTTPACTION=2");//submit the request
delay(10000);//the delay is very important, the delay time is base on the return from the website, if the return datas are very large, the time required longer.
//while(!mySerial.available());
ShowSerialData();
mySerial.println("AT+HTTPACTION=1");//submit the request
delay(10000);//the delay is very important, the delay time is base on the return from the website, if the return datas are very large, the time required longer.
//while(!mySerial.available());
ShowSerialData();
mySerial.println("AT+HTTPACTION=0");//submit the request
delay(10000);//the delay is very important, the delay time is base on the return from the website, if the return datas are very large, the time required longer.
//while(!mySerial.available());
ShowSerialData();
mySerial.println("AT+HTTPACTION=0");//submit the request
delay(10000);//the delay is very important, the delay time is base on the return from the website, if the return datas are very large, the time required longer.
//while(!mySerial.available());
ShowSerialData();
mySerial.println("AT+HTTPREAD");// read the data from the website you access
delay(300);
ShowSerialData();
//for (int i=0; i<100; i++) {
//ShowSerialData();
// delay(1);
//}
mySerial.println("");
delay(100);
}
void ShowSerialData()
{
while(mySerial.available()!=0)
{Serial.write(mySerial.read());}
}
Comments
Please log in or sign up to comment.