#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_I2CDevice.h> // Include the Adafruit I2C Device library for ESP32-C3 compatibility
#include <Adafruit_SPIDevice.h> // Include the Adafruit SPI Device library for ESP32-C3 compatibility
#include <DHT.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET 0 // Change this to the appropriate GPIO pin on your ESP32-C3
Adafruit_SSD1306 display(OLED_RESET);
#define DHTPIN 6
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(115200);
Wire.begin(8, 10);
dht.begin();
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C (for the 64x48)
display.display();
}
void loop() {
delay(1000);
// Read sensor values
float h = dht.readHumidity();
float t = dht.readTemperature();
float f = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
display.println("Failed to read from DHT sensor!");
return;
}
// Clear the display and set the cursor to the top-left corner
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
// clear buffer and create a page to Display temperature in Celsius
display.setCursor(20, 0);
display.println("Temp");
display.setCursor(32, 18);
display.print(t);
display.println(" C");//symbol for degrees Celsius
display.display();
delay(3000);
// clear buffer and create a page to Display humidity
display.clearDisplay();
display.setCursor(20, 0);
display.println("Humidity");
display.setCursor(32, 18);
display.print(h);
display.println(" %");
display.display();
delay(3000);
}