Send tempsensor value as SMS (SIM 800l)

I have done some copy /paste from different sketches and finally got my optocouplers to control my diesel-heater controller in my car via SMS. I use the SIM800L (yeah I know only 2G but in Sweden it will work for at least 4 more years.

Now I want to add the function to ask for the temperature in the car by using a DHT11 sensor. I get the sensor values in serial by sending sms: “TEMP” to the SIM800L. But now I want to use the values and send back to me in an SMS.

As you can see in the sketch I have just made a copy of the optocoupler codes for triggering the DHT read, but have no idea on how to get the values back to me. Any ideas on where to start?

#include <SoftwareSerial.h>
#include <Sim800l_EM.h>
#include <dht.h> //ADDED
#define DHT11_PIN 9 //ADDED
#define DHTTYPE DHT11 //ADDED

dht DHT;


Sim800l Sim800l;


unsigned long bauds = 9600;
String SMScontain; // SMS contain only
String SMSsender; // SMS sender's Name
String text; // Detail SMS
String ReadTXT = "Test" ;
int NUMSMS; //SMS storage
long MSGlen;
uint8_t SMS_QTY;
String dt;
uint8_t index = 1;
bool error;
String ReadMsg;
int Switch_1 = 4;// Out Put Pins
int Switch_2 = 5;// Out Put Pins
int Switch_3 = 6;// Out Put Pins
int Switch_4 = 7;// Out Put Pins
int Switch_5 = 8;// Out Put Pins


void setup() {
  Serial.begin(bauds);
  pinMode(Switch_1, OUTPUT);
  pinMode(Switch_2, OUTPUT);
  pinMode(Switch_3, OUTPUT);
  pinMode(Switch_4, OUTPUT);
  pinMode(Switch_5, OUTPUT);


  Sim800l.begin();
  delay(5000);
  Serial.print("Ready to Work");
}


void loop() {
  //-------------------------------------------------------------
  // Get SMS storage amount -------------------------------------
  //-------------------------------------------------------------
  Sim800l.AT();
  delay(500);
  //Serial.read();

  int NUMSMS = Sim800l.getCountsms();
  //SMScontain = Sim800l.readMSG(8);
  //delay(500);
  Serial.println(NUMSMS);
  if (NUMSMS != 0 ) {

    //-------------------------------------------------------------

    SMScontain = Sim800l.readMSG(index); // Filter SMS contain
    SMScontain.trim();
    text = SMScontain;
    delay(1000);
    ReadTXT = text;

    //-----------------------------------------------
    //===============================================
    if (text.length() > 0) { // GET SENDER NUMBER
      //===============================================

      SMSsender = Sim800l.getNumberSms(index);
      SMSsender = SMSsender.substring(0, 12);
      SMSsender.trim();
      //Serial.println(SMSsender.length() );
      //Serial.println(text);
      Sim800l.delAllSms();
      //  SMSsender = "+12345678910" // If you want to get fead back to a perticular number
      //-----------------------------------------------
      // FILTER MSG CONTAIN----------------------------
      //-----------------------------------------------

      if (SMScontain.length() > 0) {
        Serial.print("SMSsender: ");
        Serial.println(SMSsender);
        Serial.print("SMS : ");
        Serial.println(SMScontain);
      }
      //----------------------------------------------

    }  //===============================================

  } // END ------------------------------------>


  
  //*************************************************
  //ON OFF by SMS Switch 1----------------------------------
  //*************************************************

  if (SMScontain == "ON") {
    digitalWrite(Switch_1, HIGH);
    delay(500);
    digitalWrite(Switch_1, LOW);
    error = Sim800l.sendSms(SMSsender, "Heater is ON"); // Active if you want a feadback  
  }

  //*************************************************
  //*************************************************
  //ON OFF by SMS Switch 2----------------------------------
  //*************************************************

  if (SMScontain == "OFF") {
    digitalWrite(Switch_2, HIGH);
    delay(4000);
    digitalWrite(Switch_2, LOW);
    error = Sim800l.sendSms(SMSsender, "Heater is OFF"); // Active if you want a feadback  
  }

  //*************************************************

  //*************************************************
  //ON OFF by SMS Switch 3----------------------------------
  //*************************************************

  if (SMScontain == "UP") {
    digitalWrite(Switch_3, HIGH);
    delay(500);
    digitalWrite(Switch_3, LOW);
    error = Sim800l.sendSms(SMSsender, "Heater is turned up 1 step"); // Active if you want a feadback
  }

  //*************************************************

  //*************************************************
  //ON OFF by SMS Switch 4----------------------------------
  //*************************************************

  if (SMScontain == "DOWN") {
    digitalWrite(Switch_4, HIGH);
    delay(500);
    digitalWrite(Switch_4, LOW);
    error = Sim800l.sendSms(SMSsender, "Heater is turned down 1 step"); // Active if you want a feadback
  }


  //*************************************************

  //*************************************************
  //READ TEMP SENSOR AND SEND DATA----------------------------------
  //*************************************************
    
if (SMScontain == "TEMP") {
    DHT.read11(DHT11_PIN);
    Serial.print("Current humidity = ");
    Serial.print(DHT.humidity);
    Serial.print("%  ");
    Serial.print("temperature = ");
    Serial.print(DHT.temperature); 
    Serial.println("C  ");
    error = Sim800l.sendSms(SMSsender, "Temp is"); // Active if you want a feadback
  }

 //Clear Data
  SMSsender = "";
  SMScontain = "";
  text = "";
  if (NUMSMS > 1) {
    Sim800l.delAllSms();
  }

} //END Void Loop

Sending data via SMS is messy.
Check out my example project
https://www.forward.com.au/pfod/pfodSMS/SMS_HotWater/index.html
that uses my (paid) pfodApp to control hotwater heater via SMS

Yes it seems pretty messy, but I have found some projects that store the value and then send it via SMS, so would be great to keep the simplicity of just using the SMS interface in this case. Also since I got the buttons to work. It is not that important with the temp, but it would be nice to have.

It’s not so bad...
Just declare a char buffer...
‘print’ what you want into the buffer ‘sprintf()’ makes it easy...
Then send it like any other SMS - keeping in mind the maximum size of the medium, or using multi-part messages.

I do this with hundreds of variable messages in a big project, and it runs 24/7.

lastchancename:
It’s not so bad...
Just declare a char buffer...
‘print’ what you want into the buffer ‘sprintf()’ makes it easy...
Then send it like any other SMS - keeping in mind the maximum size of the medium, or using multi-part messages.

I do this with hundreds of variable messages in a big project, and it runs 24/7.

Thanks for pinting me in the right direction, I will read up on that and try to learn. Since the main function already works as intended I got plenty of time :slight_smile:

If you keep an index value returned by sprintf, you can keep concatenating more complex message text with
index=0;
index += sprintf(buffer+index, format_string1, values…);
index += sprintf(buffer+index, format_string2, values…);

If you are using chars, I suggest you use my SafeString library (available from Arduino library manager), instead of sprintf
SafeString makes char work like Arduino Strings,
SafeString will keep track of the number of chars and alerts you, with a detailed error message, when you overflow the char, but keeps going, just without the overflow. See the detailed tutorial and library example sketches

sprintf will just blow up you code and restart, if you get it wrong, with no hint of where the problem is.

You can print to a SafeString so you can add text like you would sending it to Serial.

cSF(sfStr, 20);   // createSafeString  sized for 20 chars + '\0'
sfStr.print(F("The value is "));
sfStr.print(5.5335,2)  // add a float to sfStr with two dec places
SMS.send(sfStr.c_str());  //  send the result   The value is 5.53
sfStr = ""; // clear for next msg

SafeString makes sure you do not overflow the char size and that is always correctly terminated.

Yeah @drmpf is right, memory isn’t important.
His library should be included as part of the standard Arduino package, or you can also download it from his website.

drmpf:
If you are using chars, I suggest you use my SafeString library (available from Arduino library manager), instead of sprintf
SafeString makes char work like Arduino Strings,
SafeString will keep track of the number of chars and alerts you, with a detailed error message, when you overflow the char, but keeps going, just without the overflow. See the detailed tutorial and library example sketches

sprintf will just blow up you code and restart, if you get it wrong, with no hint of where the problem is.

You can print to a SafeString so you can add text like you would sending it to Serial.

cSF(sfStr, 20);   // createSafeString  sized for 20 chars + '\0'

sfStr.print(F("The value is "));
sfStr.print(5.5335,2)  // add a float to sfStr with two dec places
SMS.send(sfStr.c_str());  //  send the result  The value is 5.53
sfStr = ""; // clear for next msg




SafeString makes sure you do not overflow the char[] size and that is always correctly terminated.

I have played around with the code but do not getting it to work, do you mind helping me with how to implement it in my sketch? This is what I have today, for the section regarding DHT11 sensor. I want it to send both temp and humidity. I got it to send the SMS in below code, but how do I get the actual values from the DHT11 sensor in the SMS?

if (SMScontain == "TEMP") {
    DHT.read11(DHT11_PIN);
    Serial.print("Current humidity = ");
    Serial.print(DHT.humidity);
    Serial.print("%  ");
    Serial.print("temperature = ");
    Serial.print(DHT.temperature); 
    Serial.println("C  ");

    cSF(sfStr, 20);   // createSafeString  sized for 20 chars + '\0'
    sfStr.print(F("The value is "));
    sfStr.print(5.5335,2);  // add a float to sfStr with two dec places
    Sim800l.sendSms(SMSsender, sfStr.c_str());  //  send the result   The value is 5.53
    sfStr = ""; // clear for next msg

    
 }

how do I get the actual values from the DHT11 sensor in the SMS

Because you can 'print' to a SafeString it is as easy as this.

// add this in the setup code
void setup() {
  Serial.begint(115200);
  SafeString::setOutput(Serial);  // in setup to output error messages
}

if (SMScontain == "TEMP") {
    DHT.read11(DHT11_PIN);
    Serial.print("Current humidity = ");
    Serial.print(DHT.humidity);
    Serial.print("%  ");
    Serial.print("temperature = ");
    Serial.print(DHT.temperature);
    Serial.println("C  ");

    cSF(sfStr, 20);   // createSafeString  sized for 20 chars + '\0'
    sfStr.print("Current humidity = ");
    sfStr.print(DHT.humidity);
    sfStr.print("%  ");
    sfStr.print("temperature = ");
    sfStr.print(DHT.temperature);
    sfStr.println("C  ");

    Sim800l.sendSms(SMSsender, sfStr.c_str());  //  send the result   The value is 5.53
    sfStr = ""; // clear for next msg   
 }

NOTE: carefully add

SafeString::setOutput(Serial);

to your setup( ) code
cSF(sfStr, 20);
will not longer be 'large' enough and you will get an error msg and you will need to increase 20 as appropriate for your message

drmpf:
Because you can 'print' to a SafeString it is as easy as this.

// add this in the setup code

void setup() {
  Serial.begint(115200);
  SafeString::setOutput(Serial);  // in setup to output error messages
}

if (SMScontain == "TEMP") {
    DHT.read11(DHT11_PIN);
    Serial.print("Current humidity = ");
    Serial.print(DHT.humidity);
    Serial.print("%  ");
    Serial.print("temperature = ");
    Serial.print(DHT.temperature);
    Serial.println("C  ");

cSF(sfStr, 20);  // createSafeString  sized for 20 chars + '\0'
    sfStr.print("Current humidity = ");
    sfStr.print(DHT.humidity);
    sfStr.print("%  ");
    sfStr.print("temperature = ");
    sfStr.print(DHT.temperature);
    sfStr.println("C  ");

Sim800l.sendSms(SMSsender, sfStr.c_str());  //  send the result  The value is 5.53
    sfStr = ""; // clear for next msg 
}




NOTE: carefully add 



SafeString::setOutput(Serial);



to your setup( ) code
cSF(sfStr, 20); 
will not longer be 'large' enough and you will get an error msg and you will need to increase 20 as appropriate for your message

Tanks! Really appreciate it, now all I need is a nice box :slight_smile: