Hello,
I am using a GSM module sim800l and GY-NEO6MV2 GPS Module with Arduino Mega. I want to use both of them at the same time. They are connected to Serial1 and Serial2. In the void loop(); if i use both of them serial1.available() and serial2.available(); none of them is working. I tested that if i use only the GSM module the code is working, it receives the call and then sends the location. Maybe i should put them to work for a certaint amount of time, i really dont know. Maybe you can help me.
#include <SoftwareSerial.h> //Software Serial header to communicate with GSM module
#include <TinyGPS++.h>
// The TinyGPS++ object
TinyGPSPlus gps;
// The serial connection to the GPS device
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 = "";
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
}
void setup() {
Serial.begin(9600);
Serial2.begin(9600); //Serial COM for debugging
Serial1.begin(9600);
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() {
while (Serial2.available() > 0)
{
Serial2.begin(9600);
delay (1000);
gps.encode(Serial2.read());
if (gps.location.isUpdated())
{
// Latitude in degrees (double)
Serial.print("Latitude= ");
Serial.print(gps.location.lat(), 6);
Latitude = gps.location.lat();
// Longitude in degrees (double)
Serial.print(" Longitude= ");
Serial.println(gps.location.lng(), 6);
Longitude = gps.location.lng();
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();
delay (5000);
}
}
if (Serial1.available())
{ //Check if the SIM800 Module is telling anything
Serial1.begin(9600);
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);
}
}
if (Serial.available())
{
Serial1.write(Serial.read());
}
}