Two different conditions in one function

Hi! Can anyone help me with this? In my case, i want relay and temperature can be work at the same time without any problems, relay supposed to control on Android and temperature should be automatic.

Here's my source code. Thanks in advance!

#include "FirebaseESP8266.h"
#include <ESP8266WiFi.h>
#include <DHT.h>

#define FIREBASE_HOST "smart-cage-esp8266-default-rtdb.firebaseio.com"      // Your Firebase Project URL 
#define FIREBASE_AUTH "CJowtg0z3jsMvcLBpQXsxSocT8eJHg1nrRW3NHmB"            // Your Firebase Database Secret
#define WIFI_SSID "Tebak"                                                   // your WiFi SSID
#define WIFI_PASSWORD "akusiapa"                                            // your WiFi PASSWORD

//SUHU
#define DHTPIN D3
#define DHTTYPE DHT11
 
FirebaseData fbdo;
DHT dht11(DHTPIN,DHTTYPE);

String R1, R2;
int r1, r2;
float t;
 
void setup() {

  pinMode(D0, OUTPUT);
  dht11.begin();  
  
  Serial.begin(115200);

  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  Serial.print("Connecting to Wi-Fi");
  while (WiFi.status() != WL_CONNECTED)
  {
    Serial.print(".");    
    delay(300);
  }

  Serial.println();
  Serial.print("Connected to IP : ");
  Serial.println(WiFi.localIP());
  Serial.println();

  Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
  Firebase.reconnectWiFi(true);

  if(Firebase.setInt(fbdo, "/Hasil_Pembacaan/Relay1", 0))
  {
    //Success
    Serial.println("Relay Siap");
  }else{
    //Failed, get error reason from fbdo
    Serial.println("Gagal");
    Serial.println(fbdo.errorReason());
  }

}
 
void loop() {
  t = dht11.readTemperature();
  
   // Memeriksa apakah sensor berhasil mambaca suhu dan kelembaban
   if (isnan(t)) {
    Serial.println("Gagal membaca sensor DHT11");
    delay(1000);
    return;
    }
  
    Firebase.getString(fbdo, "/Hasil_Pembacaan/Relay1");
 
    R1 = fbdo.stringData();
    r1 = R1.toInt();
    
    if(r1==0 && t < 31)
      {
        digitalWrite(D0, HIGH);
        Serial.println("Relay OFF");
      }
      else if(r1== 1 && t > 31)
      {
        digitalWrite(D0, LOW);
        Serial.println("Relay ON");          
      }

      //PENGATURAN KIRIM DATA KE FIREBASE
  if (Firebase.setFloat(fbdo, "/Hasil_Pembacaan/Suhu", t)){
      Serial.println("Suhu terkirim");
    } else{
      Serial.println("Suhu tidak terkirim");
      Serial.println("Karena: " + fbdo.errorReason()); 
    }
}

Welcome to the forum!

I don't understand what you are trying to do, so please make an effort to explain your project more clearly, and describe the problems.

After reading the "How to get the best out of this forum" post, please edit your post to add code tags.

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

hi! i want to use relay control and temperature control in one function, so relay can control from app and also temperature can be run automatically. i'm very sorry for my lack english but i hope you understand

ah okay, thank you so much for reminding me and i'm apologize for the inconvience

Explain what the program should do, and what it does instead.

if the relay is 1 (active) and the temperature is above 30, the relay (lamp) is on and if the relay is 0 (inactive) and the temperature is below 30, the relay (lamp) is off.

t = temperature (dht11)
D0 = relay
r1 = to control relay from firebase

the temperature should be automatic and the relay should be manual (controlling by Android and use Firebase).

but when i run it, only one of them can run. for example the relay control can but the temperature can't.

i got new problems, it already works now but when relay was active, the temperature data cant be sent to Firebase. it tells "send request failed"

and my crystal ball is in the dish washer.

Please see post #3.

Hello. I have a problem with this, anyone knows what is wrong?

L1 = relay control (from Android)
t = temperature
R1 = convert data relay from string to int

It works fine but when relay was ON automatic, i can't turn it off manually on Android but when relay was OFF automatic, i can turn it on manually on Android. What i suppose to do?

What i want is when L1 == 0 and t < 31 and relay active automatically, i can turn it off manually

   String R1;
   int L1;
   
   Firebase.getString(fbdo, "/Hasil_Pembacaan/Relay1");
 
    R1 = fbdo.stringData();
    L1 = R1.toInt();
    
    if((L1 == 1) || (t < 31))
    {
        digitalWrite(r1, LOW);
        Serial.println("Relay ON");        
    }         
    else if((L1 == 0) || (t > 31) || (t < 31))
    {
        digitalWrite(r1, HIGH);
        Serial.println("Relay OFF");        
    }     

...is always true unless t = 31.

Is that what you want?

@dearmybrain
Please do not open several threads on the same subject.
Cross-posting is violating of forum rules.

Threads merged as requested. Best wishes for success with your project @dearmybrain

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.