-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTask 2.1P
More file actions
79 lines (67 loc) · 2.21 KB
/
Task 2.1P
File metadata and controls
79 lines (67 loc) · 2.21 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
#include <WiFiNINA.h>
#include "secrets.h"
#include "DHT.h"
#include "ThingSpeak.h" // always include thingspeak header file after other header files and custom macros
// Prepare digital pin 3 to be used as 'DHT11_PIN'
#define DHT_PIN 3
// Prepare DHT type to be 'DHT11'
#define DHT_TYPE DHT11
// Create DHT object
DHT dht(DHT_PIN, DHT_TYPE);
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password
int keyIndex = 0; // your network key Index number (needed only for WEP)
WiFiClient client;
unsigned long myChannelNumber = SECRET_CH_ID;
const char * myWriteAPIKey = SECRET_WRITE_APIKEY;
void setup()
{
Serial.begin(115200); // Initialize serial
while (!Serial)
{
; // wait for serial port to connect. Needed for Leonardo native USB port only
}
Serial.println(F("DHTxx test!"));
dht.begin();
ThingSpeak.begin(client); // Initialize ThingSpeak
}
void loop()
{
// Connect or reconnect to WiFi
if(WiFi.status() != WL_CONNECTED)
{
Serial.print("Attempting to connect to SSID: ");
Serial.println(SECRET_SSID);
while(WiFi.status() != WL_CONNECTED)
{
WiFi.begin(ssid, pass); // Connect to WPA/WPA2 network. Change this line if using open or WEP network
Serial.print(".");
delay(5000);
}
Serial.println("\nConnected.");
}
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float humidity = dht.readHumidity();
// Read temperature as Celsius (the default)
float temperature = dht.readTemperature();
// Check if any reads failed and exit early (to try again).
if (isnan(humidity) || isnan(temperature))
{
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
ThingSpeak.setField(1, temperature);
ThingSpeak.setField(2, humidity);
// write to the ThingSpeak channel
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
}