-
Notifications
You must be signed in to change notification settings - Fork 0
/
TemSensor.ino
65 lines (52 loc) · 1.64 KB
/
TemSensor.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
#include "DHT.h"
#include <WiFiNINA.h>
#include "secrets.h"
#include "ThingSpeak.h"
#define DHTPIN 2
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password
WiFiClient client;
unsigned long myChannelNumber = SECRET_CH_ID;
const char * myWriteAPIKey = SECRET_WRITE_APIKEY;
void setup() {
Serial.begin(115200);
while (!Serial); // wait for serial port to connect
dht.begin();
ThingSpeak.begin(client);
}
void loop() {
if(WiFi.status() != WL_CONNECTED){
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
while(WiFi.status() != WL_CONNECTED){
WiFi.begin(ssid, pass);
Serial.print(".");
delay(5000);
}
Serial.println("\nConnected.");
}
float h = dht.readHumidity();
float t = dht.readTemperature(); // Temperature in Celsius
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return; // Exit early if the read failed
}
Serial.print("Humidity: ");
Serial.print(h);
Serial.print("% Temperature: ");
Serial.print(t);
Serial.println("°C");
ThingSpeak.setField(1, t); // Set temperature to field 1
ThingSpeak.setField(2, h); // Set humidity to field 2
String myStatus = "Temp and humidity updated.";
ThingSpeak.setStatus(myStatus);
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));
}
delay(20000); // Wait 20 seconds to update the channel again
}