Last active
September 13, 2021 09:36
-
-
Save TrickSumo/cb65221277fb974352b4c2589e7475a1 to your computer and use it in GitHub Desktop.
Upload data of DHT11 temperature sensor to firebase using NodeMCU ESP8266
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Code written by Rishi Tiwari | |
* Website: https://tricksumo.com | |
* https://youtu.be/hPv9uX3rUWc | |
* https://youtu.be/pA_Rfop7kDc | |
* https://gist.github.com/TrickSumo/f1aecd17972aa3b6c0668750c3309714 | |
* | |
* Libraries:- Arduino firebase Master https://github.com/FirebaseExtended/firebase-arduino | |
* Ardunio Json https://github.com/bblanchon/ArduinoJson/tree/5.x | |
* DHT11 by adafruit https://github.com/adafruit/DHT-sensor-library | |
* adafruit universal senor https://github.com/adafruit/Adafruit_Sensor | |
*/ | |
#include <ESP8266WiFi.h> | |
#include<FirebaseArduino.h> | |
#include <DHT.h> | |
#define DHTTYPE DHT11 // Sensor DHT 11 | |
#define FIREBASE_HOST "xxxxxxxxxxxx.firebaseio.com" // database URL | |
#define FIREBASE_AUTH "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" // secret key | |
#define WIFI_SSID "xxxxxx" | |
#define WIFI_PASSWORD "xxxxxxx" | |
#define dht_dpin D1 | |
DHT dht(dht_dpin, DHTTYPE); | |
void setup() { | |
dht.begin(); | |
Serial.begin(9600); | |
Serial.println("Humidity and temperature \n\n"); | |
WiFi.begin(WIFI_SSID, WIFI_PASSWORD); //try to connect with wifi | |
Serial.print("Connecting to "); | |
Serial.print(WIFI_SSID); | |
while (WiFi.status() != WL_CONNECTED) { | |
Serial.print("."); | |
delay(500); | |
} | |
Serial.println(); | |
Serial.print("Connected to "); | |
Serial.println(WIFI_SSID); | |
Serial.print("IP Address is : "); | |
Serial.println(WiFi.localIP()); //print local IP address | |
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH); // connect to firebase | |
//Start reading dht sensor | |
} | |
void loop() { | |
// Firebase Error Handling ************************************************ | |
if (Firebase.failed()) | |
{ delay(500); | |
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH); | |
Serial.println(Firebase.error()); | |
Serial.println("Ignoring firebase\n\n\n\n\n"); | |
delay(500); | |
} | |
else { | |
Serial.println("Everything is ready!"); | |
delay(300); Serial.println("Everything is ready!"); | |
delay(300); Serial.println("Everything is ready!"); | |
delay(300); | |
} | |
float h = dht.readHumidity(); // Reading humidity | |
float t = dht.readTemperature(); // Reading temperature | |
if (isnan(h) || isnan(t)) { // Check if any reads failed and exit early (to try again). | |
Serial.println("Failed to read from DHT sensor!"); | |
return; | |
} | |
Serial.print("Current humidity = "); | |
Serial.print(h); | |
Serial.print("% "); | |
Serial.print("temperature = "); | |
Serial.print(t); | |
Serial.println("C "); | |
delay(2000 ); | |
Firebase.setFloat("/data",t); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment