// https://iot1-fb31f-default-rtdb.asia-southeast1.firebasedatabase.app/
#include <Arduino.h>
#include <WiFi.h>
#include <FirebaseESP32.h>
#include <DHT.h>
//Provide the token generation process info.
#include "addons/TokenHelper.h"
//Provide the RTDB payload printing info and other helper functions.
#include "addons/RTDBHelper.h"
#define DHTPIN 13
#define DHTTYPE DHT22
// Replace with your network credentials
const char* WIFI_SSID = "Wokwi-GUEST";
const char* WIFI_PASSWORD = "";
// Replace with your Firebase project details
#define FIREBASE_HOST "https://iot1-fb31f-default-rtdb.asia-southeast1.firebasedatabase.app/"
#define FIREBASE_API_KEY "AIzaSyBFRShYY-vdKGWxlaVKqx-lG-fmHYbK9gc"
DHT dht(DHTPIN, DHTTYPE);
FirebaseData firebaseData;
FirebaseConfig firebaseConfig;
FirebaseAuth firebaseAuth;
unsigned long sendDataPrevMillis = 0;
int count = 0;
bool signupOK = false;
void setup(){
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 with IP: ");
Serial.println(WiFi.localIP());
Serial.println();
// Set Firebase configuration and authentication details
firebaseConfig.database_url = FIREBASE_HOST;
firebaseConfig.api_key = FIREBASE_API_KEY;
/* Sign up */
if (Firebase.signUp(&firebaseConfig, &firebaseAuth, "", "")){
Serial.println("ok");
signupOK = true;
}
else{
Serial.printf("%s\n", firebaseConfig.signer.signupError.message.c_str());
}
/* Assign the callback function for the long running token generation task */
firebaseConfig.token_status_callback = tokenStatusCallback; //see addons/TokenHelper.h
// Connect to Firebase
Firebase.begin(&firebaseConfig, &firebaseAuth);
Firebase.reconnectWiFi(true);
// Initialize DHT sensor
dht.begin();
}
//layout_margin
//layout_width="match_parent"
layout_height="wrap_content
//text="Student Details
textSize="30sp"
//textAlignment="center"
layout_marginTop="20dp"
//textStyle="bold"
textColor="#A11111
void loop() {
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
if (isnan(humidity) || isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Send data to Firebase
if (Firebase.setFloat(firebaseData, "/temperature", temperature)) {
Serial.println("Temperature data sent successfully!");
} else {
Serial.println("Failed to send temperature data");
Serial.println(firebaseData.errorReason());
}
orientation,layout_margin
layout_margin
//layout_width="match_parent"
layout_height="wrap_content
text="Student Details
//textSize="30sp"
textAlignment="center"
//layout_marginTop="20dp"
textColor="#A11111
if (Firebase.setFloat(firebaseData, "/humidity", humidity)) {
Serial.println("Humidity data sent successfully!");
} else {
Serial.println("Failed to send humidity data");
Serial.println(firebaseData.errorReason());
}
delay(2000); // Delay for 2 seconds before the next loop
}