Need Help To Make Sim800L Send SMS of Battery Voltage When Threshold Exceeded

Hi Team,

please i need your help in detecting why my code is not sending the current battery voltage value of in the sms alert received. Please kindly find attached my code and picture of the sms alert recieved, once the threshold is reached, the gsm module sends an sms to my phone number but it does not contain the value of the current battery voltage; it only displays 0.0V in the text. please help me:.....................................
.........................

Arduino_Sim800_Battery_Limit_Alert.ino (1.8 KB)

@isaacjohnsong007 You should always post code using code tags, as more people will see it and can help you. An attachment won't work with phones and others won't be bothered to download the code and place it in a folder.

#include <SoftwareSerial.h>
#define LIMIT 23.0 // Change it to your favoured value in Celsius.
#define NUM_SAMPLES 10
SoftwareSerial GSMSerial(9, 10);//Connect 9 to TX of GSM and 10 to RX of GSM
int sum = 0;
unsigned char sample_count = 0;
float voltage = 0.0;

void setup() {
  // Setting up the Arduino.
  GSMSerial.begin(19200);   // Setting the baud rate of GSM Module  
  Serial.begin(19200);    // Setting the baud rate of Serial Monitor (Arduino)
  delay(10000);
  GSMSerial.print("ATE0\r");
  GSMSerial.print("AT+CMGF=1\r");
  delay(1000);
  GSMSerial.println("AT+CNMI=2,2,0,0,0\r");
  delay(1000);
  Serial.println("Finished Setup Section");
}

void SendMessage(){
  GSMSerial.println("AT+CMGF=1");    //Sets the GSM Module in Text Mode
  delay(1000);  // Delay of 1000 milli seconds or 1 second
  GSMSerial.println("AT+CMGS=\"+2347064309xxx\"\r"); // Replace x with mobile number
  delay(1000);
  GSMSerial.print("POP Battery Bank Voltage is Low: ");// The SMS text you want to send
  GSMSerial.print(voltage,1);
  Serial.println(voltage);
  GSMSerial.println(" V");
  delay(1000);
  GSMSerial.println((char)26);// ASCII code of CTRL+Z
  delay(1000); //give module time to send SMS
  GSMSerial.println();
  Serial.println("Message Sent");
  delay(5000); // give module time to send sms
}

void loop() {
  // Arduino does this forever.
  while (sample_count < NUM_SAMPLES){
  sum += analogRead(0);
  sample_count++;
  delay(10);
  }
  float voltage = (((float)sum / (float)NUM_SAMPLES *30)/1024.0)*1.2; 
  
  Serial.print("Battery Voltage is :");
  Serial.print(voltage, 2); //Reading the voltage to 1 decimal point
  Serial.println("V");
  delay (500);
  sample_count = 0;
  sum = 0;

  if(voltage<=LIMIT){
    SendMessage();
    delay(3600000); // wait for 1 Hour
  }
}

You have a global float variable

float voltage = 0.0;

And then again a redeclared local variable with scope confined to loop()

float voltage = (((float)sum / (float)NUM_SAMPLES *30)/1024.0)*1.2;

Which variable to you think the function is sending? Why did you redeclare voltage in loop?

See this reference on scope.
https://www.arduino.cc/en/pmwiki.php?n=Reference/Scope

Thanks so much for bringing this to my attention. I corrected the double declaration and now i can recieve the value of the battery voltage alongside the sms.
...............................

#include <SoftwareSerial.h>
#define LIMIT 23.0
#define NUM_SAMPLES 10
SoftwareSerial GSMSerial(9, 10);//Connect 9 to TX of GSM and 10 to RX of GSM
int sum = 0;
unsigned char sample_count = 0;
float voltage;

void setup() {
  // Setting up the Arduino.
  GSMSerial.begin(19200);   // Setting the baud rate of GSM Module  
  Serial.begin(19200);    // Setting the baud rate of Serial Monitor (Arduino)
  delay(10000);
  GSMSerial.print("ATE0\r");
  GSMSerial.print("AT+CMGF=1\r");
  delay(1000);
  GSMSerial.println("AT+CNMI=2,2,0,0,0\r");
  delay(1000);
  Serial.println("Finished Setup Section");
}

void SendMessage(){
  GSMSerial.println("AT+CMGF=1");    //Sets the GSM Module in Text Mode
  delay(1000);  // Delay of 1000 milli seconds or 1 second
  GSMSerial.println("AT+CMGS=\"+232706430xxxx\"\r"); // Replace x with mobile number
  delay(1000);
  GSMSerial.print("POP Battery Bank Voltage is Low: ");// The SMS text you want to send
  GSMSerial.print(voltage,2);
  GSMSerial.print(" V");
  Serial.println(voltage);
  delay(1000);
  GSMSerial.println((char)26);// ASCII code of CTRL+Z
  delay(1000); //give module time to send SMS
  GSMSerial.println();
  Serial.println("Message Sent");
  delay(5000); // give module time to send sms

void loop() {
  // Arduino does this forever.
  while (sample_count < NUM_SAMPLES){
  sum += analogRead(0);
  sample_count++;
  delay(10);
  }
  voltage = (((float)sum / (float)NUM_SAMPLES *30)/1024.0)*1.2; 
  
  Serial.print("Battery Voltage is :");
  Serial.print(voltage, 2); //Reading the voltage to 1 decimal point
  Serial.println("V");
  delay (500);
  sample_count = 0;
  sum = 0;

  if(voltage<=LIMIT){
    SendMessage();
    delay(3600000); // wait for 1 Hour
  }
}