let's try to get the GPS working first by testing this code, I haven't tested it though
#include <TinyGPS++.h>
#define neo Serial
TinyGPSPlus gps;
String lati = " ";
String longi = " ";
void setup() {
neo.begin(9600);
}
void loop() {
if (neo.available()) {
while (neo.available() > 0) {
gps.encode(neo.read());
if (gps.location.isUpdated()) {
lati = String(gps.location.lat(), 8);
longi = String(gps.location.lng(), 6);
}
}
}
static uint32_t prev = 0;
if ( millis() - prev >= 500 ) {
prev = millis();
Serial.println("lati: "+lati);
Serial.println("longi: "+longi);
}
}
lati and longi are in the serial monitor but no coordinates. Just to verify that the GPS is working, I uploaded the old code and tested it and it worked that way. But then it only sends to me. So should I have the GPS modules TX and RX pins plugged into the Arduino Nano's RX and TX pins?
@KASSIMSAMJI YOU GOT IT WORKING!!! Thanks a bunch! I moved the GPS module closer to a window and BAM! It worked. Thank you and everyone else who helped me with this issue. I marked the solution box up in the prior code you sent. Thank you again.
Just in case anyone is interested, here is the final code. I had to put all the GSM Listening functions back in, as well as move the pins from the RX and TX back to pins 8 & 9 on the Arduino. But the way @KASSIMSAMJI and @mfusco setup the code was perfect. Thanks again to everyone, and enjoy this project.
CODE
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
SoftwareSerial GSM(11, 10);
SoftwareSerial neo(8, 9);
String textMessage;
String lampState;
String lati = "";
String longi = "";
#define redLed 3
#define greenLed 5
#define blueLed 6
const int relay = 12;
TinyGPSPlus gps;
void setup() {
pinMode(redLed, OUTPUT);
pinMode(greenLed, OUTPUT);
pinMode(blueLed, OUTPUT);
pinMode(relay, OUTPUT);
digitalWrite(redLed, HIGH);
delay(85);
digitalWrite(redLed, LOW);
digitalWrite(greenLed, HIGH);
delay(85);
digitalWrite(greenLed, LOW);
digitalWrite(blueLed, HIGH);
delay(85);
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("GETLOC") >= 0) {
smartDelay(1000);
String array_string[20];
string_splitting(textMessage,array_string,'\"'); // split it at "
String NUMBER = array_string[1]; // we have the phone number here
Serial.println("GPS data Recived\r\n");
textMessage = "";
GSM.println("AT+CMGS=\"" + NUMBER + "\"\r");
delay(500);
String pesan = "https://maps.google.com/?q=" + lati + "," + longi;
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) ;
}
}
}
SCHEMATIC