const int chipSelect = 4;
int8_t answer;
int onModulePin = 2;
char aux_str[100];
int x = 0;
char N_S, W_E;
char url[] = "demo.aiotm.in";
char frame[300];
char latitude[15];
char longitude[15];
char latitude1[15];
char longitude1[15];
char altitude[6];
char date[16];
char time[7];
char satellites[3];
char speedOTG[10];
char course[10];
char mobnum[13];
void setup() {
pinMode(onModulePin, OUTPUT);
Serial.begin(9600);
power_on();
power_onGPS();
delay(5000);
digitalWrite(onModulePin, HIGH);
delay(3000);
digitalWrite(onModulePin, LOW);
while ( (sendATcommand("AT+CREG?", "+CREG: 0,1", 1000) || sendATcommand("AT+CREG?", "+CREG: 0,5", 1000)) == 0 );
sendATcommand("AT+CLIP=1", "OK", 1000);
while ( start_GPS() == 0);
///////////////////////////////////FIFTH//////////////////////////////
while (sendATcommand("AT+CREG?", "+CREG: 0,1", 2000) == 0);
sendATcommand("AT+CGATT=1", "OK", 2000);
sendATcommand("AT+SAPBR=3,1,\"APN\",\"PPINTERNET.GDSP\"", "OK", 2000);
sendATcommand("AT+SAPBR=1,1", "OK", 2000);
//sendATcommand("AT+HTTPINIT", "OK", 2000);
//sendATcommand("AT+HTTPPARA=\"URL\",\"http://www.google.com\"", "OK", 5000);
//sendATcommand("AT+HTTPACTION=0", "+HTTPACTION:0,200", 30000);
//sendATcommand("AT+HTTPREAD=?", "OK", 2000);
//Serial.println("AT+HTTPREAD=1,1000");
//delay(5000);
//Serial.println("AT+HTTPTERM");
}
///////////////////////////sixth//////////////////////////////////////
void loop() {
get_GPS();
delay(2000);
send_HTTP();
delay(3000);
}
////////////////////////////////////////////////////////////////
///////////////////first///////////////////////////////////////
/*******
* g byte: 8
* *******/
void power_on() {
uint8_t answer = 0;
digitalWrite(onModulePin, HIGH);
delay(3000);
digitalWrite(onModulePin, LOW);
while (answer == 0) { // Send AT every two seconds and wait for the answer
answer = sendATcommand("AT", "OK", 2000);
}
}
int8_t sendATcommand(char* ATcommand, char* expected_answer, unsigned int timeout) {
uint8_t x = 0, answer = 0;
char response[200];
unsigned long previous;
memset(response, '\0', 100); // Initialice the string
delay(100);
while ( Serial.available() > 0) Serial.read(); // Clean the input buffer
if (ATcommand[0] != '\0')
{
Serial.println(ATcommand); // Send the AT command
}
x = 0;
previous = millis();
// this loop waits for the answer
do {
if (Serial.available() != 0) { // if there are data in the UART input buffer, reads it and checks for the asnwer
response[x] = Serial.read();
//Serial.print(response[x]);
x++;
if (x > 150)
{
for (int i = 0; i < 200; i++)
response[i] = 0;
x = 0;
}
if (strstr(response, expected_answer) != NULL) // check if the desired answer (OK) is in the response of the module
{
answer = 1;
}
}
} while ((answer == 0) && ((millis() - previous) < timeout)); // Waits for the asnwer with time out
for (int i = 0; i < 200; i++)
response[i] = 0;
return answer;
}
///////////////////////////////////////////////////////////////
////////////////////////second////////////////////////////////
/***************************************************************
* G var : 4 byte
* ***********************************************************/
void power_onGPS() {
uint8_t answer = 0;
// checks if the module is started
answer = sendATcommand("AT", "OK", 2000);
if (answer == 0)
{
// power on pulse
digitalWrite(onModulePin, HIGH);
delay(3000);
digitalWrite(onModulePin, LOW);
// 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);
}
}
}
//////////////////////////////////////////////////////////////
///////////////////////////////fourth//////////////////////////////
/**********************************************************
* G var : millis,4 byte
*************************************************************/
int8_t start_GPS() {
unsigned long previous;
previous = millis();
// starts the GPS
sendATcommand("AT+CGPSPWR=1", "OK", 2000);
sendATcommand("AT+CGPSRST=0", "OK", 2000);
sendATcommand("AT+CGPSINF=0", "OK", 2000);
// waits for fix GPS
while (( (sendATcommand("AT+CGPSSTATUS?", "2D Fix", 5000) ||
sendATcommand("AT+CGPSSTATUS?", "3D Fix", 5000)) == 0 ) &&
((millis() - previous) < 90000));
if ((millis() - previous) < 90000)
{
return 1;
}
else
{
return 0;
}
}
/////////////////////////////////////////////////////////////////////////
//////////////////////////////////seventh//////////////////////////////////
int8_t get_GPS() {
int crct;
int8_t counter, answer;
long previous;
previous = millis();
// First get the NMEA string
// Clean the input buffer
while ( Serial.available() > 0) Serial.read();
// request Basic string
while (( (sendATcommand("AT+CGPSSTATUS?", "2D Fix", 2000) ||
sendATcommand("AT+CGPSSTATUS?", "3D Fix", 2000)) == 0 ) &&
((millis() - previous) < 50000));
if ((millis() - previous) < 50000)
{
crct = 1;
}
else
{
crct = 0;
}
sendATcommand("AT+CGPSINF=0", "AT+CGPSINF=0\r\n\r\n", 2000);
counter = 0;
answer = 0;
memset(frame, '\0', 300); // Initialize the string
previous = millis();
// this loop waits for the NMEA string
do {
if (Serial.available() != 0) {
frame[counter] = Serial.read();
counter++;
// check if the desired answer is in the response of the module
if (strstr(frame, "OK") != NULL)
{
answer = 1;
}
}
// Waits for the asnwer with time out
}
while ((answer == 0) && ((millis() - previous) < 2000));
frame[counter - 3] = '\0';
// Parses the string
strtok(frame, ",");
strcpy(longitude, strtok(NULL, ",")); // Gets longitude
strcpy(latitude, strtok(NULL, ",")); // Gets latitude
strcpy(altitude, strtok(NULL, ".")); // Gets altitude
strtok(NULL, ",");
strcpy(date, strtok(NULL, ".")); // Gets date
strtok(NULL, ",");
strtok(NULL, ",");
strcpy(satellites, strtok(NULL, ",")); // Gets satellites
strcpy(speedOTG, strtok(NULL, ",")); // Gets speed over ground. Unit is knots.
strcpy(course, strtok(NULL, "\r")); // Gets course
Serial.println(latitude);
Serial.println(longitude);
float degg;
degg = atof(latitude);
degg /= 100;
degg += 0.037085;
Serial.println(degg);
memset(latitude, '\0', 15);
dtostrf(degg, 10, 8, latitude1);
sprintf(latitude, "%s", latitude1);
Serial.println(latitude);
degg = atof(longitude);
degg /= 100;
degg += 0.195927;
Serial.println(degg);
memset(longitude, '\0', 15);
dtostrf(degg, 10, 8, latitude1);
sprintf(longitude, "%s", latitude1);
Serial.println(longitude);
}
////////////////////eighth///////////////////////////////////////
void send_HTTP() {
uint8_t answer = 0;
// 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)
{
sprintf(aux_str, "AT+HTTPPARA=\"URL\",\"http://%s:8080/GPSDataFetcher/fetch.html?", url);
sprintf(frame, "%svisor=false&latitude=%s&longitude=%s&altitude=%s&time=%s&satellites=%s&speedOTG=%s&course=%s",
aux_str, latitude, longitude, altitude, date, satellites, speedOTG, course);
Serial.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 gtng url"));
}
sendATcommand("AT+SAPBR=0,1", "OK", 2000);
sendATcommand("AT+SAPBR=1,1", "OK", 2000);
}
else
{
Serial.println(F("url Error"));
}
}
else
{
Serial.println(F("CID error"));
}
}
else
{
Serial.println(F("Error init"));
sendATcommand("AT+SAPBR=0,1", "OK", 3000);
sendATcommand("AT+SAPBR=1,1", "OK", 5000);
}
sendATcommand("AT+HTTPTERM", "OK", 5000);
memset(aux_str, '\0', 100);
}
Did you have a question?
Have you done a sanity check on the size of the arrays you have allocated? aux_str and frame seem excessively large to me.
thankx problem solved