Mechatronics LAB
Published © GPL3+

ESP32 Weather Forecast System Using DHT and Firebase

ESP32 Weather Forecast System Using DHT and Firebase

BeginnerProtip1 hour144
ESP32 Weather Forecast System Using DHT and Firebase

Things used in this project

Hardware components

esp32
×1
dht22
×1

Story

Read more

Schematics

ESP32 Weather Forecast System Using DHT and Firebase

Code

ESP32 Weather Forecast System Using DHT and Firebase

Arduino
ESP32 Weather Forecast System Using DHT and Firebase
#include <WiFi.h>
#include <FirebaseESP32.h>
#include <DHT.h>

// Firebase configuration
#define FIREBASE_HOST "your-firebase-database.firebaseio.com" // Your Firebase Realtime Database URL
#define FIREBASE_AUTH "your-firebase-auth-key"  // Your Firebase Authentication Key

// WiFi credentials
const char* ssid = "your-SSID";
const char* password = "your-password";

// DHT sensor setup
#define DHTPIN 4         // Pin connected to DHT sensor
#define DHTTYPE DHT11    // DHT 11 or DHT22
DHT dht(DHTPIN, DHTTYPE);

// Firebase object
FirebaseData firebaseData;

void setup() {
  // Start serial communication
  Serial.begin(115200);
  
  // Connect to Wi-Fi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }
  Serial.println("Connected to WiFi");

  // Connect to Firebase
  Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);

  // Start DHT sensor
  dht.begin();
}

void loop() {
  // Read temperature and humidity values
  float temperature = dht.readTemperature(); // Celsius
  float humidity = dht.readHumidity(); // Percentage

  // Check if readings are valid
  if (isnan(temperature) || isnan(humidity)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  // Print readings to serial monitor
  Serial.print("Temperature: ");
  Serial.print(temperature);
  Serial.print(" °C ");
  Serial.print("Humidity: ");
  Serial.print(humidity);
  Serial.println(" %");

  // Send data to Firebase
  if (Firebase.setFloat(firebaseData, "/weather/temperature", temperature)) {
    Serial.println("Temperature data sent to Firebase");
  } else {
    Serial.println("Error sending temperature data");
  }

  if (Firebase.setFloat(firebaseData, "/weather/humidity", humidity)) {
    Serial.println("Humidity data sent to Firebase");
  } else {
    Serial.println("Error sending humidity data");
  }

  // Delay for 10 seconds before sending new data
  delay(10000);
}

Credits

Mechatronics LAB
75 projects • 47 followers
I am Sarful , I am a Mechatronics Engineer & also a teacher I am Interested in the evolution of technology in the automation industry .
Contact

Comments

Please log in or sign up to comment.