SIM900A stops sending when there is no available GPS

Hi, I need some help with the code for my Weather Station. The problem is the SIM900A stops sending sms when there is no available GPS.

See image below, the first day is the SIM900A is still sending the data of other components even though there is no available GPS. When the GPS regained its signal, it will send also the GPS data.
gps

But the 2nd day, the Project stops sending all the data. And I think the GPS is the problem.
gps1

Or do you guys have any Idea what's the cause of the problem? Need help! Thanks in advance!

Below is the code

void loop() {
  while (gpsSerial.available() > 0) {
    if (gps.encode(gpsSerial.read())) {
      // GPS data is available and valid
      // Process GPS data
    }
  }
              
    static unsigned long lastSensorReadTime = 0;
    static unsigned long lastAvgCalcTime = 0;
    unsigned long currentTime = millis();

    int volt = analogRead(A0);// read the input
    double voltage = map(volt,0,1023, 0, 2500) + offset - 22;
    voltage /=100;

   if (currentTime - lastSensorReadTime >= sensorReadInterval) {
    float temperature = bme.readTemperature();
    float pressure = bme.readPressure() / 100.0F;
    float humidity = bme.readHumidity();
    temperatureSum += temperature;
    pressureSum += pressure;
    humiditySum += humidity;
    sampleCount++;
    lastSensorReadTime = currentTime;
    
  }

  float rainfall = calculateRainfall();  // Calculate rainfall since last check
  
  if ((millis() - gulStart_Read_Timer) >= READ_TIME) {
  cli();
  WindSpeed = WIND_SPEED_20_PULSE_SECOND / ONE_ROTATION_SENSOR * (float)Rotations;
  sei();
  
  Rotations = 0;
  gulStart_Read_Timer = millis();
  
  avgWindSpeed += WindSpeed;
  numReadings++;

  if (WindSpeed > maxWindSpeed) {
      maxWindSpeed = WindSpeed;
  } 
      
  //SEND THE DATA  
  if (numReadings >= MAX_READINGS) {
    float avgTemperature = temperatureSum / sampleCount;
    float avgPressure = pressureSum / sampleCount;
    float avgHumidity = humiditySum / sampleCount; 
    
    avgWindSpeed /= numReadings;    
        
    uint8_t result;
    result = node.readHoldingRegisters(0x0017, 1);
    unsigned int WindDirection = node.getResponseBuffer(0x00);
    
    if (result == node.ku8MBSuccess && WindDirection < 16) {
          Serial.print("Wind direction: ");
          Serial.println(compassNames[WindDirection]);
    }   
    String message1 = "WeatherStation ";
    message1 += "(AvgSpd: " + String(avgWindSpeed) + "), ";
    message1 += "(MxSpd: " + String(maxWindSpeed) + "), ";
    message1 += "(Dir: " + String(compassNames[WindDirection]) + "), ";
    message1 += "(AvgT2: " + String(avgTemperature) + "), ";
    message1 += "(AvgHum: " + String(avgHumidity) + "), ";
    message1 += "(AvgPres: " + String(avgPressure) + "), ";
    message1 += "(TotalR: " + String(rainTotal) + "), ";
    message1 += "(Volts: " + String(voltage) + "), ";
    
    String message2 = "WSGPS ";
    if (gps.location.isValid()) {
    message2 += "(lat: " + String(gps.location.lat(), 6) + "), ";
    message2 += "(long: " + String(gps.location.lng(), 6) + "), ";
    message2 += "(alt: " + String(gps.altitude.meters()) + "), ";
    } else {
    message2 += "(No GPS data available)";
    }
    
    SIM900A.println("AT+CMGS=\"" + String(phone_number) + "\"");
    delay(1000);
    SIM900A.print(message1);
    SIM900A.write((char)26);
     
    rainTotal = 0;  
    
    delay(5000);  // Add a delay before sending the second message
    
    SIM900A.println("AT+CMGS=\"" + String(phone_number) + "\"");
    delay(1000);
    SIM900A.print(message2);
    SIM900A.write((char)26);

    Serial.println("SMS Sent2");
  
    avgWindSpeed = 0.0;
    maxWindSpeed = 0.0;
    numReadings = 0;

    temperatureSum = 0;
    pressureSum = 0;
    humiditySum = 0;
    sampleCount = 0;
    lastAvgCalcTime = currentTime;
                }
           } 
      }

Also, I want to send all the SMS data even though there is no available GPS, and when the GPS regained its signal, it will send also the GPS.
Or is it possible that the GPS will remain its Signal for long period of time without losing any Data?

The time interval of sending the all the data is 15minutes

You extensively use Strings, which, due to Arduino's poor or nonexistent memory management, have a terrible reputation for eating up memory and causing unexpected program crashes.

All of this String nonsense:

   String message1 = "WeatherStation ";
    message1 += "(AvgSpd: " + String(avgWindSpeed) + "), ";
...

can be exactly reproduced with a series of .print() statements, like this:

SIM900A.print("WeatherStation ");
SIM900A.print("(AvgSpd: ");
SIM900A.print(avgWindSpeed);
SIM900A.print("), ");
etc.

The resulting program will take up less memory, run faster and won't crash because of String problems.

1 Like

So the string is the problem not GPS itself Sir?

There may be other problems, but I am 99.7% certain that use of Strings is one of them.

okay, will work with this one

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