#define BLYNK_TEMPLATE_ID "TMPLemB7BSjW"
#define BLYNK_DEVICE_NAME "Water"
#define BLYNK_AUTH_TOKEN " "
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
BlynkTimer timer;
//เพื่อป้องกันการrefresh ของ code Blynk เลยมาคู่กับ lib ตัวนึงชื่อว่า SimpleTimer.h ใช้สำหรับแยกช่วงเวลาในการอ่านค่า Sensor ไม่ให้ Task ชนกัน
// BlynkTimer <ชื่อ> คือการกล่าวว่า จะให้ตัวนี้เป็นตัวบงบอกว่าจะใช้functionในการนับเวลา
// https://playground.arduino.cc/Code/SimpleTimer/ รวมวิธีใช้หมดแล้ว
//=======================Anounce global Variable==============================================
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "Yanyong4";
char pass[] = "4120743374";
// ใช wifi 2.4GHzเทานน
int prevsenVal = 4;
int Moistper;
int senVal;
//=======================Anounce global Variable==============================================
void setup() {
Serial.begin(115200);
Blynk.begin(auth, ssid, pass);
int Moistduration = timer.setInterval(100L, Moistsense); //อ่านค่าทุกๆ 1 วิ(1000L) ถ้ากล่วว่าจะ ถี่เกินไหปให้ ใส่ ประมาณ 1-5 นาทีก็ได้ (60e3UL - 30e4UL
// || L = Long , UL = unsiggned long เพื่อขยายขอบเขตของตัวเลขในการบันทึกลง memory ได้มากขึ้น|| 60e3 คือ 60 * 10^3 |
}
void Moistsense() {
senVal = analogRead(A12);
Moistper = (100 - ((senVal / 4095.00) * 100));
String announc = String(" ความชื้น:") + Moistper + String(" %"); //ทำเป็น Massage คำพูด
if (Moistper != prevsenVal) {
if (Moistper <= 25) {
Serial.println("แห้งมาก 😾" + announc); // 0% - 25%
Blynk.notify("แห้งมาก 😾" + announc);
prevsenVal = Moistper; //โชว์แค่ ครั้งนึง ถ้า ลบอออก จะเป็นการแจ้งเตือนหลายๆรอบ ย้ำว่าต้องรีบเติมน้ำ!
}
else if (Moistper <= 50 && Moistper > 25) { // 26% - 50%
Serial.println("แห้งนิดนึง 😿" + announc);
Blynk.notify("แห้งแห้ง 😿" + announc);
prevsenVal = Moistper; //จะแจงแค 1 ครง ถาไมเอาสามามารถ ลบ เหลอแค Blynk.notify
}
else if (Moistper <= 75 && Moistper > 50) { // 51% - 75%
Serial.println("กำลังพอดี 😺" + announc);
Blynk.notify("กำลังพอดี 😺" + announc);
prevsenVal = Moistper; //จะแจงแค 1 ครง ถาไมเอาสามามารถ ลบ เหลอแค Blynk.notify
}
else if (Moistper <= 100 && Moistper > 75) { //76% - 100%
Serial.println("ฉุ่มฉ่ำ 😹" + announc);
Blynk.notify("ฉุ่มฉ่ำ 😹" + announc);
prevsenVal = Moistper; //จะแจงแค 1 ครง ถาไมเอาสามามารถ ลบ เหลอแค Blynk.notify
}
}
}
void loop() {
timer.run();
Blynk.run();
/*
╒═════════════════════╦
│ ระดับ ║ %Percentage ║ แจ้งเตือน │
╞═════════════════════╬
│ 0 ║ 0% - 25% ║ แห้งมาก 😾 │
│ 1 ║ 26% - 50% ║ แห้งแห้ง 😿 │
│ 2 ║ 51% - 75% ║กำลังพอดี 😺 │
│ 3 ║ 76% - 100% ║ ฉุ่มฉ่ำ 😹 │
╘════╩════════╩═══════╩
Example output: แห้งมาก 😾 ความชื้น:15 %
Example output: แห้งแห้ง 😿 ความชื้น:26 %
Example output: กำลังพอดี 😺 ความชื้น:57 %
Example output: ฉุ่มฉ่ำ 😹 ความชื้น:80 %
*/
}