Skip to content

Commit 6f2e36c

Browse files
authored
Create DHT.ino
1 parent c766413 commit 6f2e36c

File tree

1 file changed

+65
-0
lines changed
  • examples/CommunitySubmitted/DHT

1 file changed

+65
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
Cayenne DHT Example
3+
This sketch shows how to send DHT Sensor data to the Cayenne Dashboard.
4+
The CayenneMQTT Library is required to run this sketch. If you have not already done so you can install it from the Arduino IDE Library Manager.
5+
6+
Steps:
7+
1. Install DHT sensor library: https://github.com/adafruit/DHT-sensor-library.
8+
2. Install Adafruit Unified Sensor Lib: https://github.com/adafruit/Adafruit_Sensor.
9+
3. Attach a DHT to your Arduino:
10+
Schematic:
11+
DHT Arduino
12+
[VDD] --- [3V3]
13+
[GND] --- [GND]
14+
[OUTPUT] --- [Digital Pin 2]
15+
4. Set the CELSIUS_TEMPERATURE_VIRTUAL_CHANNEL value below to a free virtual channel.
16+
5. Set the HUMIDITY_VIRTUAL_CHANNEL value below to a free virtual channel.
17+
6. Set the Cayenne authentication info to match the authentication info from the Dashboard.
18+
7. Compile and upload this sketch.
19+
8. Once the Arduino connects to the Dashboard it should automatically create temporary display widget with data.
20+
To make a temporary widget permanent click the plus sign on the widget.
21+
*/
22+
23+
#define CAYENNE_DEBUG
24+
#define CAYENNE_PRINT Serial // Comment this out to disable prints and save space
25+
#include <CayenneMQTTEthernet.h>
26+
#include <DHT.h>
27+
28+
// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
29+
char username[] = "MQTT_USERNAME";
30+
char password[] = "MQTT_PASSWORD";
31+
char clientID[] = "CLIENT_ID";
32+
33+
#define DHTPIN 2 // Digital pin connected to the DHT sensor
34+
35+
// Uncomment whatever type you're using!
36+
#define DHTTYPE DHT11 // DHT 11
37+
//#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
38+
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
39+
40+
#define TEMPERATURE_VIRTUAL_CHANNEL 1
41+
#define HUMIDITY_VIRTUAL_CHANNEL 2
42+
43+
DHT dht(DHTPIN, DHTTYPE); // Initialize DHT sensor.
44+
45+
void setup() {
46+
Serial.begin(9600);
47+
Cayenne.begin(username, password, clientID);
48+
dht.begin();
49+
}
50+
51+
void loop() {
52+
Cayenne.loop();
53+
}
54+
55+
// This function is called at intervals to send temperature sensor data to Cayenne in Celsius.
56+
CAYENNE_OUT(TEMPERATURE_VIRTUAL_CHANNEL)
57+
{
58+
Cayenne.virtualWrite(TEMPERATURE_VIRTUAL_CHANNEL, dht.readTemperature(), "temp", "c");
59+
}
60+
61+
// This function is called at intervals to send humidity sensor data to Cayenne.
62+
CAYENNE_OUT(HUMIDITY_VIRTUAL_CHANNEL)
63+
{
64+
Cayenne.virtualWrite(HUMIDITY_VIRTUAL_CHANNEL, dht.readHumidity(), "rel_hum", "p");
65+
}

0 commit comments

Comments
 (0)