Project tutorial
Private Real-time Weather Station

Private Real-time Weather Station © GPL3+

This project is used to monitor the weather conditions of your home in real-time on a web application.

  • 1,072 views
  • 2 comments
  • 25 respects

Components and supplies

Apps and online services

Logo firebase 8fxzjc8wki
Google Firebase
Ide web
Arduino IDE
Github Atom
Atom is a text editor by GitHub. You can use any other text editor of your choice.

About this project

In this project we will be building a personal weather station for home use. We will be using an ESP8266 based board, i.e. NodeMCU. We will be monitoring light, temperature and humidity. For this, we will be using an LDR and a DHT11 sensor.

This project consists of three parts:

1. Collecting data from the sensors.

2. Sending the data to an online database (Google Firebase in our case).

3. Displaying the data stored in the database on a web app.

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

1. Collecting Data

We will be using a light detecting resistor (LDR) to get values of atmospheric light. Connect the LDR to A0 pin of the NodeMCU as shown in the schematic.

We will be using a light detecting resistor (LDR) to get values of atmospheric light. Connect the LDR to A0 pin of the NodeMCU as shown in the Schematic.

The DHT11 sensor is used to measure the humidity and temperature. We use the DHT.h library here. Make the following connections:

Vcc ==> 3v

Gnd ==> Gnd

Data ==> D3

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

2. Sending Data to Firebase

To upload data to firebase we need to connect to the internet first and then access firebase. For this we need to use the following two libraries:

1. ESP8266WiFi.h

2. FirebaseArduino.h

Once connected to the wifi network as per the code we can now use the functions of FirebaseArduino.h library to upload data.

The sensor data is stored in light, temp and hum variables of float type.

So we will use setFloat() function to upload the value to the real time database of firebase. See the code to understand it better.

Firebase.setFloat("light", light);
 // handle error
 if (Firebase.failed()) {
     Serial.print("setting /light failed:");
     Serial.println(Firebase.error());  
     return;
 }

Once our data is uploaded we can display it on a web app in real time and access it from anywhere in the world.

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

3. Displaying Data on the Web App

Click here to see my web app.

This is were you can get really creative. If you are familiar to HTML and CSS then you can make user own UI. I used Bootstrap to create mine. For those who are new to web here is the code for a very basic UI (I'm not providing my code here as that would be spoon feeding and I want you guys to build your own UI. Trust me it is fun!)

Step 1:

Paste this code in notepad and save it as index.html in a folder lets say web at any desired location.

<html>
<head>
 <script src="https://www.gstatic.com/firebasejs/5.8.4/firebase.js"></script>
 <script src="https://www.gstatic.com/firebasejs/5.8.5/firebase-app.js"></script>
 <script src="https://www.gstatic.com/firebasejs/5.8.5/firebase-database.js"></script>
</head>
<body style="background-color: #4d4d4d;">
       <ul>
         <li><h1>LIGHT</h1>
         <pre id="light" style="font-size:24px;"></pre></li>
         <li><h1>TEMPERATURE</h1>
         <pre id="temp" style="font-size:24px;"></pre></li>
         <li><h1>HUMIDITY</h1>
         <pre id="hum" style="font-size:24px;"></pre></li>
       </ul>
 <script src="app.js"></script>
</body>
</html>

Step 2:

Create a database in firebase. Open Firebase website. Login with your Gmail account. Create a realtime database by clicking on database. Then follow the direction in the images to get you secret key.

Step 3:

Copy and paste the below code in notepad and save it as app.js in the same folder in which you saved index.html file.

(function() {
// Initialize Firebase
var config = {
firebase.initializeApp(config);
//Get element
const prelight= document.getElementById('light');
//Create refences
const dbReflight= firebase.database().ref().child('light');
// sync logs cahnges
dbReflight.on('value', snap => {
prelight.innerText=JSON.stringify(snap.val(),null,3);
});
const pretemp= document.getElementById('temp');
//Create refences
const dbReftemp= firebase.database().ref().child('temperature');
// sync logs cahnges
dbReftemp.on('value', snap => {
pretemp.innerText=JSON.stringify(snap.val(),null,3)+"° C";
});
const prehum= document.getElementById('hum');
//Create refences
const dbRefhum= firebase.database().ref().child('moisture');
// sync logs cahnges
dbRefhum.on('value', snap => {
prehum.innerText=JSON.stringify(snap.val(),null,3)+" %";
});
}());

You have to change the values inside var config as per your database.

Once this is done you are all set to go.

Upload the code on to your NodeMCU, make connections and power up the board. It will start uploading data to Firebase.

--------------------------------------------------------------------------------------

NOTE: DON'T FORGET TO CHANGE YOUR WIFI SSID, WIFI PASSWORD, SECRET CODE, AND HOST NAME IN THE.INO FILE.

#define FIREBASE_HOST "YOUR DATABASE LINK"
#define FIREBASE_AUTH "SECRET CODE"
#define WIFI_SSID "YOUR SSID"
#define WIFI_PASSWORD "YOUR PASSWORD"

--------------------------------------------------------------------------------------

Once this is done just open the index.html file to see your data in real time.

I hope you guys understood this project. Thank you for reading this tutorial. If you got any doubt or queries then do drop a comment in the comment section.

Best of luck!

Code

Weather stationC/C++
Upload this code onto nodeMCU
#include <DHT.h>        // including the library of DHT11 temperature and humidity sensor
#include <ESP8266WiFi.h>
#include <FirebaseArduino.h>
#define DHTTYPE DHT11   // DHT 11

// Set these to run example.
#define FIREBASE_HOST "YOUR DATABASE LINK"
#define FIREBASE_AUTH "SECRET CODE"
#define WIFI_SSID "YOUR SSID"
#define WIFI_PASSWORD "YOUR PASSWORD"
#define dht_dpin 0

DHT dht(dht_dpin, DHTTYPE); 
void setup()
{ 
  Serial.begin(9600);
  dht.begin();
  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);
}


void loop() {
  int n= 0;
    float light = analogRead(A0);  
    float hum = dht.readHumidity();
    float temp = dht.readTemperature();         
  delay(800);
  Serial.println(light);
  Serial.println(hum);
  Serial.println(temp);
  // set value
 
  Firebase.setFloat("light", light);
  // handle error
  if (Firebase.failed()) {
      Serial.print("setting /light failed:");
      Serial.println(Firebase.error());  
      return;
  }
  delay(100);
  Firebase.setFloat("moisture", hum);
  if (Firebase.failed()) {
      Serial.print("setting /hum failed:");
      Serial.println(Firebase.error());  
      return;
  }
  delay(100);
  Firebase.setFloat("temperature", temp);
  if (Firebase.failed()) {
      Serial.print("setting /temp failed:");
      Serial.println(Firebase.error());  
      return;
  }
  delay(100);
  //get values
  Serial.print("light: ");
  Serial.println(Firebase.getFloat("light"));
  Serial.print("humidity: ");
  Serial.println(Firebase.getFloat("moisture"));
  Serial.print("temperature: ");
  Serial.println(Firebase.getFloat("temperature"));
  delay(100);

  // append a new value to /logs
  String name = Firebase.pushInt("logs", n++);
  // handle error
  if (Firebase.failed()) {
      Serial.print("pushing /logs failed:");
      Serial.println(Firebase.error());  
      return;
  }
  Serial.print("pushed: /logs/");
  Serial.println(name);
  delay(1000);
}

  

Schematics

The Circuit
Make connections as per the Schematic.
Weatherstation qh4rxpxade

Comments

Similar projects you might like

Weather Station Based On Arduino And NodeMCU

Project tutorial by miklas

  • 5,484 views
  • 0 comments
  • 16 respects

Wireless weather station (Arduino + ESP8266 + Thingspeak)

Project tutorial by Alin-Constantin Paun

  • 9,730 views
  • 2 comments
  • 16 respects

Arduino MKR1000 Weather Station

Project tutorial by Tadeas Dvoracek

  • 8,298 views
  • 4 comments
  • 22 respects

Weather Station v.1.0

Project showcase by derapados

  • 12,745 views
  • 4 comments
  • 40 respects

Uno Weather Station

Project in progress by wrightmac

  • 8,324 views
  • 4 comments
  • 13 respects

Personal Weather Station (Arduino+ ESP8266 + Thingspeak)

Project tutorial by Jayraj Desai

  • 54,873 views
  • 34 comments
  • 116 respects
Add projectSign up / Login