Problem with lcd switching esp32

am making a multimeter that sends voltage data of 4pins with 1hour intervals.
So the thing is I wanted to switch modes by pressing a gpio14 with gnd but why I don't know without even pressing the gnd+gpio14 button (reset button) the modes switch command is shown in serial monitor like in milliseconds .and
There's no other problems .
.

const char simPIN[]   = "";

// Please select the corresponding model

// #define SIM800L_IP5306_VERSION_20190610
#define SIM800L_AXP192_VERSION_20200327
// #define SIM800C_AXP192_VERSION_20200609
// #define SIM800L_IP5306_VERSION_20200811

String SMS_Message;
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Define the serial console for debug prints, if needed
#define DUMP_AT_COMMANDS

#include<string.h>
#define RST_PIN 14  // GPIO pin connected to the RST switch
#define SDA_PIN 33  // GPIO pin connected to the SDA pin of the LCD
#define SCL_PIN 32  // GPIO pin connected to the SCL pin of the LCD
#define I2C_SDA              21
#define I2C_SCL              22
#define LCD_ADDRESS 0x23  // I2C address for the LCD
#define LCD_COLS 20
#define LCD_ROWS 4
#define SMS_TARGET_1 "+919******923"
#define SMS_TARGET_2 "+919*3300**09"
#define SMS_TARGET_3 "+9194*3103**6"
// Configure TinyGSM library
#define TINY_GSM_MODEM_SIM800      // Modem is SIM800
#define TINY_GSM_RX_BUFFER   1024  // Set RX buffer to 1Kb
#include <Wire.h>
#include <TinyGsmClient.h>
// TTGO T-Call pins
#define MODEM_RST            5
#define MODEM_PWKEY          4
#define MODEM_POWER_ON       23
#define MODEM_TX             27
#define MODEM_RX             26
char* lower = "";
char* response = " ";
String res = "";
bool new_data = 0;
int relay = 0;

#define SMS_TARGET_1  "+919******923"
char* msg = "+cmt: \"+919433103566\"";
char* msg2 = "+cmt: \"+919836556923\"";
char* msg3 = "+cmt: \"+919333004309\"";

char* ring = "+clip: \"+919******923\"";
#define SMS_TARGET_2  "+919333004309"
#define SMS_TARGET_3 "+919433103566"

char* value1 = "SEND";
char* value0 = "GETNET";

#define relay_pin 18

// Set serial for debug console (to Serial Monitor, default speed 115200)
#define SerialMon Serial
// Set serial for AT commands (to SIM800 module)
#define SerialAT  Serial1
// Define the serial console for debug prints, if needed
//#define DUMP_AT_COMMANDS
#ifdef DUMP_AT_COMMANDS
#include <StreamDebugger.h>
StreamDebugger debugger(SerialAT, SerialMon);
TinyGsm modem(debugger);
#else
TinyGsm modem(SerialAT);
#endif
#include <LiquidCrystal_I2C.h>
#define IP5306_ADDR          0x75
#define IP5306_REG_SYS_CTL0  0x00                             
bool setPowerBoostKeepOn(int en) {                          
  Wire.beginTransmission(IP5306_ADDR);                    
  Wire.write(IP5306_REG_SYS_CTL0);                     
  if (en) {
    Wire.write(0x37); // Set bit1: 1 enable 0 disable boost keep on
  } else {
    Wire.write(0x35); // 0x37 is default reg value
  }
  return Wire.endTransmission() == 0;
}

LiquidCrystal_I2C lcd(LCD_ADDRESS, LCD_COLS, LCD_ROWS);  // Address of the I2C module may vary, use i2c scanner to find it

const int voltagePinA = 39;   // Analog input pin for voltage A measurement--12--
const int voltagePinB = 12;   // Analog input pin for voltage B measurement (GPI00)
const int voltagePinC = 2;   // Analog input pin for voltage C measurement (GPI02)
const int voltagePinD = 36;  // Analog input pin for voltage D measurement (GPI015)

unsigned long previousMillis = 0;
const long interval = 60000*60;  // Interval to measure voltages (60 seconds)

int mode = 1; // Default mode is 1
bool displayMeasurements = true;  // Flag to indicate whether to display measurements or "Mode 2"
void updateSerial();
void setup() {
    // Your setup code here
    {
  Serial.begin(115200);
  // Keep power when running from battery
  Wire.begin(I2C_SDA, I2C_SCL);
  bool isOk = setPowerBoostKeepOn(1);
  SerialMon.println(String("IP5306 KeepOn ") + (isOk ? "OK" : "FAIL"));
  // Set modem reset, enable, power pins
  pinMode(MODEM_PWKEY, OUTPUT);
  pinMode(MODEM_RST, OUTPUT);
  pinMode(MODEM_POWER_ON, OUTPUT);
  digitalWrite(MODEM_PWKEY, LOW);
  digitalWrite(MODEM_RST, HIGH);
  digitalWrite(MODEM_POWER_ON, HIGH);
///////////////////////////////////////////////////
  // LCD setup
  lcd.begin(LCD_COLS, LCD_ROWS);  // Initialize the LCD
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print("Voltage A : -");
  lcd.setCursor(0, 1);
  lcd.print("Voltage B : -");
  lcd.setCursor(0, 2);
  lcd.print("Voltage C : -");
  lcd.setCursor(0, 3);
 lcd.print("Cell Volt :  ");
  /////////////////////////////////////////////////
  // Set GSM module baud rate and UART pins
  SerialAT.begin(115200, SERIAL_8N1, MODEM_RX, MODEM_TX);
  delay(3000);
  // Restart SIM800 module, it takes quite some time
  // To skip it, call init() instead of restart()
  SerialMon.println("Initializing modem...");
  modem.restart();
  // use modem.init() if you don't need the complete restart
  // Unlock your SIM card with a PIN if needed
  if (strlen(simPIN) && modem.getSimStatus() != 3 ) {
    modem.simUnlock(simPIN);
      String modemInfo = modem.getModemInfo();
  SerialMon.print("Modem Info: ");
  SerialMon.println(modemInfo);
  delay(1000);

  SerialAT.println("AT"); //Once the handshake test is successful, it will back to OK
  updateSerial();

  SerialAT.println("AT+CMGF=1"); // Configuring TEXT mode
  updateSerial();
  SerialAT.println("AT+CNMI=1,2,0,0,0"); // Decides how newly arrived SMS messages should be handled
  updateSerial();
  }

    // Measure voltage A
    int sensorValueA = analogRead(voltagePinA);
     float voltageA = sensorValueA > 0 ? (sensorValueA * (3.3 / 4095.0)) + 0.12 : 0; 

    // Measure voltage B
    int sensorValueB = analogRead(voltagePinB);
     float voltageB = sensorValueB > 0 ? (sensorValueB * (3.3 / 4095.0)) + 0.12 : 0; 

    // Measure voltage C
    int sensorValueC = analogRead(voltagePinC);
    float voltageC = sensorValueC > 0 ? (sensorValueC * (3.3 / 4095.0)) + 0.12 : 0; 

    // Measure voltage D
    int sensorValueD = analogRead(voltagePinD);
float voltageD = sensorValueD > 0 ? (sensorValueD * ((3.3 / 4095.0) * 9.24812030075188)): 0;


    // Display voltages on LCD
    lcd.setCursor(0, 0);
    lcd.print("Voltage A : -");
    lcd.print(String(voltageA, 3) + "V");

    lcd.setCursor(0, 1);
    lcd.print("Voltage B : -");
    lcd.print(String(voltageB, 3) + "V");

    lcd.setCursor(0, 2);
    lcd.print("Voltage C : -");
    lcd.print(String(voltageC, 3) + "V");

    lcd.setCursor(0, 3);
   lcd.print("Cell Volt :  ");
    lcd.print(String(voltageD, 2) + "V");

    // Display voltages on Serial Monitor
    Serial.print("Voltage A : -");
    Serial.print(voltageA, 3);
    Serial.println("V");

    Serial.print("Voltage B : -");
    Serial.print(voltageB, 3);
    Serial.println("V");

    Serial.print("Voltage C : -");
    Serial.print(voltageC, 3);
    Serial.println("V");

    Serial.print("Cell Volt :    ");
    Serial.print(voltageD, 2);
    Serial.println("V");

    // To send an SMS, call modem.sendSMS(SMS_TARGET, smsMessage)
    String smsMessage = "Voltage Measurements boot:\n";
    smsMessage += "A : -" + String(voltageA, 3) + "V\n";
    smsMessage += "B : -" + String(voltageB, 3) + "V\n";
    smsMessage += "C : -" + String(voltageC, 3) + "V\n";
    smsMessage += "Cell Volt : " + String(voltageD, 2) + "V\n";
    if (modem.sendSMS(SMS_TARGET_1, smsMessage)) {//sendVoltages(SMS_TARGET_1);
        SerialMon.println(smsMessage);
    }
    if (modem.sendSMS(SMS_TARGET_2, smsMessage)) {//sendVoltages(SMS_TARGET_2);
        SerialMon.println(smsMessage);
    }
    if (modem.sendSMS(SMS_TARGET_3, smsMessage)) {//sendVoltages(SMS_TARGET_3);
        SerialMon.println(smsMessage);
    }
    
    else {
        SerialMon.println("SMS failed to send");
    }

    // Delete all SMS messages after sending
    deleteAllSMS();    

    SerialAT.println("AT"); //Once the handshake test is successful, it will back to OK
    updateSerial();
    delay(200);
    SerialAT.println("AT+CMGF=1"); // Configuring TEXT mode
    updateSerial();
    delay(200);
    SerialAT.println("AT+CNMI=2,2,0,0,0"); // Decides how newly arrived SMS messages should be handled
    updateSerial();
    delay(2000);
    /////////////////////
}}
char smsBuffer[250]; // Buffer to store SMS messages

unsigned long automaticMeasurementInterval = 10000*6*60;  // Automatic measurement interval in milliseconds
unsigned long automaticMeasurementPreviousMillis = 0;

const long lcdInterval = 10000;  // Interval to display voltages on LCD (10 seconds)
unsigned long lcdPreviousMillis = 0;



void sendVoltages(); // Declare the function



void automaticVoltageMeasurements();  // Declare the function


void loop() {
    updateSerial();
    static String currentLine = "";


    // Measure voltages every 5 seconds
// Measure voltages every 60 seconds
   unsigned long currentMillis = millis();

    // Update LCD every 10 seconds
    if (currentMillis -lcdPreviousMillis >= lcdInterval) {
        lcdPreviousMillis = currentMillis;

    // Measure voltage A
    int sensorValueA = analogRead(voltagePinA);
     float voltageA = sensorValueA > 0 ? (sensorValueA * (3.3 / 4095.0)) + 0.12 : 0; 

    // Measure voltage B
    int sensorValueB = analogRead(voltagePinB);
     float voltageB = sensorValueB > 0 ? (sensorValueB * (3.3 / 4095.0)) + 0.12 : 0; 

    // Measure voltage C
    int sensorValueC = analogRead(voltagePinC);
    float voltageC = sensorValueC > 0 ? (sensorValueC * (3.3 / 4095.0)) + 0.12 : 0; 

    // Measure voltage D
    int sensorValueD = analogRead(voltagePinD);
float voltageD = sensorValueD > 0 ? (sensorValueD * ((3.3 / 4095.0) * 9.24812030075188)): 0;


        // Display voltages on Serial Monitor
        Serial.print("Voltage A : -");
        Serial.print(voltageA, 3);
        Serial.println("V");

        Serial.print("Voltage B : -");
        Serial.print(voltageB, 3);
        Serial.println("V");

        Serial.print("Voltage C : -");
        Serial.print(voltageC, 3);
        Serial.println("V");

        Serial.print("Cell Volt :    ");
        Serial.print(voltageD, 2);
        Serial.println("V");
        

        // Display voltages on LCD only in Mode 1
        if (mode == 1) {
       // Display voltages on LCD

       



       
    lcd.setCursor(0, 0);
    lcd.print("Voltage A : -");
    lcd.print(String(voltageA, 3) + "V");

    lcd.setCursor(0, 1);
    lcd.print("Voltage B : -");
    lcd.print(String(voltageB, 3) + "V");

    lcd.setCursor(0, 2);
    lcd.print("Voltage C : -");
    lcd.print(String(voltageC, 3) + "V");

    lcd.setCursor(0, 3);
   lcd.print("Cell Volt :  ");
    lcd.print(String(voltageD, 2) + "V");

        }
    }

    // Check if the button is pressed to switch modes
    if (digitalRead(RST_PIN) == LOW) {
        delay(100);  // Debounce delay
        if (digitalRead(RST_PIN) == LOW) {
            mode = (mode == 1) ? 2 : 1; // Toggle between Mode 1 and Mode 2
            lcd.clear(); // Clear the display
            if (mode == 1) {
                // Display initial message in Mode 1
                lcd.setCursor(0, 0);
                lcd.print("Voltage A : --");
                lcd.setCursor(0, 1);
                lcd.print("Voltage B : --");
                lcd.setCursor(0, 2);
                lcd.print("Voltage C : --");
                lcd.setCursor(0, 3);
                lcd.print("Cell Volt :    -");
            } else {
              int rssi = modem.getSignalQuality();
    int dbm = rssi -95;  // Offset to get dBm value

    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Network Strength:");
    lcd.setCursor(0, 1);
    lcd.print("dBm  =");
    if (dbm >= 0) {
        lcd.print(" ");
    }
    lcd.print(dbm);

    lcd.setCursor(0, 2);
    lcd.print("RSSI = ");
    lcd.print(rssi);
}
            delay(1000); // Delay to avoid multiple detections
        }
    }

    // Automatic voltage measurements every 60 seconds
    if (currentMillis -automaticMeasurementPreviousMillis >= automaticMeasurementInterval) {
        automaticMeasurementPreviousMillis = currentMillis;
        automaticVoltageMeasurements();
    }
}

void automaticVoltageMeasurements() {
    // Measure voltage A, B, C, D and send SMS
    sendVoltages();
}
void sendVoltages() {
    // Measure voltage A
    // Measure voltage A
    int sensorValueA = analogRead(voltagePinA);
     float voltageA = sensorValueA > 0 ? (sensorValueA * (3.3 / 4095.0)) + 0.12 : 0; 

    // Measure voltage B
    int sensorValueB = analogRead(voltagePinB);
     float voltageB = sensorValueB > 0 ? (sensorValueB * (3.3 / 4095.0)) + 0.12 : 0; 

    // Measure voltage C
    int sensorValueC = analogRead(voltagePinC);
    float voltageC = sensorValueC > 0 ? (sensorValueC * (3.3 / 4095.0)) + 0.12 : 0; 

    // Measure voltage D
    int sensorValueD = analogRead(voltagePinD);
float voltageD = sensorValueD > 0 ? (sensorValueD * ((3.3 / 4095.0) * 9.24812030075188)): 0;


    // Display voltages on Serial Monitor
    Serial.print("Voltage A : -");
    Serial.print(voltageA, 3);
    Serial.println("V");

    Serial.print("Voltage B : -");
    Serial.print(voltageB, 3);
    Serial.println("V");

    Serial.print("Voltage C : -");
    Serial.print(voltageC, 3);
    Serial.println("V");

    Serial.print("Cell Volt :    ");
    Serial.print(voltageD, 2);
    Serial.println("V");

    // Display voltages on LCD
    lcd.clear();  // Clear the LCD screen

    lcd.setCursor(0, 0);
    lcd.print("Voltage A : -");
    lcd.print(String(voltageA, 3) + "V");

    lcd.setCursor(0, 1);
    lcd.print("Voltage B : -");
    lcd.print(String(voltageB, 3) + "V");

    lcd.setCursor(0, 2);
    lcd.print("Voltage C : -");
    lcd.print(String(voltageC, 3) + "V");

    lcd.setCursor(0, 3);
   lcd.print("Cell Volt :  ");
    lcd.print(String(voltageD, 2) + "V");

    // To send an SMS, call modem.sendSMS(SMS_TARGET, smsMessage)
    String smsMessage = "Voltage Measurements:\n";
    smsMessage += "A : -" + String(voltageA, 3) + "V\n";
    smsMessage += "B : -" + String(voltageB, 3) + "V\n";
    smsMessage += "C : -" + String(voltageC, 3) + "V\n";
    smsMessage += "Cell Volt : " + String(voltageD, 2) + "V\n";

    if (modem.sendSMS(SMS_TARGET_1, smsMessage)) {
        SerialMon.println(smsMessage);
    } else {
        SerialMon.println("SMS failed to send");
    }
/////MEASURE AGAIN

    if (modem.sendSMS(SMS_TARGET_2, smsMessage)) {
        SerialMon.println(smsMessage);
    } else {
        SerialMon.println("SMS failed to send");
    }

    if (modem.sendSMS(SMS_TARGET_3, smsMessage)) {
        SerialMon.println(smsMessage);
    } else {
        SerialMon.println("SMS failed to send");
    }

  // Delete all SMS messages after sending
    deleteAllSMS();    
}
///////////
void relay_control(char* lower)
{

if(strstr(lower, msg) || strstr(lower, msg2) || strstr(lower, msg3))

  {
    String source = (String)lower;
    char* desti = &source[0];
    int i = 0;
    while (desti[i+2] != '\n' )
      i++;
    String destin = (String)desti;
    source = source.substring(51);
    
    Serial.print("Your message is = ");
    Serial.print(source);
    char* relay1 = &source[0];
    if(strstr(relay1, value1))
    {
     
      res = modem.sendSMS(SMS_TARGET_1, String("Message Processing On Get Response"));
      sendreplyVoltages(); // Send voltage measurements as a reply
      DBG("SMS:", res ? "OK" : "fail");
      relay = 1;
      return; 
            res = modem.sendSMS(SMS_TARGET_2, String("Message Processing On Get Response"));
      sendreplyVoltages();
       // Send voltage measurements as a reply
      DBG("SMS:", res ? "OK" : "fail");
      relay = 1;
      return; 

          res = modem.sendSMS(SMS_TARGET_3, String("Message Processing On Get Response"));
      sendreplyVoltages();
    // Send voltage measurements as a reply
      DBG("SMS:", res ? "OK" : "fail");
      relay = 1;
      return; 
    }
/////////////////////////////////////////////////////////////////////////////////
  {
    digitalWrite(relay_pin, LOW);
      res = modem.sendSMS(SMS_TARGET_1, String("Message Processing On Get Response"));
      sendreplyVoltages();
 // Send voltage measurements as a reply
      DBG("SMS:", res ? "OK" : "fail");
      relay = 1;
      return; 
            res = modem.sendSMS(SMS_TARGET_2, String("Message Processing On Get Response"));
      sendreplyVoltages();
       // Send voltage measurements as a reply
      DBG("SMS:", res ? "OK" : "fail");
      relay = 1;
      return; 

          res = modem.sendSMS(SMS_TARGET_3, String("Message Processing On Get Response"));
      sendreplyVoltages();
     // Send voltage measurements as a reply
      DBG("SMS:", res ? "OK" : "fail");
      relay = 1;
      return; 
  }
///////////////////////////////////////////////////////////////////////////******
  }

  if (strstr(lower, ring))
  {
    bool res = modem.callHangup();
    DBG("Hang up:", res ? "OK" : "fail"); 
    if(relay == 1)
    {
    
      relay = 0;
      sendreplyVoltages(); // Send voltage measurements as a reply
      return;    
    }
  }
}


void updateSerial() {
    delay(500);
  while (Serial.available())
  {
    SerialAT.write(Serial.read());//Forward what Serial received to Software Serial Port
  }
  while (SerialAT.available())
  {
    //Serial.write(SerialAT.read());//Forward what Software Serial received to Serial Port

    char add = SerialAT.read();
    res = res + add;
    delay(1);
    new_data = 1;
  }

  if (new_data) {
    response = &res[0];
    //-------------------------------------------------Converting every character of the String in lower form
    const int length = strlen( response ); // get the length of the text
    lower = ( char* )malloc( length + 1 ); // allocate 'length' bytes + 1 (for null terminator) and cast to char*
    lower[ length ] = 0; // set the last byte to a null terminator
    //-------------------------------------------------copy all character bytes to the new buffer using tolower
    for ( int i = 0; i < length; i++ )
    {
      lower[ i ] = tolower( response[ i ] );
    }
    Serial.println(lower);// printin the String in lower character form
    Serial.println("\n");
    relay_control( lower);
    response = "";
    res = "";
    lower = "";
    new_data = 0;

  }
  while (SerialAT.available()) {
    char c = SerialAT.read();
    Serial.write(c);
  }
}
void sendreplyVoltages() {
    // Measure voltage A
    int sensorValueA = analogRead(voltagePinA);
     float voltageA = sensorValueA > 0 ? (sensorValueA * (3.3 / 4095.0)) + 0.12 : 0; 

    // Measure voltage B
    int sensorValueB = analogRead(voltagePinB);
     float voltageB = sensorValueB > 0 ? (sensorValueB * (3.3 / 4095.0)) + 0.12 : 0; 

    // Measure voltage C
    int sensorValueC = analogRead(voltagePinC);
    float voltageC = sensorValueC > 0 ? (sensorValueC * (3.3 / 4095.0)) + 0.12 : 0; 

    // Measure voltage D
    int sensorValueD = analogRead(voltagePinD);
float voltageD = sensorValueD > 0 ? (sensorValueD * ((3.3 / 4095.0) * 9.24812030075188)): 0;

int rssi = modem.getSignalQuality();
    int dbm = rssi -95;  // Offset to get dBm value


    // To send an SMS, call modem.sendSMS(SMS_TARGET, smsMessage)
    String smsMessage = "Voltage Measurements:\n";
    smsMessage += "A : -" + String(voltageA, 3) + "V\n";
    smsMessage += "B : -" + String(voltageB, 3) + "V\n";
    smsMessage += "C : -" + String(voltageC, 3) + "V\n";
    smsMessage += "Cell Volt : " + String(voltageD, 2) + "V\n";
     smsMessage += "Network Strength";
    smsMessage += "dBm: " + String(dbm) + "\n";
    smsMessage += "RSSI: " + String(rssi);

    if (modem.sendSMS(SMS_TARGET_1, smsMessage)) {
        SerialMon.println(smsMessage);
    } else {
        SerialMon.println("SMS failed to send");
    }
/////MEASURE AGAIN

    if (modem.sendSMS(SMS_TARGET_2, smsMessage)) {
        SerialMon.println(smsMessage);
    } else {
        SerialMon.println("SMS failed to send");
    }

       if (modem.sendSMS(SMS_TARGET_3, smsMessage)) {
        SerialMon.println(smsMessage);
    } else {
        SerialMon.println("SMS failed to send");
    } 

  // Delete all SMS messages after sending
    deleteAllSMS();
}

void deleteAllSMS() {
    modem.sendAT("AT+CMGDA=\"DEL ALL\"");
    SerialMon.println("All SMS messages deleted");
}
````Use code tags to format code for the forum`

Maybe a missing line in your setup:

pinMode(RST_PIN, INPUT_PULLUP);

This will make it keep HIGH untill pressed.

1 Like

Are you using an Arduino Nano ESP32 or some generic ESP32?