Sim800l and sending multiple data via sms

Hi everyone!

I’m working on the project and I stuck with adding data from sensors to the text message.
I managed to write a code that allowed me to send the GPS data via SMS after getting a call.
[I am using Sim800l and Neo-6m] Now I need to add values to the message from other sensors. (DHT22, MQ135, TSL2561)

The problem is that after adding more data, I got an empty SMS and I don’t really know why.

Could anyone check my code and help me with that, please? I am still a newbie to programming…
I would need to decrease a memory usage somehow. Any tips and tricks how to do that?

#include <Sim800l.h>
#include "SIM900.h"
#include <SoftwareSerial.h>
#include "call.h"
#include <DHT.h>
#include "sms.h"
#include <TinyGPS++.h>
TinyGPSPlus gps;

double latitude, longitude;
SoftwareSerial mySerial(2, 3); // RX, TX
String response;
int lastStringLength = response.length();
String link;
String lat;
String lng;
SMSGSM sms;
#define DHTPIN 7
#define DHTTYPE DHT22   
DHT dht(DHTPIN, DHTTYPE);

CallGSM call;
boolean started=false;
char sms_text[250];




void setup()
{
    dht.begin();
    Serial.begin(9600);
    
    if (gsm.begin(9600)) 
    {
        Serial.println("\nstatus=READY");
        started=true;
    } 
    else 
        Serial.println("\nstatus=IDLE");
}

void loop()
{
  if(Serial.available()) {
    gps.encode(Serial.read());
  }
  if(gps.location.isUpdated()) {
    latitude = gps.location.lat();
    longitude = gps.location.lng();
    link = "www.google.com/maps/place/" + String(latitude, 6) + "," + String(longitude, 6) ;
    Serial.println(link);
    
   float humidity, temperature;
   String smsText ="";

  switch (call.CallStatus())
  {
    case CALL_NONE: // Nothing is happening

      break;

    case CALL_INCOM_VOICE : // Yes! Someone is calling us
    
      Serial.println("RECEIVING CALL");
       call.HangUp();
       delay(2000);
       humidity = dht.readHumidity();
       temperature = dht.readTemperature();
       delay(2000);

       smsText = "Location: www.google.com/maps/place/" + String(latitude, 6) + "," + String(longitude, 6); // + "Temperature: "+String(temperature,1)+"C Humidity: "+String(humidity,1)+"%";
       smsText.toCharArray(sms_text,250);
       //Serial.println(smsText);
       sms.SendSMS("+447544935837",sms_text);

      break;

    case CALL_COMM_LINE_BUSY:  // In this case the call would be established

      Serial.println("TALKING. Line busy.");
      
      break;
  }
  delay(1000);
}
}

Thank you in advance!

  if(Serial.available()) {
    gps.encode(Serial.read());
  }

The encode() method returns a value. Why are you ignoring it?

       humidity = dht.readHumidity();
       temperature = dht.readTemperature();

Why are you doing this only if the phone rings?

       smsText = "Location: www.google.com/maps/place/" + String(latitude, 6) + "," + String(longitude, 6); // + "Temperature: "+String(temperature,1)+"C Humidity: "+String(humidity,1)+"%";

What is in smsText after this?

I ignored that because I wasn’t able to print these values in the message. How can I use these values to make it work?

I am getting values of temperature and humidity only while a phone is ringing to save the energy. Because everything should work in the external power source, in terms of the power consumption there is no point to check the values of DHT all the time. However, GPS and GSM need to be active all the time because these sensors need some time to get a fix.

In the text message, I’m getting my location. However, if I uncomment the rest of the code I’m getting a blank message…
Fun fact, if I make this line like that: smsText = "Temperature: "+String(temperature,1)+"C Humidity: "+String(humidity,1)+"%"; //"Location: www.google.com/maps/place/" + String(latitude, 6) + "," + String(longitude, 6);
I’m getting the temperature and humidity in the message.

The problem appears while I want to merge these two pieces of code together.

Which Arduino are you using? I'm suspecting that you are running out of memory.

I am using Arduino Uno R3. Is there any possibility to decrease the memory usage? Maybe this is the main issue.

Is there any possibility to decrease the memory usage?

Of course there is. Stop using Strings.

dtostrf() and sprintf() will do everything you are doing now, with far less memory demands.

Hi, I've read about a sprintf() and dtostrf() and tried change my string by using them but without luck...

Well, I know that sprintf() should look somehow that: sprintf(char*str, const char*format)

and: dtostrf(float_variable, String_length, Digits_after_decimal_point, buffer)

so based on that I tried to use dtostrf():

static float temperature = 4;
static char tmp_str[20];
temperature = dht.readTemperature();
dtostrf(temperature, 4, 2, tmp_str);
smsText = "Temperature: " + mySerial.println(temperature, tmp_str);

While using the code above, I'm getting a message with the word "Temperature: " only. There are no values printed in the message. It looks like I'm doing this function wrong. Could anyone show me an example how to make it correctly, please?

Serial.begin(9600);

float temperature;
char tmp_str[20];
temperature = 23.4;

Serial.print("Temperature: ");
Serial.println(dtostrf(temperature, 4, 2, tmp_str));

works for me when printing to Serial Monitor

not sure what you are doing with all that static .

smsText = "Temperature: " + mySerial.println(temperature, tmp_str);

What does println() return? Why would you want to add, or append, that to the string literal?