hi guys I'm asking for your help for my capstone project that using gps, vibration and sim800l sensor, I have the complete code of the integration of 3 sensors but I cannot get the longitude and latitude but when I'm separate the gps code from the main code and create a sketch and run it it gives me the latitude and longitude I wonder what is the problem
Here's the code:
#include <SoftwareSerial.h>
#include <TinyGPS++.h>
TinyGPSPlus gps;
SoftwareSerial gpsSerial(9, 10);
SoftwareSerial SIM800L(7, 6);
int vib1 = 13; // Vibration sensor 1
int vib2 = 3; // Vibration sensor 2
unsigned long vibrationStart = 0;
// Global SMS delays
const int SMS_DELAY1 = 2000;
const int SMS_DELAY2 = 5000;
// Vibration thresholds
const int VIBRATION_THRESHOLD1 = 10;
const int VIBRATION_THRESHOLD2 = 4000;
const int VIBRATION_THRESHOLD3 = 9000;
void setup() {
pinMode(vib1, INPUT);
pinMode(vib2, INPUT);
Serial.begin(115200);
gpsSerial.begin(9600);
SIM800L.begin(9600);
Serial.println("System Initializing...");
// Initial delay for system stabilization
delay(10000);
// Notify system initialization
sendSMS("xxxxxxxxxxx", "System Initialized.");
// Now, activate the vibration sensors
Serial.println("Activating Vibration Sensors...");
}
void loop() {
long measurement1 = vibration(vib1);
long measurement2 = vibration(vib2);
Serial.print("Vibration 1: ");
Serial.println(measurement1);
Serial.print("Vibration 2: ");
Serial.println(measurement2);
// Read GPS data
while (gpsSerial.available() > 0) {
char c = gpsSerial.read();
gps.encode(c);
}
// Check if GPS data is updated
if (gps.location.isUpdated()) {
Serial.print("Latitude: ");
Serial.println(gps.location.lat(), 6);
Serial.print("Longitude: ");
Serial.println(gps.location.lng(), 6);
}
if (measurement1 > VIBRATION_THRESHOLD1 || measurement2 > VIBRATION_THRESHOLD1) {
if (vibrationStart == 0) {
vibrationStart = millis();
Serial.print("Vibration started at: ");
Serial.println(vibrationStart);
}
unsigned long vibrationDuration = millis() - vibrationStart;
Serial.print("Vibration duration: ");
Serial.println(vibrationDuration);
String gpsData = getGPSData();
if (vibrationDuration >= VIBRATION_THRESHOLD1 && vibrationDuration <= VIBRATION_THRESHOLD2) {
sendSMS("xxxxxxxxxxx", "Vibration alert (1-3 sec) - " + gpsData);
Serial.println(gpsData);
Serial.println("Vibration alert (1-3 sec)");
delay(SMS_DELAY1);
} else if (vibrationDuration >= VIBRATION_THRESHOLD2 && vibrationDuration <= VIBRATION_THRESHOLD3) {
sendSMS("xxxxxxxx", "Vibration alert (4-6 sec) - " + gpsData);
Serial.println("Vibration alert (4-6 sec)");
delay(SMS_DELAY2);
} else if (vibrationDuration >= VIBRATION_THRESHOLD3) {
sendSMS("xxxxxxx", "Vibration alert (7+ sec) - " + gpsData);
Serial.println("Vibration alert (7+ sec)");
delay(SMS_DELAY2);
}
} else {
vibrationStart = 0;
}
delay(50);
}
long vibration(int pin) {
long measurement = pulseIn(pin, HIGH);
return measurement;
}
String getGPSData() {
String gpsData = "";
while (gpsSerial.available() > 0) {
char c = gpsSerial.read();
Serial.write(c);
gpsData += c;
}
return gpsData;
}
void sendSMS(String number, String message) {
SIM800L.println("AT+CMGF=1");
delay(1000);
SIM800L.print("AT+CMGS="");
SIM800L.print(number);
SIM800L.println(""");
delay(2000);
SIM800L.print(message);
delay(100);
SIM800L.write(0x1A);
delay(5000);
while (SIM800L.available()) {
Serial.write(SIM800L.read());
}
delay(2000);
Serial.println();
Serial.println("SMS Sent");
}