-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIot_monitor.ino
127 lines (99 loc) · 3.02 KB
/
Iot_monitor.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <ESP8266WiFi.h>
#include <DHT.h>
#include <ThingSpeak.h>
#include <ESP8266WebServer.h>
#include <ArduinoJson.h>
// Wi-Fi settings
const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";
// ThingSpeak settings
unsigned long myChannelNumber = YOUR_CHANNEL_NUMBER;
const char* myWriteAPIKey = "YOUR_WRITE_API_KEY";
// DHT sensor settings
#define DHTPIN D4 // Pin where the DHT sensor is connected
#define DHTTYPE DHT22 // DHT11 or DHT22
DHT dht(DHTPIN, DHTTYPE);
// Initialize Wi-Fi, ThingSpeak, and Web Server
WiFiClient client;
ESP8266WebServer server(80);
void setup() {
Serial.begin(115200);
delay(10);
// Initialize DHT sensor
dht.begin();
// Attempt to connect to Wi-Fi
connectToWiFi();
// Initialize ThingSpeak
ThingSpeak.begin(client);
// Setup web server routes
server.on("/", handleRoot);
server.on("/data", handleData);
server.begin();
Serial.println("HTTP server started");
}
void loop() {
// Serve any client requests
server.handleClient();
// Read temperature and humidity from DHT sensor
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
// Check if any reads failed
if (isnan(humidity) || isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Print the data to the Serial Monitor
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.print("% Temperature: ");
Serial.print(temperature);
Serial.println("°C");
// Send data to ThingSpeak
ThingSpeak.setField(1, temperature);
ThingSpeak.setField(2, humidity);
int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
if (x == 200) {
Serial.println("Channel update successful.");
} else {
Serial.println("Problem updating channel. HTTP error code " + String(x));
}
// Put the ESP8266 into deep sleep mode for 10 minutes (600e6 microseconds)
Serial.println("Going to sleep...");
ESP.deepSleep(600e6);
}
void connectToWiFi() {
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
int retries = 0;
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
if (retries++ > 20) {
Serial.println("Failed to connect to Wi-Fi. Restarting...");
ESP.restart();
}
}
Serial.println();
Serial.print("Connected! IP address: ");
Serial.println(WiFi.localIP());
}
void handleRoot() {
server.send(200, "text/plain", "Welcome to the ESP8266 Temperature and Humidity Monitor!");
}
void handleData() {
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
// Check if any reads failed
if (isnan(humidity) || isnan(temperature)) {
server.send(500, "application/json", "{\"error\":\"Failed to read from sensor\"}");
return;
}
// Create a JSON response
StaticJsonDocument<200> jsonDoc;
jsonDoc["temperature"] = temperature;
jsonDoc["humidity"] = humidity;
String response;
serializeJson(jsonDoc, response);
server.send(200, "application/json", response);
}