String value show invalid value when input on serial monitor

I try to change int to string. I use String() to change it. However the first serial print give correct value , however, after that the value is error. Could you give me some advice on this

main.ino


#include <Arduino.h>
#include "temperature.h"
#include "charge.h"
#include "current.h"
#include <Wire.h>
#include <WiFi.h>
#include <ESPAsyncWebServer.h>
#include <AsyncTCP.h>
#include "SPIFFS.h"




// Create AsyncWebServer object on port 80
AsyncWebServer server(80);

String temperature_text;
String percentage_text ;

temperature bms;
charge  battery ;
current battery_2;


void setup() {
 
  Serial.begin(115200);
  Serial.println("Hello, ESP32!");


  // Initialize SPIFFS
  if(!SPIFFS.begin()){
    Serial.println("An Error has occurred while mounting SPIFFS");
    return;
  }

  // Connect to Wi-Fi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi..");
  }

  // Print ESP32 Local IP Address
  Serial.println(WiFi.localIP());

  // Route for root / web page
  server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send(SPIFFS, "/index.html");
  });
  server.on("/temperature", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "text/plain", temperature_text.c_str());
  });
  server.on("/percentage", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "text/plain",  percentage_text.c_str());
    
  });


 
  server.begin();
  
  battery.begin();
  bms.begin();
  battery_2.begin(); 
}

void loop() {
  temperature_text = String(temperature_battery);
  percentage_text = String(25);
  Serial.println (percentage_text);
  bms.calculate_temperature();
  battery.voltage_read();
  battery.check_voltage();
  battery.percentage(); 
  battery_2.current_read ();
  battery_2.check_current();
  
}


temperature.cpp:



#include "temperature.h"
#include <Arduino.h>
#include "DHTesp.h"

#define turn_on_fan_pin 19  
const int DHT_PIN = 15;

float temperature_battery ;
DHTesp dht;


void temperature::begin(){
  dht.setup(DHT_PIN, DHTesp::DHT22);
}

void temperature::calculate_temperature(){

  TempAndHumidity data = dht.getTempAndHumidity();

 temperature_battery = data.temperature;

  if (temperature_battery > 35)
  {
    digitalWrite( turn_on_fan_pin, HIGH);
  }  

}

temperture.h :

#ifndef temperature_h
#define temperature_h


extern float temperature_battery;

class temperature {


  public:
  void begin();
  void calculate_temperature();
 

};

#endif 

charge.cpp

#include "charge.h"
#include <Arduino.h>


#define voltage_read_pin 34
#define cut_off_pin 22

float total_cell_charge;

float cell[3];
int percentage_value;
volatile boolean interrupt_timer1;


hw_timer_t * timer1 = NULL;
portMUX_TYPE timerMux1 = portMUX_INITIALIZER_UNLOCKED;
 
void IRAM_ATTR onTimer1() {
  portENTER_CRITICAL_ISR(&timerMux1);
  interrupt_timer1 = true;
  portEXIT_CRITICAL_ISR(&timerMux1);
 
}
 
void charge::begin(){
                      



  timer1 = timerBegin(0, 80, true);
  timerAttachInterrupt(timer1 , &onTimer1, true);
  timerAlarmWrite(timer1, 2000000, true);
  timerAlarmEnable(timer1);
               



}


void charge::voltage_read (void){
  if (interrupt_timer1 = true) {
    portENTER_CRITICAL(&timerMux1);
    portEXIT_CRITICAL(&timerMux1);
     for (int i = 0; i <3; i++)
     {
        float cell_temp =  analogRead(voltage_read_pin)*3.3/4095;
        cell[i] =  cell_temp * 0.09090909091; 
 
     }

    interrupt_timer1 = false; 
  }
  
}

void charge::check_voltage(){
  
  total_cell_charge = (cell[0]+cell[1]+cell[2])/3 ;
 
  

 
  if (total_cell_charge > 27 ) {
    digitalWrite(cut_off_pin, HIGH);
  } 

  if (total_cell_charge < 16 ) {

    digitalWrite(cut_off_pin,HIGH);
  } 
}


void charge::percentage(){
  if (total_cell_charge >= 27 ){
    percentage_value = 100 ;

  }
  else if (total_cell_charge < 27 and total_cell_charge > 26 ){
    percentage_value = 95 ;
  }
  else if (total_cell_charge <= 26 and total_cell_charge > 25 ){
    percentage_value = 90 ;
  }
  else if (total_cell_charge <= 25 and total_cell_charge > 24.5 ){
    percentage_value = 85 ;
  }
  else if (total_cell_charge <= 24.5 and total_cell_charge > 23.5 ){
    percentage_value = 80 ;
  }
  else if (total_cell_charge <= 23.5 and total_cell_charge > 23 ){
    percentage_value = 75 ;
  }
  else if (total_cell_charge <= 23 and total_cell_charge > 22.8 ){
    percentage_value = 70 ;
  }
  else if (total_cell_charge <= 22.8 and total_cell_charge > 22.5 ){
    percentage_value = 65 ;
  }
  else if (total_cell_charge <= 22.5 and total_cell_charge > 22 ){
    percentage_value = 60 ;
  }
  else if (total_cell_charge <= 22 and total_cell_charge > 21.8 ){
    percentage_value = 55 ;
  }
   else if (total_cell_charge <= 21.8 and total_cell_charge > 21.5 ){
    percentage_value = 50 ;
  }
   else if (total_cell_charge <= 21.5 and total_cell_charge > 21.3 ){
    percentage_value = 45 ;
  }
   else if (total_cell_charge <= 21.3 and total_cell_charge > 21 ){
    percentage_value = 35 ;
  }
  
  else if (total_cell_charge <= 21 and total_cell_charge > 20.9 ){
    percentage_value = 30 ;
  }
  else if (total_cell_charge <= 20.8 and total_cell_charge > 20.5 ){
    percentage_value = 25 ;
  }
  else if (total_cell_charge <= 20.5 and total_cell_charge > 20 ){
    percentage_value = 20 ;
  }
  else if (total_cell_charge <= 20 and total_cell_charge > 19.8 ){
    percentage_value = 10 ;
  }
  else {
   percentage_value = 0 ; 
  }



}

charge.h:

#ifndef charge_h
#define charge_h

extern int percentage_value;
extern float total_cell_charge;

class charge {

  public:
  void begin(); 
  void check_voltage();

  void voltage_read (void);
  void percentage();  
};

#endif 

current.cpp


#include "current.h"
#include <Arduino.h>
#include <Wire.h>
#include <Adafruit_ADS1015.h>

Adafruit_ADS1115 ads(0x48);


#define current_pin 35
#define contactor_pin 22

float current_cell[3];
float total_current; 
volatile boolean interrupt_timer2;


hw_timer_t * timer2 = NULL;
portMUX_TYPE timerMux2 = portMUX_INITIALIZER_UNLOCKED;
 
void IRAM_ATTR onTimer2() {
  portENTER_CRITICAL_ISR(&timerMux2);
  interrupt_timer2 = true;
  portEXIT_CRITICAL_ISR(&timerMux2);
 
}


void current::begin (){

  ads.begin();




  timer2 = timerBegin(0, 80, true);
  timerAttachInterrupt(timer2 , &onTimer2, true);
  timerAlarmWrite(timer2, 2000000, true);
  timerAlarmEnable(timer2);
  pinMode(contactor_pin , OUTPUT);
   
}

void current::current_read (void){
  if (interrupt_timer2 = true) {
    portENTER_CRITICAL(&timerMux2);
    portEXIT_CRITICAL(&timerMux2);
     for (int t = 0; t < 10; t++)
     {  int16_t  adc_temp  = ads.readADC_SingleEnded(0);
        current_cell[t] =  adc_temp *100/0.075;
     }

    interrupt_timer2 = false; 
  }
  
}

void current:: check_current(){
  total_current =  (current_cell[1] +current_cell[2]+current_cell[3] )/3;
  
  if (total_current > 100 )
  {
    digitalWrite(contactor_pin,HIGH);


  }


}

current.h

#ifndef  current_h
#define current_h

class  current{
  public:
  void begin(); 
  void check_current();
  void current_read (void); 
};

#endif 

post the full code. what is temperature_battery for example ?

sorry ,i just repost

are this .ino or .cpp ? what's in temperature.h ?

oh , you need the header file as well ? give me second

I'd like to understand the structure

temperature_battery is defined in one of your file, how is it known in the other file is the question

also what's you Arduino?

Yes I using esp32 on arduino IDE. I just add everything in

image

it really weird when first value is good but , below is error. I set the baud serial to 11520. I test with different baud . similar error still appear

where does Adafruit_ADS1015.h come from? (I'm aware of Adafruit_ADS1X15)

also missing DHTesp.h

it is the library that you could download from the arduino library system.

I only see this

also missing DHTesp.h (or did you mean that's DHT_sensor_library_for_ESPx)?

I noticed in current.cpp

  if (interrupt_timer2 = true) {

you probably meant == or just

  if (interrupt_timer2) {

same in charge.cpp

if (interrupt_timer1 = true) {
➜ remove the assignment

I mean if (interrupt_timer2)

As general rule, I avoid "String" variables, better switch to the so-called "C string" or "char*", especially if you need to send them back to client as that.
Then use "sprintf()" to create a string from integer values, dtostrf() for floats, and "strcpy()" to just copy strings.
Try this (assuming you'll never have values with more than 6 digits/chars):

...
char temperature_text[7];
char percentage_text[7];
...
void setup() {
  strcpy(temperature_text, "NONE");
  strcpy(percentage_text, "NONE");
...
  server.on("/temperature", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "text/plain", temperature_text);
  });
  server.on("/percentage", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "text/plain",  percentage_text);
  });
...
void loop() {
  // Just a guess of the source variables/properties, you need to check them...
  dtostrf(bms.temperature_battery, 4, 6, temperature_text);
  sprintf(percentage_text, "%d", battery.percentage_value);

  Serial.println (percentage_text);
  Serial.println (temperature_text);
...

can you provide the correct link to the adafruit library?

I found Adafruit_ADS1115/Adafruit_ADS1115.h at master · nipoutch/Adafruit_ADS1115 · GitHub which seems to match but is not from adafruit

also undefined behavior triggered there

  total_current =  (current_cell[1] + current_cell[2] + current_cell[3] ) / 3;

➜ you are reading beyond current_cell's bound. there is no index 3 (only 0, 1 and 2)

Here is the link :

GitHub - adafruit/Adafruit_ADS1X15: Driver for TI's ADS1015: 12-bit Differential or Single-Ended ADC with PGA and Comparator

they don't offer this constructor

Adafruit_ADS1115 ads(0x48);

how can you compile?

Thank you . I just post the link

Because the address of the extend analog is 0x48

have you seen the bug

  total_current =  (current_cell[1] + current_cell[2] + current_cell[3] ) / 3;

yeah , I just going to check and fix the issue

I already fix the bug you point out , but the result still the same