#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <WiFi.h>
#include <time.h>
#include <HTTPClient.h>
#define LEBAR_LAYAR 128
#define TINGGI_LAYAR 64
Adafruit_SSD1306 oled(LEBAR_LAYAR, TINGGI_LAYAR, &Wire, -1);
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* googleScriptUrl = "https://script.google.com/macros/s/AKfycbzY0bPmbU6rNs1r6W1AUViNKNQXBWwilx09Bm7EgJta-6eKv53ci5VS_6JKlnLFalak/exec";
const char* lineNotifyToken = "ij3BTrbSEN804X54cvkb4h2FdneatPFXkENlWBrXlRp"; // ใส่ LINE Notify Token ของคุณที่นี่
const int switchPin = 2;
const int LDR_PIN = 34;
const float GAMMA = 0.7;
const float RL10 = 50;
int analogValue;
float voltage;
float resistance;
float lux;
unsigned long previousMillis = 0;
const long interval = 10000; // 10 seconds
const long duration = 600000; // 10 minutes
unsigned long startMillis;
const float LUX_THRESHOLD = 90000.0; // เปอร์เซ็นต์ของค่า LUX สูงสุดที่กำหนด
void setup()
{
Serial.begin(9600);
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C))
{
Serial.println(F("failed to start SSD1306 OLED"));
while (1);
}
oled.clearDisplay();
oled.setTextSize(1);
oled.setTextColor(WHITE);
pinMode(switchPin, INPUT);
pinMode(LDR_PIN, INPUT);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
configTime(25200, 0, "pool.ntp.org", "time.nist.gov");
startMillis = millis();
}
void sendLineNotification(String message) {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin("https://notify-api.line.me/api/notify");
http.addHeader("Authorization", "Bearer " + String(lineNotifyToken));
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
String payload = "message=" + message;
int httpCode = http.POST(payload);
if (httpCode > 0) {
String response = http.getString();
Serial.println(response);
} else {
Serial.print("Error code: ");
Serial.println(httpCode);
}
http.end();
} else {
Serial.println("WiFi not connected.");
}
}
void loop()
{
unsigned long currentMillis = millis();
analogValue = analogRead(LDR_PIN);
voltage = analogValue * 5 / 4095.0;
resistance = 2000 * voltage / (1 - voltage / 5);
lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
int btn = digitalRead(switchPin);
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
Serial.println("Failed to obtain time");
}
char timeString[30];
strftime(timeString, sizeof(timeString), "%Y-%m-%d %H:%M:%S", &timeinfo);
oled.clearDisplay();
oled.setTextSize(1);
oled.setCursor(0, 0);
oled.println(timeString);
oled.setCursor(0, 20);
oled.println("Lux");
oled.setTextSize(2);
oled.setCursor(0, 30);
oled.println(lux, 1);
oled.setTextSize(1);
oled.setCursor(0, 50);
oled.print("Switch State: ");
oled.println(btn);
oled.display();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
if (currentMillis - startMillis < duration) {
// ส่งข้อมูลไปยัง Google Sheets
HTTPClient http;
String url = String(googleScriptUrl) + "?switch=" + String(btn) + "&lux=" + String(lux, 1);
http.begin(url);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
int httpCode = http.GET();
if (httpCode > 0) {
String payload = http.getString();
Serial.println(payload);
} else {
Serial.print("Error code: ");
Serial.println(httpCode);
}
http.end();
// ส่งการแจ้งเตือน LINE ถ้าสวิตซ์อยู่ที่ 0
if (btn == 0) {
sendLineNotification("มีผู้บุกรุก");
}
// ส่งการแจ้งเตือน LINE ถ้าค่าของเซ็นเซอร์สูงกว่าค่าที่กำหนด
if (lux > LUX_THRESHOLD) {
sendLineNotification("ค่าของเซ็นเซอร์สูงกว่าค่าที่กำหนด");
}
} else {
Serial.println("10 minutes elapsed, stopping data send");
while (true); // Stop sending data
}
}
delay(1000);
}