// ============ Include Library ===================
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "DHTesp.h"
// ============ End Include Library ===================
// ============ Define Pin ===================
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define DHT_PIN 18
// ============ End Define Pin ==============
// ============ Class ===================
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
DHTesp dht;
// ============ End Class ===================
// ============ Variabel ===================
float temperature, humadity;
float celcius, fahrenheit, kelvin;
// ============ End Variabel ===================
// ============ Function ===================
void dht_init();
void dht_read();
void convert_temperature();
void oled_init();
void oled_display();
// ============ End Function ===================
void setup() {
Serial.begin(9600);
dht_init();
oled_init();
}
void loop() {
dht_read();
convert_temperature();
oled_display();
}
void dht_init() {
dht.setup(DHT_PIN, DHTesp::DHT22);
}
void dht_read() {
TempAndHumidity data = dht.getTempAndHumidity();
temperature = data.temperature;
humadity = data.humidity;
}
void convert_temperature() {
celcius = temperature;
fahrenheit = (9 / 5 * celcius) + 32;
kelvin = celcius + 273.15;
}
void oled_init() {
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("failed to start SSD1306 OLED"));
while (1);
}
}
void oled_display() {
oled.clearDisplay();
oled.setTextSize(1);
oled.setTextColor(SSD1306_WHITE);
oled.setCursor(0, 0);
oled.println("Celcius: ");
oled.setCursor(0, 10);
oled.println(celcius);
oled.setCursor(0, 20);
oled.println("Fahrenheit: ");
oled.setCursor(0, 30);
oled.println(fahrenheit);
oled.setCursor(0, 40);
oled.println("Kelvin: ");
oled.setCursor(0, 50);
oled.println(kelvin);
oled.setCursor(64, 0);
oled.println("Humadity: ");
oled.setCursor(64, 10);
oled.println(humadity);
oled.display();
}