Skip to content

Commit 8228e60

Browse files
committed
add dht22 example
1 parent 0a17be7 commit 8228e60

File tree

2 files changed

+143
-0
lines changed

2 files changed

+143
-0
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
// Adafruit IO Temperature & Humidity Example
2+
// Tutorial Link: https://learn.adafruit.com/adafruit-io-basics-temperature-and-humidity
3+
//
4+
// Adafruit invests time and resources providing this open source code.
5+
// Please support Adafruit and open source hardware by purchasing
6+
// products from Adafruit!
7+
//
8+
// Written by Todd Treece for Adafruit Industries
9+
// Copyright (c) 2016-2017 Adafruit Industries
10+
// Licensed under the MIT license.
11+
//
12+
// All text above must be included in any redistribution.
13+
14+
/************************** Configuration ***********************************/
15+
16+
// edit the config.h tab and enter your Adafruit IO credentials
17+
// and any additional configuration needed for WiFi, cellular,
18+
// or ethernet clients.
19+
#include "config.h"
20+
21+
/************************ Example Starts Here *******************************/
22+
#include <Adafruit_Sensor.h>
23+
#include <DHT.h>
24+
#include <DHT_U.h>
25+
26+
// pin connected to DH22 data line
27+
#define DATA_PIN 2
28+
29+
// create DHT22 instance
30+
DHT_Unified dht(DATA_PIN, DHT22);
31+
32+
// set up the 'temperature' and 'humidity' feeds
33+
AdafruitIO_Feed *temperature = io.feed("temperature");
34+
AdafruitIO_Feed *humidity = io.feed("humidity");
35+
36+
void setup() {
37+
38+
// start the serial connection
39+
Serial.begin(115200);
40+
41+
// wait for serial monitor to open
42+
while(! Serial);
43+
44+
// initialize dht22
45+
dht.begin();
46+
47+
// connect to io.adafruit.com
48+
Serial.print("Connecting to Adafruit IO");
49+
io.connect();
50+
51+
// wait for a connection
52+
while(io.status() < AIO_CONNECTED) {
53+
Serial.print(".");
54+
delay(500);
55+
}
56+
57+
// we are connected
58+
Serial.println();
59+
Serial.println(io.statusText());
60+
61+
}
62+
63+
void loop() {
64+
65+
// io.run(); is required for all sketches.
66+
// it should always be present at the top of your loop
67+
// function. it keeps the client connected to
68+
// io.adafruit.com, and processes any incoming data.
69+
io.run();
70+
71+
sensors_event_t event;
72+
dht.temperature().getEvent(&event);
73+
74+
float celsius = event.temperature;
75+
float fahrenheit = (celsius * 1.8) + 32;
76+
77+
Serial.print("celsius: ");
78+
Serial.print(celsius);
79+
Serial.println("C");
80+
81+
Serial.print("fahrenheit: ");
82+
Serial.print(fahrenheit);
83+
Serial.println("F");
84+
85+
// save fahrenheit (or celsius) to Adafruit IO
86+
temperature->save(fahrenheit);
87+
88+
dht.humidity().getEvent(&event);
89+
90+
Serial.print("humidity: ");
91+
Serial.print(event.relative_humidity);
92+
Serial.println("%");
93+
94+
// save humidity to Adafruit IO
95+
humidity->save(event.relative_humidity);
96+
97+
// wait 5 seconds (5000 milliseconds == 5 seconds)
98+
delay(5000);
99+
100+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/************************ Adafruit IO Config *******************************/
2+
3+
// visit io.adafruit.com if you need to create an account,
4+
// or if you need your Adafruit IO key.
5+
#define IO_USERNAME "your_username"
6+
#define IO_KEY "your_key"
7+
8+
/******************************* WIFI **************************************/
9+
10+
// the AdafruitIO_WiFi client will work with the following boards:
11+
// - HUZZAH ESP8266 Breakout -> https://www.adafruit.com/products/2471
12+
// - Feather HUZZAH ESP8266 -> https://www.adafruit.com/products/2821
13+
// - Feather M0 WiFi -> https://www.adafruit.com/products/3010
14+
// - Feather WICED -> https://www.adafruit.com/products/3056
15+
16+
#define WIFI_SSID "your_ssid"
17+
#define WIFI_PASS "your_pass"
18+
19+
// comment out the following two lines if you are using fona or ethernet
20+
#include "AdafruitIO_WiFi.h"
21+
AdafruitIO_WiFi io(IO_USERNAME, IO_KEY, WIFI_SSID, WIFI_PASS);
22+
23+
24+
/******************************* FONA **************************************/
25+
26+
// the AdafruitIO_FONA client will work with the following boards:
27+
// - Feather 32u4 FONA -> https://www.adafruit.com/product/3027
28+
29+
// uncomment the following two lines for 32u4 FONA,
30+
// and comment out the AdafruitIO_WiFi client in the WIFI section
31+
// #include "AdafruitIO_FONA.h"
32+
// AdafruitIO_FONA io(IO_USERNAME, IO_KEY);
33+
34+
35+
/**************************** ETHERNET ************************************/
36+
37+
// the AdafruitIO_Ethernet client will work with the following boards:
38+
// - Ethernet FeatherWing -> https://www.adafruit.com/products/3201
39+
40+
// uncomment the following two lines for ethernet,
41+
// and comment out the AdafruitIO_WiFi client in the WIFI section
42+
// #include "AdafruitIO_Ethernet.h"
43+
// AdafruitIO_Ethernet io(IO_USERNAME, IO_KEY);

0 commit comments

Comments
 (0)