I am working on a GPS tracker project that works by sending a text to the SIM800L v2 module, it gets its info from the NEO-6M module, then sends an SMS back to the pre programmed number. It works as it was designed. What I'd like to do is allow a pre-determined number of people to send the SMS and get the info, but I only want the person who sent the request to get the info. Make sense? I don't know how to declare variables and write the code for that. I'm thinking some sort of "if" statement but I wouldn't know where to begin. Below is the code I found on youtube. I modified it to suit my needs, I don't use the relay, the only portion I use is the "GETLOC" message to get the current location. Thanks in advance.
#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;
const String JAMES = "5555555555";
const String JACK = "4444444444";
const String JOHN = "3333333333";
const String JOESEPH = "2222222222";
void setup() {
pinMode(redLed, OUTPUT);
pinMode(greenLed, OUTPUT);
pinMode(blueLed, OUTPUT);
pinMode(relay, OUTPUT);
digitalWrite(relay, HIGH);
Serial.begin(115200);
GSM.begin(9600);
neo.begin(9600);
GSM.listen();
delay(5000);
digitalWrite(blueLed, 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(blueLed, LOW);
}
void loop() {
GSM.listen();
delay(2);
while (GSM.available() > 0) {
digitalWrite(redLed, HIGH);
textMessage = GSM.readString();
Serial.print(textMessage);
delay(10);
digitalWrite(redLed, LOW);
}
neo.listen();
if (textMessage.indexOf("ON") >= 0) {
digitalWrite(relay, LOW);
lampState = "ON";
Serial.println("Bike set to ON\r\n");
textMessage = "";
GSM.println("AT+CMGS=\"" + JOSEPH + "\"\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) {
digitalWrite(relay, HIGH);
lampState = "OFF";
Serial.println("Bike set to OFF\r\n");
textMessage = "";
GSM.println("AT+CMGS=\"" + JOSEPH + "\"\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) {
smartDelay(1000);
Serial.println("GPS data Recived\r\n");
textMessage = "";
GSM.println("AT+CMGS=\"" + JOSEPH + "\"\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);
}
The device sending the data to the device with the code above needs some kind of variable embedded in the text so that your code can parse the text and determine where to send the text to. Assuming you can configure what the text sends, you can embed a phone number or name within the text, and within the if (textMessage.indexOf("GETLOC") >= 0) { section of code, implement something like this:
String text = "Example text from: 1111111111\\"; // sample text
int beginIndex = text.indexOf("from:") + 6; // get beginning index of number in text
int endIndex = text.indexOf("\\"); // get ending index of number in text
String number = text.substring(beginIndex, endIndex); // use substring to get phone number
Serial.println(number);
You could also implement the indexOf() and substring() method to parse for a name, and then use a switch or series of if statements to match an inputted name to a phone number to send the message to.
As stated before, I'm a truck driver so I'm not home that often. Anyhoo, I'm back home now for the next 5 days. I'm going to play around with the code and see if I can't get it working. I'm sure I'll be needing help, just wanted to let you know that I'm back so if you'll have some time to help me, I'll be forever in your debt. Thanks a bunch in advance.
@mfusco
Okay so here's what I have and it isn't working. I'm getting an error at the point where I'm declaring "number" to send the response. Here's the code and serial monitor output.
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;
const String MOM = "5555555555";
const String JACK = "4444444444";
void setup() {
pinMode(redLed, OUTPUT);
pinMode(greenLed, OUTPUT);
pinMode(blueLed, OUTPUT);
//pinMode(relay, OUTPUT);
// digitalWrite(relay, HIGH);
Serial.begin(115200);
GSM.begin(9600);
neo.begin(9600);
GSM.listen();
delay(5000);
digitalWrite(blueLed, 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(blueLed, 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);
}
if (textMessage.indexOf("GETLOC") >= 0) {
String text = "Example text from: number\\"; // sample text`
int beginIndex = text.indexOf("from:") + 6; // get beginning index of number in text
int endIndex = text.indexOf("\\"); // get ending index of number in text
String number = text.substring(beginIndex, endIndex); // use substring to get phone number
Serial.println(number);
smartDelay(1000);
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);
}
SERIAL MONITOR OUTPUT
GSM ready...
AT+CMGF=1
OK
AT+CNMI=2,2,0,0,0
OK
+CMT: "+4444444444","","24/04/05,09:47:40-16"
GETLOC
number
0.00000000
0.000000
I saw that one. It sends a text to multiple numbers no matter what. I'm trying to send a return text to the number that originated the request (GETLOC) otherwise every time a user sends a text all of the numbers will get it and that's not what I want it to do. Thanks for the link by the way. I'm open to any help LOL.
the SMS comes in form of this string +CMT: "+4444444444","","24/04/05,09:47:40-16" \n GETLOC
you have to split the string at delimiter " and extract the phone number we need
String array_string[20];
string_splitting(textMessage,array_string,'\"'); // split it at "
String number = array_string[1]; // we have the phone number here
all you have to do now with your code is replace this block here
String text = "Example text from: number\\"; // sample text`
int beginIndex = text.indexOf("from:") + 6; // get beginning index of number in text
int endIndex = text.indexOf("\\"); // get ending index of number in text
String number = text.substring(beginIndex, endIndex); // use substring to get phone number
with this
String array_string[20];
string_splitting(textMessage,array_string,'\"'); // split it at "
String number = array_string[1]; // we have the phone number here
and add this to the very bottom of your code
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) ;
}
}
}
Moving in the right direction! Now it responds to everyone's request individually, which is exactly what I want. But now it sends all zero's (0) for the GPS coordinates. Any ideas? Here's the new code.
CODE
void loop() {
GSM.listen();
delay(2);
while (GSM.available() > 0) {
digitalWrite(blueLed, HIGH);
textMessage = GSM.readString();
Serial.print(textMessage);
delay(10);
digitalWrite(blueLed, LOW);
}
if (textMessage.indexOf("GETLOC") >= 0) {
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(number);
smartDelay(1000);
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) ;
}
}
}
Get rid of soft serial instance for Neo-GPS module, get of rid of listen() calls in your code
Set baud to 9600 when your serial monitor is open
SoftwareSerial GSM(11, 10);
//SoftwareSerial neo(8, 9);
#define neo Serial
Connect the GPS Module's TX pin to Arduino's RX (GPIO-0) pin
Also also, make sure the GPS module's antenna is outside (or close to the window if indoors) with a good view of the sky for better picking up of the coordinates
This is what's in the serial monitor. I moved the GPS TX pin to the RX pin of the Nano and still not getting a GPS coordinate. I'm getting a map with all zero's.
SERIAL MONITOR
+CMT: "+15555555555","","24/04/06,11:25:39-16"
GETLOC
+15555555555
0.00000000
0.000000
GPS data Recived