#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define WIDTH 128 // pixel ความกว้าง
#define HEIGHT 64 // pixel ความสูง
#define RESET -1
#include <DHT.h>
#define DHTPin 8
#define DHTTYPE DHT22
float temp,humi;
Adafruit_SSD1306 OLED(WIDTH, HEIGHT, &Wire, RESET);
DHT dht(DHTPin,DHTTYPE);
void setup() {
OLED.begin(SSD1306_SWITCHCAPVCC, 0x3C);
OLED.clearDisplay();
dht.begin();
}
void loop() {
dataTemp();
OLED.display();
delay(3000);
dataHumi();
OLED.display();
delay(3000);
}
//---------------------------------------------------------------------------------
void dataTemp(){
OLED.clearDisplay();
temp = dht.readTemperature();
if(isnan(temp)){
Serial.println("failed to read from Tempurature");
delay(200);
}
OLED.setTextColor(WHITE, BLACK);
OLED.setTextSize(1);
OLED.setCursor(25, 5);
OLED.println("TEMPURATURE");
OLED.setTextSize(2);
OLED.setCursor(25, 30);
OLED.print(temp);
OLED.setCursor(84, 25);
OLED.print(" C");
OLED.drawCircle(88, 25, 3, WHITE);
}
//-----------------------------------------------------------------------------------
void dataHumi(){
OLED.clearDisplay();
humi = dht.readHumidity();
if(isnan(humi)){
Serial.println("failed to read from Humidity");
delay(200);
}
OLED.setTextColor(WHITE, BLACK);
OLED.setTextSize(1);
OLED.setCursor(30, 5);
OLED.println("HUMIDITY");
OLED.setTextSize(2);
OLED.setCursor(20, 25);
OLED.print(humi);
OLED.print("%RH");
}