Working with ESP32 SPIFFS

I suggest to have a short part of code with ALL pin assignements and use these pin assignement in the beginning of the code. NO mixture of "BtnA" and hardocded lcd(10, 9, 8,...) in the sketch.

by the way, common practice is to use const byte for pin's not precompiler #define

Yes! its working great Now!

So I need to get the actual inputs from the sensor now?

Okay, thanks for the improvisation, I'll change it accordingly :slight_smile:

That is a very good idea. And in addition:
keep the lines of code that do the inital calls like

SoftwareSerial mySerial(SoftSerRX_Pin, SoftSerTX_Pin); // RX, TX

HX711_ADC LoadCell(HX711_dout, HX711_sck);

LiquidCrystal lcd (lcdenablePin, lcdD0_Pin, lcdD1_Pin, lcdD2_Pin, lcdD3_Pin);

close together
best regards Stefan

1 Like

Yes.
You simple change from

boolean simulate_LoadCell = true;
boolean simulate_NTC      = true;

to

boolean simulate_LoadCell = false;
boolean simulate_NTC      = false;

to make the code use the real sensor-values
best regards Stefan

1 Like

some notes on the code (just examples):

Serial.println("lcd.begin creating chars");

vs

Serial.println( F("lcd.begin creating chars done") );

if you use the F-Makro, use it consequently

byte Degs[] = {B00110, B01001, B01001, B00110, B00000, B00000, B00000, B00000};

the degree symbol can be found in nearly all character ROMs, no need for a custom char

lcd.write(0xDF); // degree

will work for the common HD44780 A00 ROM.

declare you byte arrays at least const.

use the default notation for binary falues 0b00110 instead of the #defines B00110

Naming things:
functions and variable names: let them start with small letters
use camelCase OR underscores_if_you_prefere_them - but avoid mixing within one sketch
use CAPITAL for precompiler #defines ... but avoid them if not necessary

(by the way I assume one central #define STEFAN for the HW parts differing between your skteches would make the support for Stefan a lot easier ...)

1 Like

That really makes much of the confusing part easier!

I'm waiting for ESP32 to arrive, its supposed to get here in one or two hours. I'll run everything on it and see how it goes and get back here!

I want to emphasise that an ESP32 is a 3.3V device this means:

For the serial-line Arduino-TX-5V--->------ESP32-Rx-3.3V you need a voltage-divider

For the ESP32-Tx-3.3V--->-------Arduino-Rx-5V you need a voltage-level-shifter.
At least a small circuit like this one


Most voltage-level-shifters use MOS-FETs but MOS-FETs are sensitive to electrostatic discharge
best regards Stefan

Yes, I will use the level shifters for sure

Now that you have found that the large arrays fit on the Mega, I am even more confused by the need for the ESP 32. Previously you stated

I'll still try to summarize.
I already have a hardware which reads loadcell temperature has some switches and 20X4 LCD and I intend to display those calculations on it. but the amount of data I need to deal with is too large for UNO so there comes the other board in the picture.

Now you might again ask then why not use ESP32 only. the answer is I have to use ESP32 later on but first I need to implement this thing on the hardware I have right now, show it to my client and then tell him to revise the hardware.

The change from the Uno to the Mega should be a fit for the special Uno based hardware.

Count has a special designed PCB which is already in service at the customer. This PCB uses the processor like on the Arduino-Uno. And this is the reason for the additional ESP32

best regards Stefan

Okay apologies for getting back here late. But my client asked me to work on the other project I had posted the link earlier again. So I had to sort it out first before I get back to this project again.

Anyways I connected my NTC to my arduino and uploaded the following code:

#include <math.h>
const int thermistor_output = A0;
float steinhart;
void setup() {
  Serial.begin(115200);  /* Define baud rate for serial communication */
}

void loop() {
  int thermistor_adc_val;
  double output_voltage, thermistor_resistance, therm_res_ln, temperature; 
  thermistor_adc_val = analogRead(thermistor_output);
  output_voltage = ( (thermistor_adc_val * 5.0) / 1023.0 );
  thermistor_resistance = ( ( 5 * ( 10.0 / output_voltage ) ) - 10 ); /* Resistance in kilo ohms */
  thermistor_resistance = thermistor_resistance * 1000 ; /* Resistance in ohms   */
  therm_res_ln = log(thermistor_resistance);
  /*  Steinhart-Hart Thermistor Equation: */
  /*  Temperature in Kelvin = 1 / (A + B[ln(R)] + C[ln(R)]^3)   */
  /*  where A = 0.001129148, B = 0.000234125 and C = 8.76741*10^-8  */
  temperature = ( 1 / ( 0.001129148 + ( 0.000234125 * therm_res_ln ) + ( 0.0000000876741 * therm_res_ln * therm_res_ln * therm_res_ln ) ) ); /* Temperature in Kelvin */
  temperature = temperature - 273.15; /* Temperature in degree Celsius */
  steinhart = temperature;
  Serial.print("Temperature in degree Celsius = ");
  Serial.println(temperature);
  Serial.print("temp in Float:");
  Serial.println(steinhart);
  Serial.print("Resistance in ohms = ");
  Serial.println(thermistor_resistance);
 
  delay(1000);
}

And I get correct output for the temperatures. So I decided to use the exact same code in our sketch and did this:

#include <HX711_ADC.h>
#include <SoftwareSerial.h>
#include <math.h>
/* coment this to change back to your LCD 
#include <Wire.h>
//#include <SBWire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4);  // adjust to the right I2C of your device
/* coment this to change back to your LCD */

boolean debugActive = true;
#define debugTxtVar(myParameterText, variableName, mySuffix) \
        if (debugActive) { \
          Serial.print( F(#myParameterText " " #variableName" ~") ); \
          Serial.print(variableName); \
          Serial.println( F(#mySuffix) ); \
        } 


const byte SoftSerRX_Pin = 2;
const byte SoftSerTX_Pin = 3;
SoftwareSerial mySerial(SoftSerRX_Pin, SoftSerTX_Pin); // RX, TX

const int HX711_dout = A4; //mcu > HX711 dout pin
const int HX711_sck  = A5; //mcu > HX711 sck pin
boolean Flag;

HX711_ADC LoadCell(HX711_dout, HX711_sck);

const byte numChars = 128;

char receivedChars[numChars];
char tempChars[numChars];        // temporary array for use when parsing
boolean newData = false;
boolean newDataReady = false;

char rcESP32;
char receivedESP32Chars[numChars];
boolean newESP32Data = false;
boolean newESP32DataReady = false;
boolean recvESP32InProgress = false;

const int serialPrintInterval = 500; //increase value to slow down serial print activity
static unsigned long lastSerialTime;

float calibrationValue = 445.84;//437.27;//432.54;//436.01; // uncomment this if you want to set the calibration value in the sketch327.49
long TareOffset = 0; //8399692;//8382896;//8398661;//9532802;//8344838//8344747//8345467//8345516

boolean simulate_LoadCell = false;//true;
boolean simulate_NTC      = false;//true;

#include <LiquidCrystal.h>
LiquidCrystal lcd (10, 9, 8, 7, 6, 5);

#define BtnA 4
#define BtnB A1
#define BtnC A2
#define BtnD A3

int page = 0;

/*
#define THERMISTORPIN A0 // analog pin to connect Thermistor
#define NUMSAMPLES 5// Number of samples for Thermistor resistance values. Increase for greater accuracy.
#define SERIESRESISTOR 10000// R1 value
#define THERMISTORNOMINAL 10000 //Resistance of Thermistor at room temperature (Considered 25 C)     
#define TEMPERATURENOMINAL 25 //Considered normal room temperature
#define BCOEFFICIENT 3950//// The beta coefficient of the thermistor (usually 3000-4000)

int samples[NUMSAMPLES];//Array to hold samples for Thermistor resistance

float average;//Holds the average value of thermistor resistance
float steinhart;//To hold temperature in DegC.*/

int thermistor_adc_val;
double output_voltage, thermistor_resistance, therm_res_ln, temperature; 
float steinhart;//To hold temperature in DegC.*/
const int thermistor_output = A0;

int weight;
float f_weight;
float Density;
float apig;

int petrol = 0;
int diesel = 0;

int A_ButtonState = 0;
int B_ButtonState = 0;
int C_ButtonState = 0;
int D_ButtonState = 0;

int last_A_ButtonState = 0;
int last_B_ButtonState = 0;
int last_C_ButtonState = 0;
int last_D_ButtonState = 0;

volatile boolean SwA = false;
volatile boolean SwB = false;
volatile boolean SwC = false;
volatile boolean SwD = false;

unsigned long previousMillis = 0;
const long interval = 60000;
unsigned long currentMillis;

unsigned long previousMillis1 = 0;
const long interval1 = 5000;
unsigned long currentMillis1;

byte Degs[] = {B00110, B01001, B01001, B00110, B00000, B00000, B00000, B00000};

byte P[] = {B11111, B10001, B10101, B10001, B10111, B10111, B10111, B11111};

byte E[] = {B11111, B10001, B10111, B10011, B10011, B10111, B10001, B11111};

byte T[] = {B11111, B10001, B11011, B11011, B11011, B11011, B11011, B11111};

byte D[] = {B11111, B10011, B10101, B10101, B10101, B10101, B10011, B11111};

byte I[] = {B11111, B10001, B11011, B11011, B11011, B11011, B10001, B11111};

byte L[] = {B11111, B10111, B10111, B10111, B10111, B10111, B10001, B11111};

byte V[] = {B11111, B10101, B10101, B10101, B10101, B10101, B11011, B11111};

void PrintFileNameDateTime() {
  Serial.println("Code running comes from file ");
  Serial.println(__FILE__);
  Serial.print("  compiled ");
  Serial.print(__DATE__);
  Serial.print(" ");
  Serial.println(__TIME__);  
}


boolean TimePeriodIsOver (unsigned long &periodStartTime, unsigned long TimePeriod) {
  unsigned long currentMillis  = millis();  
  if ( currentMillis - periodStartTime >= TimePeriod )
  {
    periodStartTime = currentMillis; // set new expireTime
    return true;                // more time than TimePeriod) has elapsed since last time if-condition was true
  } 
  else return false;            // not expired
}

unsigned long MyTestTimer = 0;                   // variables MUST be of type unsigned long
const byte    OnBoard_LED = 13;


void BlinkHeartBeatLED(int IO_Pin, int BlinkPeriod) {
  static unsigned long MyBlinkTimer;
  pinMode(IO_Pin, OUTPUT);
  
  if ( TimePeriodIsOver(MyBlinkTimer,BlinkPeriod) ) {
    digitalWrite(IO_Pin,!digitalRead(IO_Pin) ); 
  }
}

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

void checkIf_A_ButtonIsPressed()
{
  if (A_ButtonState != last_A_ButtonState)
  {
    if (A_ButtonState == 0)
    {
      SwA = true;
    }
    delay(30);
  }
  last_A_ButtonState = A_ButtonState;
}

//---------------------------------------------
void checkIf_B_ButtonIsPressed()
{
  if (B_ButtonState != last_B_ButtonState)
  {
    if (B_ButtonState == 0)
    {
      SwB = true;

    }
    delay(30);
  }
  last_B_ButtonState = B_ButtonState;
}

//----------------------------------------------------
void checkIf_C_ButtonIsPressed()
{
  if (C_ButtonState != last_C_ButtonState)
  {
    if (C_ButtonState == 0)
    {
      SwC = true;

    }
    delay(30);
  }
  last_C_ButtonState = C_ButtonState;
}

//----------------------------------------------------
void checkIf_D_ButtonIsPressed()
{
  if (D_ButtonState != last_D_ButtonState)
  {
    if (D_ButtonState == 0)
    {
      SwD = true;
    }
    delay(30);
  }
  last_D_ButtonState = D_ButtonState;
}
//---------------------------------------------------------------------------------------------------------------------------------
void setup() {
  Serial.begin(115200);
  Serial.println("Setup-Start");
  PrintFileNameDateTime();
  mySerial.begin(9600);

  pinMode(BtnA, INPUT_PULLUP);
  pinMode(BtnB, INPUT_PULLUP);
  pinMode(BtnC, INPUT_PULLUP);
  pinMode(BtnD, INPUT_PULLUP);

  analogReference(EXTERNAL);
  Serial.println("analogReference(EXTERNAL) done");

  lcd.begin(20, 4);
 /* lcd.init();                      // initialize the lcd
  lcd.backlight();                 //Backlight ON if under program control
*/
  Serial.println("lcd.begin creating chars");
  lcd.createChar(0, Degs);
  lcd.createChar(1, P);
  lcd.createChar(2, E);
  lcd.createChar(3, T);
  lcd.createChar(4, D);
  lcd.createChar(5, I);
  lcd.createChar(6, L);
  lcd.createChar(7, V);
  Serial.println( F("lcd.begin creating chars done") );

  Serial.println( F("LoadCell.begin()") );
  LoadCell.begin();
  Serial.println( F("LoadCell.begin() done") );
  long stabilizingtime = 2000; // preciscion right after power-up can be improved by adding a few seconds of stabilizing time
  boolean _tare = false; //set this to false if you don't want tare to be performed in the next step

  Serial.print( F("LoadCell.start(") );
  Serial.print(stabilizingtime);
  Serial.print( F(",") );
  Serial.print(_tare);
  Serial.println( F(");") );
  LoadCell.start(stabilizingtime, _tare);
  Serial.println( F("LoadCell.start( done") );

  lcd.begin(20, 4);

  lcd.setCursor(5, 0);
  lcd.print( F("Welcome to") );
  lcd.setCursor(2, 1);
  lcd.print( F("Ready Automotive") );
  lcd.setCursor(0, 3);
  lcd.print( F("Technology Solutions") );

  Serial.println( F("Welcome to Ready Automotive Technology Solutions") );

  delay(3000);
  lcd.clear();

  Serial.println( F("execute LoadCell.getTareTimeoutFlag()") );
  if (LoadCell.getTareTimeoutFlag())
  {
    Serial.println( F("Timeout, check MCU>HX711 wiring and pin designations") );
    while (1);
  }
  else {
    Serial.println( F("LoadCell.getTareTimeoutFlag() returns false") );
    LoadCell.setCalFactor(calibrationValue); // set calibration value (float)
    Serial.print( F("LoadCell.setCalFactor(") );
    Serial.print(calibrationValue);
    Serial.println( F(")") );
    
    LoadCell.setTareOffset(TareOffset);
    Serial.print( F("LoadCell.setTareOffset(") );
    Serial.print(TareOffset);
    Serial.println( F(")") );
    Serial.println( F("Startup is complete") );
  }

}

void loop() {
  BlinkHeartBeatLED(OnBoard_LED,500);

  if ( TimePeriodIsOver(MyTestTimer,3000) ) {
    HomePage();
    Serial.println();
  }  
  
  A_ButtonState = digitalRead(BtnA);
  B_ButtonState = digitalRead(BtnB);
  C_ButtonState = digitalRead(BtnC);
  D_ButtonState = digitalRead(BtnD);

  checkIf_A_ButtonIsPressed();
  checkIf_B_ButtonIsPressed();
  checkIf_C_ButtonIsPressed();
  checkIf_D_ButtonIsPressed();

  if (SwA)//Select Petrol
  {
    SwA = false;//reset key status
    LoadCell.tareNoDelay();
    petrol = 1;
    diesel = 0;
    if (LoadCell.getTareStatus() == true)
    {
      //select fuel
      Serial.println( F("A key pressed, Petrol Selected") );
    }
  }

  if (SwB)//Select diesel
  {
    SwB = false;//reset key status
    LoadCell.tareNoDelay();
    //select fuel
    petrol = 0;
    diesel = 1;
    if (LoadCell.getTareStatus() == true)
    {
      Serial.println( F("B key pressed, Diesel Selected") );
    }

  }

  if (SwC)//Show density and API-G
  {
    SwC = false;
    Density2_APIG();//call a function to send data to NODE MCU;
  }

  if (SwD) //Activate live Mode
  {
    SwD = false;
    LiveLogo();
    Serial.print( F("D key pressed, Live Mode ON") );
  }

}

void get_Temperature() {
  Serial.println( F("entering get_Temperature() ") );

  uint8_t i;

  if (!simulate_NTC)
  {
    thermistor_adc_val = analogRead(thermistor_output);
    output_voltage = ( (thermistor_adc_val * 5.0) / 1023.0 );
    thermistor_resistance = ( ( 5 * ( 10.0 / output_voltage ) ) - 10 ); /* Resistance in kilo ohms */
    thermistor_resistance = thermistor_resistance * 1000 ; /* Resistance in ohms   */
    therm_res_ln = log(thermistor_resistance);
  /*  Steinhart-Hart Thermistor Equation: */
  /*  Temperature in Kelvin = 1 / (A + B[ln(R)] + C[ln(R)]^3)   */
  /*  where A = 0.001129148, B = 0.000234125 and C = 8.76741*10^-8  */
    temperature = ( 1 / ( 0.001129148 + ( 0.000234125 * therm_res_ln ) + ( 0.0000000876741 * therm_res_ln * therm_res_ln * therm_res_ln ) ) ); /* Temperature in Kelvin */
    temperature = temperature - 273.15; /* Temperature in degree Celsius */
    steinhart = temperature;
    Serial.print("Temperature in degree Celsius = ");
    Serial.println(temperature);
    Serial.print("temp in Float:");
    Serial.println(steinhart);
    Serial.print("Resistance in ohms = ");
    Serial.println(thermistor_resistance);
  } // if (!simulate_NTC) {
  else { // NTC sensor is simulated assign a demo-temperature
    if (steinhart == 40.0) {
      steinhart = 0.0;        
    }
    else { 
    steinhart = 40.0; 
    }
  }
  Serial.println( F("leaving get_Temperature() ") );
}


void get_Weight() {
   Serial.println( F("entering get_Weight() ") );

  if (!simulate_LoadCell) {
    // check for new data/start next conversion:
    if (LoadCell.update()) newDataReady = true;
  
    // get smoothed value from the dataset:
    if (newDataReady)
    {
      if (millis() - lastSerialTime >= serialPrintInterval)
      {
        weight = LoadCell.getData();
        //f_weight = weight;
        /*  Serial.print("Load cell output value:");
          Serial.println(f_weight);*/
        newDataReady = 0;
        lastSerialTime = millis();
      }
    }
  
    f_weight = weight * 10;//convert 100ml weight to 1ltr weight
    debugTxtVar(B:,f_weight,~);
  } //if (!Simulate_LoadCell) { 
  else { // load-cell gets simulated assign a demo-value
    if (f_weight == 654) { 
      f_weight = 800;
    }  
    else {
      f_weight = 654;   
    }
  }
  Serial.println( F("leaving get_Weight() ") );
}

void receiveFromESP32() {
  Serial.println( F("Entering receiveFromESP32()") );

  static char rc;
  static char startMarker = '<';
  static char endMarker = '>';

  static int ndx;
  
  while (mySerial.available() > 0 && newESP32Data == false) {
    rc = mySerial.read();
      debugTxtVar(receiveFromESP32,rc,~);
    if (recvESP32InProgress == true) {
      if (rc != endMarker) {
        receivedESP32Chars[ndx] = rc;
        ndx++;
        if (ndx >= numChars) {
          ndx = numChars - 1;
        }
      }
      else {
        receivedESP32Chars[ndx] = '\0'; // terminate the string
        recvESP32InProgress = false;
        ndx = 0;
        newESP32Data = true;
        //debugTxtVar(first parameter , variablename, suffix);
        debugTxtVar(1: ,receivedESP32Chars,~);
      }
    }

    else if (rc == startMarker) {
      recvESP32InProgress = true;
    }
  }
 Serial.println( F("leaving receiveFromESP32()") ); 
}


void Density2_APIG() {
  Serial.println( F("entering Density2_APIG()") );
  
  mySerial.print("<");
  mySerial.print(f_weight);
  mySerial.print(",");
  mySerial.print(steinhart);
  mySerial.print(">");

  delay(200);  //wait to hear from NODE-MCU
  receiveFromESP32();
  if (newESP32Data) {
    newESP32Data = false;
    Density = atof(receivedESP32Chars);
    
    Serial.print("Density: ");
    Serial.println(Density);

    lcd.setCursor(9, 2);
    lcd.print(Density);
    //Flag = false;
  }
  //calculate API-G and display it on LCD
  Serial.println( F("leaving Density2_APIG()") );
}


void HomePage() {
  Serial.println( F("entering HomePage()") );
  get_Temperature();
  get_Weight();
  Density2_APIG();
  lcd.setCursor(0, 0);
  lcd.print("H.Meter: ");
  lcd.print(f_weight);

  lcd.setCursor(0, 1);
  lcd.print("Temp: ");
  lcd.print(steinhart);
  lcd.write(byte(0));
  lcd.print("C");

  lcd.setCursor(0, 2);
  lcd.print("Density: ");
  lcd.print(Density);
  lcd.print("g/ml");

  apig = (141500.0 / Density) - 131.5;
  debugTxtVar(A: ,apig,~);

  lcd.setCursor(0, 3);
  lcd.print("API-G: ");
  lcd.print(apig);
  DisplayFuel();

  Serial.println( F("leaving HomePage()") );
  //mySerial.println("<653,-9.75>");
  //Serial.println( F("mySerial.println('<653,-9.75>');") );
}


void DisplayFuel()
{
  if (petrol == 1)
  {
    lcd.setCursor(15, 3);
    lcd.write(byte(1));
    lcd.write(byte(2));
    lcd.write(byte(3));
  }
  else if (diesel == 1)
  {
    lcd.setCursor(15, 3);
    lcd.write(byte(4));
    lcd.write(byte(5));
    lcd.write(byte(6));
  }
}

void LiveLogo()
{
  lcd.setCursor(19, 0);
  lcd.write(byte(6));


  lcd.setCursor(19, 1);
  lcd.write(byte(5));

  lcd.setCursor(19, 2);
  lcd.write(byte(7));

  lcd.setCursor(19, 3);
  lcd.write(byte(2));
}

@StefanL38 I have disabled the debug thing by setting those booleans to false, is that right?

I got this output:

11:50:59.616 -> entering HomePage()
11:50:59.616 -> entering get_Temperature() 
11:50:59.616 -> Temperature in degree Celsius = -273.15
11:50:59.616 -> temp in Float:-273.15
11:50:59.616 -> Resistance in ohms = 0.00
11:50:59.616 -> leaving get_Temperature() 
11:50:59.616 -> entering get_Weight() 
11:50:59.616 -> B: f_weight ~-8608.00~
11:50:59.616 -> leaving get_Weight() 
11:50:59.616 -> entering Density2_APIG()
11:50:59.801 -> Entering receiveFromESP32()
11:50:59.801 -> leaving receiveFromESP32()
11:50:59.801 -> leaving Density2_APIG()
11:50:59.848 -> A: apig ~inf~
11:50:59.848 -> leaving HomePage()
11:50:59.848 -> 
11:51:02.598 -> entering HomePage()
11:51:02.598 -> entering get_Temperature() 
11:51:02.598 -> Temperature in degree Celsius = -273.15
11:51:02.598 -> temp in Float:-273.15
11:51:02.598 -> Resistance in ohms = 0.00
11:51:02.598 -> leaving get_Temperature() 
11:51:02.598 -> entering get_Weight() 
11:51:02.598 -> B: f_weight ~-8608.00~
11:51:02.598 -> leaving get_Weight() 
11:51:02.598 -> entering Density2_APIG()
11:51:02.801 -> Entering receiveFromESP32()
11:51:02.801 -> leaving receiveFromESP32()
11:51:02.801 -> leaving Density2_APIG()
11:51:02.848 -> A: apig ~inf~
11:51:02.848 -> leaving HomePage()
11:51:02.848 -> 
11:51:05.604 -> entering HomePage()
11:51:05.604 -> entering get_Temperature() 
11:51:05.604 -> Temperature in degree Celsius = -273.15
11:51:05.604 -> temp in Float:-273.15
11:51:05.604 -> Resistance in ohms = 0.00
11:51:05.604 -> leaving get_Temperature() 
11:51:05.604 -> entering get_Weight() 
11:51:05.604 -> B: f_weight ~-8608.00~
11:51:05.604 -> leaving get_Weight() 
11:51:05.604 -> entering Density2_APIG()
11:51:05.792 -> Entering receiveFromESP32()
11:51:05.840 -> leaving receiveFromESP32()
11:51:05.840 -> leaving Density2_APIG()
11:51:05.840 -> A: apig ~inf~
11:51:05.840 -> leaving HomePage()
11:51:05.840 -> 
11:51:08.587 -> entering HomePage()
11:51:08.587 -> entering get_Temperature() 
11:51:08.587 -> Temperature in degree Celsius = -273.15
11:51:08.587 -> temp in Float:-273.15
11:51:08.587 -> Resistance in ohms = 0.00
11:51:08.587 -> leaving get_Temperature() 
11:51:08.587 -> entering get_Weight() 
11:51:08.587 -> B: f_weight ~-8608.00~
11:51:08.587 -> leaving get_Weight() 
11:51:08.587 -> entering Density2_APIG()
11:51:08.819 -> Entering receiveFromESP32()
11:51:08.819 -> leaving receiveFromESP32()
11:51:08.819 -> leaving Density2_APIG()
11:51:08.819 -> A: apig ~inf~
11:51:08.819 -> leaving HomePage()
11:51:08.819 -> 
11:51:11.572 -> entering HomePage()
11:51:11.572 -> entering get_Temperature() 
11:51:11.572 -> Temperature in degree Celsius = -273.15
11:51:11.572 -> temp in Float:-273.15
11:51:11.572 -> Resistance in ohms = 0.00
11:51:11.572 -> leaving get_Temperature() 
11:51:11.572 -> entering get_Weight() 
11:51:11.572 -> B: f_weight ~-8608.00~
11:51:11.572 -> leaving get_Weight() 
11:51:11.572 -> entering Density2_APIG()
11:51:11.806 -> Entering receiveFromESP32()
11:51:11.806 -> leaving receiveFromESP32()
11:51:11.806 -> leaving Density2_APIG()
11:51:11.853 -> A: apig ~inf~
11:51:11.853 -> leaving HomePage()

What I can see is that I get wrong readings for my loadcell as well as for my NTC reading, wy is that?

Thanks and regards

Hi Count,

I did a content-compare with TotalCommander and the lines of code that aquire the ADC-value and calculate temp are identical. Both codes use

const int thermistor_output = A0;

So I added debugOutput to each step of the "get-temperature-process" to narrow down where different values occure

I added a constant VersionInfo at the top of the code as another possability to identify the exact version
You should change the versioninfo string each time you start to modify the code
Then keep the version info until you do a substantial change

const char VersionInfo[] = "21-06-13-001-StefanL38";

here is the entire code I copied from your posting above
then added debugoutput inside

void get_Temperature()

The titles are numbered for clearly identfying which line is what output

As a first step I did not modify for I2C-display.
This means I only compiled to code but did no test on my setup
temperature = -273.15indicates that variable teperature is zero before the line

    temperature = temperature - 273.15; /* Temperature in degree Celsius */

So let's see through the debugout what happens .
Line by line

#include <HX711_ADC.h>
#include <SoftwareSerial.h>
#include <math.h>
/* coment this to change back to your LCD 
#include <Wire.h>
//#include <SBWire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4);  // adjust to the right I2C of your device
/* coment this to change back to your LCD */

const char VersionInfo[] = "21-06-13-001-StefanL38";

boolean debugActive = true;
#define debugTxtVar(myParameterText, variableName, mySuffix) \
        if (debugActive) { \
          Serial.print( F(#myParameterText " " #variableName" ~") ); \
          Serial.print(variableName); \
          Serial.println( F(#mySuffix) ); \
        } 


const byte SoftSerRX_Pin = 2;
const byte SoftSerTX_Pin = 3;
SoftwareSerial mySerial(SoftSerRX_Pin, SoftSerTX_Pin); // RX, TX

const int HX711_dout = A4; //mcu > HX711 dout pin
const int HX711_sck  = A5; //mcu > HX711 sck pin
boolean Flag;

HX711_ADC LoadCell(HX711_dout, HX711_sck);

const byte numChars = 128;

char receivedChars[numChars];
char tempChars[numChars];        // temporary array for use when parsing
boolean newData = false;
boolean newDataReady = false;

char rcESP32;
char receivedESP32Chars[numChars];
boolean newESP32Data = false;
boolean newESP32DataReady = false;
boolean recvESP32InProgress = false;

const int serialPrintInterval = 500; //increase value to slow down serial print activity
static unsigned long lastSerialTime;

float calibrationValue = 445.84;//437.27;//432.54;//436.01; // uncomment this if you want to set the calibration value in the sketch327.49
long TareOffset = 0; //8399692;//8382896;//8398661;//9532802;//8344838//8344747//8345467//8345516

boolean simulate_LoadCell = false;//true;
boolean simulate_NTC      = false;//true;

#include <LiquidCrystal.h>
LiquidCrystal lcd (10, 9, 8, 7, 6, 5);

#define BtnA 4
#define BtnB A1
#define BtnC A2
#define BtnD A3

int page = 0;

/*
#define THERMISTORPIN A0 // analog pin to connect Thermistor
#define NUMSAMPLES 5// Number of samples for Thermistor resistance values. Increase for greater accuracy.
#define SERIESRESISTOR 10000// R1 value
#define THERMISTORNOMINAL 10000 //Resistance of Thermistor at room temperature (Considered 25 C)     
#define TEMPERATURENOMINAL 25 //Considered normal room temperature
#define BCOEFFICIENT 3950//// The beta coefficient of the thermistor (usually 3000-4000)

int samples[NUMSAMPLES];//Array to hold samples for Thermistor resistance

float average;//Holds the average value of thermistor resistance
float steinhart;//To hold temperature in DegC.*/

int thermistor_adc_val;
double output_voltage, thermistor_resistance, therm_res_ln, temperature; 
float steinhart;//To hold temperature in DegC.*/
const int thermistor_output = A0;

int weight;
float f_weight;
float Density;
float apig;

int petrol = 0;
int diesel = 0;

int A_ButtonState = 0;
int B_ButtonState = 0;
int C_ButtonState = 0;
int D_ButtonState = 0;

int last_A_ButtonState = 0;
int last_B_ButtonState = 0;
int last_C_ButtonState = 0;
int last_D_ButtonState = 0;

volatile boolean SwA = false;
volatile boolean SwB = false;
volatile boolean SwC = false;
volatile boolean SwD = false;

unsigned long previousMillis = 0;
const long interval = 60000;
unsigned long currentMillis;

unsigned long previousMillis1 = 0;
const long interval1 = 5000;
unsigned long currentMillis1;

byte Degs[] = {B00110, B01001, B01001, B00110, B00000, B00000, B00000, B00000};

byte P[] = {B11111, B10001, B10101, B10001, B10111, B10111, B10111, B11111};

byte E[] = {B11111, B10001, B10111, B10011, B10011, B10111, B10001, B11111};

byte T[] = {B11111, B10001, B11011, B11011, B11011, B11011, B11011, B11111};

byte D[] = {B11111, B10011, B10101, B10101, B10101, B10101, B10011, B11111};

byte I[] = {B11111, B10001, B11011, B11011, B11011, B11011, B10001, B11111};

byte L[] = {B11111, B10111, B10111, B10111, B10111, B10111, B10001, B11111};

byte V[] = {B11111, B10101, B10101, B10101, B10101, B10101, B11011, B11111};

void PrintFileNameDateTime() {
  Serial.println("Code running comes from file ");
  Serial.println(__FILE__);
  Serial.print("  compiled ");
  Serial.print(__DATE__);
  Serial.print(" ");
  Serial.println(__TIME__);  
  Serial.print ("VersionInfo ");
  Serial.println(VersionInfo);
}


boolean TimePeriodIsOver (unsigned long &periodStartTime, unsigned long TimePeriod) {
  unsigned long currentMillis  = millis();  
  if ( currentMillis - periodStartTime >= TimePeriod )
  {
    periodStartTime = currentMillis; // set new expireTime
    return true;                // more time than TimePeriod) has elapsed since last time if-condition was true
  } 
  else return false;            // not expired
}

unsigned long MyTestTimer = 0;                   // variables MUST be of type unsigned long
const byte    OnBoard_LED = 13;


void BlinkHeartBeatLED(int IO_Pin, int BlinkPeriod) {
  static unsigned long MyBlinkTimer;
  pinMode(IO_Pin, OUTPUT);
  
  if ( TimePeriodIsOver(MyBlinkTimer,BlinkPeriod) ) {
    digitalWrite(IO_Pin,!digitalRead(IO_Pin) ); 
  }
}

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

void checkIf_A_ButtonIsPressed()
{
  if (A_ButtonState != last_A_ButtonState)
  {
    if (A_ButtonState == 0)
    {
      SwA = true;
    }
    delay(30);
  }
  last_A_ButtonState = A_ButtonState;
}

//---------------------------------------------
void checkIf_B_ButtonIsPressed()
{
  if (B_ButtonState != last_B_ButtonState)
  {
    if (B_ButtonState == 0)
    {
      SwB = true;

    }
    delay(30);
  }
  last_B_ButtonState = B_ButtonState;
}

//----------------------------------------------------
void checkIf_C_ButtonIsPressed()
{
  if (C_ButtonState != last_C_ButtonState)
  {
    if (C_ButtonState == 0)
    {
      SwC = true;

    }
    delay(30);
  }
  last_C_ButtonState = C_ButtonState;
}

//----------------------------------------------------
void checkIf_D_ButtonIsPressed()
{
  if (D_ButtonState != last_D_ButtonState)
  {
    if (D_ButtonState == 0)
    {
      SwD = true;
    }
    delay(30);
  }
  last_D_ButtonState = D_ButtonState;
}
//---------------------------------------------------------------------------------------------------------------------------------
void setup() {
  Serial.begin(115200);
  Serial.println("Setup-Start");
  PrintFileNameDateTime();
  mySerial.begin(9600);

  pinMode(BtnA, INPUT_PULLUP);
  pinMode(BtnB, INPUT_PULLUP);
  pinMode(BtnC, INPUT_PULLUP);
  pinMode(BtnD, INPUT_PULLUP);

  analogReference(EXTERNAL);
  Serial.println("analogReference(EXTERNAL) done");

  lcd.begin(20, 4);
 /* lcd.init();                      // initialize the lcd
  lcd.backlight();                 //Backlight ON if under program control
*/
  Serial.println("lcd.begin creating chars");
  lcd.createChar(0, Degs);
  lcd.createChar(1, P);
  lcd.createChar(2, E);
  lcd.createChar(3, T);
  lcd.createChar(4, D);
  lcd.createChar(5, I);
  lcd.createChar(6, L);
  lcd.createChar(7, V);
  Serial.println( F("lcd.begin creating chars done") );

  Serial.println( F("LoadCell.begin()") );
  LoadCell.begin();
  Serial.println( F("LoadCell.begin() done") );
  long stabilizingtime = 2000; // preciscion right after power-up can be improved by adding a few seconds of stabilizing time
  boolean _tare = false; //set this to false if you don't want tare to be performed in the next step

  Serial.print( F("LoadCell.start(") );
  Serial.print(stabilizingtime);
  Serial.print( F(",") );
  Serial.print(_tare);
  Serial.println( F(");") );
  LoadCell.start(stabilizingtime, _tare);
  Serial.println( F("LoadCell.start( done") );

  lcd.begin(20, 4);

  lcd.setCursor(5, 0);
  lcd.print( F("Welcome to") );
  lcd.setCursor(2, 1);
  lcd.print( F("Ready Automotive") );
  lcd.setCursor(0, 3);
  lcd.print( F("Technology Solutions") );

  Serial.println( F("Welcome to Ready Automotive Technology Solutions") );

  delay(3000);
  lcd.clear();

  Serial.println( F("execute LoadCell.getTareTimeoutFlag()") );
  if (LoadCell.getTareTimeoutFlag())
  {
    Serial.println( F("Timeout, check MCU>HX711 wiring and pin designations") );
    while (1);
  }
  else {
    Serial.println( F("LoadCell.getTareTimeoutFlag() returns false") );
    LoadCell.setCalFactor(calibrationValue); // set calibration value (float)
    Serial.print( F("LoadCell.setCalFactor(") );
    Serial.print(calibrationValue);
    Serial.println( F(")") );
    
    LoadCell.setTareOffset(TareOffset);
    Serial.print( F("LoadCell.setTareOffset(") );
    Serial.print(TareOffset);
    Serial.println( F(")") );
    Serial.println( F("Startup is complete") );
  }

}

void loop() {
  BlinkHeartBeatLED(OnBoard_LED,500);

  if ( TimePeriodIsOver(MyTestTimer,3000) ) {
    HomePage();
    Serial.println();
  }  
  
  A_ButtonState = digitalRead(BtnA);
  B_ButtonState = digitalRead(BtnB);
  C_ButtonState = digitalRead(BtnC);
  D_ButtonState = digitalRead(BtnD);

  checkIf_A_ButtonIsPressed();
  checkIf_B_ButtonIsPressed();
  checkIf_C_ButtonIsPressed();
  checkIf_D_ButtonIsPressed();

  if (SwA)//Select Petrol
  {
    SwA = false;//reset key status
    LoadCell.tareNoDelay();
    petrol = 1;
    diesel = 0;
    if (LoadCell.getTareStatus() == true)
    {
      //select fuel
      Serial.println( F("A key pressed, Petrol Selected") );
    }
  }

  if (SwB)//Select diesel
  {
    SwB = false;//reset key status
    LoadCell.tareNoDelay();
    //select fuel
    petrol = 0;
    diesel = 1;
    if (LoadCell.getTareStatus() == true)
    {
      Serial.println( F("B key pressed, Diesel Selected") );
    }

  }

  if (SwC)//Show density and API-G
  {
    SwC = false;
    Density2_APIG();//call a function to send data to NODE MCU;
  }

  if (SwD) //Activate live Mode
  {
    SwD = false;
    LiveLogo();
    Serial.print( F("D key pressed, Live Mode ON") );
  }

}

void get_Temperature() {
  Serial.println( F("entering get_Temperature() ") );

  uint8_t i;

  if (!simulate_NTC)
  {
    debugTxtVar(GetT0:,simulate_NTC,~);
    
    thermistor_adc_val = analogRead(thermistor_output);
    debugTxtVar(GetT1:,thermistor_adc_val,~);

    output_voltage = ( (thermistor_adc_val * 5.0) / 1023.0 );
    debugTxtVar(GetT2:,output_voltage,~);
    
    thermistor_resistance = ( ( 5 * ( 10.0 / output_voltage ) ) - 10 ); /* Resistance in kilo ohms */
    debugTxtVar(GetT3:,thermistor_resistance,~);

    thermistor_resistance = thermistor_resistance * 1000 ; /* Resistance in ohms   */
    debugTxtVar(GetT4:,thermistor_resistance,~);

    therm_res_ln = log(thermistor_resistance);
    debugTxtVar(GetT5:,therm_res_ln,~);
  /*  Steinhart-Hart Thermistor Equation: */
  /*  Temperature in Kelvin = 1 / (A + B[ln(R)] + C[ln(R)]^3)   */
  /*  where A = 0.001129148, B = 0.000234125 and C = 8.76741*10^-8  */
    temperature = ( 1 / ( 0.001129148 + ( 0.000234125 * therm_res_ln ) + ( 0.0000000876741 * therm_res_ln * therm_res_ln * therm_res_ln ) ) ); /* Temperature in Kelvin */
    debugTxtVar(GetT6:,temperature,~);

    temperature = temperature - 273.15; /* Temperature in degree Celsius */
    debugTxtVar(GetT7:,temperature,~);

    steinhart = temperature;
    Serial.print("Temperature in degree Celsius = ");
    Serial.println(temperature);
    Serial.print("temp in Float:");
    Serial.println(steinhart);
    Serial.print("Resistance in ohms = ");
    Serial.println(thermistor_resistance);
  } // if (!simulate_NTC) {
  else { // NTC sensor is simulated assign a demo-temperature
    if (steinhart == 40.0) {
      steinhart = 0.0;        
    }
    else { 
    steinhart = 40.0; 
    }
  }
  Serial.println( F("leaving get_Temperature() ") );
}


void get_Weight() {
   Serial.println( F("entering get_Weight() ") );

  if (!simulate_LoadCell) {
    // check for new data/start next conversion:
    if (LoadCell.update()) newDataReady = true;
  
    // get smoothed value from the dataset:
    if (newDataReady)
    {
      if (millis() - lastSerialTime >= serialPrintInterval)
      {
        weight = LoadCell.getData();
        //f_weight = weight;
        /*  Serial.print("Load cell output value:");
          Serial.println(f_weight);*/
        newDataReady = 0;
        lastSerialTime = millis();
      }
    }
  
    f_weight = weight * 10;//convert 100ml weight to 1ltr weight
    debugTxtVar(B:,f_weight,~);
  } //if (!Simulate_LoadCell) { 
  else { // load-cell gets simulated assign a demo-value
    if (f_weight == 654) { 
      f_weight = 800;
    }  
    else {
      f_weight = 654;   
    }
  }
  Serial.println( F("leaving get_Weight() ") );
}

void receiveFromESP32() {
  Serial.println( F("Entering receiveFromESP32()") );

  static char rc;
  static char startMarker = '<';
  static char endMarker = '>';

  static int ndx;
  
  while (mySerial.available() > 0 && newESP32Data == false) {
    rc = mySerial.read();
      debugTxtVar(receiveFromESP32,rc,~);
    if (recvESP32InProgress == true) {
      if (rc != endMarker) {
        receivedESP32Chars[ndx] = rc;
        ndx++;
        if (ndx >= numChars) {
          ndx = numChars - 1;
        }
      }
      else {
        receivedESP32Chars[ndx] = '\0'; // terminate the string
        recvESP32InProgress = false;
        ndx = 0;
        newESP32Data = true;
        //debugTxtVar(first parameter , variablename, suffix);
        debugTxtVar(1: ,receivedESP32Chars,~);
      }
    }

    else if (rc == startMarker) {
      recvESP32InProgress = true;
    }
  }
 Serial.println( F("leaving receiveFromESP32()") ); 
}


void Density2_APIG() {
  Serial.println( F("entering Density2_APIG()") );
  
  mySerial.print("<");
  mySerial.print(f_weight);
  mySerial.print(",");
  mySerial.print(steinhart);
  mySerial.print(">");

  delay(200);  //wait to hear from NODE-MCU
  receiveFromESP32();
  if (newESP32Data) {
    newESP32Data = false;
    Density = atof(receivedESP32Chars);
    
    Serial.print("Density: ");
    Serial.println(Density);

    lcd.setCursor(9, 2);
    lcd.print(Density);
    //Flag = false;
  }
  //calculate API-G and display it on LCD
  Serial.println( F("leaving Density2_APIG()") );
}


void HomePage() {
  Serial.println( F("entering HomePage()") );
  get_Temperature();
  get_Weight();
  Density2_APIG();
  lcd.setCursor(0, 0);
  lcd.print("H.Meter: ");
  lcd.print(f_weight);

  lcd.setCursor(0, 1);
  lcd.print("Temp: ");
  lcd.print(steinhart);
  lcd.write(byte(0));
  lcd.print("C");

  lcd.setCursor(0, 2);
  lcd.print("Density: ");
  lcd.print(Density);
  lcd.print("g/ml");

  apig = (141500.0 / Density) - 131.5;
  debugTxtVar(A: ,apig,~);

  lcd.setCursor(0, 3);
  lcd.print("API-G: ");
  lcd.print(apig);
  DisplayFuel();

  Serial.println( F("leaving HomePage()") );
  //mySerial.println("<653,-9.75>");
  //Serial.println( F("mySerial.println('<653,-9.75>');") );
}


void DisplayFuel()
{
  if (petrol == 1)
  {
    lcd.setCursor(15, 3);
    lcd.write(byte(1));
    lcd.write(byte(2));
    lcd.write(byte(3));
  }
  else if (diesel == 1)
  {
    lcd.setCursor(15, 3);
    lcd.write(byte(4));
    lcd.write(byte(5));
    lcd.write(byte(6));
  }
}

void LiveLogo()
{
  lcd.setCursor(19, 0);
  lcd.write(byte(6));


  lcd.setCursor(19, 1);
  lcd.write(byte(5));

  lcd.setCursor(19, 2);
  lcd.write(byte(7));

  lcd.setCursor(19, 3);
  lcd.write(byte(2));
}

First, I re calibrated my load cell and found out that my tare offset value was 0 in the sketch we were trying, so I set it to the calibrated value. But still my temperature values were getting messed up and I also noticed that key presses are taking longer time than what I was actually expecting. I don't know what was causing this, but then...
I don't know what crossed my mind and I just went on disabling all the debug commands and the code started working just fine!

Now, my biggest concern is that I want to keep all those debug stuff it's really helpful to know what's happening there inside the core, we can actually see it! and its just amazing!

Now, I have got two sketches one with the debug that gives me wrong values and one without debug that gives me correct values. How do I get correct values with debug?

Post both codeversions and post what debug-output you get.
best regards Stefan