Gps and sim800L

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");
}

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

This is the easiest way to tidy up the code and add the code tags

Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.

1 Like
SoftwareSerial gpsSerial(9, 10);
SoftwareSerial SIM800L(7, 6);

Using 2 instances of SoftwareSerial is the problem

may ask how to fix it?

Which Arduino board are you using ?

Arduino Uno

Then your options are limited

Two instances of SoftwareSerial will not work because they share the same input buffer and cannot work simultaneously

If you are prepared to run the project with nothing printed on the Serial monitor then it would be possible to use the hardware Serial interface for one of them and SoftwareSerial for the other

The best solution would be to use an Arduino with more than one Serial interface such as a Mega

Thanks for the advice I really appreciate it.

If you do decide to use the hardwareSerial interface for one device and not print anything to the Serial monitor then be aware that you will need to disconnect whatever is connected to it in order to upload code.

The best solution really is to use separate hardware serial interfaces such as those provided by the Mega

2 Likes

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.