ขอสอบถามวิธีการเพิ่มระบบ Manual ลงในระบบ Auto หน่อยครับ

ตัวโปรแกรมควบคุมผ่านแอพ Blynk สามารถวัดความชื้นและอุณหภูมิได้ สามารถตั้งค่าเพื่อให้ปั้มน้ำและพัดลมทำงานตามช่วงความชื้นและอุณหภูมิที่กำหนดได้ แต่ผมอยากเพิ่มปุ่มกดเปลี่ยนโหมดระหว่าง Auto และ Manual ครับ โดยผมอยากให้ตัวปุ่ม V10 เป็นปุ่ม Auto / Manual เพื่อที่จะเอาไว้ควบคุมปุ่ม V8และV9 ที่ตอนนี้เป็นแค่ปุ่มแสดงสถานะการทำงานของพัดลมและปั้มน้ำครับ ความคืบหน้าคือระบบสามารถใช้งานได้แล้วแต่ยังไม่มีระบบ Manual ครับ


#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <EEPROM.h>
#define  BLYNK_PRINT Serial
#include <BlynkSimpleEsp8266.h>
#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <ESP_WiFiManager.h>
//--------------------------------------------------
#define ESP_getChipId() (ESP.getChipId())
String ssid = " ";
const char* password = " ";
String Router_SSID;
String Router_Pass;
#define USE_AVAILABLE_PAGES     false
//--------------------------------------------------
#include <DHT.h>
DHT dht;
//------------ LCD ---------------------------
LiquidCrystal_I2C lcd(0x27,20,4);// PCF8574A =  0x3F

#define  PUMP D5 
#define  FAN  D6 
#define  Buzzer D0  
float    Humidity,Temperature;
float    Humidity1,Temperature1;
float    Humidity2,Temperature2;
unsigned char  HMin,HMax,TMin,TMax;
unsigned char  S1,S2;

BlynkTimer timer;
char auth[] = 

//======================================================
void  WifiConnect()
{unsigned int i,TimeOut= 0;
   ESP_WiFiManager ESP_wifiManager;
   ESP_wifiManager.setMinimumSignalQuality(-1);
   ESP_wifiManager.setSTAStaticIPConfig(IPAddress(192,168,2,114), IPAddress(192,168,2,1), 
                                      IPAddress(255,255,255,0), 
                                      IPAddress(192,168,2,1), IPAddress(8,8,8,8));
   Router_SSID = ESP_wifiManager.WiFi_SSID();
   Router_Pass = ESP_wifiManager.WiFi_Pass();
   Serial.println("Stored: SSID = " + Router_SSID + ", Pass = " + Router_Pass);
   Serial.println("Opening configuration portal.");
   if(digitalRead(D3)== 0)
   { lcd.setCursor(0,0);lcd.print("  Setting  Wifi...  "); 
     Beep();
     ESP_wifiManager.setConfigPortalTimeout(60); //If no access point name has been previously entered disable timeout.
     Serial.println("Timeout 60s");
     ssid.toUpperCase();  
     if (!ESP_wifiManager.startConfigPortal((const char *) ssid.c_str(), password)){Serial.println("Not connected to WiFi.");}
                                                                              else{Serial.println("WiFi connected...yeh :)");}
   }
   Serial.print("Connecting to "); Serial.println(Router_SSID);    
   while ( (WiFi.status() != WL_CONNECTED) && (TimeOut < 30) )
   {  WiFi.mode(WIFI_STA);
      WiFi.persistent (true);      
      WiFi.begin(Router_SSID.c_str(), Router_Pass.c_str());
      while((!WiFi.status() || WiFi.status() >= WL_DISCONNECTED) && i++ < 200){delay(200);}   
      TimeOut++; 
      Serial.print(".");//delay(50);
   }
  if (WiFi.status() == WL_CONNECTED){Serial.print("connected. Local IP: ");
                                     Serial.println(WiFi.localIP());  }
}
//---------------------------
void sendSensor()
{
  Blynk.virtualWrite(V0, Humidity);
  Blynk.virtualWrite(V1, Temperature);
  Blynk.virtualWrite(V2,String(HMin)+" - "+String(HMax));
  Blynk.virtualWrite(V3,String(TMin)+" - "+String(TMax));
  if(S1 == 0){Blynk.virtualWrite(V8, LOW);}else{Blynk.virtualWrite(V8, HIGH);}
  if(S2 == 0){Blynk.virtualWrite(V9, LOW);}else{Blynk.virtualWrite(V9, HIGH);}
  
}
//=====================================================
BLYNK_WRITE(V4) 
{  
  int value = param.asInt();
  Serial.println(value,DEC);
  HMin = value;
  EEPROM.write(1,value);
  EEPROM.commit();
  lcd.setCursor(13,2);lcd.print(HMin);
  Beep();
}
//=====================================================
BLYNK_WRITE(V5) 
{  
  int value = param.asInt();
  Serial.println(value,DEC);
  HMax = value;
  EEPROM.write(2,value);
  EEPROM.commit();
  lcd.setCursor(16,2);lcd.print(HMax);
  Beep();
}
//=====================================================
BLYNK_WRITE(V6) 
{  
  int value = param.asInt();
  Serial.println(value,DEC);
  TMin = value;
  EEPROM.write(3,value);
  EEPROM.commit();
  lcd.setCursor(13,3);lcd.print(TMin);
  Beep();
}

//=====================================================
BLYNK_WRITE(V7) 
{  
  int value = param.asInt();
  Serial.println(value,DEC);
  TMax = value;
  EEPROM.write(4,value);
  EEPROM.commit();
  lcd.setCursor(16,3);lcd.print(TMax);
  Beep();
}
//===============================
void setup() 
{
  Serial.begin(115200);
  EEPROM.begin(5);
  lcd.begin();
  
  pinMode(PUMP,OUTPUT);digitalWrite(PUMP,HIGH);
  pinMode(FAN,OUTPUT);digitalWrite(FAN,HIGH);
  pinMode(Buzzer,OUTPUT);digitalWrite(Buzzer,HIGH);
  
  delay(1000);
  WifiConnect(); 
  lcd.setCursor(0,0);lcd.print("Humidity =         %"); 
  lcd.setCursor(0,1);lcd.print("Temp     =         C"); 
  lcd.setCursor(0,2);lcd.print("Set-H    =     -   %"); 
  lcd.setCursor(0,3);lcd.print("Set-T    =     -   C"); 
  
  delay(500);
  HMin = EEPROM.read(1);delay(500);
  HMax = EEPROM.read(2);delay(500);
  TMin = EEPROM.read(3);delay(500);
  TMax = EEPROM.read(4); 
  lcd.setCursor(13,2);lcd.print(HMin);
  lcd.setCursor(16,2);lcd.print(HMax);
  lcd.setCursor(13,3);lcd.print(TMin);
  lcd.setCursor(16,3);lcd.print(TMax);

  
  
   
  Blynk.config(auth);
  dht.setup(D7);
  timer.setInterval(1000L, sendSensor); 
  Beep();
}

void loop() 
{  
  Blynk.run();
  timer.run();
  Humidity    = dht.getHumidity();
  Temperature = dht.getTemperature();
 // Humidity2    = dht2.getHumidity();
 // Temperature2 = dht2.getTemperature();
  if( isnan(Humidity1) || isnan(Temperature1)){}//|| isnan(Humidity2) || isnan(Temperature2)){}
  else
  { 
    lcd.setCursor(14,0);lcd.print(Humidity,1);
    lcd.setCursor(14,1);lcd.print(Temperature,1);  
  }  

  if(Humidity <  HMin){digitalWrite(PUMP,LOW);S1 = 255;}
  if(Humidity >= HMax){digitalWrite(PUMP,HIGH); S1 = 0;}
  if(Temperature <= TMin){digitalWrite(FAN,HIGH); S2 = 0;}
  if(Temperature >= TMax){digitalWrite(FAN,LOW);  S2 = 255;}
  delay(200);
}
//=======================================================
void Beep()
{
  digitalWrite(Buzzer,LOW);delay(50);
  digitalWrite(Buzzer,HIGH);
}

เบื้องต้น เริ่มจากศึกษากระทู้นี้ดูครับ

แก้ได้แล้วครับตัวนี้คือโค้ดในส่วนที่ไปเขียนเพิ่มมาครับ

LiquidCrystal_I2C lcd(0x27,20,4);// PCF8574A =  0x3F
#define  PUMP D5 
#define  FAN  D6 
#define  Buzzer D0  
float    Humidity,Temperature;
float    Humidity1,Temperature1;
float    Humidity2,Temperature2;
unsigned char  HMin,HMax,TMin,TMax;
//----------
unsigned char  S1,S2,S3;
unsigned char AUTO = 0;
//----------

void sendSensor()
{ Serial.println(Temperature,2); 
  Serial.println(Humidity,2);
  Serial.println("----------------------");
  Blynk.virtualWrite(V0, Humidity);
  Blynk.virtualWrite(V1, Temperature);
  Blynk.virtualWrite(V2,String(HMin)+" - "+String(HMax));
  Blynk.virtualWrite(V3,String(TMin)+" - "+String(TMax));
  if(S1 == 0){Blynk.virtualWrite(V8, LOW);}else{Blynk.virtualWrite(V8, HIGH);}
  if(S2 == 0){Blynk.virtualWrite(V9, LOW);}else{Blynk.virtualWrite(V9, HIGH);}
//------  
  if(AUTO == 0){Blynk.virtualWrite(V10, LOW);}else{Blynk.virtualWrite(V10, HIGH);}
//----- 
//=====================================================
BLYNK_WRITE(V8) 
{  
  int value = param.asInt();
  Serial.println(value,DEC);
  if(AUTO == 1)
  {
  if(value == 1){digitalWrite(PUMP,LOW);S1 =1;}else{digitalWrite(PUMP,HIGH); S1 = 0;}     
  }
  Beep();
}
//=====================================================
BLYNK_WRITE(V9) 
{  
  int value = param.asInt();
  Serial.println(value,DEC);
  if(AUTO == 1)
  {
  if(value == 1){digitalWrite(FAN,LOW);S2 = 1;}else{digitalWrite(FAN,HIGH); S2 = 0;}     
  }
  Beep();
}
//=====================================================
BLYNK_WRITE(V10) 
{  
  int value = param.asInt();
  Serial.println(value,DEC);
  AUTO = value;
  EEPROM.write(0,value);
  EEPROM.commit();  
  if(AUTO == 1){S1 = 0; S2 = 0;}
  Beep();
}

void loop() 
{float Temp,Humi;  
  Blynk.run();
  timer.run();
  Humi    = dht.getHumidity();
  Temp = dht.getTemperature();

  if( isnan(Humi) || isnan(Temp)){}
  else
  { Humidity = Humi;
    Temperature = Temp;
    lcd.setCursor(14,0);lcd.print(Humidity,1);
    lcd.setCursor(14,1);lcd.print(Temperature,1); 
    
  }  
 if(AUTO == 0)
 {
  if(Humidity <  HMin){digitalWrite(PUMP,LOW);S1 = 255;}
  if(Humidity >= HMax){digitalWrite(PUMP,HIGH); S1 = 0;}
  if(Temperature <= TMin){digitalWrite(FAN,HIGH); S2 = 0;}
  if(Temperature >= TMax){digitalWrite(FAN,LOW);  S2 = 255;}
 } 
  delay(200);
}
2 Likes