iv been having a problem with my thesis project and I found out that when I put the gps codes together with a timer code the gps wont send the coordinates.
#include <Streamers.h>
#include <NMEAGPS.h>
#include <NeoSWSerial.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#define deviceid 1
#define j9 5
#define NEO_RX 10
#define NEO_TX 11
NeoSWSerial neogps_Serial(NEO_RX, NEO_TX); // RX, TX //GPS
LiquidCrystal_I2C lcd(0x27, 16, 2);
//GPS Variables
static NMEAGPS gps;
static gps_fix fix;
//LCD Variables
String sim808data;
int runtime;
int countminutes;
int countseconds = 0;
bool overdue = false;
unsigned long previousMillis;
bool runme = false;
bool getgps = false;
void setup() {
Wire.begin();
//Sim808 Switch Bypass
pinMode(j9, OUTPUT);
digitalWrite(j9, HIGH);
delay(1000);
digitalWrite(j9, LOW);
//Start Serial
Serial.begin(9600);
while (!Serial);
neogps_Serial.begin(19200);
delay(1000);
//Serial.println(F("Serial Check."));
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("STARTING DEVICE!");
delay(1000);
clear_Sim();
Serial.println(F("\nSMS Cleared!"));
lcd.clear();
lcd.setCursor(6, 0);
lcd.print("SMS");
lcd.setCursor(4, 1);
lcd.print("CLEARED!");
neogps_Serial.print("AT+CGNSPWR=1\r\n");
delay(100);
neogps_Serial.print("AT+CGNSTST=1\r\n");
delay(100);
neogps_Serial.print("AT+CSCS=\"GSM\"\r\n");
neogps_Serial.print("AT+CMGF=1\r\n");
neogps_Serial.print("AT+CNMI=2,2,0,0,0\r\n");
delay(5000);
Serial.println(F("GPS Check."));
lcd.clear();
lcd.setCursor(2, 0);
lcd.print("SYSTEM READY");
Serial.println(F("Device Ready to Use!"));
}
//Loop Declarations
uint32_t loopCount = 0;
uint16_t gpsSeconds;
uint16_t lastTrackSent;
//Milli_Codes
unsigned long time_loc1 = 0;
unsigned long time_send1 = 0;
unsigned long time_loop1 = 0;
unsigned long time_send5 = 0;
//Milli Intervals
const int D_1000 = 1000;
void loop() {
//proc_LOCATION();
proc_SIM808();
// proc_TIMER();
}
void clear_Sim() {
neogps_Serial.print("AT+CMGD=\"Del All\"");
delay(300);
while (neogps_Serial.available())
Serial.write(neogps_Serial.read());
}
void send_SMS(char* data) {
neogps_Serial.println("AT+CMGF=1\r\n");
delay(500);
neogps_Serial.println("AT+CMGS=\"+63XXXXXXX\"");
delay(100);
neogps_Serial.println(data);
delay(100);
neogps_Serial.println((char)26);
delay(100);
time_send5 = millis();
neogps_Serial.println();
delay(500);
}
void proc_SIM808() {
if (neogps_Serial.available()) {
sim808data = neogps_Serial.readString();
if (sim808data.indexOf("start") > 0) {
int value = sim808data.indexOf("start");
runtime = sim808data.substring(value + 5).toInt();
countseconds = 0;
runme = true;
}
if (sim808data.indexOf("stop") > 0) {
if (overdue)
{
lcd.setCursor(0, 1);
lcd.print("--DEVICE STOP!--");
} else {
lcd.setCursor(0, 1);
lcd.print("--DEVICE STOP!--");
}
runme = false;
overdue = false;
}
if (sim808data.indexOf("gps") > 0) { // Theproblem is here
proc_LOCATION();
}
while (Serial.available()) {
Serial.read();
}
while (neogps_Serial.available()) {
neogps_Serial.read();
}
}
}
void proc_LOCATION() {
while (gps.available(neogps_Serial)) {
fix = gps.read();
gpsSeconds++;
//Serial.print(F("Position: "));
if (fix.valid.location && ((gpsSeconds - lastTrackSent) > 5)) {
char floatlat[10];
char floatlon[11];
char message[30] = {0};
float dlat = fix.latitude();
float dlon = fix.longitude();
dtostrf(dlat, 9, 6, floatlat);
dtostrf(dlon, 10, 6, floatlon);
//Serial.println(floatlat);
//Serial.println(floatlon);
//Transfer Data to dataLocation
strncat(message, floatlat, 30);
strncat(message, ";", 30);
strncat(message, floatlon, 30);
send_SMS(message);
lastTrackSent = gpsSeconds; // reset the timer
} else {
//Serial.println(F("Delaying Response"));
proc_SIM808();
}
}
}
void proc_TIMER() {
if (runme) {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= 1000 ) {
lcd.clear();
if (runtime <= 0 && overdue) {
if (countseconds == 59)
{
runtime--;
countseconds = 0;
}
lcd.setCursor(0, 1);
lcd.print("Cost:");
lcd.print((runtime * -1));
countseconds++;
} else if (runtime >= 0) {
if (countseconds == 0) {
runtime--;
countseconds = 59;
if (runtime == -1) {
overdue = true;
runtime = 0;
countseconds = 1;
}
}
countseconds--;
}
lcd.setCursor(5, 0);
if (runtime == 0 && overdue) {
lcd.print("-" + runtime);
}
lcd.print(runtime);
lcd.print(":");
lcd.print(countseconds);
previousMillis = currentMillis;
}
}
}
this is my full code
and this is the part where I get the problem
void proc_SIM808() {
if (neogps_Serial.available()) {
sim808data = neogps_Serial.readString();
if (sim808data.indexOf("start") > 0) {
int value = sim808data.indexOf("start");
runtime = sim808data.substring(value + 5).toInt();
countseconds = 0;
runme = true;
}
if (sim808data.indexOf("stop") > 0) {
if (overdue)
{
lcd.setCursor(0, 1);
lcd.print("--DEVICE STOP!--");
} else {
lcd.setCursor(0, 1);
lcd.print("--DEVICE STOP!--");
}
runme = false;
overdue = false;
}
if (sim808data.indexOf("gps") > 0) { // this part
proc_LOCATION();
}
while (Serial.available()) {
Serial.read();
}
while (neogps_Serial.available()) {
neogps_Serial.read();
}
}
}
EDITED:
PROBLEM 1 (SOLVED): im thinking that maybe its imposible to send the coordinates together with my timer codes if I call the whole gps codes? is there a way that I can call the coordinates from the GPS Codes without calling the whole gps codes? I really appreciate any help from you guys its been days since I have this problem.