|
| 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 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 correct DHT type you are using and the pin attached to the Arduino. |
| 18 | + 7. Set the Cayenne authentication info to match the authentication info from the Dashboard. |
| 19 | + 8. Compile and upload this sketch. |
| 20 | + 9. Once the Arduino connects to the Dashboard it should automatically create temporary display widget with data. |
| 21 | + To make a temporary widget permanent click the plus sign on the widget. |
| 22 | +*/ |
| 23 | + |
| 24 | +#define CAYENNE_DEBUG |
| 25 | +#define CAYENNE_PRINT Serial // Comment this out to disable prints and save space |
| 26 | +#include <CayenneMQTTEthernet.h> |
| 27 | +#include <DHT.h> |
| 28 | + |
| 29 | +// Cayenne authentication info. This should be obtained from the Cayenne Dashboard. |
| 30 | +char username[] = "MQTT_USERNAME"; |
| 31 | +char password[] = "MQTT_PASSWORD"; |
| 32 | +char clientID[] = "CLIENT_ID"; |
| 33 | + |
| 34 | +#define DHTPIN 2 // Digital pin connected to the DHT sensor |
| 35 | + |
| 36 | +// Uncomment whatever type you're using! |
| 37 | +#define DHTTYPE DHT11 // DHT 11 |
| 38 | +//#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321 |
| 39 | +//#define DHTTYPE DHT21 // DHT 21 (AM2301) |
| 40 | + |
| 41 | +#define TEMPERATURE_VIRTUAL_CHANNEL 1 |
| 42 | +#define HUMIDITY_VIRTUAL_CHANNEL 2 |
| 43 | + |
| 44 | +DHT dht(DHTPIN, DHTTYPE); // Initialize DHT sensor. |
| 45 | + |
| 46 | +void setup() { |
| 47 | + Serial.begin(9600); |
| 48 | + Cayenne.begin(username, password, clientID); |
| 49 | + dht.begin(); |
| 50 | +} |
| 51 | + |
| 52 | +void loop() { |
| 53 | + Cayenne.loop(); |
| 54 | +} |
| 55 | + |
| 56 | +// This function is called at intervals to send temperature sensor data to Cayenne in Celsius. |
| 57 | +CAYENNE_OUT(TEMPERATURE_VIRTUAL_CHANNEL) |
| 58 | +{ |
| 59 | + Cayenne.virtualWrite(TEMPERATURE_VIRTUAL_CHANNEL, dht.readTemperature(), "temp", "c"); |
| 60 | +} |
| 61 | + |
| 62 | +// This function is called at intervals to send humidity sensor data to Cayenne. |
| 63 | +CAYENNE_OUT(HUMIDITY_VIRTUAL_CHANNEL) |
| 64 | +{ |
| 65 | + Cayenne.virtualWrite(HUMIDITY_VIRTUAL_CHANNEL, dht.readHumidity(), "rel_hum", "p"); |
| 66 | +} |
0 commit comments