Hi guys,
I’m trying alot to get my code work, but i fail, so I need your help or someone who cane explain some things to me.
My project is getting the data from a gps and post it to http as explaind in this page. If i uncomment everything that has to do with GSM module, i can get everything on Serial monitor, but with the delays needed by GSM, time is misplaced and instead of lat and long i get 000.0000N, etc.
I hope you understand what i mean and can explain why my arduino dose’t hold that values until the next loop or something.
I’m using a sim800H, mt3339, atmega328p with 8mhz internal bootloader. (maybe the frequency is too low?)
My code is attached below.
Serial output is like this(with gsm):
Startready
No FIX4
Start to post
"http://updates.opengps.net/index.php?imei=862950020674233&key=VnrZd391cH&data=212736.00,4815.0712N,01417.5147E,1.58,105.80,3,0.00,0.00,0.00,40215,4"
DONEE
1022
End POST
Start to post
"http://updates.opengps.net/index.php?imei=862950020674233&key=VnrZd391cH&data=0.00,0.0000N,04815.0683E,1.00,4.00,3,0.00,0.00,0.00,40215,0"
DONEE
1022
End POST
Start to post
"http://updates.opengps.net/index.php?imei=862950020674233&key=VnrZd391cH&data=212748.98,0.0000N,00.0000E,197.84,40215.00,3,0.00,0.00,0.00,40215,0"
DONEE
1022
End POST
Start to post
"http://updates.opengps.net/index.php?imei=862950020674233&key=VnrZd391cH&data=212755.00,0.0000N,00.0000E,203.14,40215.00,3,0.00,0.00,0.00,40215,1"
DONEE
1022
End POST
Maybe someone can point me to other example so i can create a string with all this values?
Anything is appreciated and thanks in advance.
#include<SoftwareSerial.h>
#include <TinyGPS.h>
SoftwareSerial sim900 (2, 3);
SoftwareSerial GPS (5, 6);
#define BUFFSIZ 190
TinyGPS gps;
const unsigned int MAX_INPUT = 165;
static unsigned int input_pos = 0;
float latitude, longitude;
float altitude, timef, kmph, knots, HDOP, COG;
char lat, lon;
boolean fix;
uint8_t fixquality, satellites;
uint32_t date;
int freeRam () {
extern int __heap_start, *__brkval;
int v;
return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
}
//void(*resetFunc) (void) = 0; //reset function
void setup() {
Serial.begin(9600);
sim900.begin(4800);
GPS.begin(9600);
Serial.print("Start");
delay(1000);
//GPS.println("$PMTK314,0,5,5,5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*2D");
GPS.println("$PMTK314,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*29");
//GPS.println("$PMTK314,0,5,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*2D");
delay(500);
GPS.println("$PMTK220,200*2C");
config_sim900();
Serial.println("ready");
}
void loop() {
read_sim900();
}
void config_sim900() {
sim900.println("AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\"");
//Serial.print(sim900.read());
//toSerial();
delay(500);
sim900.println("AT+SAPBR=3,1,\"APN\",\"webaut\"");
delay(500);
//sim900.println("AT+SAPBR=3,1,\"USER\",\"lmat\"");
delay(500);
//sim900.println("AT+SAPBR=3,1,\"PWD\",\"plus\"");
delay(500);
sim900.println("AT+SAPBR=1,1");
delay(3000);
}
void read_sim900() {
static char input_line [MAX_INPUT];
//GPS.listen();
if (GPS.available() > 0) {
//Serial.println("SIM900 OK!");
while (GPS.available () > 0) {
char inByte = GPS.read();
switch (inByte) {
case '\n': // end of text
input_line [input_pos] = 0; // terminating null byte
// terminator reached! process input_line here ...
process_data (input_line);
// reset buffer for next time
input_pos = 0;
break;
case '\r': // discard carriage return
break;
default:
// keep adding if not full ... allow for terminating null byte
if (input_pos < (MAX_INPUT - 1))
input_line [input_pos++] = inByte;
break;
} // end of switch
} // end of while incoming data
} // end of if incoming data
}
void process_data (char * data) {
//Serial.print("***Data:");
//Serial.println(data);
//delay(1000);
//Get the time
if (strstr(data, "$GPGGA")) {
char *p = data;
p = strchr(p, ',')+1;
timef = atof(p);
//Serial.print("Time:"); Serial.println(timef);
p = strchr(p, ',')+1;
latitude = atof(p);
//Serial.println(latitude, 6);
p = strchr(p, ',')+1;
if (',' != *p) {
if(p[0] == 'N') lat = 'N';
else if (p[0] == 'S') lat = 'S';
//Serial.println(lat);
}
p = strchr(p, ',')+1;
longitude = atof(p);
//Serial.println(longitude, 6); //need to work on this one to display the f
p = strchr(p, ',')+1;
if (',' != *p); {
if (p[0] == 'W') lon = 'W';
else if (p[0] == 'E') lon = 'E';
//Serial.println(lon);
}
p = strchr(p, ',')+1;
if (',' != *p)
{
fixquality = atoi(p);
//Serial.print("FixQ"); Serial.println(fixquality);
}
p = strchr(p, ',')+1;
if (',' != *p)
{
satellites = atoi(p);
//Serial.print("Sat:"); Serial.println(satellites);
}
p = strchr(p, ',')+1;
if (',' != *p)
{
HDOP = atof(p);
//Serial.print("HDOP "); Serial.println(HDOP);
}
p = strchr(p, ',')+1;
if (',' != *p)
{
altitude = atof(p);
//Serial.print("Alt:"); Serial.println(altitude);
}
p = strchr(p, ',')+1;
p = strchr(p, ',')+1;
//geoidheight = atof(p);
}
if (strstr(data, "$GPRMC")) {
char *p = data;
// get time
p = strchr(p, ',')+1;
p = strchr(p, ',')+1;
// Serial.println(p);
if (p[0] == 'A')
fix = true;
else if (p[0] == 'V')
fix = false;
else
p = strchr(p, ',')+1;
p = strchr(p, ',')+1;
p = strchr(p, ',')+1;
p = strchr(p, ',')+1;
p = strchr(p, ',')+1;
p = strchr(p, ',')+1;
p = strchr(p, ',')+1;
p = strchr(p, ',')+1;
if (',' != *p)
{
date = atof(p);
//double fulldate = atof(p);
//Serial.print("date: "); Serial.println(date);
}
}
if (strstr(data, "$GPVTG")) {
char *p = data;
p = strchr(p, ',')+1; //Get the COG
COG = atof(p);
//Serial.print("COG: "); Serial.println(COG);
p = strchr(p, ',')+1; //T not used
p = strchr(p, ',')+1; //MOG not used
p = strchr(p, ',')+1; //M not used
p = strchr(p, ',')+1;
knots = atof(p);
//Serial.print("Knots: "); Serial.println(knots);
p = strchr(p, ',')+1; //N not used
p = strchr(p, ',')+1;
kmph = atof(p);
//Serial.print("KMPH: "); Serial.println(kmph);
}
if (!fix) {
Serial.print(F("No FIX"));
Serial.println(satellites);
}
else {
Serial.println(F("Start to post"));
PostToHttp();
Serial.println(F("End POST"));
}
}
void PostToHttp() {
sim900.println("AT+HTTPINIT");
delay(500);
sim900.print("AT+HTTPPARA=\"URL\",");
sim900.print("\"http://updates.opengps.net/index.php?imei=862950020674233&key=VnrZd391cH&data=");
sim900.print(timef);
sim900.print(",");
sim900.print(latitude, 4);
sim900.print(lat);
sim900.print(",");
sim900.print("0"); //longitude needs to start with 0
sim900.print(longitude, 4);
sim900.print(lon);
sim900.print(",");
sim900.print(HDOP); //hdop
sim900.print(",");
sim900.print(altitude); // Altitude or freeRam??
sim900.print(",");
sim900.print("3"); //3 = 3D fix
sim900.print(",");
sim900.print(COG);
sim900.print(",");
sim900.print(kmph);
sim900.print(",");
sim900.print(knots);
sim900.print(",");
sim900.print(date);
sim900.print(",");
sim900.print(satellites); //Sattelites
sim900.println("\"");
Serial.print("\"http://updates.opengps.net/index.php?imei=862950020674233&key=VnrZd391cH&data=");
Serial.print(timef);
Serial.print(",");
Serial.print(latitude, 4);
Serial.print(lat);
Serial.print(",");
Serial.print("0"); //longitude needs to start with 0
Serial.print(longitude, 4);
Serial.print(lon);
Serial.print(",");
Serial.print(HDOP); //hdop
Serial.print(",");
Serial.print(altitude); // Altitude or freeRam??
Serial.print(",");
Serial.print("3"); //3 = 3D fix
Serial.print(",");
Serial.print(COG);
Serial.print(",");
Serial.print(kmph);
Serial.print(",");
Serial.print(knots);
Serial.print(",");
Serial.print(date);
Serial.print(",");
Serial.print(satellites); //Sattelites
Serial.println("\"");
sim900.println("AT+HTTPACTION=0");
delay(2000);
sim900.println("AT+HTTPREAD");
delay(2000);
sim900.println("AT+HTTTERM");
Serial.println(F("DONEE"));
Serial.println(freeRam());
}
temp.ino (6.75 KB)