-
Notifications
You must be signed in to change notification settings - Fork 0
/
esp32temp.ino
109 lines (90 loc) · 3.12 KB
/
esp32temp.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <WiFi.h>
#include <FirebaseESP32.h>
#include "DHT.h"
// Replace with your network credentials
const char* ssid = "Wifi name";
const char* password = "Wifi psw";
// Firebase Project details
const char* FIREBASE_HOST = "firebase id";
const char* FIREBASE_AUTH = "firebase api key";
FirebaseData firebaseData;
FirebaseConfig config;
FirebaseAuth auth;
#define DHTPIN 4 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.print("Connecting to Wi-Fi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.println("Connected to Wi-Fi");
// Assign the Firebase credentials
config.host = FIREBASE_HOST;
config.signer.tokens.legacy_token = FIREBASE_AUTH;
Firebase.begin(&config, &auth);
Firebase.reconnectWiFi(true);
dht.begin();
}
void loop() {
// Wait a few seconds between measurements
delay(2000);
// Reading temperature or humidity takes about 250 milliseconds!
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Compute heat index in Fahrenheit (the default)
float hif = dht.computeHeatIndex(f, h);
// Compute heat index in Celsius (isFahreheit = false)
float hic = dht.computeHeatIndex(t, h, false);
// Print values to the serial monitor
Serial.print("Humidity: ");
Serial.print(h);
Serial.print("% Temperature: ");
Serial.print(t);
Serial.print("°C ");
Serial.print(f);
Serial.print("°F Heat index: ");
Serial.print(hic);
Serial.print("°C ");
Serial.print(hif);
Serial.println("°F");
// Send values to Firebase Realtime Database
if (Firebase.ready()) {
if (Firebase.setFloat(firebaseData, "/DHT11/Humidity", h)) {
Serial.println("Humidity value sent to Firebase");
} else {
Serial.println("Failed to send humidity value to Firebase");
Serial.println(firebaseData.errorReason());
}
if (Firebase.setFloat(firebaseData, "/DHT11/Temperature", t)) {
Serial.println("Temperature value sent to Firebase");
} else {
Serial.println("Failed to send temperature value to Firebase");
Serial.println(firebaseData.errorReason());
}
if (Firebase.setFloat(firebaseData, "/DHT11/HeatIndexC", hic)) {
Serial.println("Heat Index (Celsius) value sent to Firebase");
} else {
Serial.println("Failed to send heat index value (Celsius) to Firebase");
Serial.println(firebaseData.errorReason());
}
if (Firebase.setFloat(firebaseData, "/DHT11/HeatIndexF", hif)) {
Serial.println("Heat Index (Fahrenheit) value sent to Firebase");
} else {
Serial.println("Failed to send heat index value (Fahrenheit) to Firebase");
Serial.println(firebaseData.errorReason());
}
}
}