[c] firebase android
Viewer
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
- #include "FirebaseESP8266.h"
- #include <ESP8266WiFi.h>
- #include <DHT.h>
- #define FIREBASE_HOST "esiot-20231-default-rtdb.firebaseio.com"
- #define FIREBASE_AUTH "4Z62yZuINfC9pDboHQb5ItLho5c2hJix6FsRmOKM"
- const char* ssid = "Laptop";
- const char* password = "asdfghjkl";
- #define DHTPIN D5
- int led = D2;
- #define DHTTYPE DHT11
- DHT dht(DHTPIN, DHTTYPE);
- FirebaseData firebaseData;
- FirebaseData ledData;
- FirebaseJson json;
- void setup()
- {
- Serial.begin(115200);
- dht.begin();
- pinMode(led, OUTPUT);
- WiFi.begin(ssid, password);
- Serial.print("Connecting to Wi-Fi");
- while (WiFi.status() != WL_CONNECTED)
- {
- Serial.print(".");
- delay(500);
- }
- Serial.println();
- Serial.print("Connected with IP: ");
- Serial.println(WiFi.localIP());
- Serial.println();
- Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
- Firebase.reconnectWiFi(true);
- }
- void sensorUpdate()
- {
- // Reading temperature or humidity takes about 250 milliseconds!
- // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
- //float h = dht.readHumidity();
- float h = 26;
- // Read temperature as Celsius (the default)
- //float t = dht.readTemperature();
- float t = 32;
- // Read temperature as Fahrenheit (isFahrenheit = true)
- float f = dht.readTemperature(true);
- // Check if any reads failed and exit early (to try again).
- if (isnan(h) || isnan(t) || isnan(f)) {
- Serial.println(F("Failed to read from DHT sensor!"));
- return;
- }
- Serial.print(F("Humidity: "));
- Serial.print(h);
- Serial.print(F("% Temperature: "));
- Serial.print(t);
- Serial.print(F("°C ,"));
- Serial.print(f);
- Serial.println(F("°F "));
- if (Firebase.setFloat(firebaseData, "/esiot-db/suhu", t))
- {
- Serial.println("PASSED");
- Serial.println("PATH: " + firebaseData.dataPath());
- Serial.println("TYPE: " + firebaseData.dataType());
- Serial.println("ETag: " + firebaseData.ETag());
- Serial.println("------------------------------------");
- Serial.println();
- }
- else
- {
- Serial.println("FAILED");
- Serial.println("REASON: " + firebaseData.errorReason());
- Serial.println("------------------------------------");
- Serial.println();
- }
- if (Firebase.setFloat(firebaseData, "/esiot-db/kelembapan", h))
- {
- Serial.println("PASSED");
- Serial.println("PATH: " + firebaseData.dataPath());
- Serial.println("TYPE: " + firebaseData.dataType());
- Serial.println("ETag: " + firebaseData.ETag());
- Serial.println("------------------------------------");
- Serial.println();
- }
- else
- {
- Serial.println("FAILED");
- Serial.println("REASON: " + firebaseData.errorReason());
- Serial.println("------------------------------------");
- Serial.println();
- }
- }
- void loop() {
- sensorUpdate();
- if (Firebase.getString(ledData, "/esiot-db/led")) {
- Serial.println(ledData.stringData());
- if (ledData.stringData() == "1") {
- digitalWrite(led, HIGH);
- }
- else if (ledData.stringData() == "0") {
- digitalWrite(led, LOW);
- }
- }
- delay(100);
- }
Editor
You can edit this paste and save as new: