I have a GSM 800L and GPS Neo 6M all connected to the Arduino Uno
I want to create a code that if i send a sms text to the device example "Location" it will reply the coordinates
The problem is when im combining the receiving the incoming messages and GPS, the GPS will not work and i try to modify the code to prioritize the GPS to work thats when also the GSM 800L not work to receive a incoming message and reply
I have a problem combining that two functions
Code:
#include <SoftwareSerial.h>
#include <TinyGPS++.h>
// Define pins for SIM800L
SoftwareSerial gsmSerial(11, 10); // RX, TX
SoftwareSerial gpsSerial(3, 4); // GPS Neo-6M: RX, TX
String incomingSMS; // Variable to store incoming SMS
// TinyGPS++ instance
TinyGPSPlus gps;
// Variables to store GPS coordinates
String latitude = "";
String longitude = "";
// Define button pin
const int buttonPin = 7; // Push button connected to D2
int buttonState = 0; // Variable to store button state
void setup() {
// Initialize button pin
pinMode(buttonPin, INPUT_PULLUP); // Using internal pull-up resistor
// Initialize serial communication for debugging
Serial.begin(9600);
Serial.println("Initializing...");
// Initialize SIM800L communication
gpsSerial.begin(9600); // GPS module
delay(1000);
gsmSerial.begin(9600);
// Give the module time to initialize
delay(2000);
// AT commands to configure the SIM800L
if (sendCommand("AT", "OK")) {
Serial.println("SIM800L is ready.");
} else {
Serial.println("SIM800L failed to respond. Check connections.");
}
sendCommand("AT+CMGF=1", "OK"); // Set SMS to text mode
sendCommand("AT+CNMI=2,2,0,0,0", "OK"); // Configure to show SMS on serial when received
Serial.println("Setup Complete. Waiting for SMS...");
}
void loop() {
// Read the button state
buttonState = digitalRead(buttonPin);
// Continuously parse GPS data
while (gpsSerial.available()) {
char d = gpsSerial.read();
gps.encode(d);
}
// Check for incoming messages from SIM800L
if (gsmSerial.available()) {
char c = gsmSerial.read();
incomingSMS += c;
if (c == '\n') { // End of the message
Serial.println("Received SMS:");
Serial.println(incomingSMS); // Debug: Show the full SMS content
if (incomingSMS.indexOf("TEST") >= 0) { // Check for keyword "TEST"
Serial.println("Keyword 'TEST' detected. Sending reply...");
//sendReply("+639000000000", "Testing reply"); // Replace with your phone number
if (gps.location.isUpdated()) {
latitude = String(gps.location.lat(), 6);
longitude = String(gps.location.lng(), 6);
// Send the SMS with the location link
sendSMS("+63900000000", "" + latitude + "N" +" "+ longitude+"E"); // Replace with your phone number
sendSMS("+639000000000", "e copy paste sa google map" ); // Replace with your phone number
}
else {
Serial.println("No valid GPS data available.");
}
} else {
Serial.println("No valid keyword found in SMS.");
}
incomingSMS = ""; // Clear the buffer for the next message
}
}
// Check if the button is pressed
if (buttonState == LOW) { // Button pressed (LOW because of pull-up)
Serial.println("Button pressed, retrieving GPS data...");
// Check if GPS data is updated
if (gps.location.isUpdated()) {
latitude = String(gps.location.lat(), 6);
longitude = String(gps.location.lng(), 6);
// Send the SMS with the location link
sendSMS("+639102189769", "" + latitude + "N" +" "+ longitude+"E"); // Replace with your phone number
sendSMS("+639102189769", "e copy paste sa google map" ); // Replace with your phone number
} else {
Serial.println("No valid GPS data available.");
}
delay(5000); // Wait to avoid multiple triggers
}
}
// Function to send SMS
void sendSMS(String phoneNumber, String message) {
gsmSerial.println("AT+CMGF=1"); // Set SMS to text mode
delay(1000);
gsmSerial.print("AT+CMGS=\"");
gsmSerial.print(phoneNumber); // Phone number
gsmSerial.println("\"");
delay(1000);
// Send the message
Serial.println("Sending SMS with content: " + message); // Debugging output
gsmSerial.print(message); // Message content
delay(1000);
gsmSerial.write(26); // ASCII code for CTRL+Z to send SMS
delay(5000);
Serial.println("SMS sent!");
}
void sendReply(String phoneNumber, String message) {
Serial.println("Preparing to send reply...");
sendCommand("AT+CMGF=1", "OK"); // Set SMS to text mode
gsmSerial.print("AT+CMGS=\"");
gsmSerial.print(phoneNumber);
gsmSerial.println("\"");
delay(100);
gsmSerial.println(message);
delay(100);
gsmSerial.write(26); // ASCII code for CTRL+Z to send SMS
delay(5000);
Serial.println("Reply sent successfully.");
}
bool sendCommand(String command, String expectedResponse) {
gsmSerial.println(command); // Send command to SIM800L
delay(1000);
String response = "";
while (gsmSerial.available()) {
response += (char)gsmSerial.read(); // Read the response from SIM800L
}
Serial.print("Command: ");
Serial.println(command);
Serial.print("Response: ");
Serial.println(response);
if (response.indexOf(expectedResponse) >= 0) {
Serial.println("Expected response received.");
return true;
} else {
Serial.println("Expected response not received.");
return false;
}
}
Serial Monitor
Received SMS:
TEST
Keyword 'TEST' detected. Sending reply...
No valid GPS data available.
Button pressed, retrieving GPS data...
No valid GPS data available.