#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <TridentTD_LineNotify.h>
#include <DHT.h>
#include <ESP8266WiFi.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SH110X.h>
// กำหนดค่าต่าง ๆ
#define SSID "iPhoneนวรัตน์"
#define PASSWORD "00000000"
#define LINE_TOKEN "C06pt60lo220Wa6MY3UZB1HcbgpJBSdn2s21Ma0kUqT"
#define DHTPIN D4
#define DHTTYPE DHT11
#define GAS_SENSOR_PIN A0 // ใช้ขา A0 ในการอ่านค่าจาก MQ-2
#define SCREEN_WIDTH 128 // กำหนดความกว้างของหน้าจอ OLED
#define SCREEN_HEIGHT 64 // กำหนดความสูงของหน้าจอ OLED
#define OLED_RESET -1 // การกำหนดค่ารีเซ็ตของ OLED (หากไม่ใช้สามารถกำหนดเป็น -1 ได้)
// กำหนดพินสำหรับ LED
#define LED_TEMP_HIGH_PIN D6 // LED1 สำหรับเซนเซอร์อุณหภูมิ
#define LED_HUMIDITY_HIGH_PIN D7 // LED2 สำหรับเซนเซอร์ความชื้น
#define LED_GAS_HIGH_PIN D8 // LED3 สำหรับเซนเซอร์แก๊ส
#define LED_NORMAL_PIN D5 // LED4 สำหรับสถานะค่าการวัดทั้งหมดปกติ
DHT dht(DHTPIN, DHTTYPE);
Adafruit_SH1106G OLED(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
float tempThreshold = 40.0; // ค่าเกณฑ์อุณหภูมิ
float humidityThreshold = 30.0; // ค่าเกณฑ์ความชื้น
int gasThreshold = 500; // ค่าเกณฑ์แก๊ส (ปรับตามความต้องการของ MQ-2)
bool tempAlertSent = false;
bool humidityAlertSent = false;
bool gasAlertSent = false;
void setup() {
Serial.begin(115200);
Serial.println();
WiFi.begin(SSID, PASSWORD);
Serial.printf("WiFi connecting to %s\n", SSID);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(400);
}
Serial.printf("\nWiFi connected\nIP : ");
Serial.println(WiFi.localIP());
LINE.setToken(LINE_TOKEN);
dht.begin();
if (!OLED.begin()) { // Use default I2C address 0x3C
Serial.println("OLED allocation failed");
} else {
Serial.println("OLED initialized");
}
OLED.clearDisplay(); // ลบภาพในหน้าจอทั้งหมด
OLED.setTextColor(SH110X_WHITE); //กำหนดข้อความสีขาว
OLED.setCursor(0, 0); // กำหนดตำแหน่ง x,y ที่จะแสดงผล
OLED.setTextSize(2); // กำหนดขนาดตัวอักษร
OLED.println("OLED 1.3"); // แสดงผลข้อความ
OLED.setTextSize(1);
OLED.print("Welcome to");
OLED.setCursor(0, 30);
OLED.setTextSize(2);
OLED.setTextColor(SH110X_WHITE); //กำหนดสีพื้นหลัง
OLED.print("AEI.th");
OLED.setCursor(0, 50);
OLED.setTextColor(SH110X_WHITE);
OLED.setTextSize(1);
OLED.println("Laboratory");
OLED.display(); // สั่งให้จอแสดงผล
delay(500);
// กำหนดโหมดพิน LED
pinMode(LED_TEMP_HIGH_PIN, OUTPUT);
pinMode(LED_HUMIDITY_HIGH_PIN, OUTPUT);
pinMode(LED_GAS_HIGH_PIN, OUTPUT);
pinMode(LED_NORMAL_PIN, OUTPUT);
// เริ่มต้นปิด LED ทั้งหมด
digitalWrite(LED_TEMP_HIGH_PIN, LOW);
digitalWrite(LED_HUMIDITY_HIGH_PIN, LOW);
digitalWrite(LED_GAS_HIGH_PIN, LOW);
digitalWrite(LED_NORMAL_PIN, LOW);
}
void loop() {
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
if (isnan(temperature) || isnan(humidity)) {
Serial.println("Failed to read from DHT sensor!");
OLED.clearDisplay();
OLED.setCursor(0, 0);
OLED.print("Failed to read DHT!");
OLED.display();
return;
}
Serial.printf("Temperature: %.2f °C, Humidity: %.2f %%\n", temperature, humidity);
// ตั้งค่าตัวแปรควบคุม LED
bool tempHigh = false;
bool humidityHigh = false;
bool gasHigh = false;
bool allNormal = false;
// ตรวจสอบเงื่อนไขการแจ้งเตือนอุณหภูมิ และส่งแจ้งเตือนเพียงครั้งเดียว
if (temperature > tempThreshold && !tempAlertSent) {
LINE.notify(String("อุณหภูมิสูงเกินไป! อุณหภูมิปัจจุบัน: ") + String(temperature) + " °C");
tempAlertSent = true;
} else if (temperature <= tempThreshold && tempAlertSent) {
LINE.notify(String("อุณหภูมิกลับมาปกติแล้ว: ") + String(temperature) + " °C");
tempAlertSent = false;
}
// ตรวจสอบเงื่อนไขการแจ้งเตือนความชื้นต่ำ และส่งแจ้งเตือนเพียงครั้งเดียว
if (humidity < humidityThreshold && !humidityAlertSent) {
LINE.notify(String("ความชื้นต่ำเกินไป! ความชื้นปัจจุบัน: ") + String(humidity) + " %");
humidityAlertSent = true;
} else if (humidity >= humidityThreshold && humidityAlertSent) {
LINE.notify(String("ความชื้นกลับมาปกติแล้ว: ") + String(humidity) + " %");
humidityAlertSent = false;
}
// อ่านค่าจากเซ็นเซอร์แก๊ส MQ-2
int gasValue = analogRead(GAS_SENSOR_PIN);
Serial.print("Gas Value: ");
Serial.println(gasValue); // ตรวจสอบว่ามีค่าถูกอ่านมาหรือไม่
if (gasValue > gasThreshold && !gasAlertSent) {
LINE.notify(String("ตรวจพบแก๊ส! ค่าการวัดปัจจุบัน: ") + String(gasValue));
gasAlertSent = true;
} else if (gasValue <= gasThreshold && gasAlertSent) {
LINE.notify(String("ค่าการวัดแก๊สกลับมาปกติแล้ว: ") + String(gasValue));
gasAlertSent = false;
}
// กำหนดสถานะ LED ตามเงื่อนไข
tempHigh = temperature > tempThreshold;
humidityHigh = humidity < humidityThreshold;
gasHigh = gasValue > gasThreshold;
// ตรวจสอบว่าเงื่อนไขทั้งหมดปกติหรือไม่
allNormal = !tempHigh && !humidityHigh && !gasHigh;
// ควบคุมการเปิด-ปิด LED
digitalWrite(LED_TEMP_HIGH_PIN, tempHigh ? HIGH : LOW); // เปิด/ปิด LED1 ตามอุณหภูมิ
digitalWrite(LED_HUMIDITY_HIGH_PIN, humidityHigh ? HIGH : LOW); // เปิด/ปิด LED2 ตามความชื้น
digitalWrite(LED_GAS_HIGH_PIN, gasHigh ? HIGH : LOW); // เปิด/ปิด LED3 ตามค่าการวัดแก๊ส
digitalWrite(LED_NORMAL_PIN, allNormal ? HIGH : LOW); // เปิด/ปิด LED4 ตามสถานะค่าปกติ
// แสดงผลบนหน้าจอ OLED
OLED.clearDisplay();
OLED.setCursor(0, 0);
OLED.print("Temp: ");
OLED.print(temperature);
OLED.print(" C");
OLED.setCursor(0, 10);
OLED.print("Humidity: ");
OLED.print(humidity);
OLED.print(" %");
OLED.setCursor(0, 20);
OLED.print("Gas Value: ");
OLED.print(gasValue);
OLED.setCursor(0, 30);
OLED.print(allNormal ? "All Normal" : "Warning!");
OLED.display();
delay(2000);
}