Hello Again All,
I thought I was home free with this project but I guess not. I built a GPS tracking device which works great! It uses SMS to send GPS data to a user who sends an SMS to the unit with a specific line (GETLOC) then the unit responds with GPS data. However, I started adding information to the SMS and everything is working fine except for the date. I managed to get the timezone adjusted correctly but the date is still a date ahead. Please look through my code and help me. I checked the data and it's all good, no errors and the stream is steady. I added a screenshot of the general idea. The correct date should be 4/10/2024. Once I get this resolved I can move onto daylight savings time (oh joy.) Thanks in advance.
PHOTO OF TEXT MESSAGE
CODE
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
SoftwareSerial GSM(11, 10);
SoftwareSerial neo(8, 9);
String textMessage;
String lampState;
String lati = "";
String longi = "";
String speed = "";
String direction = "";
String rawmonth = "";
String rawday = "";
String rawyear = "";
String rawhour = "";
String rawminute = "";
String rawsecond = "";
String weekday = "";
String num_sat = "";
String elevation = "";
#define redLed 3
#define greenLed 5
#define blueLed 6
const int relay = 12;
TinyGPSPlus gps;
const int timezone_hours = -4;
const int timezone_minutes = 0;
void setup() {
pinMode(redLed, OUTPUT);
pinMode(greenLed, OUTPUT);
pinMode(blueLed, OUTPUT);
pinMode(relay, OUTPUT);
digitalWrite(redLed, HIGH);
delay(200);
digitalWrite(redLed, LOW);
digitalWrite(greenLed, HIGH);
delay(200);
digitalWrite(greenLed, LOW);
digitalWrite(blueLed, HIGH);
delay(200);
digitalWrite(blueLed, LOW);
digitalWrite(relay, HIGH);
Serial.begin(115200);
GSM.begin(9600);
neo.begin(9600);
GSM.listen();
delay(5000);
digitalWrite(greenLed, HIGH);
Serial.print("GSM ready...\r\n");
GSM.print("AT+CMGF=1\r\n");
delay(1000);
GSM.print("AT+CNMI=2,2,0,0,0\r\n");
delay(1000);
digitalWrite(greenLed, LOW);
}
void loop() {
GSM.listen();
delay(2);
while (GSM.available() > 0) {
digitalWrite(blueLed, HIGH);
textMessage = GSM.readString();
Serial.print(textMessage);
delay(10);
digitalWrite(blueLed, LOW);
}
neo.listen();
if (textMessage.indexOf("ON") >= 0) {
String array_string[20];
string_splitting(textMessage,array_string,'\"');
String NUMBER = array_string[1];
digitalWrite(relay, LOW);
lampState = "ON";
Serial.println("Bike set to ON\r\n");
textMessage = "";
GSM.println("AT+CMGS=\"" + NUMBER + "\"\r");
delay(500);
GSM.print("Bike set to ON\r");
GSM.write(0x1a);
delay(1000);
GSM.println("AT+CMGD=1,4");
}
if (textMessage.indexOf("OFF") >= 0) {
String array_string[20];
string_splitting(textMessage,array_string,'\"');
String NUMBER = array_string[1];
digitalWrite(relay, HIGH);
lampState = "OFF";
Serial.println("Bike set to OFF\r\n");
textMessage = "";
GSM.println("AT+CMGS=\"" + NUMBER + "\"\r");
delay(500);
GSM.print("Bike set to OFF\r");
GSM.write(0x1a);
delay(1000);
GSM.println("AT+CMGD=1,4");
}
if (textMessage.indexOf("GETLOC") >= 0) {
Serial.println("_____________________________________");
smartDelay(1000);
String array_string[20];
string_splitting(textMessage,array_string,'\"'); //
String NUMBER = array_string[1];
Serial.println("GPS data Recived\r\n");
textMessage = "";
Serial.print("Speed: ");
Serial.print(gps.speed.mph());
Serial.println(" Miles Per Hour");
Serial.print("Number of Sattelites: ");
Serial.println(num_sat);
Serial.print("Raw course in degrees = ");
Serial.println(gps.course.value());
Serial.print("Elevation: ");
Serial.println(elevation);
Serial.println("_____________________________________");
GSM.println("AT+CMGS=\"" + NUMBER + "\"\r");
delay(500);
String pesan = "https://maps.google.com/?q=" + lati + "," + longi;
rawmonth = gps.date.month();
rawday = gps.date.day();
rawyear = gps.date.year();
rawhour = gps.time.hour();
rawminute = gps.time.minute();
rawsecond = gps.time.second();
speed = gps.speed.mph();
direction = gps.course.deg();
num_sat = gps.satellites.value();
elevation = gps.altitude.feet();
GSM.print("Date ");
GSM.print(rawmonth);
GSM.print("/");
GSM.print(rawday);
GSM.print("/");
GSM.println(rawyear);
GSM.print("Time ");
Get_GPS();
GSM.print(rawhour);
GSM.print(":");
GSM.print(rawminute);
GSM.print(":");
GSM.println(rawsecond);
GSM.print("Speed ");
GSM.print(speed);
GSM.println(" MPH");
GSM.print("Direction ");
GSM.println(direction);
GSM.print("Number of Sattelites ");
GSM.println(num_sat);
GSM.print("Elevation ");
GSM.print(elevation);
GSM.println(" Feet ASL");
GSM.print(pesan);
GSM.write(0x1a);
delay(1000);
GSM.println("AT+CMGD=1,4");
}
}
static void smartDelay(unsigned long ms) {
unsigned long start = millis();
do {
neo.listen();
delay(2);
while (neo.available())
gps.encode(neo.read());
} while (millis() - start < ms);
lati = String(gps.location.lat(), 8);
longi = String(gps.location.lng(), 6);
Serial.println(lati);
Serial.println(longi);
}
void string_splitting(String splitMsg, String bb[], char delimiter) {
int counter = 0 ;
for ( int i = 0 ; i < splitMsg.length() ; i ++ ) {
if ( splitMsg.charAt(i) == delimiter ) {
counter++;
}
else {
bb[counter] += splitMsg.charAt(i) ;
}
}
}
void Get_GPS(){
if (gps.time.isValid()){
int hour = gps.time.hour();
int minute = gps.time.minute();
rawsecond = gps.time.second();
abc(hour, minute);
rawhour = String(hour);
rawminute = String(minute);
rawhour = (rawhour.length() == 1) ? "0"+rawhour : rawhour;
rawminute = (rawminute.length() == 1) ? "0"+rawminute : rawminute;
}
}
void abc(int &rawhour, int &rawminute){
Serial.println("UTC = "+String(rawhour)+":"+String(rawminute)+":"+String(rawsecond));
rawminute = rawminute + timezone_minutes;
if(rawminute >= 60){
rawminute = rawminute - 60;
rawhour = rawhour + 1;
}
if(rawminute < 0){
rawminute = rawminute + 60;
rawhour = rawhour - 1;
}
rawhour = rawhour + timezone_hours;
if(rawhour >= 24){
rawhour = rawhour - 24;
}
else if(rawhour < 0){
rawhour = rawhour + 24;
}
Serial.println("Time= "+String(rawhour)+":"+String(rawminute)+":"+String(rawsecond));
}