|
| 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 | +} |
0 commit comments