#include "DHT.h"
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define DHTPIN 2 // Digital pin connected to the DHT sensor
// Feather HUZZAH ESP8266 note: use pins 3, 4, 5, 12, 13 or 14 --
// Pin 15 can work but DHT must be disconnected during program upload.
// Uncomment whatever type you're using!
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT11 // DHT 22 (AM2302), AM2321
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
DHT dht(2, DHTTYPE);
#define SCREEN_WIDTH 128 // pixel ความกว้าง
#define SCREEN_HEIGHT 64 // pixel ความสูง
// กำหนดขาต่อ I2C กับจอ OLED
#define OLED_RESET -1 //ขา reset เป็น -1 ถ้าใช้ร่วมกับขา Arduino reset
Adafruit_SSD1306 OLED(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
void setup() {
Serial.begin(9600);
dht.begin();
if (!OLED.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // สั่งให้จอ OLED เริ่มทำงานที่ Address 0x3C
Serial.println("SSD1306 allocation failed");
} else {
Serial.println("AMP");
}
}
void loop() {
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)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" *c");
Serial.print("\n");
delay(500);
OLED.clearDisplay(); // ลบภาพในหน้าจอทั้งหมด
OLED.setTextColor(WHITE, BLACK); //กำหนดข้อความสีขาว ฉากหลังสีดำ
OLED.setCursor(0, 0); // กำหนดตำแหน่ง x,y ที่จะแสดงผล
OLED.setTextSize(2); // กำหนดขนาดตัวอักษร
OLED.println("Humidity and Temperature"); // แสดงผลข้อความ Humidity and Temperature
OLED.setCursor(0, 40);
OLED.setTextSize(1);
OLED.println("\n Humidity status: ");
if(h <= 30){
OLED.setCursor(0, 60);
OLED.setTextSize(1);
OLED.print(" low");
}
else if (h >= 50){
OLED.setCursor(0, 60);
OLED.setTextSize(1);
OLED.print(" High");
}
OLED.setCursor(0, 70);
OLED.setTextSize(1);
OLED.println("Temperature status: ");
if(t <= 12){
OLED.print(" Cool");
}
else if (t >= 32){
OLED.setCursor(0, 80);
OLED.setTextSize(1);
OLED.print(" Hot");
}
else {
OLED.setCursor(0, 80);
OLED.setTextSize(1);
OLED.setCursor(0, 70);
OLED.setTextSize(1);
OLED.print(" Good");
}
OLED.display(); // สั่งให้จอแสดงผล
delay(500);
}