#define BLYNK_PRINT Serial #include #include #include #include #include #include "RTClib.h" #include "PCF8574.h" char auth[] = "hlg4EVStnSTSj0vhpIXxDbudcdj6I8tm";//Codeappในมือถือ char ssid[] = "PANISARA"; char pass[] = "pk130617pk"; BlynkTimer timer; WidgetLED led1(V12); RTC_DS3231 rtc; PCF8574 relay(0x20); float kcbnn[] = {2.21,2.02,2.22,1.94,1.74,1.78,1.96,2.07,2.18,2.18,1.88,1.86}; float kckanun[] = {1.29,1.01,1.29,1.59,1.73,1.77,1.38,1.58,1.83,0.84,0.65,1.27}; float kcmg[] = {1.69,1.61,1.27,1.24,1.19,2.1,2.46,2.53,2.28,2.29,2.5,1.9}; float nkpt[] = {3.7,4.35,5.15,5.12,4.02,4,3.63,3.16,3.44,3.69,3.92,3.66}; float auty[] = {3.95,4.2,4.58,4.58,4.02,4.1,3.73,3.68,3.36,3.46,3.92,3.94}; float spbr[] = {3.45,4.11,4.83,5.01,4.36,3.99,3.89,3.32,3.45,3.45,3.58,3.5}; int m, n, a, u, k,pinValuehr,pinValuemin,e,r,pinValueday,pinValue,pinValueYear,d,o,Switch,Num,Check; double dlay,Vall,Ause,dlay1,depth,kc,eto,pinValuemeter,pinValueq,pinValuekc,pinValueeto,Cube, area; char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; const char* mon[] = {"Jan","Feb","Mar","Apr","May","June","July","Aug","Sep","Oct","Apr","Dec"};//Nouse const char* fruit[] = {"Mango","Banana","Kanun"}; const char* Area[] = {"Nakornphatom","Ayutthaya","Suphanburi"}; double ck = 0; BLYNK_WRITE(V2) { //Hrset pinValuehr = param.asInt(); } BLYNK_WRITE(V3) { //Minset pinValuemin = param.asInt(); } BLYNK_WRITE(V6) { //switchMannual pinValue = param.asInt(); } BLYNK_WRITE(V7) { //Diameter ทรงพุ่ม pinValuemeter = param.asFloat(); } BLYNK_WRITE(V8) { //อัตราการไหลน้ำจากปั้ม pinValueq = param.asFloat(); } BLYNK_WRITE(V5) { //kcกรอกเอง pinValuekc = param.asFloat(); } BLYNK_WRITE(V10) { //etoกรอกเอง pinValueeto = param.asFloat(); } BLYNK_WRITE(V11) { //etoกรอกเอง Switch = param.asFloat(); } BLYNK_WRITE(V14) { //จำนวนต้น Num = param.asFloat(); } BLYNK_WRITE(V1) { //kcพืช switch (param.asInt()) { case 1: a = 0; kc = kcmg[m]; break; case 2: a = 1; kc = kcbnn[m]; break; case 3: a = 2; kc = kckanun[m]; break; default: Serial.println("Unknown item selected"); } } BLYNK_WRITE(V0) { //Eto switch (param.asInt()) { case 1: n = 0; eto = nkpt[m]; break; case 2: n = 1; eto = auty[m]; break; case 3: n = 2; eto = spbr[m] ; break; default: Serial.println("Unknown item selected"); } } void seet()//แสดงเวลา { DateTime now = rtc.now(); u = now.month(); m = u - 1; Serial.print(now.year(), DEC); Serial.print('/'); Serial.print(now.month(), DEC); Serial.print('/'); Serial.print(now.day(), DEC); Serial.print(" ("); Serial.print(daysOfTheWeek[now.dayOfTheWeek()]); Serial.print(") "); Serial.print(now.hour(), DEC); Serial.print(':'); Serial.print(now.minute(), DEC); Serial.print(':'); Serial.println(now.second(), DEC); Serial.print("TimeSet : "); Serial.print(pinValuehr); Serial.print(":"); Serial.println(pinValuemin); String currentTime = String(now.hour()) + ":" + now.minute() + ":" + now.second(); Blynk.virtualWrite(V9, currentTime);//แสดงเวลาในBlynk String currentTimeSet = String(pinValuehr) + ":" + pinValuemin ; Blynk.virtualWrite(V13, currentTimeSet); if(pinValue == 0) { Serial.println("Auto"); } else { Serial.println("Mannual"); } } void see()//Auto { if( pinValue == 0) { Serial.print("Fruit = "); Serial.println(fruit[a]); Serial.print("Area = "); Serial.println(Area[n]); Serial.print("Kc = "); Serial.println(kc); Serial.print("Eto = "); Serial.println(eto); depth = kc*eto*0.001; area = ( 3.14159 * pinValuemeter *pinValuemeter * Num)/4 ; Serial.print("Area = "); Serial.println(area); Vall = depth * area ; Serial.print("Q = "); Serial.println(pinValueq); if(pinValueq == 0) { pinValueq++;//กันnanถ้าไม่ใส่ค่าให้Q=1 } Cube = pinValueq/60000; dlay = Vall / Cube ; Serial.print("DelaySolenoid = "); Serial.print(dlay); Serial.println(" Sec"); Blynk.virtualWrite(V4, dlay);//แสดงลงBlynkค่าDelay Serial.println("-------------------------------------"); } } void see1()//Manual { if( pinValue == 1) { Serial.print("Kc = "); Serial.println(pinValuekc); Serial.print("Eto = "); Serial.println(pinValueeto); depth = pinValuekc*pinValueeto*0.001; area = ( 3.14159 * pinValuemeter *pinValuemeter * Num)/4 ; Serial.print("Area = "); Serial.println(area); Vall = depth * area ; Serial.print("Q = "); Serial.println(pinValueq); if(pinValueq == 0) { pinValueq++;//กันnanถ้าไม่ใส่ค่าให้Q=1 } Cube = pinValueq/60000; dlay = Vall / Cube ; Serial.print("DelaySolenoid = "); Serial.print(dlay); Serial.println(" Sec"); Blynk.virtualWrite(V4, dlay);//แสดงลงBlynkค่าDelay Serial.println("-------------------------------------"); } } void timetoopensolenoi()//เวลาเริ่มให้วาว์ลเปิด { DateTime now = rtc.now(); e = now.hour(); r = now.minute(); k = now.second(); if(e == pinValuehr ) { if(r == pinValuemin){ if(k <= 1){ dlay1 = dlay*1000; Check = 1 ; } } } } void open(){//ฟังค์ชั่นให้เปิดปิดวาวล์ทันที if(Check == 1) if( ck <= dlay1) { ck = ck + 1000; led1.on();//LED Blynkเปิด if(Switch == 1){//กดสวิตช์ในBlynkวาวล์จะเปิด relay.digitalWrite(P1, HIGH); } } if(Switch == 0){ relay.digitalWrite(P1, LOW); led1.off(); } if( ck > dlay1 ){ ck = 0; relay.digitalWrite(P1, LOW); led1.off(); Check = 0; } } void setup() { Serial.begin(9600); Wire.begin(); relay.begin(); Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80); pinMode(D8, OUTPUT); digitalWrite(D8, LOW); relay.pinMode(P1, OUTPUT); relay.pinMode(P6, OUTPUT); // Buzzer digitalWrite(D8, HIGH); #ifndef ESP8266 while (!Serial); #endif rtc.adjust(DateTime(2021, 5, 14, 23, 5, 0));//กำหนดเวลาRTC if (rtc.lostPower()) { Serial.println("RTC lost power, let's set the time!"); } Blynk.virtualWrite(V15, "ระบบให้น้ำอัจฉริยะ"); timer.setInterval(1000L, seet); timer.setInterval(1000L, see); timer.setInterval(1000L, see1); timer.setInterval(1000L, timetoopensolenoi); timer.setInterval(1000L, open); } void loop() { relay.digitalWrite(P6, LOW);//ปิดเสียงBuzzer Blynk.run(); timer.run(); }