Can anyone please help me with my controls on firebase with ESP32

The code is perfectly fine working for the push buttons for present modes, and the monitoring through firebase database. but i want to add a other control for the user to have their own set of time and temp (code_1).

but the thing is their is an issue on this. cuz when i add this code (code_2) on my actual code, the preset push buttons are delaying their response to my input when i push them it takes couple of seconds before it works. how can i make it work better?

here's the code_1:

#include <Wire.h>
#include <LiquidCrystal_PCF8574.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <DHT11.h>
#include <Firebase_ESP_Client.h>
#include <addons/TokenHelper.h>
#include <addons/RTDBHelper.h>

#define WIFI_SSID "rj"
#define WIFI_PASSWORD "Password123#"
#define API_KEY ""
#define DATABSE_URL ""

#define BUTTON_BANANA 12
#define BUTTON_MANGO 14
#define BUTTON_TOMATO 27
#define BUTTON_EGGPLANT 26

#define BUTTON_TAMBAN 32
#define BUTTON_GALUNGGONG 13
#define BUTTON_SAMPLE1 15

#define RELAY_1 25
#define RELAY_2 33
#define RELAY_3 16
#define RELAY_4 17
#define RELAY_5 5
#define RELAY_6 18
#define TEMP_SENSOR 4

DHT11 dht11(2);

FirebaseData fbdo;
FirebaseAuth auth;
FirebaseConfig config;

unsigned long sendDataPrevMillis = 0;
bool signupOk = false;

int buttonBananaState = HIGH, lastButtonBananaState = HIGH;
int buttonMangoState = HIGH, lastButtonMangoState = HIGH;
int buttonTomatoState = HIGH, lastButtonTomatoState = HIGH;
int buttonEggplantState = HIGH, lastButtonEggplantState = HIGH;
int buttonTambanState = HIGH, lastButtonTambanState = HIGH;
int buttonGalunggongState = HIGH, lastButtonGalunggongState = HIGH;
int buttonSample1State = HIGH, lastButtonSample1State = HIGH;

LiquidCrystal_PCF8574 lcd(0x27);
OneWire oneWire(TEMP_SENSOR);
DallasTemperature sensors(&oneWire);

int setHours = 0, setMinutes = 0, setSeconds = 0;
int setTemperature = 25;
int currentTemp = 0;
int humidity = 0;

bool relay1State = false;
bool relay2State = false;
bool relay3State = false;
bool relay4State = false;
bool relay5State = false;
bool relay6State = false;

bool processStarted = false;

const unsigned long debounceDelay = 200;


enum Setting { HOURS, MINUTES, SECONDS, TEMPERATURE };
Setting currentSetting = HOURS; 

const int BananaPresetTime = 1;
const int BananaPresetTemp = 29; 

const int MangoPresetTime = 1; 
const int MangoPresetTemp = 28; 

const int TomatoPresetTime = 1;
const int TomatoPresetTemp = 31;

const int EggplantPresetTime = 1; 
const int EggplantPresetTemp = 33;

const int TambanPresetTime = 1; 
const int TambanPresetTemp = 30;

const int GalunggongPresetTime = 1;
const int GalunggongPresetTemp = 32;

const int Sample1PresetTime = 1;
const int Sample1PresetTemp = 32;

void setRelayState(int relayPin, bool state, const String &firebasePath, bool &relayStateVar) {
  digitalWrite(relayPin, state ? LOW : HIGH);
  relayStateVar = state;
  if (Firebase.RTDB.setBool(&fbdo, firebasePath, state)) {
    Serial.println("Relay state updated: " + firebasePath + " = " + String(state));
  } else {
    Serial.println("Failed to update relay state for " + firebasePath + ": " + fbdo.errorReason());
  }
}

void toggleRelays(bool state) {
  setRelayState(RELAY_1, state, "Relays/heater_1", relay1State);
  setRelayState(RELAY_2, state, "Relays/heater_2", relay2State);
  setRelayState(RELAY_3, state, "Relays/fan_1", relay3State);
  setRelayState(RELAY_4, state, "Relays/fan_2", relay4State);
  setRelayState(RELAY_5, state, "Relays/fan_3", relay5State);
  setRelayState(RELAY_6, state, "Relays/dc_motor", relay6State);
}

void startPresetProcess(int presetTime, int presetTemp) {
  setHours = presetTime / 60;
  setMinutes = presetTime % 60;
  setTemperature = presetTemp;
  startProcess();
}

void startProcess() {
  processStarted = true;
  toggleRelays(true);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Processing...");
}

void setup() {
  Serial.begin(115200);

  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  Serial.print("Connecting to Wifi");
  while(WiFi.status() != WL_CONNECTED){
    Serial.print("."); 
    delay(300); 
  }
  Serial.println();
  Serial.print("Connected with IP: ");
  Serial.println(WiFi.localIP());
  Serial.println();

  config.api_key = API_KEY;
  config.database_url = DATABSE_URL;
  if (Firebase.signUp(&config, &auth, "", "")){
    Serial.println("signUp Ok");
    signupOk = true;
  } else{
    Serial.printf("%s\n", config.signer.signupError.message.c_str());
  }

  config.token_status_callback = tokenStatusCallback;
  Firebase.begin(&config, &auth);
  Firebase.reconnectWiFi(true);
  Wire.begin();                 
  lcd.begin(20, 4);             
  lcd.setBacklight(255);        
  sensors.begin();

  pinMode(BUTTON_BANANA, INPUT_PULLUP);
  pinMode(BUTTON_MANGO, INPUT_PULLUP);
  pinMode(BUTTON_TOMATO, INPUT_PULLUP);
  pinMode(BUTTON_EGGPLANT, INPUT_PULLUP);

  pinMode(BUTTON_TAMBAN, INPUT_PULLUP);
  pinMode(BUTTON_GALUNGGONG, INPUT_PULLUP);
  pinMode(BUTTON_SAMPLE1, INPUT_PULLUP);

  pinMode(RELAY_1, OUTPUT);
  pinMode(RELAY_2, OUTPUT);
  pinMode(RELAY_3, OUTPUT);
  pinMode(RELAY_4, OUTPUT);
  pinMode(RELAY_5, OUTPUT);
  pinMode(RELAY_6, OUTPUT);
  toggleRelays(false);

  lastButtonBananaState = digitalRead(BUTTON_BANANA);
  lastButtonMangoState = digitalRead(BUTTON_MANGO);
  lastButtonTomatoState = digitalRead(BUTTON_TOMATO);
  lastButtonEggplantState = digitalRead(BUTTON_EGGPLANT);
  lastButtonTambanState = digitalRead(BUTTON_TAMBAN);
  lastButtonGalunggongState = digitalRead(BUTTON_GALUNGGONG);
  lastButtonSample1State = digitalRead(BUTTON_SAMPLE1);

  lcd.setCursor(4, 1);
  lcd.print(">>Machine<<");
  lcd.setCursor(2, 2);
  lcd.print("Initializing....");
  delay(2000);

  lcd.clear();
  lcd.setCursor(2, 1);
  lcd.print("Manually Setting");
  lcd.setCursor(1, 2);
  lcd.print(">>Touch to Start<<");

  lcd.setCursor(0, 0);
  lcd.print("--------------------");
  lcd.setCursor(0, 1);
  lcd.print("|");
  lcd.setCursor(0, 2);
  lcd.print("|");
  lcd.setCursor(19, 1);
  lcd.print("|");
  lcd.setCursor(19, 2);
  lcd.print("|");
  lcd.setCursor(0, 3);
  lcd.print("--------------------");
}

void updateDisplay() {
  lcd.clear();

  lcd.setCursor(5, 0);
  lcd.print("Setting Up");

  lcd.setCursor(3, 1);
  lcd.print("Set ");
  if (currentSetting == HOURS) lcd.print("Hours:");
  else if (currentSetting == MINUTES) lcd.print("Minutes:");
  else if (currentSetting == SECONDS) lcd.print("Seconds:");
  else if (currentSetting == TEMPERATURE) lcd.print("Temper:");

  lcd.setCursor(3, 2);
  lcd.print(">>>");

  lcd.setCursor(5, 2);
  if (currentSetting == TEMPERATURE) {
    lcd.print(setTemperature);
    lcd.print((char)223);
    lcd.print("C");

    lcd.setCursor(0, 0);
    lcd.print("----");
    lcd.setCursor(16, 0);
    lcd.print("----");
    lcd.setCursor(0, 1);
    lcd.print("|");
    lcd.setCursor(0, 2);
    lcd.print("|");
    lcd.setCursor(19, 1);
    lcd.print("|");
    lcd.setCursor(19, 2);
    lcd.print("|");
    lcd.setCursor(0, 3);
    lcd.print("--------------------");

  } else {
    lcd.print(setHours);
    lcd.print("h ");
    lcd.print(setMinutes);
    lcd.print("m ");
    lcd.print(setSeconds);
    lcd.print("s");

    lcd.setCursor(0, 0);
    lcd.print("----");
    lcd.setCursor(16, 0);
    lcd.print("----");
    lcd.setCursor(0, 1);
    lcd.print("|");
    lcd.setCursor(0, 2);
    lcd.print("|");
    lcd.setCursor(19, 1);
    lcd.print("|");
    lcd.setCursor(19, 2);
    lcd.print("|");
    lcd.setCursor(0, 3);
    lcd.print("--------------------");
  }
}

void UpdateFirebase (){
  if(Firebase.ready() && signupOk && (millis() - sendDataPrevMillis > 500 || sendDataPrevMillis == 0));
  sendDataPrevMillis = millis();

  if (Firebase.RTDB.setBool(&fbdo, "Process_Started", processStarted)){
    Serial.println(); Serial.print(processStarted);
    Serial.print(" - succesfully saved to: " + fbdo.dataPath());
    Serial.print("(" + fbdo.dataType() + ")");
  } else{
    Serial.println("Field: " + fbdo.errorReason());
  }  
  if (Firebase.RTDB.setInt(&fbdo, "Timer/hours", setHours)){
    Serial.println(); Serial.print(setHours);
    Serial.print(" - succesfully saved to: " + fbdo.dataPath());
    Serial.print("(" + fbdo.dataType() + ")");
  } else{
    Serial.println("Field: " + fbdo.errorReason());
  }
  if (Firebase.RTDB.setInt(&fbdo, "Timer/minutes", setMinutes)){
    Serial.println(); Serial.print(setMinutes);
    Serial.print(" - succesfully saved to: " + fbdo.dataPath());
    Serial.print("(" + fbdo.dataType() + ")");
  } else{
    Serial.println("Field: " + fbdo.errorReason());
  }
  if (Firebase.RTDB.setInt(&fbdo, "Timer/seconds", setSeconds)){
    Serial.println(); Serial.print(setSeconds);
    Serial.print(" - succesfully saved to: " + fbdo.dataPath());
    Serial.print("(" + fbdo.dataType() + ")");
  } else{
    Serial.println("Field: " + fbdo.errorReason());
  }
  if (Firebase.RTDB.setInt(&fbdo, "Temperature/current", currentTemp)){
    Serial.println(); Serial.print(currentTemp);
    Serial.print(" - succesfully saved to: " + fbdo.dataPath());
    Serial.print("(" + fbdo.dataType() + ")");
  } else{
    Serial.println("Field: " + fbdo.errorReason());
  }
  if (Firebase.RTDB.setInt(&fbdo, "Temperature/set", setTemperature)){
    Serial.println(); Serial.print(setTemperature);
    Serial.print(" - succesfully saved to: " + fbdo.dataPath());
    Serial.print("(" + fbdo.dataType() + ")");
  } else{
    Serial.println("Field: " + fbdo.errorReason());
  }
    if (Firebase.RTDB.setInt(&fbdo, "Moisture/humidity", humidity)){
    Serial.println(); Serial.print(humidity);
    Serial.print(" - succesfully saved to: " + fbdo.dataPath());
    Serial.print("(" + fbdo.dataType() + ")");
  } else{
    Serial.println("Field: " + fbdo.errorReason());
  }
}



void resetSettings() {
  setTemperature = 25; // Default temperature
  currentTemp = 0; // Default current temperature
  humidity = 0;
  UpdateFirebase(); // Update Firebase with the reset values
}


void handleBananaPreset() {
  buttonBananaState = digitalRead(BUTTON_BANANA);

  if (buttonBananaState == LOW && lastButtonBananaState == HIGH && !processStarted) {
    startPresetProcess(BananaPresetTime, BananaPresetTemp);
    delay(debounceDelay);
  }

  lastButtonBananaState = buttonBananaState;
}

void handleMangoPreset() {
  buttonMangoState = digitalRead(BUTTON_MANGO);  

  if (buttonMangoState == LOW && lastButtonMangoState == HIGH && !processStarted) {
    startPresetProcess(MangoPresetTime, MangoPresetTemp); 
    delay(debounceDelay);
  }

  lastButtonMangoState = buttonMangoState;
}

void handleTomatoPreset() {
  buttonTomatoState = digitalRead(BUTTON_TOMATO);

  if (buttonTomatoState == LOW && lastButtonTomatoState == HIGH && !processStarted) {
    startPresetProcess(TomatoPresetTime, TomatoPresetTemp); 
    delay(debounceDelay); 
  }

  lastButtonTomatoState = buttonTomatoState;
}

void handleEggplantPreset() {
  buttonEggplantState = digitalRead(BUTTON_EGGPLANT);

  if (buttonEggplantState == LOW && lastButtonEggplantState == HIGH && !processStarted) {
    startPresetProcess(EggplantPresetTime, EggplantPresetTemp);
    delay(debounceDelay);
  }

  lastButtonEggplantState = buttonEggplantState;
}

void handleTambanPreset() {
  buttonTambanState = digitalRead(BUTTON_TAMBAN);  

  if (buttonTambanState == LOW && lastButtonTambanState == HIGH && !processStarted) {
    startPresetProcess(TambanPresetTime, TambanPresetTemp);
    delay(debounceDelay);
  }

  lastButtonTambanState = buttonTambanState;
}

void handleGalunggongPreset() {
  buttonGalunggongState = digitalRead(BUTTON_GALUNGGONG);

  if (buttonGalunggongState == LOW && lastButtonGalunggongState == HIGH && !processStarted) {
    startPresetProcess(GalunggongPresetTime, GalunggongPresetTemp); 
    delay(debounceDelay); 
  }

  lastButtonGalunggongState = buttonGalunggongState;
}

void handleSample1Preset() {
  buttonSample1State = digitalRead(BUTTON_SAMPLE1);

  if (buttonSample1State == LOW && lastButtonSample1State == HIGH && !processStarted) {
    startPresetProcess(Sample1PresetTime, Sample1PresetTemp); 
    delay(debounceDelay); 
  }

  lastButtonSample1State = buttonSample1State;
}



void loop() {
  handleBananaPreset();
  handleMangoPreset();
  handleTomatoPreset();
  handleEggplantPreset();
  handleTambanPreset();
  handleGalunggongPreset();
  handleSample1Preset();

  if (!processStarted) {

  } else {
    sensors.requestTemperatures();
    currentTemp = sensors.getTempCByIndex(0);
  	humidity = dht11.readHumidity();

    if (humidity != DHT11::ERROR_CHECKSUM && humidity != DHT11::ERROR_TIMEOUT) {
      lcd.setCursor(0, 0);
      lcd.print("Moisture: ");
      lcd.print(humidity);  
      lcd.print("%");
    }

    lcd.setCursor(0, 1);
    lcd.print("Cur_Temp: ");
    lcd.print(currentTemp);
    lcd.print((char)223);
    lcd.print("C");
    
    lcd.setCursor(0, 2);
    lcd.print("Set_Temp: ");
    lcd.print(setTemperature);
    lcd.print((char)223);
    lcd.print("C");

    char timerBuffer[9];
    sprintf(timerBuffer, "%02d:%02d:%02d", setHours, setMinutes, setSeconds);
    lcd.setCursor(0, 3);
    lcd.print("Set_Timer: ");
    lcd.print(timerBuffer);

    if (currentTemp >= setTemperature) {
      digitalWrite(RELAY_1, HIGH);
      digitalWrite(RELAY_2, HIGH);
    } else {
      digitalWrite(RELAY_1, LOW);
      digitalWrite(RELAY_2, LOW);
    }

    delay(500);
    if (setSeconds > 0) {
      setSeconds--;
      UpdateFirebase();
    } else if (setMinutes > 0) {
      setMinutes--;
      setSeconds = 59;
      UpdateFirebase();
    } else if (setHours > 0) {
      setHours--;
      setMinutes = 59;
      setSeconds = 59;
      UpdateFirebase();

    } else {
      toggleRelays(false);
      processStarted = false;
      resetSettings(); // Reset the settings after the process

      lcd.clear();
      lcd.setCursor(3, 1);
      lcd.print("-Dehydration!-");
      lcd.setCursor(1, 2);
      lcd.print("Process Complete!!");
      delay(5000);

      lcd.clear();
      lcd.setCursor(2, 1);
      lcd.print("Manually Setting");
      lcd.setCursor(1, 2);
      lcd.print(">>Touch to Start<<");

      lcd.setCursor(0, 0);
      lcd.print("--------------------");
      lcd.setCursor(0, 1);
      lcd.print("|");
      lcd.setCursor(0, 2);
      lcd.print("|");
      lcd.setCursor(19, 1);
      lcd.print("|");
      lcd.setCursor(19, 2);
      lcd.print("|");
      lcd.setCursor(0, 3);
      lcd.print("--------------------");
    }
  }
}

here's the code_2:

  Firebase.RTDB.setInt(&fbdo, "Controls/UP", 0);
  Firebase.RTDB.setInt(&fbdo, "Controls/DOWN", 0);
  Firebase.RTDB.setInt(&fbdo, "Controls/SELECT", 0);
  Firebase.RTDB.setInt(&fbdo, "Controls/START", 0);

  // Non-blocking Firebase updates
  Firebase.RTDB.getInt(&fbdo, "Controls/UP");
  int upValue = fbdo.intData();
  if (upValue == 1) {
    if (currentSetting == HOURS) setHours++;
    else if (currentSetting == MINUTES) setMinutes++;
    else if (currentSetting == SECONDS) setSeconds++;
    else if (currentSetting == TEMPERATURE) setTemperature++;
    updateDisplay();
    UpdateFirebase(); // Update Firebase with the reset values
    Firebase.RTDB.setInt(&fbdo, "Controls/UP", 0);
  }

  Firebase.RTDB.getInt(&fbdo, "Controls/DOWN");
  int downValue = fbdo.intData();
  if (downValue == 1) {
    if (currentSetting == HOURS && setHours > 0) setHours--;
    else if (currentSetting == MINUTES && setMinutes > 0) setMinutes--;
    else if (currentSetting == SECONDS && setSeconds > 0) setSeconds--;
    else if (currentSetting == TEMPERATURE && setTemperature > 0) setTemperature--;
    updateDisplay();
    UpdateFirebase(); // Update Firebase with the reset values
    Firebase.RTDB.setInt(&fbdo, "Controls/DOWN", 0);
  }

  Firebase.RTDB.getInt(&fbdo, "Controls/SELECT");
  int selectValue = fbdo.intData();
  if (selectValue == 1) {
    currentSetting = static_cast<Setting>((currentSetting + 1) % 4);
    updateDisplay();
    UpdateFirebase(); // Update Firebase with the reset values
    Firebase.RTDB.setInt(&fbdo, "Controls/SELECT", 0);
  }

  Firebase.RTDB.getInt(&fbdo, "Controls/START");
  int startValue = fbdo.intData();
  if (startValue == 1 && !processStarted) {
    startProcess();
    UpdateFirebase(); // Update Firebase with the reset values
    Firebase.RTDB.setInt(&fbdo, "Controls/START", 0);
  }

It looks like you tried to use code tags and failed. Please edit your first post and add them to the code

In my experience the easiest way to tidy up the code and add the code tags is as follows

Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.

1 Like

Your other topics on the same subject deleted.

Please do not duplicate your questions as doing so wastes the time and effort of the volunteers trying to help you as they are then answering the same thing in different places.

Please create one topic only for your question and choose the forum category carefully. If you have multiple questions about the same project then please ask your questions in the one topic as the answers to one question provide useful context for the others, and also you won’t have to keep explaining your project repeatedly.

Repeated duplicate posting could result in a temporary or permanent ban from the forum.

Could you take a few moments to Learn How To Use The Forum

It will help you get the best out of the forum in the future.

Thank you.

1 Like

Are you actually using an Arduino Nano ESP32 or a generic ESP32 board ?

how can i delete post?

Are you actually using an Arduino Nano ESP32 or a generic ESP32 board ?

  • generic ESP32

Posting your code using code tags prevents parts of it being interpreted as HTML coding and makes it easier to copy for examination

In my experience the easiest way to tidy up the code and add the code tags is as follows

Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.

1 Like

is it okay now?

That's better
Note how much easier it is to read and copy for examination

Your topic has been moved to a more generic forum category to avoid confusion

What do you want to delete and why ?

do you have any idea on my code? on how can i fix it?