Created
March 27, 2017 22:39
-
-
Save azihassan/5c3fb3ee6356d20fc1a65b4d7917c7db to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SoftwareSerial.h> | |
#include "SIM900.h" | |
bool gprs_active; | |
char incoming[255] = ""; | |
bool handling_call; | |
void setup() | |
{ | |
handling_call = false; | |
Serial.begin(9600); | |
Serial.print("Booting GSM... "); | |
if(gsm.begin(1200)) | |
Serial.println("GSM booted successfully !"); | |
else | |
Serial.println("Failed to boot GSM."); | |
gsm.SimpleWriteln("AT+CMEE=2"); | |
gsm.WhileSimpleRead(); | |
Serial.println("Connecting to the internet..."); | |
connectGPRS("wap.iamgprs.ma"); | |
} | |
void loop() | |
{ | |
if(Serial.available()) | |
{ | |
char cmd[100] = ""; | |
byte length = Serial.readBytesUntil('\n', cmd, 99); | |
if(length > 0) | |
{ | |
if(strcmp(cmd, "POST") == 0) | |
{ | |
sendPOSTRequest("http://aqueous-plateau-61750.herokuapp.com/gpsdata", "{\"longitude\": 22, \"latitude\": 31}"); | |
Serial.println("Done !"); | |
} | |
else if(strcmp(cmd, "MSG") == 0) | |
{ | |
Serial.println("Retrieving position..."); | |
char raw[100] = "", longitude[20] = "", latitude[20] = "", ignore[11]; | |
retrievePosition(raw, 99); | |
Serial.print("Received response : "); | |
Serial.println(raw); | |
parsePosition(raw, latitude, longitude, ignore, ignore); | |
Serial.print("Latitude : "); | |
Serial.println(latitude); | |
Serial.print("Longitude : "); | |
Serial.println(longitude); | |
char message[100] = ""; | |
sprintf(message, "Latitude : %s, longitude = %s\n", latitude, longitude); | |
Serial.print("Sending message : "); | |
Serial.println(message); | |
sendSMS("+212637945221", message); | |
Serial.println("Done !"); | |
} | |
else if(strcmp(cmd, "POSITION") == 0) | |
{ | |
Serial.println("Retrieving position..."); | |
char response[100] = "", json[100] = ""; | |
char latitude[20] = "", longitude[20] = "", ignore[11] = ""; | |
retrievePosition(response, 99); | |
Serial.print("Response : "); | |
Serial.println(response); | |
Serial.println("Parsing position..."); | |
parsePosition(response, latitude, longitude, ignore, ignore); | |
Serial.print("Latitude : "); | |
Serial.println(latitude); | |
Serial.print("Longitude : "); | |
Serial.println(longitude); | |
sprintf(json, "{\"latitude\": \"%s\", \"longitude\" : \"%s\"}", latitude, longitude); | |
Serial.print("JSON : "); | |
Serial.println(json); | |
Serial.println("Sending coordinates to endpoint.."); | |
sendPOSTRequest("http://aqueous-plateau-61750.herokuapp.com/gpsdata", json); | |
Serial.println("Done !"); | |
} | |
else | |
{ | |
Serial.print("Sending command "); | |
Serial.println(cmd); | |
gsm.SimpleWriteln(cmd); | |
delay(3000); | |
gsm.WhileSimpleRead(); | |
} | |
} | |
} | |
//gsm.WhileSimpleRead(); | |
if(gsm.read(incoming, 99) > 0) | |
{ | |
Serial.println(incoming); | |
//+CLIP: "+212637945221",145,"",,"",0 | |
if(strstr(incoming, "+CLIP") != NULL) | |
{ | |
char raw[100] = ""; | |
handling_call = true; | |
char phone_number[20] = ""; | |
sscanf(incoming, "+CLIP: \"%[^\"]\"", phone_number); | |
Serial.print("Received a phone call from "); | |
Serial.println(phone_number); | |
retrievePosition(raw, 99); | |
parsePosition(raw, | |
} | |
} | |
} | |
void sendPosition(const char *to, const char *latitude, const char *longitude, int type) | |
{ | |
char formatted[100] = ""; | |
switch(type) | |
{ | |
case EMAIL: | |
Serial.println("Email : not implemented yet."); | |
break; | |
case SMS: | |
snprintf(formatted, 99, "Latitude : %s, longitude : %s", latitude, longitude); | |
sendSMS(to, formatted); | |
break; | |
case HTTP: | |
snprintf(formatted, 99, "{\"longitude\": \"%s\", \"latitude\": \"%s\"}", longitude, latitude); | |
sendPOSTRequest(to, json); | |
break; | |
} | |
} | |
int parsePosition(const char *response, char *latitude, char *longitude, char *date, char *time) | |
{ | |
int ignore; | |
return sscanf(response, "\n+CIPGSMLOC: %d,%20[^,],%20[^,],%11[^,],%9[^,]", &ignore, longitude, latitude, date, time); | |
} | |
void connectGPRS(const char *apn) | |
{ | |
gsm.SimpleWriteln("AT+SAPBR=3,1,\"CONTYPE\",\"gprs\""); | |
delay(4000); | |
gsm.WhileSimpleRead(); | |
gsm.SimpleWrite("AT+SAPBR=3,1,\"APN\",\""); | |
gsm.SimpleWrite(apn); | |
gsm.SimpleWriteln("\""); | |
delay(4000); | |
gsm.WhileSimpleRead(); | |
gsm.SimpleWriteln("AT+SAPBR=1,1"); | |
delay(4000); | |
gsm.WhileSimpleRead(); | |
gsm.SimpleWriteln("AT+SAPBR=2,1"); | |
delay(4000); | |
gsm.WhileSimpleRead(); | |
gprs_active = true; | |
} | |
void retrievePosition(char *response, byte length) | |
{ | |
if(!gprs_active) | |
{ | |
Serial.println("Unable to retrieve coordinates : not connected to internet."); | |
return; | |
} | |
gsm.WhileSimpleRead(); | |
gsm.SimpleWriteln("AT+CIPGSMLOC=1,1"); | |
delay(8000); | |
gsm.read(response, length); | |
} | |
void sendPOSTRequest(const char *url, const char *data) | |
{ | |
if(!gprs_active) | |
{ | |
Serial.println("Unable to send HTTP request : not connected to internet."); | |
return; | |
} | |
gsm.SimpleWriteln("AT+HTTPINIT"); | |
Serial.println("AT+HTTPINIT"); | |
delay(4000); | |
gsm.WhileSimpleRead(); | |
gsm.WhileSimpleRead(); | |
gsm.SimpleWriteln("AT+HTTPPARA=\"CID\",1"); | |
Serial.println("AT+HTTPPARA=\"CID\",1"); | |
delay(4000); | |
gsm.WhileSimpleRead(); | |
gsm.WhileSimpleRead(); | |
gsm.SimpleWrite("AT+HTTPPARA=\"URL\",\""); | |
Serial.print("AT+HTTPPARA=\"URL\",\""); | |
gsm.SimpleWrite(url); | |
Serial.print(url); | |
gsm.SimpleWriteln("\""); | |
Serial.println("\""); | |
delay(4000); | |
gsm.WhileSimpleRead(); | |
gsm.WhileSimpleRead(); | |
gsm.SimpleWriteln("AT+HTTPPARA=\"CONTENT\",\"application/json\""); | |
Serial.println("AT+HTTPPARA=\"CONTENT\",\"application/json\""); | |
delay(4000); | |
gsm.WhileSimpleRead(); | |
gsm.WhileSimpleRead(); | |
gsm.SimpleWrite("AT+HTTPDATA="); | |
Serial.print("AT+HTTPDATA="); | |
gsm.SimpleWrite(strlen(data)); | |
Serial.print(strlen(data)); | |
gsm.SimpleWriteln(",10000"); | |
Serial.println(",10000"); | |
delay(2000); | |
gsm.WhileSimpleRead(); | |
gsm.WhileSimpleRead(); | |
gsm.SimpleWriteln(data); | |
gsm.WhileSimpleRead(); | |
gsm.WhileSimpleRead(); | |
delay(4000); | |
gsm.SimpleWriteln("AT+HTTPACTION=1"); | |
Serial.println("AT+HTTPACTION=1"); | |
//gsm.SimpleWriteln(method); | |
delay(4000); | |
gsm.WhileSimpleRead(); | |
gsm.WhileSimpleRead(); | |
gsm.SimpleWriteln("AT+HTTPREAD"); | |
Serial.println("AT+HTTPREAD"); | |
delay(4000); | |
gsm.WhileSimpleRead(); | |
gsm.WhileSimpleRead(); | |
} | |
void sendSMS(const char *phone_number, const char *message) | |
{ | |
char fin[2] = {0x1a, '\0'}; | |
gsm.SimpleWriteln("AT+CMGF=1"); | |
delay(2000); | |
gsm.WhileSimpleRead(); | |
gsm.SimpleWrite("AT+CMGS=\""); | |
gsm.SimpleWrite(phone_number); | |
gsm.SimpleWrite("\","); | |
gsm.SimpleWriteln(phone_number[0] == '+' ? 145 : 129); | |
delay(2000); | |
gsm.WhileSimpleRead(); | |
gsm.SimpleWriteln(message); | |
delay(2000); | |
gsm.WhileSimpleRead(); | |
gsm.SimpleWrite(fin); | |
delay(2000); | |
gsm.WhileSimpleRead(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment