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

