Hello,
I'm working on a project where i want to know the location of the device. I implemented this logic where when i put the digital pin 30 to GND it would call the function for GPS. I tried this code inside where the GPS isn't available and it's working, it sends the data from the GSM, and pin30 was connected constant to GND. Is this logic good, i didn't tested outside, where i should have the location from the GPS module. Can you tell me if it's good, if i missed something.
Basically at this project i want to know the location from the GPS and if i am inside i want to know the location from the GSM and sends this data to a number using GSM.
The code is :
#include <TinyGPS++.h>
// The TinyGPS++ object
TinyGPSPlus gps;
#define GPS_INTERRUPT_PIN 30
String Link = "The current Location is https://www.google.com/maps/place/"; //we will append the Lattitude and longitude value later int the program
String responce = "";
String Longitude = "";
String Latitude = "";
String Date ="";
String Time ="";
String result2 = "";
volatile bool gpsDataAvailable = false;
String SIM800_send(String incoming) //Function to communicate with SIM800 module
{
Serial1.println(incoming); delay(100); //Print what is being sent to GSM module
String result = "";
while (Serial1.available()) //Wait for result
{
char letter = Serial1.read();
result = result + String(letter); //combine char to string to get result
}
return result; //return the result
}
// Funcție pentru tratarea datelor GPS disponibile
void handleGPSData()
{
while (Serial2.available() > 0) {
gps.encode(Serial2.read());
}
// Setează flag-ul pentru a indica că datele GPS sunt disponibile
gpsDataAvailable = true;
}
void setup() {
Serial.begin(9600);
Serial2.begin(9600); //Serial COM for debugging
Serial1.begin(9600);
pinMode(GPS_INTERRUPT_PIN, INPUT_PULLUP); // Configurează pinul de întrerupere ca intrare cu rezistență pull-up
attachInterrupt(digitalPinToInterrupt(GPS_INTERRUPT_PIN), handleGPSData, LOW);
delay(1000); //wait for serial COM to get ready
responce = SIM800_send("ATE1"); //Enable Echo if not enabled by default
Serial.print ("Responce:"); Serial.println(responce);
delay(1000);
responce = SIM800_send("AT+CGATT=1"); //Set the SIM800 in GPRS mode
Serial.print ("Responce:"); Serial.println(responce);
delay(1000);
responce = SIM800_send("AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\" "); //Activate Bearer profile
Serial.print ("Responce:"); Serial.println(responce);
delay(1000);
responce = SIM800_send("AT+SAPBR=3,1,\"APN\",\"net\""); //Set VPN options => 'RCMNET' 'www'
Serial.print ("Responce:"); Serial.println(responce);
delay(2000);
responce = SIM800_send("AT+SAPBR=1,1"); //Open bearer Profile
Serial.print ("Responce:"); Serial.println(responce); //Open bearer Profile
delay(2000);
responce = SIM800_send("AT+SAPBR=2,1"); //Get the IP address of the bearer profile
Serial.print ("Responce:"); Serial.println(responce);
delay(1000);
}
void prepare_message()
{
//Sample Output for AT+CLBS=1,1
int first_comma = responce.indexOf(','); //Find the position of 1st comma
int second_comma = responce.indexOf(',', first_comma+1); //Find the position of 2nd comma
int third_comma = responce.indexOf(',', second_comma+1); //Find the position of 3rd comma
for(int i=first_comma+1; i<second_comma; i++) //Values form 1st comma to 2nd comma is Longitude
Longitude = Longitude + responce.charAt(i);
for(int i=second_comma+1; i<third_comma; i++) //Values form 2nd comma to 3rd comma is Latitude
Latitude = Latitude + responce.charAt(i);
Serial.println(Latitude); Serial.println(Longitude);
Link = Link + Latitude + "," + Longitude; //Update the Link with latitude and Logitude values
Serial.println(Link);
}
String incoming = "";
void loop() {
if (gpsDataAvailable)
{
delay (1000);
gps.encode(Serial2.read());
if (gps.location.isUpdated())
{
// Latitude in degrees (double)
Serial.print("Latitude= ");
Serial.print(gps.location.lat(), 6);
Latitude = String(gps.location.lat(), 6);;
// Longitude in degrees (double)
Serial.print(" Longitude= ");
Serial.println(gps.location.lng(), 6);
Longitude = String(gps.location.lng(), 6);
Link = Link + Latitude + "," + Longitude;
// Raw date in DDMMYY format (u32)
Serial.print("Raw date DDMMYY = ");
Serial.println(gps.date.value());
Date = gps.date.value();
// Raw time in HHMMSSCC format (u32)
Serial.print("Raw time in HHMMSSCC = ");
Serial.println(gps.time.value());
Time = gps.time.value();
gpsDataAvailable = false;
}
}
else
{
if (Serial1.available())
{ //Check if the SIM800 Module is telling anything
char a = Serial1.read();
Serial.write(a); //print what the module tells on serial monitor
incoming = incoming + String(a);
if (a == 13) //check for new line
incoming =""; //clear the string if new line is detected
incoming.trim(); //Remove /n or /r from the incomind data
if (incoming=="RING") //If an incoming call is detected the SIM800 module will say "RING" check for it
{
Serial.println ("Sending sms"); delay(1000);
responce = SIM800_send("ATH"); //Hand up the incoming call using ATH
delay (1000);
responce = SIM800_send("ATE0"); //Disable Echo
delay (1000);
responce = "";
if ( (Latitude=="") && (Longitude=="") )
{
Serial1.println("AT+CLBS=1,1"); delay(5000); //Request for location data
while (Serial1.available())
{
char letter = Serial1.read();
responce = responce + String(letter); //Store the location information in string responce
}
Serial.print("Result Obtained as:"); Serial.print(responce); Serial.println("*******");
prepare_message(); delay(1000); //use prepare_message funtion to prepare the link with the obtained LAT and LONG co-ordinates
}
Serial1.println("AT+CMGF=1"); //Set the module in SMS mode
delay(1000);
Serial1.println("AT+CMGS=\"+40753159104\""); //Send SMS to this number
delay(1000);
Serial1.println(Link); // we have send the string in variable Link
delay(1000);
Serial1.println((char)26);// ASCII code of CTRL+Z - used to terminate the text message
delay(1000);
}
}
}
}
3 posts - 2 participants