The adafruitio_15_temp_humidity example uses digital pin 2 by default on all boards, and that can be modified if needed by changing the DATA_PIN define.
// pin connected to DH22 data line #define DATA_PIN 2
The next chunk of code creates an instance of the DHT class, and also sets up feed instances for the temperature and humidity feeds.
// create DHT22 instance DHT_Unified dht(DATA_PIN, DHT22); // set up the 'temperature' and 'humidity' feeds AdafruitIO_Feed *temperature = io.feed("temperature"); AdafruitIO_Feed *humidity = io.feed("humidity");
The setup function initializes the DHT22 sensor, and also connects your feather to Adafruit IO. The code will wait until you have a valid connection to Adafruit IO before continuing with the sketch. If you have any issues connecting, check config.h for any typos in your username or key.
void setup() { // start the serial connection Serial.begin(115200); // wait for serial monitor to open while(! Serial); // initialize dht22 dht.begin(); // connect to io.adafruit.com Serial.print("Connecting to Adafruit IO"); io.connect(); // wait for a connection while(io.status() < AIO_CONNECTED) { Serial.print("."); delay(500); } // we are connected Serial.println(); Serial.println(io.statusText()); }
Next, we have the main loop()
function. The first line of the loop function calls io.run();
this line will need to be present at the top of your loop in every sketch. It helps keep your device connected to Adafruit IO, and processes any incoming data.
void loop() { // io.run(); is required for all sketches. // it should always be present at the top of your loop // function. it keeps the client connected to // io.adafruit.com, and processes any incoming data. io.run();
The next chunk of code inside the loop() checks the current DHT22 temperature value, and saves the value in the celsius and fahrenheit variables.
We then print both celsius and fahrenheit to the Arduino Serial Monitor, and save the fahrenheit value to the temperature feed on Adafruit IO.
sensors_event_t event; dht.temperature().getEvent(&event); float celsius = event.temperature; float fahrenheit = (celsius * 1.8) + 32; Serial.print("celsius: "); Serial.print(celsius); Serial.println("C"); Serial.print("fahrenheit: "); Serial.print(fahrenheit); Serial.println("F"); // save fahrenheit (or celsius) to Adafruit IO temperature->save(fahrenheit);
If you prefer to log celsius values, you can modify the call to the save() function.
temperature->save(celsius);
The final chunk of the loop()
function requests a humidity reading from the DHT22, and prints the value to the Arduino Serial Monitor. We also save the humidity value to the humidity feed on Adafruit IO.
dht.humidity().getEvent(&event); Serial.print("humidity: "); Serial.print(event.relative_humidity); Serial.println("%"); // save humidity to Adafruit IO humidity->save(event.relative_humidity); // wait 5 seconds (5000 milliseconds == 5 seconds) delay(5000); }
Upload the sketch to your board, and open the Arduino Serial Monitor. Your board should now connect to Adafruit IO.
Connecting to Adafruit IO.... Adafruit IO connected.
You should now see the temperature and humidity values being sent to Adafruit IO.
celsius: 18.30C fahrenheit: 64.94F humidity: 34.90% celsius: 18.20C fahrenheit: 64.76F humidity: 35.40%
Check your dashboard on Adafruit IO, and you should see the line chart update with the changes in temperature and humidity.