#include <AdafruitIO_WiFi.h>
#include <DHT_U.h>
#include "config.h"
#define ONE_MINUTE_IN_MILLISECONDS 60000
// Initialize the Adafruit IO WiFi Connection
AdafruitIO_WiFi io(IO_USERNAME, IO_KEY, WIFI_SSID, WIFI_PASS);
// Define Adafruit IO feeds
AdafruitIO_Feed *temperature = io.feed("temperature");
AdafruitIO_Feed *humidity = io.feed("humidity");
// Initialize the DHT Unified Library
DHT_Unified dht(DHT_PIN, DHT22);
void setup() {
Serial.begin(115200);
// Set the Blue LED to output mode.
pinMode(BLUE_LED_PIN, OUTPUT);
// Connect to Wiwok WiFi.
io.connect();
// Wait until ESP32 has connected to Wiwok WiFi.
while (io.status() < AIO_CONNECTED) {
Serial.print('.');
delay(500);
}
// Print the WiFi Status after ESP32 has connected to Wiwok WiFi.
Serial.println(io.statusText());
// Start & initialize the DHT object.
dht.begin();
}
void loop() {
// Blink the Blue LED to indicate the start of a loop cycle.
blinkLED(100);
// Keep the client connected to Adafruit IO & process data.
io.run();
sensors_event_t event;
// Save the DHT22 temperatureto Adafruit IO temperaturefeed.
temperature->save(getTemperature(event));
// Save the DHT22 Humidity to Adafruit IO humidity feed.
humidity->save(getHumidity(event));
// Wait one minute before continuing.
delay(ONE_MINUTE_IN_MILLISECONDS);
}
// Blinks the Blue LED on the ESP32 with a specified duration.
void blinkLED(int duration) {
digitalWrite(BLUE_LED_PIN, HIGH);
delay(duration);
digitalWrite(BLUE_LED_PIN, LOW);
}
// Gets & returns the temperature value from the DHT22 sensor.
float getTemperature(sensors_event_t &evt) {
// Get a reference to the temperature event.
dht.temperature().getEvent(&evt);
// Read the temperature value from the event in degrees celsius.
float temp_in_celsius = evt.temperature;
// Convert the temperature value in degrees celsius to degrees fahrenheit.
float temp_in_fahrenheit = (temp_in_celsius * 1.8) + 32;
// Print the celsius and fahrenheit temperature values to the Serial output.
Serial.println("Temperature: " + String(temp_in_celsius) + "°C / " + String(temp_in_fahrenheit) + "°F");
// Return the temperature in degrees fahrenheit.
return temp_in_fahrenheit;
}
// Gets & returns the relative humidity value from the DHT22 sensor.
float getHumidity(sensors_event_t event) {
// Get a reference to the relative humidity event.
dht.humidity().getEvent(&event);
// Read the relative_humidity value from the event.
float relative_humidity = event.relative_humidity;
// Print the relative humidity to the Serial output.
Serial.println("Relative Humidity: " + String(relative_humidity) + "%");
// Return the relative_humidity value.
return relative_humidity;
}
Loading
esp32-devkit-v1
esp32-devkit-v1