-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2.1pCODE
More file actions
75 lines (59 loc) · 1.84 KB
/
2.1pCODE
File metadata and controls
75 lines (59 loc) · 1.84 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
#include <WiFiNINA.h>
#include "DHT.h"
#include "secrets.h"
#include "ThingSpeak.h"
#define DHTPIN 2
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
WiFiClient client;
// WiFi credentials
char ssid[] = SECRET_SSID;
char pass[] = SECRET_PASS;
int keyIndex = 0; //
// ThingSpeak channel details
unsigned long DHT11ChannelNumber = SECRET_CH_ID;
int statusCode = 0;
int field[8] = {1, 2, 3, 4, 5, 6, 7, 8};
void setup() {
Serial.begin(115200);
while (!Serial);
Serial.println(F("DHT11 test!"));
dht.begin();
ThingSpeak.begin(client);
// Connect to WiFi
Serial.print("Attempting to connect to SSID: ");
Serial.println(SECRET_SSID);
while (WiFi.status() != WL_CONNECTED) {
WiFi.begin(ssid, pass);
Serial.print(".");
delay(5000);
}
Serial.println("\nConnected");
}
void loop() {
delay(2000);
// Read temperature and humidity
float humidity = dht.readHumidity();
float temperatureC = dht.readTemperature();
float temperatureF = dht.readTemperature(true);
if (isnan(humidity) || isnan(temperatureC) || isnan(temperatureF)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
Serial.print(F("Humidity: "));
Serial.print(humidity);
Serial.print(F("% Temperature: "));
Serial.print(temperatureC);
Serial.print(F("°C "));
Serial.print(temperatureF);
// Send data to ThingSpeak
ThingSpeak.setField(field[0], temperatureF);
ThingSpeak.setField(field[1], humidity);
statusCode = ThingSpeak.writeFields(DHT11ChannelNumber, SECRET_API_KEY);
if (statusCode == 200) {
Serial.println("Data sent to ThingSpeak successfully.");
} else {
Serial.println("Problem reading channel. HTTP error code " + String(statusCode));
}
delay(60000); // 60s gap
}