dtostrf problem

Hello everyoneI have a problem and would be happy to resolve itI use SIM800 and send SMS to phone.I send a START text message and then I send a number.When I want to send a number I use the DTOSTRF function and it works.Then I send a new WATER text message and no problem.When I want to send the first START message again it sends me the previous number at the beginning instead of sending the word START

//Programa: Control  Arduino  SMS  SIM800L
#include <stdlib.h>
#include <Sim800l.h>
#include <SoftwareSerial.h>
#define SIM800_TX_PIN 7
#define SIM800_RX_PIN 8
int analogInput = 3;
float vout = 0.0;
float vin = 0.0;
float R1 = 99700.0; // resistance of R1 (100K) -see text!
float R2 = 9940.0; // resistance of R2 (10K) - see text!
int value = 0;
float total1 = 0;
float total = 0;
Sim800l Sim800l;
SoftwareSerial serialSIM800(SIM800_TX_PIN, SIM800_RX_PIN);
String textSms, numberSms;
uint8_t LED1 = 4; //on
char* message ;

bool error;

void setup()
{
  Serial.begin(9600);
  pinMode(LED1, OUTPUT);
  pinMode(5, OUTPUT);
  digitalWrite(5, 1);
  digitalWrite(LED1, 1);
  pinMode(analogInput, INPUT);
  Serial.print("DC VOLTMETER");

}

void voltreader()
{
  // read the value at analog input
  value = analogRead(analogInput);
  vout = (value * 5.0) / 1024.0;
  vin = vout / (R2 / (R1 + R2));
  Serial.print("INPUT V= ");
  Serial.println(vin);
  delay(5000);

}

void sms()
{
  //Being serial communication witj Arduino and SIM800
  serialSIM800.begin(9600);
  delay(1000);

  Serial.println("Setup Complete!");
  Serial.println("Sending SMS...");

  //Set SMS format to ASCII
  serialSIM800.write("AT+CMGF=1\r\n");
  delay(1000);

  //Send new SMS command and message number
  serialSIM800.write("AT+CMGS=\"0509401778\"\r\n");
  delay(1000);

  //Send SMS content
  serialSIM800.write (message);
  delay(1000);

  //Send Ctrl+Z / ESC to denote SMS message is complete
  serialSIM800.write((char)26);
  delay(1000);

  Serial.println("SMS Sent!");
  delay(15000);
}

void loop()
{

  total = (analogRead(A0)) / 204.6;
  //total = val / 204.6;
  total1 = (analogRead(A1)) / 204.6;
  //total1 = val1 / 204.6;
  Sim800l.begin();
  //Serial.print("clean old sms...");
  error = Sim800l.delAllSms();
  Serial.println(" off!");
  Serial.println("\nWaiting for commands by SMS ... ");
  //delay(1000);
  //the SMS of position 1
  textSms = Sim800l.readSms(1);

  //Checks if SMS has been received and is valid
  if (textSms.indexOf("OK") != -1)
  {
    //Checks if SMS is empty
    if (textSms.length() > 7)
    {
      // Returns the number of the cell phone that sent SMS
      numberSms = Sim800l.getNumberSms(1);
      //Displays the phone number in the Serial Monitor
      Serial.println(numberSms);
      //Converts text to uppercase
      textSms.toUpperCase();


      if (textSms.indexOf("ON") != -1)
      {
        Serial.println("Led 1 ON");
        digitalWrite(LED1, 0);
        delay(20000);
        digitalWrite(LED1, 1);
        Serial.println("Led 1 OFF");
      }

      Sim800l.delAllSms();
    }
  }
  Serial.println(total);
  //Serial.println("val1");
  if (total1 > 4 and total > 4)
  {
    Serial.println("pins a0 + a1 is high");
    message = "End!";
    sms ();
  }

  else if (total1 < 4 and total > 4)
  {
    Serial.println("pin a0 high and pin a1 low"); 
    message = "Started!";
    sms ();
    voltreader ();
    dtostrf(vin, 2, 2, message);
    sms ();
    dtostrf(0, 2, 2, message);
  }

  else if (total1 > 4 and  total < 4)
  {
    Serial.println("pin a1 high and pin a0 low");
    message = "Water In!";
    sms ();
  }


}

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

char* message ;

I think that declaring a null pointer and then using it to point dynamically at different messages is creating problems. The compiler is also giving warnings.

I think that this is the cause of your incorrect message.

Here’s an approach which puts the messages in an array and calls them by index. A message is created for the dtostrf value. The compiler is happy. It’s not tested with an 800L, but when I apply dummy values, I can see the different messages to be sent.

//Programa: Control  Arduino  SMS  SIM800L
#include <stdlib.h>
#include <Sim800L.h>
#include <SoftwareSerial.h>
#define SIM800_TX_PIN 7
#define SIM800_RX_PIN 8
int analogInput = 3;
float vout = 0.0;
float vin = 0.0;
float R1 = 99700.0; // resistance of R1 (100K) -see text!
float R2 = 9940.0; // resistance of R2 (10K) - see text!
int value = 0;
float total1 = 0;
float total = 0;
Sim800L Sim800l;
SoftwareSerial serialSIM800(SIM800_TX_PIN, SIM800_RX_PIN);
String textSms, numberSms;
uint8_t LED1 = 4; //on
//char* message ;
char message[][10] =  //size for max length of message plus null
{
  "End!",//indx 0
  "Started!",//indx 1
  "Water In!",//indx 2
  "1.23" //indx 3 dtostrf place holder message[3] 5wide x.xx +null
};

bool error;

void setup()
{
  Serial.begin(9600);
  pinMode(LED1, OUTPUT);
  pinMode(5, OUTPUT);
  digitalWrite(5, 1);
  digitalWrite(LED1, 1);
  pinMode(analogInput, INPUT);
  Serial.print("DC VOLTMETER");
}

void voltreader()
{
  // read the value at analog input
  value = analogRead(analogInput);
  vout = (value * 5.0) / 1024.0;
  vin = vout / (R2 / (R1 + R2));
  Serial.print("INPUT V= ");
  Serial.println(vin);
  delay(5000);
}

void sms(byte indx)
{
  //Being serial communication with Arduino and SIM800
  serialSIM800.begin(9600);
  delay(1000);

  Serial.println("Setup Complete!");
  Serial.println("Sending SMS...");

  //Set SMS format to ASCII
  serialSIM800.write("AT+CMGF=1\r\n");
  delay(1000);

  //Send new SMS command and message number
  serialSIM800.write("AT+CMGS=\"0509401778\"\r\n");
  delay(1000);

  //Send SMS content
  serialSIM800.write(message[indx]);
  Serial.println(message[indx]);
  delay(1000);

  //Send Ctrl+Z / ESC to denote SMS message is complete
  serialSIM800.write((char)26);
  delay(1000);

  Serial.println("SMS Sent!");
  delay(15000);
}

void loop()
{

  total = (analogRead(A0)) / 204.6;
  //total = val / 204.6;
  total1 = (analogRead(A1)) / 204.6;
  //total1 = val1 / 204.6;
  Sim800l.begin();
  //Serial.print("clean old sms...");
  error = Sim800l.delAllSms();
  Serial.println(" off!");
  Serial.println("\nWaiting for commands by SMS ... ");
  //delay(1000);
  //the SMS of position 1
  textSms = Sim800l.readSms(1);

  //Checks if SMS has been received and is valid
  if (textSms.indexOf("OK") != -1)
  {
    //Checks if SMS is empty
    if (textSms.length() > 7)
    {
      // Returns the number of the cell phone that sent SMS
      numberSms = Sim800l.getNumberSms(1);
      //Displays the phone number in the Serial Monitor
      Serial.println(numberSms);
      //Converts text to uppercase
      textSms.toUpperCase();


      if (textSms.indexOf("ON") != -1)
      {
        Serial.println("Led 1 ON");
        digitalWrite(LED1, 0);
        delay(20000);
        digitalWrite(LED1, 1);
        Serial.println("Led 1 OFF");
      }

      Sim800l.delAllSms();
    }
  }
  Serial.println(total); 
 
  if (total1 > 4 and total > 4)
  {
    Serial.println("pins a0 + a1 is high");
    //message = "End!";
    sms(0);
  } 
  else if (total1 < 4 and total > 4)
  {
    Serial.println("pin a0 high and pin a1 low");
    //message = "Started!";
    sms (1);//"Started"
    voltreader ();
    dtostrf(vin, 3, 2, message[3]);
    sms(3);//dtostrf value
    //dtostrf(0, 2, 2, message);
  }

  else if (total1 > 4 and  total < 4)
  {
    Serial.println("pin a1 high and pin a0 low");
    //message = "Water In!";
    sms (2);
  }
}

That's just painful! I'll opt for the beating.

-jim lee

You are passing messages to sms() through a global pointer and also using the global pointer as the location to store the results of dtostrf(). I recommend you use a “const char *” argument to sms() instead and then declare a small local buffer into which you can format the float.

To get this to compile I installed the “Sim800L Library Revised by Vittorio Esposito” from Library Manager. This uses the name “Sim800L” rather than “Sim800l” so I had to change ‘l’ to ‘L’ in two places.

//Programa: Control  Arduino  SMS  SIM800L
#include <stdlib.h>
#include <Sim800L.h>
#include <SoftwareSerial.h>
#define SIM800_TX_PIN 7
#define SIM800_RX_PIN 8
int analogInput = 3;
float vout = 0.0;
float vin = 0.0;
float R1 = 99700.0; // resistance of R1 (100K) -see text!
float R2 = 9940.0; // resistance of R2 (10K) - see text!
int value = 0;
float total1 = 0;
float total = 0;
Sim800L Sim800l;
SoftwareSerial serialSIM800(SIM800_TX_PIN, SIM800_RX_PIN);
String textSms, numberSms;
uint8_t LED1 = 4; //on


bool error;


void setup()
{
  Serial.begin(9600);
  pinMode(LED1, OUTPUT);
  pinMode(5, OUTPUT);
  digitalWrite(5, 1);
  digitalWrite(LED1, 1);
  pinMode(analogInput, INPUT);
  Serial.print("DC VOLTMETER");


}


void voltreader()
{
  // read the value at analog input
  value = analogRead(analogInput);
  vout = (value * 5.0) / 1024.0;
  vin = vout / (R2 / (R1 + R2));
  Serial.print("INPUT V= ");
  Serial.println(vin);
  delay(5000);


}


void sms(const char *message)
{
  //Being serial communication witj Arduino and SIM800
  serialSIM800.begin(9600);
  delay(1000);


  Serial.println("Setup Complete!");
  Serial.println("Sending SMS...");


  //Set SMS format to ASCII
  serialSIM800.write("AT+CMGF=1\r\n");
  delay(1000);


  //Send new SMS command and message number
  serialSIM800.write("AT+CMGS=\"0509401778\"\r\n");
  delay(1000);


  //Send SMS content
  serialSIM800.write (message);
  delay(1000);


  //Send Ctrl+Z / ESC to denote SMS message is complete
  serialSIM800.write((char)26);
  delay(1000);


  Serial.println("SMS Sent!");
  delay(15000);
}


void loop()
{


  total = (analogRead(A0)) / 204.6;
  //total = val / 204.6;
  total1 = (analogRead(A1)) / 204.6;
  //total1 = val1 / 204.6;
  Sim800l.begin();
  //Serial.print("clean old sms...");
  error = Sim800l.delAllSms();
  Serial.println(" off!");
  Serial.println("\nWaiting for commands by SMS ... ");
  //delay(1000);
  //the SMS of position 1
  textSms = Sim800l.readSms(1);


  //Checks if SMS has been received and is valid
  if (textSms.indexOf("OK") != -1)
  {
    //Checks if SMS is empty
    if (textSms.length() > 7)
    {
      // Returns the number of the cell phone that sent SMS
      numberSms = Sim800l.getNumberSms(1);
      //Displays the phone number in the Serial Monitor
      Serial.println(numberSms);
      //Converts text to uppercase
      textSms.toUpperCase();

      if (textSms.indexOf("ON") != -1)
      {
        Serial.println("Led 1 ON");
        digitalWrite(LED1, 0);
        delay(20000);
        digitalWrite(LED1, 1);
        Serial.println("Led 1 OFF");
      }
      Sim800l.delAllSms();
    }
  }
  Serial.println(total);
  //Serial.println("val1");
  if (total1 > 4 and total > 4)
  {
    Serial.println("pins a0 + a1 is high");
    sms ("End!");
  }
  else if (total1 < 4 and total > 4)
  {
    char buff[17];
    Serial.println("pin a0 high and pin a1 low");
    sms ("Started!");
    voltreader ();
    dtostrf(vin, 2, 2, buff);
    sms (buff);
    // dtostrf(0, 2, 2, buff);
  }
  else if (total1 > 4 and  total < 4)
  {
    Serial.println("pin a1 high and pin a0 low");
    sms ("Water In!");
  }
}

its work
thank u