สลับการทำงาน Manual Auto Blynk

อยากจะสลับโหมดการทำงาน Manual Auto โดยใช้อุณภูมิความชื้นควบคุมการทำงานพัดลมในส่วน Auto และใช้ปุ่มกดควบคุมขณะ Manual ซึ่งอยากจะแยกการทำงานจากกันโดยขณะใช้งานในโหมด Auto จะไม่สามรถกดปุ่ม on/off ได้ ครับ

หน้าการทำงานครับ

#include <DHT.h>
#include "RTClib.h"
#define  BLYNK_PRINT Serial
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
RTC_DS3231 RTC;
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
#define DHTPIN  15
#define DHTTYPE DHT22
int pin_FAN1 = 12;
int pin_FAN2 = 14;
int pin_FAN3 = 27;
int pin_FAN4 = 26;
int pin_FAN5 = 25;
int pin_FAN6 = 33;
int pin_PUM  = 35;


bool _button_ma = false;
bool _button = false;
bool _button1 = false;
bool _button2 = false;
bool _button3 = false;
bool _button4 = false;
bool _button5 = false;
bool _button6 = false;
bool _button7 = false;

WidgetLED led1(V30);
WidgetLED led2(V31);
WidgetLED led3(V32);
WidgetLED led4(V33);
WidgetLED led5(V34);
WidgetLED led6(V35);
WidgetLED led7(V36);

BLYNK_WRITE(V0)
{
  if (param.asInt() == 1) {

    _button_ma = true;

  } else {

    _button_ma = false;

  }
}

BLYNK_WRITE(12) {

  if (param.asInt() == 1) {
    _button1 = true;

  } else {
    _button1 = false;

  }
}

BLYNK_WRITE(14) {

  if (param.asInt() == 1) {
    _button2 = true;

  } else {
    _button2 = false;
  }
}

BLYNK_WRITE(27) {

  if (param.asInt() == 1) {
    _button3 = true;
  } else {
    _button3 = false;

  }
}

BLYNK_WRITE(26) {

  if (param.asInt() == 1) {
    _button4 = true;

  } else {
    _button4 = false;

  }
}
BLYNK_WRITE(25) {

  if (param.asInt() == 1) {
    _button5 = true;

  } else {
    _button5 = false;

  }
}
BLYNK_WRITE(33) {

  if (param.asInt() == 1) {
    _button6 = true;

  } else {
    _button6 = false;

  }
}
BLYNK_WRITE(35) {

  if (param.asInt() == 1) {
    _button7 = true;

  } else {
    _button7 = false;

  }
}

int WEEK = 1;
char sbuf[64];
char buf[64];
DHT dht(DHTPIN, DHTTYPE);
SimpleTimer timer;
char auth[] = "O2s1eMsptXXQwnK2Nhxn_7gDY9f7kSIG";
char ssid[] = "iPhone";
char pass[] = "back55140";
int btRead;
int bt = 1;
int bt1 = 1 ;
int show = -1;
int Menu = 0;
int mainMenu = 0;
bool lcdChange = 1;
bool lcdSetup = 1;
int setHr = 0;
int setMin = 0;
int setweek = 1;
int  G = 100;

void setup() {
  Serial.println("DHTxx test!");
  lcd.clear();
  dht.begin();
  lcd.begin();
  lcd.backlight();
  pinMode(pin_FAN1 , OUTPUT);
  pinMode(pin_FAN2 , OUTPUT);
  pinMode(pin_FAN3 , OUTPUT);
  pinMode(pin_FAN4 , OUTPUT);
  pinMode(pin_FAN5 , OUTPUT);
  pinMode(pin_FAN6 , OUTPUT);
  pinMode(pin_PUM , OUTPUT);
    digitalWrite (pin_FAN1, 1);
    digitalWrite (pin_FAN2, 1);
    digitalWrite (pin_FAN3, 1);
    digitalWrite (pin_FAN4, 1);
    digitalWrite (pin_FAN5, 1);
    digitalWrite (pin_FAN6, 1);
    digitalWrite (pin_PUM, 1);

  Blynk.begin(auth, ssid, pass, "128.199.173.118", 8080);
  Serial.begin(115200);
  Wire.begin();
  RTC.begin();
  RTC.adjust(DateTime(__DATE__, __TIME__));
  Serial.print('Time and date set');
}
void loop() {
  DHTTEM();
  Week();

  //  Button();
  //  if (bt1 == 1) {
  //    DHTTEM();
  //    Week();
  //    Time ();
  //    data();
  //  }
  //  if (bt == 5) {
  //    while (bt == 0);
  //    bt1 = 1;
  //  }
  //  if (bt == 1) {
  //    while (bt == 0);
  //    bt1 = 0;
  //    lcdChange = 1;
  //  }
  //  if (bt == 2) {
  //    while (bt == 0);
  //    if (mainMenu = 0) {
  //
  //      mainMenu = 0;
  //    }
  //    lcdChange = 1;
  //  }
  //  if (bt == 3) { //A
  //    while (bt == 0);
  //
  //    if (mainMenu == 0) {
  //      setweek++;
  //      if (setweek > 6)setweek = 1;
  //    }
  //    lcdChange = 1;
  //  }
  //  if (bt == 4) { //C
  //    while (bt == 0);
  //    if (mainMenu == 0) {
  //      setweek--;
  //      if (setweek < 1)setweek = 1;
  //    }
  //
  //    lcdChange = 1;
  //  }
  //  if (lcdChange == 1) {
  //    lcd.setCursor(1, 0); lcd.print("     SETTING   ");
  //    lcd.setCursor(1, 1); lcd.print("WEEK = "); lcd.print(setweek); lcd.print("         ");
  //    lcd.setCursor(1, 2); lcd.print("               ");
  //    if (mainMenu == 0) {
  //      lcd.setCursor(0, 0); lcd.print(" ");
  //      lcd.setCursor(0, 1); lcd.print("*");
  //      lcd.setCursor(0, 2); lcd.print(" ");
  //    }
  //    lcdChange = 0;
  //  }
}
void Week () {
  float humi = dht.readHumidity();
  float temp = dht.readTemperature();
  float f = dht.readTemperature(true);
  float hif = dht.computeHeatIndex(f, humi);
  float hic = dht.computeHeatIndex(temp, humi, false);
  if (isnan(humi) || isnan(temp) || isnan(f)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  if ( _button_ma == true) {
    _button == false;
    switch (setweek) {
      case 1 ://สัปดาห์ที่ 1
        if (temp >= 24) {
          digitalWrite(pin_FAN1 , 1);
          led1.on();
          Serial.println("Relay HIGH 1 ");
        }
        else if (temp <  24) {
          digitalWrite(pin_FAN1 , 0);
          led1.off();
        }
        if (temp >= 25) {
          digitalWrite(pin_FAN2 , 1);
           led2.on();
          Serial.println("Relay HIGH 2 ");
        }
        else if (temp <  25) {
          digitalWrite(pin_FAN2 , 0);
           led2.off();
        }
        if (temp >= 31) {
          digitalWrite(pin_FAN3 , 1);
          Serial.println("Relay HIGH 3 ");
        }
        else if (temp <  31) {
          digitalWrite(pin_FAN3 , 0);
        }
        if (temp >= 32) {
          digitalWrite(pin_FAN4 , 1);
          Serial.println("Relay HIGH 4 ");
        }
        else if (temp <  32) {
          digitalWrite(pin_FAN4 , 0);
        }
        if (temp >= 33) {
          digitalWrite(pin_FAN5 , 1);
          Serial.println("Relay HIGH 5 ");
        }
        else if (temp <  33) {
          digitalWrite(pin_FAN5 , 0);
        }
        if (temp >= 34) {
          digitalWrite(pin_FAN6 , 1);
          Serial.println("Relay HIGH 6 ");
        }
        else if (temp <  34) {
          digitalWrite(pin_FAN6 , 0);
        }
        if (humi <= 75) {
          digitalWrite(pin_PUM, 1);
          Serial.println("Relay HIGH PUM ");
        }
        else if (humi >  75) {
          digitalWrite(pin_PUM  , 0);
        }
        break;
      case 2 ://สัปดาห์ที่ 2
        if (temp >= 24) {
          digitalWrite(pin_FAN1 , 1);
          Serial.println("Relay HIGH 1 ");
        }
        else if (temp <  24) {
          digitalWrite(pin_FAN1 , 0);
        }
        if (temp >= 30) {
          digitalWrite(pin_FAN2 , 1);
          Serial.println("Relay HIGH 2 ");
        }
        else if (temp <  30) {
          digitalWrite(pin_FAN2 , 0);
        }
        if (temp >= 31) {
          digitalWrite(pin_FAN3 , 1);
          Serial.println("Relay HIGH 3 ");
        }
        else if (temp <  31) {
          digitalWrite(pin_FAN3 , 0);
        }
        if (temp >= 32) {
          digitalWrite(pin_FAN4 , 1);
          Serial.println("Relay HIGH 4 ");
        }
        else if (temp <  32) {
          digitalWrite(pin_FAN4 , 0);
        }
        if (temp >= 33) {
          digitalWrite(pin_FAN5 , 1);
          Serial.println("Relay HIGH 5 ");
        }
        else if (temp <  33) {
          digitalWrite(pin_FAN5 , 0);
        }
        if (temp >= 34) {
          digitalWrite(pin_FAN6 , 1);
          Serial.println("Relay HIGH 6 ");
        }
        else if (temp <  34) {
          digitalWrite(pin_FAN6 , 0);
        }
        if (humi <= 75) {
          digitalWrite(pin_PUM, 1);
          Serial.println("Relay HIGH PUM ");
        }
        else if (humi >  75) {
          digitalWrite(pin_PUM  , 0);
        }
        break;

      case 3 : //สัปดาห์ที่ 3
        if (temp >= 24) {
          digitalWrite(pin_FAN1 , 1);
          Serial.println("Relay HIGH 1 ");
        }
        else if (temp <  24) {
          digitalWrite(pin_FAN1 , 0);
        }
        if (temp >= 30) {
          digitalWrite(pin_FAN2 , 1);
          Serial.println("Relay HIGH 2 ");
        }
        else if (temp <  30) {
          digitalWrite(pin_FAN2 , 0);
        }
        if (temp >= 31) {
          digitalWrite(pin_FAN3 , 1);
          Serial.println("Relay HIGH 3 ");
        }
        else if (temp <  31) {
          digitalWrite(pin_FAN3 , 0);
        }
        if (temp >= 32) {
          digitalWrite(pin_FAN4 , 1);
          Serial.println("Relay HIGH 4 ");
        }
        else if (temp <  32) {
          digitalWrite(pin_FAN4 , 0);
        }
        if (temp >= 33) {
          digitalWrite(pin_FAN5 , 1);
          Serial.println("Relay HIGH 5 ");
        }
        else if (temp <  33) {
          digitalWrite(pin_FAN5 , 0);
        }
        if (temp >= 34) {
          digitalWrite(pin_FAN6 , 1);
          Serial.println("Relay HIGH 6 ");
        }
        else if (temp <  34) {
          digitalWrite(pin_FAN6 , 0);
        }
        if (humi <= 75) {
          digitalWrite(pin_PUM, 1);
          Serial.println("Relay HIGH PUM ");
        }
        else if (humi >  75) {
          digitalWrite(pin_PUM  , 0);
        }
        break;

      case 4 ://สัปดาห์ที่ 4
        if (temp >= 24) {
          digitalWrite(pin_FAN1 , 1);
          Serial.println("Relay HIGH 1 ");
        }
        else if (temp <  24) {
          digitalWrite(pin_FAN1 , 0);
        }
        if (temp >= 30) {
          digitalWrite(pin_FAN2 , 1);
          Serial.println("Relay HIGH 2 ");
        }
        else if (temp <  30) {
          digitalWrite(pin_FAN2 , 0);
        }
        if (temp >= 31) {
          digitalWrite(pin_FAN3 , 1);
          Serial.println("Relay HIGH 3 ");
        }
        else if (temp <  31) {
          digitalWrite(pin_FAN3 , 0);
        }
        if (temp >= 32) {
          digitalWrite(pin_FAN4 , 1);
          Serial.println("Relay HIGH 4 ");
        }
        else if (temp <  32) {
          digitalWrite(pin_FAN4 , 0);
        }
        if (temp >= 33) {
          digitalWrite(pin_FAN5 , 1);
          Serial.println("Relay HIGH 5 ");
        }
        else if (temp <  33) {
          digitalWrite(pin_FAN5 , 0);
        }
        if (temp >= 34) {
          digitalWrite(pin_FAN6 , 1);
          Serial.println("Relay HIGH 6 ");
        }
        else if (temp <  34) {
          digitalWrite(pin_FAN6 , 0);
        }
        if (humi <= 75) {
          digitalWrite(pin_PUM, 1);
          Serial.println("Relay HIGH PUM ");
        }
        else if (humi >  75) {
          digitalWrite(pin_PUM  , 0);
        }
        break;


      case 5 ://สัปดาห์ที่ 5
        if (temp >= 24) {
          digitalWrite(pin_FAN1 , 1);
          Serial.println("Relay HIGH 1 ");
        }
        else if (temp <  24) {
          digitalWrite(pin_FAN1 , 0);
        }
        if (temp >= 30) {
          digitalWrite(pin_FAN2 , 1);
          Serial.println("Relay HIGH 2 ");
        }
        else if (temp <  30) {
          digitalWrite(pin_FAN2 , 0);
        }
        if (temp >= 31) {
          digitalWrite(pin_FAN3 , 1);
          Serial.println("Relay HIGH 3 ");
        }
        else if (temp <  31) {
          digitalWrite(pin_FAN3 , 0);
        }
        if (temp >= 32) {
          digitalWrite(pin_FAN4 , 1);
          Serial.println("Relay HIGH 4 ");
        }
        else if (temp <  32) {
          digitalWrite(pin_FAN4 , 0);
        }
        if (temp >= 33) {
          digitalWrite(pin_FAN5 , 1);
          Serial.println("Relay HIGH 5 ");
        }
        else if (temp <  33) {
          digitalWrite(pin_FAN5 , 0);
        }
        if (temp >= 34) {
          digitalWrite(pin_FAN6 , 1);
          Serial.println("Relay HIGH 6 ");
        }
        else if (temp <  34) {
          digitalWrite(pin_FAN6 , 0);
        }
        if (humi <= 75) {
          digitalWrite(pin_PUM, 1);
          Serial.println("Relay HIGH PUM ");
        }
        else if (humi >  75) {
          digitalWrite(pin_PUM  , 0);
        }
        break;
      case 6: //สัปดาห์ที่ 6
        if (temp >= 24) {
          digitalWrite(pin_FAN1 , 1);
          Serial.println("Relay HIGH 1 ");
        }
        else if (temp <  24) {
          digitalWrite(pin_FAN1 , 0);
        }
        if (temp >= 30) {
          digitalWrite(pin_FAN2 , 1);
          Serial.println("Relay HIGH 2 ");
        }
        else if (temp <  30) {
          digitalWrite(pin_FAN2 , 0);
        }
        if (temp >= 31) {
          digitalWrite(pin_FAN3 , 1);
          Serial.println("Relay HIGH 3 ");
        }
        else if (temp <  31) {
          digitalWrite(pin_FAN3 , 0);
        }
        if (temp >= 32) {
          digitalWrite(pin_FAN4 , 1);
          Serial.println("Relay HIGH 4 ");
        }
        else if (temp <  32) {
          digitalWrite(pin_FAN4 , 0);
        }
        if (temp >= 33) {
          digitalWrite(pin_FAN5 , 1);
          Serial.println("Relay HIGH 5 ");
        }
        else if (temp <  33) {
          digitalWrite(pin_FAN5 , 0);
        }
        if (temp >= 34) {
          digitalWrite(pin_FAN6 , 1);
          Serial.println("Relay HIGH 6 ");
        }
        else if (temp <  34) {
          digitalWrite(pin_FAN6 , 0);
        }
        if (humi <= 75) {
          digitalWrite(pin_PUM, 1);
          Serial.println("Relay HIGH PUM ");
        }
        else if (humi >  75) {
          digitalWrite(pin_PUM  , 0);
        }
        break;
    }
  }else{
  if (_button1 == true) {
      digitalWrite(pin_FAN1, 1);
      led1.on();
    } else {
      digitalWrite(pin_FAN1, 0);
      led1.off();
    }
    if (_button2 == true) {
      digitalWrite(pin_FAN2, 1);
      led2.on();
    } else {
      digitalWrite(pin_FAN2, 0);
      led2.off();
    }
    if (_button3 == true) {
      digitalWrite(pin_FAN3, 1);
      led3.on();
    } else {
      digitalWrite(pin_FAN3, 0);
      led3.off();
    }
    if (_button4 == true) {
      digitalWrite(pin_FAN4, 1);
      led4.on();
    } else {
      digitalWrite(pin_FAN4, 0);
      led4.off();
    }
    if (_button5 == true) {
      digitalWrite(pin_FAN5, 1);
      led5.on();
    } else {
      digitalWrite(pin_FAN5, 0);
      led5.off();
    }
    if (_button6 == true) {
      digitalWrite(pin_FAN6, 1);
      led6.on();
    } else {
      digitalWrite(pin_FAN6, 0);
      led6.off();
    }
    if (_button7 == true) {
      digitalWrite(pin_PUM, 1);
      led7.on();
    } else {
      digitalWrite(pin_PUM, 0);
      led7.off();
    }
 }
}
 
void Button() {
  btRead = analogRead(A0);
  Serial.println(btRead);
  Serial.print("\t");
  bt = 0;
  if ((btRead > 500) && (btRead < 525))bt = 1;
  if ((btRead > 600) && (btRead < 625))bt = 2;
  if ((btRead > 700) && (btRead < 775))bt = 3;
  if ((btRead > 980) && (btRead < 1015))bt = 4;
  if (btRead > 1020)bt = 5;
  delay(200);
}
void DHTTEM() {
  float humi = dht.readHumidity();
  float temp = dht.readTemperature();
  float f = dht.readTemperature(true);
  if (isnan(humi) || isnan(temp) || isnan(f)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  float hif = dht.computeHeatIndex(f, humi);
  float hic = dht.computeHeatIndex(temp, humi  , false);
  Serial.print("Humidity: ");
  Serial.print(humi);
  Serial.print(" %\t");
  Serial.print("Temperature: ");
  Serial.print(temp);
  Serial.print(" *C ");
  Serial.print(f);
  Serial.print(" *F\t");
  Serial.print("Heat index: ");
  Serial.print(hic);
  Serial.print(" *C ");
  Serial.print(hif);
  Serial.println(" *F");
  lcd.home();
  lcd.setCursor(0, 0);
  lcd.print("      WEEK = ");
  lcd.print(  setweek  );
  lcd.setCursor(0, 1);
  lcd.print("Temp : ");
  lcd.print(temp);
  lcd.setCursor(0, 2);
  lcd.print("Humi : ");
  lcd.print(humi);
  lcd.print("%");
  DateTime now = RTC.now();
  Serial.print(now.year(), DEC);
  Serial.print('/');
  Serial.print(now.month(), DEC);
  Serial.print('/');
  Serial.print(now.day(), DEC);
  Serial.print(' ');
  Serial.print(now.hour(), DEC);
  Serial.print(':');
  Serial.print(now.minute(), DEC);
  Serial.print(':');
  Serial.print(now.second(), DEC);
  Serial.println();
  sprintf( sbuf, "%02d:%02d", now.hour(), now.minute());
  sprintf( buf, "%02d/%02d/%02d", now.day(), now.month(), now.year() % 100);
  lcd.setCursor(1, 3);
  lcd.print(sbuf);
  //
  lcd.setCursor(12, 3);
  lcd.print(buf);
  Blynk.run();
  timer.run();
  {
    Blynk.virtualWrite(V1, temp);
    Blynk.virtualWrite(V2, humi);
    Blynk.virtualWrite(V3, temp);
    Blynk.virtualWrite(V4, humi);
  }
}

1 Like

แล้วตอนนี้ทำอะไรได้แล้วบางครับ ลิสรายการสิ่งที่ต้องการ กับสิ่งที่ทำได้แล้วมาดูหน่อย

สลับการทำงานได้แล้ว แต่ยังไม่สมบูรณ์ตอนเป็น Auto ยังกดสั่งการทำงานผ่านปุ่มได้ ตอนManual เซนเซอร์ยังควบคุมการทำงานอยู่ครับ

ถ้าต้องการ lock ปุ่มทำงานระหว่าง เป็น auto ก็ไม่ยากครับ เราก็ครอบ if ไว้ที่ ปุ่มกด สักตัว ก็จบแล้ว เช่น

bool lockAuto = false;

if( autoMode == true ) {  //เข้าโหมด auto
  lockAuto = true;
}else{
  lockAuto = false;
}


BLYNK_WRITE(V0)
{
  if (param.asInt() == 1) {

     if(lockAuto == false){ // lock เมื่อกด auto จะไม่เข้าไปทำงานคำสั่งนี้
    _button_ma = true;
}

  } else {

    _button_ma = false;

  }
}

หลักการก็มีแค่นี้ครับ ลองไปศึกษาและประยุกค์ใช้ดู

bool lockAuto = false;

if( autoMode == true ) {  //เข้าโหมด auto
  lockAuto = true;
}else{
  lockAuto = false;
}

ขออณุญาตนะครับ ชุดคำสั่งนี้ใส่ตรงไหนครับ แฮ่กๆๆ

แล้วเวลาเรากดโหมด auto มันอยู่ตรงไหนก็แปลงไปใส่ตรงนั่นครับ