Virtual Pin Need Help With My Project

Tôi đang hệ thống tưới cây sử dụng Blynk + ESP8266 Node MCU. Tôi muốn sử dụng 1 Virtual Pin để set chế độ điều khiển tự động hoặc thao tác điều khiển qua Digital pin. Tôi đã thử code nhưng không được. Mong mọi người có thể giúp tôi! Tôi xin chân thành cảm ơn!
Đây là đoạn code mà tôi đã lập trình:

BLYNK_WRITE (V3)
{
  int val = param.asInt();
  if(val == 1)
  {
     AutoControl();
  }
  else
  return;
}

void AutoControl()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  
  if(h < LOW_HUMI)
    digitalWrite(PUMP_PIN, LOW);    
  if(h>=HIGH_HUMI)
    digitalWrite(PUMP_PIN, HIGH); 
}

Shouldn’t be needed.

For your own troubleshooting, you can add in Serial debug and prints to your code and track whether pressing the button properly runs the code? for example…

BLYNK_WRITE (V3)
{
  int val = param.asInt();
  if(val == 1)
  {
     Serial.println("Running Auto Control routine");
     AutoControl();
  }
Serial.println("Doing Nothing");
}

And your AutoControl() function seems to be missing brackets?

Should be like this…

void AutoControl()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature();

  if (h < LOW_HUMI)
  {
    digitalWrite(PUMP_PIN, LOW);
  }
  if (h >= HIGH_HUMI)
  {
    digitalWrite(PUMP_PIN, HIGH);
  }
}

Hi.I tried your way. But AutoControl ( ) not run. Can you help me? Please. Thank you very much!

yes it is, just look better at the code. It enters the blynk_write it also enters the if statement and then exits it again when done and then prints ‘doing nothing’. Might be a bit confusing through, perhaps:

BLYNK_WRITE (V3)
{
  Serial.println("Change on pin 3 detected");
  int val = param.asInt();
  if(val == 1)
  {
     Serial.println("Running Auto Control routine");
     AutoControl();
     Serial.println("Done with Auto Control routine");
  } else {
	Serial.println("Not doing anything with it");
  }
}

The problem I encountered here is that when V3 is pressed, the AutoControl (); is not running and I don’t know where wrong :frowning:

so you tried the code I provided? What was the generated output?

Ý của bạn là khi chọn chế độ chạy tự động thì nó sẽ kiểm tra độ ẩm nếu thấp hơn mức ngưỡng thì bơm tưới mở?

Bạn dùng 1 pin ảo V3 để bật chế độ tự động này lên. Vấn đề là khi bạn nhấn nút trên Blynk thông qua pin ảo V3 thì hàm AutoControl() kia chỉ chạy đúng lúc đó nhưng nếu độ ẩm từ cảm biến đo chưa dưới ngưỡng thì bơm đâu có mở làm bạn tưởng bơm không chạy.

Theo mình bạn tạo 1 timer trong trong chương trình với chu ký cứ 3 phút kiểm tra một lần. Còn pin ảo V3 cứ mỗi lần ON thì bạn gán cho biến AutoChay (biến global) là true và trong hàm loop cứ mỗi khi biến AutoChay là true thì timer kia chạy :slight_smile:

#include <BlynkSimpleEsp8266.h>

bool AutoChay = false;

BlynkTimer timer1;

void AutoControl();

BLYNK_WRITE (V3)
{
  int val = param.asInt();
  if (val == 1)
  {
     Serial.println("mode: Auto ON");
     AutoChay = true;
  }
  else
  {
	 Serial.println("mode: Auto OFF");
     AutoChay = false;
  }
}

void AutoControl()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  
  if(h < LOW_HUMI)
    digitalWrite(PUMP_PIN, LOW);    
  if(h>=HIGH_HUMI)
    digitalWrite(PUMP_PIN, HIGH); 
}

void Setup()
{
   timer1.setInterval(180000L, AutoControl);
}

void Loop()
{
   if (AutoChay == true) timer1.run();
}