-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdht11.ino
59 lines (47 loc) · 1.41 KB
/
dht11.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
#include <DHT.h>
#include <WiFiNINA.h>
#include <ThingSpeak.h>
#define DHTPIN 4
#define DHTTYPE DHT11
char ssid[] = "Galaxy M30s2DOD"; // wiii ssid
char pass[] = "vcvw3532";// wifi password
unsigned long channelID = 2250545; // Your ThingSpeak Channel ID
const char *apiKey = "9OU7D717BJPF74OF"; // Your ThingSpeak API Key
DHT dht(DHTPIN, DHTTYPE);
WiFiClient client;
void setup() {
Serial.begin(9600);
dht.begin();
establishWiFiConnection();
ThingSpeak.begin(client);
}
void loop() {
delay(2000);
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
if (!isnan(temperature) && !isnan(humidity)) {
sendToThingSpeak(temperature, humidity);
} else {
Serial.println("Failed to read data from DHT sensor!");
}
delay(30000);
}
void establishWiFiConnection() {
while (WiFi.status() != WL_CONNECTED) {
Serial.print("Connecting to WiFi...");
WiFi.begin(ssid, pass);
delay(1000);
}
Serial.println("Connected to WiFi");
}
void sendToThingSpeak(float temperature, float humidity) {
ThingSpeak.setField(channelID, 1, temperature, apiKey);
ThingSpeak.setField(channelID, 2, humidity, apiKey);
int response = ThingSpeak.writeFields(channelID);
if (response == 200) {
Serial.println("Data sent to ThingSpeak");
} else {
Serial.print("Error sending data to ThingSpeak. HTTP Response Code: ");
Serial.println(response);
}
}