Forum >Gps tracker with my server
Gps tracker with my server

Hello DFrobot, I tried to create a crawler via arduino and the sim808 module, before writing the code I tested the module with the program SSCOM32 by means of AT commands, it has been tested with HTTPARA = URL, xxx and the server mounted on my Home (linux server) receives perfectly .. then from there I wanted to set the code, as it costs me to still program at a higher level then I want to know if you can help me to complete the code and let it work ..
-----------------------------------------------------------------------------------------------------------
I explain how it should be
Depending on the server to send the URL must have this format
make_clickable_callback(MAGIC_URL_FULL, '
', 'Http://my', '', ' class="postlink"') ip xxxx:5055/?id=123456&lat=xxxx&lon=xxx×tamp=xxx&hdop=xx&altitude=xxx&speed=xxx
Where:
my ip - server address or domain name (e.g., it can be your server public IP address)
123456 - your device unique identifier (e.g., IMEI number)
xxx numbers..
-----------------------------------------------------------------------------------
Now I'll put the code
In this case I do not want to use a php script I just want to store the data and set the URL and send it directly to my server where I have the database and the only one then is responsible for mounting it on the map .. I just want to solve that and another issue Is that takes in NMEA and lat and long must be in GRADES, I would be very grateful if you help me with this project THANKS
CODE :
#include<SoftwareSerial.h>
extern uint8_t SmallFont[];
#define rxPin 7
#define txPin 8
int powersoft = 9;
SoftwareSerial mySerial(rxPin, txPin);
char inChar = 0;
int8_t answer;
char aux_str[100];
char url[] = "my server:7070";
char frame[200];
char response[100];
char latitude[15];
char longitude[15];
char altitude[16];
char date[24];
char TTFF[3];
char satellites[3];
char speedOTG[10];
char course[15];
//**********************************************************************************************************
void setup(){
mySerial.begin(9600);
Serial.begin(9600);
pinMode(powersoft,OUTPUT);
Serial.println("Starting...");
power_on();
// starts the GPS and waits for signal
start_GPS();
while (sendATcommand("AT+CREG?", "+CREG: 0,1", 2000) == 0);
sendATcommand("AT+SAPBR=3,1,\"Contype\",\"GPRS\"", "OK", 2000);
sendATcommand("AT+SAPBR=3,1,\"APN\",\"internet.tigo.py\"", "OK", 2000);
sendATcommand("AT+SAPBR=3,1,\"USER\",\"\"", "OK", 2000);
sendATcommand("AT+SAPBR=3,1,\"PWD\",\"\"", "OK", 2000);
// gets the GPRS bearer
sendATcommand("AT+SAPBR=1,1", "OK", 20000);
delay(5000);
// while (sendATcommand("AT+SAPBR=1,1", "ERROR", 20000) == 0)
// {
// sendATcommand("AT+SAPBR=1,0", "OK", 20000);
// return power_on;
//}
}
void loop(){
// gets GPS data
get_GPS();
// sends GPS data to the script
send_HTTP();
//sendNMEALocation("81596199",frame);
//delay(3000);
}
//**************************************************************************************
void power_on(){
uint8_t answer=0;
// checks if the module is started
answer = sendATcommand("AT", "OK", 2000);
if (answer == 0)
{
// waits for an answer from the module
while(answer == 0){
// Send AT every two seconds and wait for the answer
answer = sendATcommand("AT", "OK", 2000);
}
}
}
//************************************************************************************
int8_t start_GPS(){
unsigned long previous;
sendATcommand("AT+CIPSHUT", "OK", 2000);
previous = millis();
// starts the GPS
while(sendATcommand("AT+CGPSPWR=1", "OK", 2000)==0);
//while(sendATcommand("AT+CGPSRST=0", "OK", 2000)==0);
delay(2000);
// waits for fix GPS
while(( (sendATcommand("AT+CGPSSTATUS?", "2D Fix", 5000) ||
sendATcommand("AT+CGPSSTATUS?", "3D Fix", 5000)) == 0 ) );
return 1;
}
int8_t get_GPS(){
char * auxChar;
// request Basic string
sendATcommand("AT+CGPSINF=0", "OK", 8000);
auxChar = strstr(response, "+CGPSINF:");
if (auxChar != NULL)
{
// Parses the string
memset(longitude, '\0', 15);
memset(latitude, '\0', 15);
memset(altitude, '\0', 16);
memset(date, '\0', 24);
// memset(TTFF, '\0', 3);
//memset(satellites, '\0', 3);
memset(speed, '\0', 10);
// memset(course, '\0', 15);
strcpy (response, auxChar);
Serial.println(response);
strtok(response, ",");
strcpy(longitude,strtok(NULL, ",")); // Gets longitude
strcpy(latitude,strtok(NULL, ",")); // Gets latitude
strcpy(altitude,strtok(NULL, ",")); // Gets altitude
strcpy(date,strtok(NULL, ",")); // Gets date
//strcpy(TTFF,strtok(NULL, ","));
//strcpy(satellites,strtok(NULL, ",")); // Gets satellites
strcpy(speed,strtok(NULL, ",")); // Gets speed over ground. Unit is knots.
//strcpy(course,strtok(NULL, "\r")); // Gets course
answer = 1;
}
else
answer = 0;
return answer;
}
void sendNMEALocation(char * cellPhoneNumber, char * message)
{
char ctrlZString[2];
char sendSMSString[100];
// Started sendNMEALocation.
memset(ctrlZString, '\0', 2);
ctrlZString[0] = 26;
memset(sendSMSString, '\0', 100);
sprintf(sendSMSString,"AT+CMGS=\"%s\"",cellPhoneNumber);
// request Basic string
sendATcommand(sendSMSString, ">", 2000);
mySerial.println(message);
sendATcommand(ctrlZString, "OK", 6000);
//Ended sendNMEALocation.
}
void send_HTTP(){
// Initializes HTTP service
answer = sendATcommand("AT+HTTPINIT", "OK", 10000);
if (answer == 1)
{
// Sets CID parameter
answer = sendATcommand("AT+HTTPPARA=\"CID\",1", "OK", 5000);
if (answer == 1)
{
// Sets url
char url[] = "my ip xxxx :5055";
memset(aux_str, '\0', 100);
sprintf(aux_str, "AT+HTTPPARA=\"URL\",\"%s", url);
//limpar antesLIMPAR ANTES
mySerial.print(aux_str);
Serial.println(aux_str);
memset(frame, '\0', 200);
sprintf(frame, "/?id=12345&lat=0%s&lon=0%s×tamp=0000%s&altitude=00%s&hdop=2%s&speed=00%s", latitude, longitude, date, altitude, speed);
Serial.println(frame);
mySerial.print(frame);
answer = sendATcommand("\"", "OK", 5000);
if (answer == 1)
{
// Starts GET action
answer = sendATcommand("AT+HTTPACTION=0", "+HTTPACTION: 0,200", 30000);
if (answer == 1)
{
Serial.println(F("Done!"));
}
else
{
Serial.println(F("Error getting url"));
}
}
else
{
Serial.println(F("Error setting the url"));
}
}
else
{
Serial.println(F("Error setting the CID"));
}
}
else
{
Serial.println(F("Error initializating"));
}
sendATcommand("AT+HTTPTERM", "OK", 5000);
}
int8_t sendATcommand(char* ATcommand, char* expected_answer1, unsigned int timeout){
uint8_t x=0, answer=0;
unsigned long previous;
char readVar[100];
char * auxChar;
memset(response, '\0', 100); // Initialize the string
memset(readVar, '\0', 100); // Initialize the string
while( mySerial.available() > 0) mySerial.read(); // Clean the input buffer
while( Serial.available() > 0) Serial.read(); // Clean the input buffer
mySerial.write(ATcommand); // Send the AT command
mySerial.write("\r\n\r\n"); // Send enter
Serial.println(ATcommand);
x = 0;
previous = millis();
// this loop waits for the answer
do{
if(mySerial.available() != 0){
readVar[x] = mySerial.read();
x++;
// check if the desired answer is in the response of the module
auxChar = strstr(readVar, expected_answer1);
if (auxChar != NULL)
{
if( strstr(readVar, "+CGPSINF:") == NULL)
strcpy (response, auxChar);
else
strcpy (response, readVar);
Serial.println(response);
answer = 1;
}
}
// Waits for the asnwer with time out
}
while((answer == 0) && ((millis() - previous) < timeout));
if(auxChar == NULL)
Serial.println(readVar);
return answer;
}
-----------------------------------------------------------------------------------------------------------
I explain how it should be
Depending on the server to send the URL must have this format
make_clickable_callback(MAGIC_URL_FULL, '
', 'Http://my', '', ' class="postlink"') ip xxxx:5055/?id=123456&lat=xxxx&lon=xxx×tamp=xxx&hdop=xx&altitude=xxx&speed=xxx
Where:
my ip - server address or domain name (e.g., it can be your server public IP address)
123456 - your device unique identifier (e.g., IMEI number)
xxx numbers..
-----------------------------------------------------------------------------------
Now I'll put the code
In this case I do not want to use a php script I just want to store the data and set the URL and send it directly to my server where I have the database and the only one then is responsible for mounting it on the map .. I just want to solve that and another issue Is that takes in NMEA and lat and long must be in GRADES, I would be very grateful if you help me with this project THANKS
CODE :
#include<SoftwareSerial.h>
extern uint8_t SmallFont[];
#define rxPin 7
#define txPin 8
int powersoft = 9;
SoftwareSerial mySerial(rxPin, txPin);
char inChar = 0;
int8_t answer;
char aux_str[100];
char url[] = "my server:7070";
char frame[200];
char response[100];
char latitude[15];
char longitude[15];
char altitude[16];
char date[24];
char TTFF[3];
char satellites[3];
char speedOTG[10];
char course[15];
//**********************************************************************************************************
void setup(){
mySerial.begin(9600);
Serial.begin(9600);
pinMode(powersoft,OUTPUT);
Serial.println("Starting...");
power_on();
// starts the GPS and waits for signal
start_GPS();
while (sendATcommand("AT+CREG?", "+CREG: 0,1", 2000) == 0);
sendATcommand("AT+SAPBR=3,1,\"Contype\",\"GPRS\"", "OK", 2000);
sendATcommand("AT+SAPBR=3,1,\"APN\",\"internet.tigo.py\"", "OK", 2000);
sendATcommand("AT+SAPBR=3,1,\"USER\",\"\"", "OK", 2000);
sendATcommand("AT+SAPBR=3,1,\"PWD\",\"\"", "OK", 2000);
// gets the GPRS bearer
sendATcommand("AT+SAPBR=1,1", "OK", 20000);
delay(5000);
// while (sendATcommand("AT+SAPBR=1,1", "ERROR", 20000) == 0)
// {
// sendATcommand("AT+SAPBR=1,0", "OK", 20000);
// return power_on;
//}
}
void loop(){
// gets GPS data
get_GPS();
// sends GPS data to the script
send_HTTP();
//sendNMEALocation("81596199",frame);
//delay(3000);
}
//**************************************************************************************
void power_on(){
uint8_t answer=0;
// checks if the module is started
answer = sendATcommand("AT", "OK", 2000);
if (answer == 0)
{
// waits for an answer from the module
while(answer == 0){
// Send AT every two seconds and wait for the answer
answer = sendATcommand("AT", "OK", 2000);
}
}
}
//************************************************************************************
int8_t start_GPS(){
unsigned long previous;
sendATcommand("AT+CIPSHUT", "OK", 2000);
previous = millis();
// starts the GPS
while(sendATcommand("AT+CGPSPWR=1", "OK", 2000)==0);
//while(sendATcommand("AT+CGPSRST=0", "OK", 2000)==0);
delay(2000);
// waits for fix GPS
while(( (sendATcommand("AT+CGPSSTATUS?", "2D Fix", 5000) ||
sendATcommand("AT+CGPSSTATUS?", "3D Fix", 5000)) == 0 ) );
return 1;
}
int8_t get_GPS(){
char * auxChar;
// request Basic string
sendATcommand("AT+CGPSINF=0", "OK", 8000);
auxChar = strstr(response, "+CGPSINF:");
if (auxChar != NULL)
{
// Parses the string
memset(longitude, '\0', 15);
memset(latitude, '\0', 15);
memset(altitude, '\0', 16);
memset(date, '\0', 24);
// memset(TTFF, '\0', 3);
//memset(satellites, '\0', 3);
memset(speed, '\0', 10);
// memset(course, '\0', 15);
strcpy (response, auxChar);
Serial.println(response);
strtok(response, ",");
strcpy(longitude,strtok(NULL, ",")); // Gets longitude
strcpy(latitude,strtok(NULL, ",")); // Gets latitude
strcpy(altitude,strtok(NULL, ",")); // Gets altitude
strcpy(date,strtok(NULL, ",")); // Gets date
//strcpy(TTFF,strtok(NULL, ","));
//strcpy(satellites,strtok(NULL, ",")); // Gets satellites
strcpy(speed,strtok(NULL, ",")); // Gets speed over ground. Unit is knots.
//strcpy(course,strtok(NULL, "\r")); // Gets course
answer = 1;
}
else
answer = 0;
return answer;
}
void sendNMEALocation(char * cellPhoneNumber, char * message)
{
char ctrlZString[2];
char sendSMSString[100];
// Started sendNMEALocation.
memset(ctrlZString, '\0', 2);
ctrlZString[0] = 26;
memset(sendSMSString, '\0', 100);
sprintf(sendSMSString,"AT+CMGS=\"%s\"",cellPhoneNumber);
// request Basic string
sendATcommand(sendSMSString, ">", 2000);
mySerial.println(message);
sendATcommand(ctrlZString, "OK", 6000);
//Ended sendNMEALocation.
}
void send_HTTP(){
// Initializes HTTP service
answer = sendATcommand("AT+HTTPINIT", "OK", 10000);
if (answer == 1)
{
// Sets CID parameter
answer = sendATcommand("AT+HTTPPARA=\"CID\",1", "OK", 5000);
if (answer == 1)
{
// Sets url
char url[] = "my ip xxxx :5055";
memset(aux_str, '\0', 100);
sprintf(aux_str, "AT+HTTPPARA=\"URL\",\"%s", url);
//limpar antesLIMPAR ANTES
mySerial.print(aux_str);
Serial.println(aux_str);
memset(frame, '\0', 200);
sprintf(frame, "/?id=12345&lat=0%s&lon=0%s×tamp=0000%s&altitude=00%s&hdop=2%s&speed=00%s", latitude, longitude, date, altitude, speed);
Serial.println(frame);
mySerial.print(frame);
answer = sendATcommand("\"", "OK", 5000);
if (answer == 1)
{
// Starts GET action
answer = sendATcommand("AT+HTTPACTION=0", "+HTTPACTION: 0,200", 30000);
if (answer == 1)
{
Serial.println(F("Done!"));
}
else
{
Serial.println(F("Error getting url"));
}
}
else
{
Serial.println(F("Error setting the url"));
}
}
else
{
Serial.println(F("Error setting the CID"));
}
}
else
{
Serial.println(F("Error initializating"));
}
sendATcommand("AT+HTTPTERM", "OK", 5000);
}
int8_t sendATcommand(char* ATcommand, char* expected_answer1, unsigned int timeout){
uint8_t x=0, answer=0;
unsigned long previous;
char readVar[100];
char * auxChar;
memset(response, '\0', 100); // Initialize the string
memset(readVar, '\0', 100); // Initialize the string
while( mySerial.available() > 0) mySerial.read(); // Clean the input buffer
while( Serial.available() > 0) Serial.read(); // Clean the input buffer
mySerial.write(ATcommand); // Send the AT command
mySerial.write("\r\n\r\n"); // Send enter
Serial.println(ATcommand);
x = 0;
previous = millis();
// this loop waits for the answer
do{
if(mySerial.available() != 0){
readVar[x] = mySerial.read();
x++;
// check if the desired answer is in the response of the module
auxChar = strstr(readVar, expected_answer1);
if (auxChar != NULL)
{
if( strstr(readVar, "+CGPSINF:") == NULL)
strcpy (response, auxChar);
else
strcpy (response, readVar);
Serial.println(response);
answer = 1;
}
}
// Waits for the asnwer with time out
}
while((answer == 0) && ((millis() - previous) < timeout));
if(auxChar == NULL)
Serial.println(readVar);
return answer;
}