#include <WiFi.h>
#include <HTTPClient.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SH110X.h>
// ขนาดจอ OLED
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SH1106G display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
// กำหนดขา DHT22
#define DHTPIN 26 // ขาที่เชื่อมต่อกับ DHT22
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
// กำหนดขา Ultrasonic
#define TRIG_PIN 12
#define ECHO_PIN 13
// กำหนดขา Buzzer
#define BUZZER_PIN 27
// ข้อมูล WiFi
const char* ssid = "eNeasTa_2.4G";
const char* password = "Apichai.";
// Token สำหรับ Line Notify
String lineToken = "lEOBABVeDy3ROrCi0SRp7V5yQx4bSzQ14CSKU4fdsR8";
// ฟังก์ชันแจ้งเตือนผ่าน Line Notify
void sendLineNotify(String message) {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin("https://notify-api.line.me/api/notify");
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
http.addHeader("Authorization", "Bearer " + lineToken);
String data = "" + message;
int httpResponseCode = http.POST(data);
http.end();
}
}
void setup() {
Serial.begin(115200);
// เริ่มการทำงานของจอ OLED
if (!display.begin()) { // Use default I2C address 0x3C
Serial.println("display allocation failed");
} else {
Serial.println("display initialized");
}
display.clearDisplay();
display.setTextColor(SH110X_WHITE);
display.setCursor(0, 0);
display.print("PROJECT READY");
display.display();
// เริ่มการทำงานของ DHT22
dht.begin();
// กำหนดขา Ultrasonic
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
// กำหนดขา Buzzer
pinMode(BUZZER_PIN, OUTPUT);
// เชื่อมต่อ WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
}
void loop() {
// อ่านข้อมูลจาก DHT22
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
// อ่านข้อมูลจาก Ultrasonic
long duration;
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
duration = pulseIn(ECHO_PIN, HIGH);
float distance = duration / 58.2; // คำนวณระยะทางเป็นเซนติเมตร
// แสดงข้อมูลที่หน้าจอ OLED
display.clearDisplay();
display.setTextColor(SH110X_WHITE);
display.setCursor(16, 0);
display.print("--PROJECT EN2R2--");
display.setCursor(0, 16);
display.println("Temperature: " + String(temperature) + " C");
display.setCursor(0, 32);
display.println("Humidity : " + String(humidity) + " %");
display.setCursor(0, 48);
display.println("Distance : " + String(distance) + " cm");
display.display();
// แจ้งเตือนผ่านไลน์ถ้าอุณหภูมิสูงเกินไป
if (temperature > 20) {
sendLineNotify("message= อุณหภูมิสูงเกิน 30 *C - Mr.Apichai 🌡");
Serial.println("High temperature message sent");
Serial.println("==================================");
digitalWrite(18, HIGH);}
delay(1000);
// แจ้งเตือนผ่านไลน์ถ้าความชื้นมากเกินไป
if (humidity > 50) {
sendLineNotify("message= ความชื้นสูงเกิน 60 % - Mr.Apichai 🌫");
Serial.println("High humidity message sent");
Serial.println("==================================");
digitalWrite(19, HIGH);}
delay(1000);
// แจ้งเตือนด้วยเสียงถ้าวัตถุอยู่ใกล้เกินไป
if (distance < 20) {
tone(BUZZER_PIN, 1000); // ส่งเสียงเตือน
} else {
noTone(BUZZER_PIN); // ปิดเสียงเตือน
}
delay(2000); // หน่วงเวลาการอ่านข้อมูลใหม่
}