Skip to content

Instantly share code, notes, and snippets.

@almeida1492
Created October 21, 2019 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save almeida1492/50f651f8cc18727ac08156b69bfe22b1 to your computer and use it in GitHub Desktop.
Save almeida1492/50f651f8cc18727ac08156b69bfe22b1 to your computer and use it in GitHub Desktop.
GPS-GPRS-tracker-sensor
#include <SoftwareSerial.h>
SoftwareSerial SIM900(7, 8);
String testJSON="{ \"coordinateX\" : 0406, \"coordinateY\" : 1992, }";
void setup()
{
Serial.begin(19200);
//Abre comunicação entre Arduino e SIM900
SIM900.begin(19200);
Serial.println("Inicializando...");
delay(1000);
handshaking();
connectGPRS();
postData();
delay(1000);
}
void loop()
{
updateSerial();
}
void updateSerial()
{
delay(500);
while (Serial.available())
{
SIM900.write(Serial.read());
}
while(SIM900.available())
{
Serial.write(SIM900.read());
}
}
void handshaking(){
SIM900.println("AT"); //Handshaking com SIM900
delay(1000);
updateSerial();
SIM900.println("AT+CMEE=1");
delay(1000);
updateSerial();
SIM900.println("AT+CSQ"); //Qualidade de sinal – variação enter 0-31, 31 é o melhor
delay(1000);
updateSerial();
SIM900.println("AT+CCID"); //Lê informação de cartão SIM para confirmar que está inserido
delay(1000);
updateSerial();
SIM900.println("AT+CREG?"); //Confirma registro na rede
delay(1000);
updateSerial();
}
//Abre conexão GPRS
void connectGPRS(){
SIM900.println("AT+SAPBR=3,1,\"Contype\",\"GPRS\"");
delay(1000);
updateSerial();
SIM900.println("AT+SAPBR=3,1,\"APN\",\"www\"");//APN
delay(1000);
updateSerial();
SIM900.println("AT+SAPBR=1,1");
delay(1000);
updateSerial();
SIM900.println("AT+SAPBR=2,1");
delay(1000);
updateSerial();
}
//Performa chamada HTTP POST
void postData(){
SIM900.println("AT+HTTPINIT");
delay(1000);
updateSerial();
SIM900.println("AT+HTTPPARA=\"CID\",1");
delay(1000);
updateSerial();
SIM900.println("AT+HTTPPARA=\"URL\",\"https://uenf-tracker-api.herokuapp.com/locations\"");
delay(1000);
updateSerial();
SIM900.println("AT+HTTPPARA=\"Content-Type\",\"application/json\"");
delay(1000);
updateSerial();
SIM900.println("AT+HTTPDATA=" + String(testJSON.length()) + ",20000");
delay(1000);
updateSerial;
SIM900.println(testJSON);
delay(1000);
updateSerial;
SIM900.println("AT+HTTPACTION=1");
delay(10000);
updateSerial();
SIM900.println("AT+HTTPREAD");
delay(1000);
updateSerial();
SIM900.println("AT+HTTPTERM");
delay(1000);
updateSerial();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment