#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_ADS1X15.h>
#include <WiFi.h>
#include <HTTPClient.h>
// WiFi 資訊
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// Line Notify 權杖(Token)
const char* lineToken = "4MoqPh94RvUo8L0WjEB4YLuXEbgeq36W63ZGitAb0QL";
// 土壤溼度感測器的引腳
const int soilMoisturePin = A0;
// 濕度閾值
const int moistureThreshold = 500;
void setup() {
Serial.begin(115200);
// 連接到WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
// 初始化土壤溼度感測器
pinMode(soilMoisturePin, INPUT);
}
void loop() {
// 讀取土壤濕度值
int soilMoisture = analogRead(soilMoisturePin);
// 判斷濕度是否低於閾值
if (soilMoisture < moistureThreshold) {
// 發送 Line Notify 通知
sendLineNotify("土壤濕度過低,請檢查澆水狀況!");
delay(300000); // 延遲 5 分鐘,避免過度通知
}
delay(1000); // 每秒檢查一次濕度
}
// 函式:發送 Line Notify 通知
void sendLineNotify(String message) {
HTTPClient http;
String url = "https://notify-api.line.me/api/notify";
http.begin(url);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
http.addHeader("Authorization", "Bearer " + String(lineToken));
int httpCode = http.POST("message=" + message);
http.end();
}