Untitled

 avatar
unknown
plain_text
4 years ago
2.6 kB
5
Indexable
//
// Copyright 2015 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

// FirebaseDemo_ESP8266 is a sample that demo the different functions
// of the FirebaseArduino API.

#include <ESP8266WiFi.h>
#include <FirebaseArduino.h>
#include <DHT.h>
#include <DHT_U.h>

// Set these to run example.
#define FIREBASE_HOST "iotlet-default-rtdb.firebaseio.com"
#define FIREBASE_AUTH "TH2ZLNAQXy5FFDgyZjBGlTEg2XD95YMupmno3aul"
#define WIFI_SSID "GONDRONG"
#define WIFI_PASSWORD "cayya5799"

float t;
float h;
DHT dht(D8, DHT11);

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

  // connect to wifi.
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  Serial.print("connecting");
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
  }
  Serial.println();
  Serial.print("connected: ");
  Serial.println(WiFi.localIP());
  
  
  Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
  dht.begin();
  pinMode(D7, OUTPUT);
  digitalWrite(D7, HIGH);
}

void loop() {
  print_dht();
  // ambil nilai relay_on dari firebase 
  bool relay_on = Firebase.getBool("relay_on");
  
  // nyalakan relay jika variabel relay_on bernilai false
  if (relay_on == true){
    nyalakan_relay();
  } 
  // matikanrelay jika variabel relay_on bernilai true
  else if (relay_on == false) {
    matikan_relay();
  }
  
  delay(500);
}

void print_dht() {
  h = dht.readHumidity();
  t = dht.readTemperature();
  Firebase.setFloat("suhu", t);
  Firebase.setFloat("kelembaban", h);
  Serial.println(h);
  Serial.println(t);
  // handle error
  if (Firebase.failed()) {
      Serial.print("update suhu gagal:");
      Serial.println(Firebase.error());  
      return;
  }
}

void nyalakan_relay() {
  digitalWrite(D7, LOW); // di perangkat relay
  // handle error
  if (Firebase.failed()) {
      Serial.print("setting relay_on failed:");
      Serial.println(Firebase.error());  
      return;
  }
}

void matikan_relay() {
  // matikan relay
  digitalWrite(D7, HIGH); // di perangkat relay
  // handle error
  if (Firebase.failed()) {
      Serial.print("setting relay_on MATI failed:");
      Serial.println(Firebase.error());  
      return;
  }
}
Editor is loading...