108 lines
2.8 KiB
C++
108 lines
2.8 KiB
C++
#include <ESP8266WiFi.h>
|
|
#include <FirebaseESP8266.h>
|
|
#include <Wire.h>
|
|
#include <LiquidCrystal_I2C.h>
|
|
|
|
// WiFi credentials
|
|
#define WIFI_SSID "Jalak Teras"
|
|
#define WIFI_PASSWORD "J@lakTer@s"
|
|
|
|
// Firebase credentials
|
|
#define FIREBASE_HOST "https://airsavvy-6e5e1-default-rtdb.firebaseio.com/"
|
|
#define FIREBASE_AUTH "P1A3xXJF66kjDms3EXTtBxRauFpsGY6b2MHhdoXr"
|
|
|
|
// Firebase objects
|
|
FirebaseData firebaseData;
|
|
FirebaseConfig config;
|
|
FirebaseAuth auth;
|
|
|
|
// LCD settings
|
|
LiquidCrystal_I2C lcd(0x27, 16, 2);
|
|
|
|
// Variables to store data from Firebase
|
|
String kategori_CO2 = "";
|
|
String kategori_CO = "";
|
|
int CO2_ppm = 0;
|
|
int CO_ppm = 0;
|
|
|
|
void setup() {
|
|
Serial.begin(9600); // Start serial monitor for debugging
|
|
|
|
// Initialize LCD
|
|
lcd.begin();
|
|
lcd.backlight();
|
|
lcd.clear();
|
|
|
|
// Display initial message on LCD
|
|
lcd.setCursor(0, 0);
|
|
lcd.print(" Please Wait...");
|
|
|
|
// Connect to Wi-Fi
|
|
WiFi.begin(WIFI_SSID, WIFI_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());
|
|
|
|
// Configure Firebase
|
|
config.host = FIREBASE_HOST;
|
|
config.database_url = FIREBASE_HOST; // Make sure to add this line
|
|
config.signer.tokens.legacy_token = FIREBASE_AUTH; // Use the correct field for the legacy token
|
|
Firebase.begin(&config, &auth);
|
|
Firebase.reconnectWiFi(true);
|
|
|
|
Serial.println("Connected to Firebase");
|
|
lcd.clear(); // Clear the initial message
|
|
}
|
|
|
|
void loop() {
|
|
// Retrieve string data for Kategori_CO2 from Firebase
|
|
if (Firebase.getString(firebaseData, "/Kategori_CO2")) {
|
|
kategori_CO2 = firebaseData.stringData();
|
|
} else {
|
|
Serial.print("Error getting Kategori_CO2: ");
|
|
Serial.println(firebaseData.errorReason());
|
|
}
|
|
|
|
// Retrieve string data for Kategori_CO from Firebase
|
|
if (Firebase.getString(firebaseData, "/Kategori_CO")) {
|
|
kategori_CO = firebaseData.stringData();
|
|
} else {
|
|
Serial.print("Error getting Kategori_CO: ");
|
|
Serial.println(firebaseData.errorReason());
|
|
}
|
|
|
|
/*
|
|
// Retrieve numeric data for CO2_ppm from Firebase
|
|
if (Firebase.getInt(firebaseData, "/CO2_ppm")) {
|
|
CO2_ppm = firebaseData.intData();
|
|
} else {
|
|
Serial.print("Error getting CO2_ppm: ");
|
|
Serial.println(firebaseData.errorReason());
|
|
}
|
|
|
|
// Retrieve numeric data for CO_ppm from Firebase
|
|
if (Firebase.getInt(firebaseData, "/CO_ppm")) {
|
|
CO_ppm = firebaseData.intData();
|
|
} else {
|
|
Serial.print("Error getting CO_ppm: ");
|
|
Serial.println(firebaseData.errorReason());
|
|
}
|
|
*/
|
|
|
|
// Display data on LCD
|
|
lcd.clear();
|
|
lcd.setCursor(0, 0);
|
|
lcd.print("CO2 ");
|
|
lcd.print(kategori_CO2);
|
|
lcd.setCursor(0, 1);
|
|
lcd.print("CO ");
|
|
lcd.print(kategori_CO);
|
|
|
|
delay(1000); // Avoiding rate limits by adding delay
|
|
}
|