-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWriteMultipleFields.ino
More file actions
104 lines (82 loc) · 2.96 KB
/
WriteMultipleFields.ino
File metadata and controls
104 lines (82 loc) · 2.96 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
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
/*
WriteMultipleFields
Description: Writes values to fields 1,2,3,4 and status in a single ThingSpeak update every 20 seconds.
Hardware: Arduino MKR1000
!!! IMPORTANT - Modify the secrets.h file for this project with your network connection and ThingSpeak channel details. !!!
Note:
- Requires WiFi101 library version 0.15.3 or newer.
- This example is written for a network using WPA encryption. For WEP or WPA, change the WiFi.begin() call accordingly.
ThingSpeak ( https://www.thingspeak.com ) is an analytic IoT platform service that allows you to aggregate, visualize, and
analyze live data streams in the cloud. Visit https://www.thingspeak.com to sign up for a free account and create a channel.
Documentation for the ThingSpeak Communication Library for Arduino is in the README.md folder where the library was installed.
See https://www.mathworks.com/help/thingspeak/index.html for the full ThingSpeak documentation.
For licensing information, see the accompanying license file.
Copyright 2020, The MathWorks, Inc.
*/
#include <WiFiNINA.h>
#include "secrets.h"
#include <DHT.h>
#include "ThingSpeak.h"
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;
#define DHTPIN 2
#define DHTTYPE DHT11
//DHT object for temperature and humidity.
DHT dht(DHTPIN, DHTTYPE);
//Method to set up.
void setup()
{
//Initialize serial
Serial.begin(115200);
while (!Serial)
{
; //Wait for serial port to connect. Needed for Leonardo native USB port only
}
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)
{
//Connect to WPA/WPA2 network. Change this line if using open or WEP network
Serial.print(".");
WiFi.begin(ssid, pass);
delay(5000);
}
Serial.println("\nConnected.");
}
//Read temperature and humidity from DHT sensor
float temp = dht.readTemperature();
float humidity = dht.readHumidity();
//Check sensor readings.
if (isnan(humidity) || isnan(temp))
{
Serial.println("Failed to read from DHT sensor!");
delay(2000);
return;
}
//Set ThingSpeak fields with sensor readings
ThingSpeak.setField(1, temp);
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));
}
//Wait 20 seconds to update the channel again
delay(20000);
}