-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.txt
More file actions
126 lines (104 loc) · 3.73 KB
/
code.txt
File metadata and controls
126 lines (104 loc) · 3.73 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <ESP8266WiFi.h>
#include <WiFiManager.h>
#include <FirebaseESP8266.h>
#include <DHT.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h>
// Firebase credentials
const char* firebaseHost = "https://all-in-agri-default-rtdb.firebaseio.com/";
const char* firebaseAuth = "AIzaSyCskTPMKHMATcjJ5OXh4j_kLa-pNrxoaBo";
const char* firebaseWindPath = "/wind";
const char* firebasePressurePath = "/pressure";
const char* firebaseTempPath = "/temp";
const char* firebaseHumidityPath = "/humidity";
// OpenWeatherMap API credentials
const char* apiKey = "5505e4995e00f1755c61da65f6e74231";
const char* cityID = "2294768";
// Define the pins
#define DHTPIN 4 // DHT11 pin
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
// Firebase object
FirebaseData firebaseData;
void setup() {
Serial.begin(115200);
dht.begin();
// Initialize WiFiManager
WiFiManager wifiManager;
// Automatically connect to saved WiFi, or start the configuration portal
if (!wifiManager.autoConnect("WeatherMonitoringAP")) {
Serial.println("Failed to connect and hit timeout");
ESP.restart();
}
Serial.println("Connected to WiFi");
// Initialize Firebase
Firebase.begin(firebaseHost, firebaseAuth);
}
void loop() {
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
if (isnan(humidity) || isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
float pressure = 0.0;
float windSpeed = 0.0;
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
String url = String("http://api.openweathermap.org/data/2.5/weather?id=") + cityID + "&appid=" + apiKey + "&units=metric";
http.begin(url);
int httpCode = http.GET();
if (httpCode > 0) {
String payload = http.getString();
DynamicJsonDocument doc(1024);
deserializeJson(doc, payload);
pressure = doc["main"]["pressure"].as<float>();
windSpeed = doc["wind"]["speed"].as<float>();
Serial.print("Pressure: ");
if (Firebase.set(firebaseData, firebasePressurePath, pressure)) {
Serial.println("pressure sent to Firebase successfully!");
}
Serial.print(pressure);
Serial.println(" hPa");
Serial.print("Wind Speed: ");
Serial.print(windSpeed);
if (Firebase.set(firebaseData, firebaseWindPath, windSpeed)) {
Serial.println("windSpeed sent to Firebase successfully!");
}
Serial.println(" m/s");
} else {
Serial.println("Error on HTTP request");
}
http.end();
}
// Prepare Firestore document data as a JSON object
DynamicJsonDocument json(1024);
json["fields"]["temperature"]["doubleValue"] = temperature;
json["fields"]["humidity"]["doubleValue"] = humidity;
json["fields"]["pressure"]["doubleValue"] = pressure;
json["fields"]["windSpeed"]["doubleValue"] = windSpeed;
if (Firebase.set(firebaseData, firebaseTempPath, temperature)){
Serial.println("temperature sent to Firebase successfully!");
}
if (Firebase.set(firebaseData, firebaseHumidityPath, humidity)) {
Serial.println("windSpeed sent to Firebase successfully!");
}
// Send data to Firestore using HTTP
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
String url = String(firebaseHost) + "/weatherData?key=" + firebaseAuth;
http.begin(url);
http.addHeader("Content-Type", "application/json");
String jsonStr;
serializeJson(json, jsonStr);
int httpCode = http.POST(jsonStr);
if (httpCode > 0) {
Serial.println("Data sent successfully to Firestore");
Serial.println(http.getString());
} else {
Serial.print("Error sending data: ");
Serial.println(http.errorToString(httpCode));
} http.end();
}
delay(60000); // Delay before sending the next data (1 minute)
}