can you help me fix the issue on this code, cuz when i didnt add the firebase database on the code the button control is very responsive when i press it, but when i add the firebase database on the code for storing the data and for dual control (actual button and on firebase) the actual button is not responsive anymore and very slow to read when i press the button.
CODE:
#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 "AIzaSyDDSO3p2FznRsgMjTuhx0g1Xt5qkbZTHhw"
#define DATABSE_URL "https://automatedfooddehydrator-default-rtdb.firebaseio.com/"
#define BUTTON_UP 12
#define BUTTON_DOWN 14
#define BUTTON_SELECT 27
#define BUTTON_START 26
#define BUTTON_FRUIT 32
#define BUTTON_VEGETABLE 13
#define BUTTON_FISH 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);
int buttonUpState = HIGH, lastButtonUpState = HIGH;
int buttonDownState = HIGH, lastButtonDownState = HIGH;
int buttonSelectState = HIGH, lastButtonSelectState = HIGH;
int buttonStartState = HIGH, lastButtonStartState = HIGH;
int buttonFruitState = HIGH, lastButtonFruitState = HIGH;
int buttonVegeState = HIGH, lastButtonVegeState = HIGH;
int buttonFishState = HIGH, lastButtonFishState = HIGH;
LiquidCrystal_PCF8574 lcd(0x27);
OneWire oneWire(TEMP_SENSOR);
DallasTemperature sensors(&oneWire);
FirebaseData fbdo;
FirebaseAuth auth;
FirebaseConfig config;
int setHours = 0, setMinutes = 0, setSeconds = 0;
int setTemperature = 25;
int currentTemp = 0;
int humidity = 0;
bool processStarted = false;
bool relay1State = false;
bool relay2State = false;
bool relay3State = false;
bool relay4State = false;
bool relay5State = false;
bool relay6State = false;
const unsigned long debounceDelay = 200;
unsigned long sendDataPrevMillis = 0;
bool signupOk = false;
enum Setting { HOURS, MINUTES, SECONDS, TEMPERATURE };
Setting currentSetting = HOURS;
// Preset times and temperatures
const int fruitPresetTime = 60; // 1 hour
const int fruitPresetTemp = 30; // 30°C
const int vegetablePresetTime = 90; // 1.5 hours
const int vegetablePresetTemp = 31; // 31°C
const int fishPresetTime = 120; // 2 hours
const int fishPresetTemp = 32; // 32°C
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_UP, INPUT_PULLUP);
pinMode(BUTTON_DOWN, INPUT_PULLUP);
pinMode(BUTTON_SELECT, INPUT_PULLUP);
pinMode(BUTTON_START, INPUT_PULLUP);
pinMode(BUTTON_FRUIT, INPUT_PULLUP);
pinMode(BUTTON_VEGETABLE, INPUT_PULLUP);
pinMode(BUTTON_FISH, 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);
lastButtonFruitState = digitalRead(BUTTON_FRUIT);
lastButtonVegeState = digitalRead(BUTTON_VEGETABLE);
lastButtonFishState = digitalRead(BUTTON_FISH);
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();
sensors.requestTemperatures();
int currentTemp = sensors.getTempCByIndex(0);
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());
}
// Initialize control paths
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);
Firebase.RTDB.setInt(&fbdo, "Controls/FRUITS", 0);
Firebase.RTDB.setInt(&fbdo, "Controls/VEGETABLES", 0);
Firebase.RTDB.setInt(&fbdo, "Controls/FISH", 0);
}
void HandleFirebaseControls() {
// Read Control/UP
if (Firebase.RTDB.getInt(&fbdo, "Controls/UP")) {
int upValue = fbdo.intData();
Serial.println("Read Controls/UP: " + String(upValue)); // Debug log
if (upValue == 1) {
// Simulate TOUCH_UP
if (currentSetting == HOURS) setHours++;
else if (currentSetting == MINUTES) setMinutes++;
else if (currentSetting == SECONDS) setSeconds++;
else if (currentSetting == TEMPERATURE) setTemperature++;
updateDisplay();
UpdateFirebase();
Firebase.RTDB.setInt(&fbdo, "Controls/UP", 0); {
Serial.println("Failed to reset Controls/UP: " + fbdo.errorReason());
}
}
} else {
Serial.println("Failed to read Controls/UP: " + fbdo.errorReason());
}
// Read Control/DOWN
if (Firebase.RTDB.getInt(&fbdo, "Controls/DOWN")) {
int downValue = fbdo.intData();
Serial.println("Read Controls/DOWN: " + String(downValue)); // Debug log
if (downValue == 1) {
// Simulate TOUCH_DOWN
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();
Firebase.RTDB.setInt(&fbdo, "Controls/DOWN", 0); {
Serial.println("Failed to reset Controls/DOWN: " + fbdo.errorReason());
}
}
} else {
Serial.println("Failed to read Controls/DOWN: " + fbdo.errorReason());
}
// Read Control/SELECT
if (Firebase.RTDB.getInt(&fbdo, "Controls/SELECT")) {
int selectValue = fbdo.intData();
Serial.println("Read Controls/SELECT: " + String(selectValue)); // Debug log
if (selectValue == 1) {
// Simulate TOUCH_SELECT
currentSetting = static_cast<Setting>((currentSetting + 1) % 4);
updateDisplay();
UpdateFirebase();
Firebase.RTDB.setInt(&fbdo, "Controls/SELECT", 0); // Reset Control/SELECT to 0
}
}
// Read Control/START
if (Firebase.RTDB.getInt(&fbdo, "Controls/START")) {
int startValue = fbdo.intData();
Serial.println("Read Controls/START: " + String(startValue)); // Debug log
if (startValue == 1 && !processStarted) {
// Simulate TOUCH_START
startProcess();
UpdateFirebase();
Firebase.RTDB.setInt(&fbdo, "Controls/START", 0); // Reset Control/START to 0
}
}
if (Firebase.RTDB.getInt(&fbdo, "Controls/FRUITS")) {
int fruitValue = fbdo.intData();
Serial.println("Read Controls/FRUITS: " + String(fruitValue)); // Debug log
if (fruitValue == 1 && !processStarted) {
startPresetProcess(fruitPresetTime, fruitPresetTemp);
Firebase.RTDB.setInt(&fbdo, "Controls/FRUITS", 0); // Reset Control/FRUITS to 0
}
}
if (Firebase.RTDB.getInt(&fbdo, "Controls/VEGETABLES")) {
int vegetableValue = fbdo.intData();
Serial.println("Read Controls/VEGETABLES: " + String(vegetableValue)); // Debug log
if (vegetableValue == 1 && !processStarted) {
startPresetProcess(vegetablePresetTime, vegetablePresetTemp);
Firebase.RTDB.setInt(&fbdo, "Controls/VEGETABLES", 0); // Reset Control/FRUITS to 0
}
}
if (Firebase.RTDB.getInt(&fbdo, "Controls/FISH")) {
int fishValue = fbdo.intData();
Serial.println("Read Controls/FISH: " + String(fishValue)); // Debug log
if (fishValue == 1 && !processStarted) {
startPresetProcess(fishPresetTime, fishPresetTemp);
Firebase.RTDB.setInt(&fbdo, "Controls/FISH", 0); // Reset Control/FRUITS to 0
}
}
}
void handleFruitPreset() {
buttonFruitState = digitalRead(BUTTON_FRUIT);
if (buttonFruitState == LOW && lastButtonFruitState == HIGH && !processStarted) {
startPresetProcess(fruitPresetTime, fruitPresetTemp);
delay(debounceDelay);
}
lastButtonFruitState = buttonFruitState;
}
void handleVegetablesPreset() {
buttonVegeState = digitalRead(BUTTON_VEGETABLE);
if (buttonVegeState == LOW && lastButtonVegeState == HIGH && !processStarted) {
startPresetProcess(vegetablePresetTime, vegetablePresetTemp); // Start the vegetable preset process
delay(debounceDelay);
}
lastButtonVegeState = buttonVegeState;
}
void handleFishPreset() {
buttonFishState = digitalRead(BUTTON_FISH);
if (buttonFishState == LOW && lastButtonFishState == HIGH && !processStarted) {
startPresetProcess(fishPresetTime, fishPresetTemp);
delay(debounceDelay);
}
lastButtonFishState = buttonFishState;
}
void handleButtonUp() {
if (currentSetting == HOURS) setHours++;
else if (currentSetting == MINUTES) setMinutes++;
else if (currentSetting == SECONDS) setSeconds++;
else if (currentSetting == TEMPERATURE) setTemperature++;
updateDisplay();
UpdateFirebase();
}
void handleButtonDown() {
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();
}
void handleButtonSelect() {
currentSetting = static_cast<Setting>((currentSetting + 1) % 4);
updateDisplay();
UpdateFirebase();
}
void handleButtonStart() {
startProcess();
UpdateFirebase();
}
void loop() {
handleFruitPreset();
handleVegetablesPreset();
handleFishPreset();
if (!processStarted) {
HandleFirebaseControls();
buttonUpState = digitalRead(BUTTON_UP);
buttonDownState = digitalRead(BUTTON_DOWN);
buttonSelectState = digitalRead(BUTTON_SELECT);
buttonStartState = digitalRead(BUTTON_START);
if (buttonUpState == LOW && lastButtonUpState == HIGH) {
handleButtonUp();
delay(debounceDelay);
}
if (buttonDownState == LOW && lastButtonDownState == HIGH) {
handleButtonDown();
delay(debounceDelay);
}
if (buttonSelectState == LOW && lastButtonSelectState == HIGH) {
handleButtonSelect();
delay(debounceDelay);
}
if (buttonStartState == LOW && lastButtonStartState == HIGH) {
handleButtonStart();
delay(debounceDelay);
}
lastButtonUpState = buttonUpState;
lastButtonDownState = buttonDownState;
lastButtonSelectState = buttonSelectState;
lastButtonStartState = buttonStartState;
} else {
sensors.requestTemperatures();
int currentTemp = sensors.getTempCByIndex(0);
lcd.setCursor(0, 1);
lcd.print("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("Timer:");
lcd.print(timerBuffer);
int humidity = dht11.readHumidity();
if (humidity != DHT11::ERROR_CHECKSUM && humidity != DHT11::ERROR_TIMEOUT) {
lcd.setCursor(0, 0);
lcd.print("Humidity:");
lcd.print(humidity);
lcd.print("%");
} else {
Serial.println(DHT11::getErrorString(humidity));
}
if (currentTemp >= setTemperature) {
digitalWrite(RELAY_1, HIGH);
digitalWrite(RELAY_2, HIGH);
} else {
digitalWrite(RELAY_1, LOW);
digitalWrite(RELAY_2, LOW);
}
delay(1000);
if (setSeconds > 0) {
setSeconds--;
} else if (setMinutes > 0) {
setMinutes--;
setSeconds = 59;
} else if (setHours > 0) {
setHours--;
setMinutes = 59;
setSeconds = 59;
} else {
toggleRelays(false);
processStarted = false;
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("--------------------");
}
UpdateFirebase();
}
}