อยากทำ Auto/Manual ใน Blynk

ดูมาหลายกระทู้แล้วครับ ไม่เข้าใจจริงๆ ครับ ทำส่วน Auto ได้แล้วครับ โดยกำหนดที่ slider bar ใน Blynk แต่ยังติดที่ Manual ครับ ปุ่ม Auto V0 รบกวนช่วยดูให้หน่อยนะครับ มือใหม่ครับ

#define BLYNK_PRINT Serial
 
#include "DHT.h"
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

int Relay1 = D0;
int Relay2 = D1;
int Relay3 = D2;
int Relay4 = D3;

int setTemp;
int setHumid;

WidgetLED led1(V30);
WidgetLED led2(V31);
WidgetLED led3(V32);
WidgetLED led4(V33);

BLYNK_WRITE(V22) //temperature slider in app on pin V22
{
setTemp = param.asInt();
}
BLYNK_WRITE(V23) //temperature slider in app on pin V23
{
setHumid = param.asInt();
}


char auth[] = "bs-Ym_aA_ladfla8yA8U2p7uXj3kLQrI";
char ssid[] = "SWEETSMILE";
char pass[] = "88888888";
char server[] = "oasiskit.com";
int port = 8080;

BLYNK_CONNECTED() {
    Blynk.syncAll();
}

#define DHT1PIN D5   
#define DHT2PIN D6
#define DHT1TYPE DHT22   // probobly not needed to do twice, clean later
#define DHT2TYPE DHT22   
 
DHT dht1(DHT1PIN, DHT1TYPE);
DHT dht2(DHT2PIN, DHT2TYPE);

BlynkTimer timer;

void sendsensor1() {
  float h1 = dht1.readHumidity();
  float t1 = dht1.readTemperature();
  t1 = dht1.readTemperature();
  t1 = t1;
  h1 = dht1.readHumidity();
  h1 = h1;
  Blynk.virtualWrite(V11, h1);
  Blynk.virtualWrite(V10, t1);
  }
  
void sendsensor2() {      // probobly not needed to do two void sendsensor, clean later
  float h2 = dht2.readHumidity();
  float t2 = dht2.readTemperature();
   t2 = dht2.readTemperature();
   t2 = t2;
   h2 = dht2.readHumidity();
   h2 = h2;
  
  Blynk.virtualWrite(V13, h2);
  Blynk.virtualWrite(V12, t2);
  }

void setup() {
  Serial.begin(9600); 
  pinMode (Relay1,OUTPUT);
  pinMode (Relay2,OUTPUT);
  pinMode (Relay3,OUTPUT);
  pinMode (Relay4,OUTPUT);

  Blynk.begin(auth, ssid, pass, "oasiskit.com", 8080);
  dht1.begin();
  dht2.begin(); // this seems to fail in combination with blynk, works just fine "offline" doesnt seam to read sensor 2 with blynk part of the code.. 

  timer.setInterval(2000L, sendsensor1);
  timer.setInterval(2000L, sendsensor2); // probobly not needed twice, timers are wierd things... wiki.. 
}

void loop() {
   //delay(2000);
  float h1 = dht1.readHumidity();
  float t1 = dht1.readTemperature();
  float f1 = dht1.readTemperature(true);
  if (isnan(h1) || isnan(t1) || isnan(f1)) {
  Serial.println(F("Failed to read from DHT sensor!"));
  return;
  }
    if (t1 >= setTemp )
    {
    digitalWrite (Relay1,LOW);
    digitalWrite (Relay2,LOW);
    led1.on();
    led2.on();
    }
  else if (t1 < setTemp)
  {
    digitalWrite (Relay1,HIGH);
    digitalWrite (Relay2,HIGH);
    led1.off();
    led2.off();
  }
  if (h1 <= setHumid)
  {
    digitalWrite (Relay3,LOW);
    digitalWrite (Relay4,LOW);  
    led3.on();
    led4.on();
  }
  else if (h1 > setHumid)
  {
    digitalWrite (Relay3,HIGH);
    digitalWrite (Relay4,HIGH);
    led3.off();
    led4.off();
  }

//  float hif = dht1.computeHeatIndex(f1, h1);
//  float hic = dht1.computeHeatIndex(t1, h1, false);
//  Serial.print(F("Humidity: "));
//  Serial.print(h1);
//  Serial.print(F("% Temperature: "));
//  Serial.print(t1);
//  Serial.print(F(" C "));
//  Serial.print(f1);
//  Serial.print(F(" F Heat index: "));
//  Serial.print(hic);
//  Serial.print(F(" C "));
//  Serial.print(hif);
//  Serial.println(F(" F"));

//  float h2 = dht2.readHumidity();
//  float t2 = dht2.readTemperature();
//  float f2 = dht2.readTemperature(true);
//  if (isnan(h2) || isnan(t2) || isnan(f2)) {
//  Serial.println(F("Failed to read from DHT sensor!"));
//  return;
//  }
//    if (t2 >= 31 ){
//    digitalWrite (Relay1,LOW);
//    digitalWrite (Relay2,LOW);
//    digitalWrite (Relay3,LOW);
//  }
//  else if (t2 < 31)
//  {
//    digitalWrite (Relay1,HIGH);
//    digitalWrite (Relay2,HIGH);
//    digitalWrite (Relay3,HIGH);
//    Serial.println("Relay 1,2,3 HIGH!");
//  }
//  if (h2 <= 20){
//    digitalWrite (Relay4,LOW);
//
//  }
//  else if (h2 > 20)
//  {
//    digitalWrite (Relay4,HIGH);
//    Serial.println("Relay 4 HIGH!");
//  }
//  float hif1 = dht2.computeHeatIndex(f2, h2);
//  float hic1 = dht2.computeHeatIndex(t2, h2, false);
//  Serial.print(F("Humidity: "));
//  Serial.print(h2);
//  Serial.print(F("% Temperature: "));
//  Serial.print(t2);
//  Serial.print(F(" C "));
//  Serial.print(f2);
//  Serial.print(F(" F Heat index: "));
//  Serial.print(hic);
//  Serial.print(F(" C "));
//  Serial.print(hif);
//  Serial.println(F(" F"));
  Blynk.run();
  timer.run();
  
}

อันนี้จะให้โหมดนี้ทำอะไรบ้างครับ เห็นว่ายังไม่มีโค้ตรองรับเลย

โค้ตส่วนไหนที่จะให้ทำงานแบบ ออโต้ ต้องคลุมด้วย if()

bool _button_ma = false;

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

_button_ma = true;

 }else{

_button_ma = false;

    }
 }

void loop() {

if ( _button_ma == true) {

//โค้ตส่วน auto

}else{

//โค้ต Manual

}

}

นี้ผมใส่ส่วนที่เป็น auto แล้วลอง compile ผ่านนะครับ แต่ส่วน manual เริ่มไงครับ

  if ( _button_ma == true){
    if (t1 >= setTemp )
    {
    digitalWrite (Relay1,LOW);
    digitalWrite (Relay2,LOW);
    led1.on();
    led2.on();
    }
  else if (t1 < setTemp)
  {
    digitalWrite (Relay1,HIGH);
    digitalWrite (Relay2,HIGH);
    led1.off();
    led2.off();
  }
  if (h1 <= setHumid)
  {
    digitalWrite (Relay3,LOW);
    digitalWrite (Relay4,LOW);  
    led3.on();
    led4.on();
  }
  else if (h1 > setHumid)
  {
    digitalWrite (Relay3,HIGH);
    digitalWrite (Relay4,HIGH);
    led3.off();
    led4.off();
  }
  }else{
    //manual
  }

manual คือจะให้มันทำอะไรบ้าง

เมื่อ V0 = off เราสามารถ
กด V40 แบบ sw ให้ Relay1 ทำงาน/ไม่ทำงาน
กด V41 แบบ sw ให้ Relay2 ทำงาน/ไม่ทำงาน
กด V42 แบบ sw ให้ Relay3 ทำงาน/ไม่ทำงาน
กด V43 แบบ sw ให้ Relay4 ทำงาน/ไม่ทำงาน

เมื่อ V0 = on ก็เข้า Auto

bool _button_ma = false;
bool _button1 = false;

BLYNK_WRITE(V0){
    if (param.asInt() == 1){
        _button_ma = true;
    }else{
        _button_ma = false;
    }
}

BLYNK_WRITE(V40){

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

void loop(){

    if (_button_ma == true){
        //โค้ตส่วน auto
    }else{

        //โค้ต Manual

        if (_button1 == true){
            digitalWrite(Relay1, HIGH);
        }else{
            digitalWrite(Relay1, LOW);
        }
    }
}

โอ้โห้ ขอบคุณมากครับ ผมติดอันนี้มันเป็นเดือน นี้ผมพึ่งหัดตั้งกระทู้ด้วยไม่ค่อยกล้าถาม กลัวสื่อสารไม่เข้าใจ ขอบคุณมากครับผม นิดนึงครับ Relay ผม Active Low ผมต้องสั่ง LOW ให้มันทำงานตลอดเลยใช่ไหม

#define BLYNK_PRINT Serial

#include "DHT.h"
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

int Relay1 = D0;
int Relay2 = D1;
int Relay3 = D2;
int Relay4 = D3;

int setTemp;
int setHumid;
bool _button_ma = false;
bool _button1 = false;
bool _button2 = false;
bool _button3 = false;
bool _button4 = false;

WidgetLED led1(V30);
WidgetLED led2(V31);
WidgetLED led3(V32);
WidgetLED led4(V33);

BLYNK_WRITE(V22) //temperature slider in app on pin V22
{
setTemp = param.asInt();
}
BLYNK_WRITE(V23) //temperature slider in app on pin V23
{
setHumid = param.asInt();
}

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

_button_ma = true;

 }else{

_button_ma = false;

    }
 }

 BLYNK_WRITE(V40){

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

BLYNK_WRITE(V41){

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

BLYNK_WRITE(V42){

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

BLYNK_WRITE(V43){

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


char auth[] = "bs-Ym_aA_ladfla8yA8U2p7uXj3kLQrI";
char ssid[] = "SWEETSMILE";
char pass[] = "88888888";
char server[] = "oasiskit.com";
int port = 8080;

BLYNK_CONNECTED() {
    Blynk.syncAll();
}

#define DHT1PIN D5   
#define DHT2PIN D6
#define DHT1TYPE DHT22   // probobly not needed to do twice, clean later
#define DHT2TYPE DHT22   
 
DHT dht1(DHT1PIN, DHT1TYPE);
DHT dht2(DHT2PIN, DHT2TYPE);

BlynkTimer timer;

void sendsensor1() {
  float h1 = dht1.readHumidity();
  float t1 = dht1.readTemperature();
  t1 = dht1.readTemperature();
  t1 = t1;
  h1 = dht1.readHumidity();
  h1 = h1;
  Blynk.virtualWrite(V11, h1);
  Blynk.virtualWrite(V10, t1);
  }
  
void sendsensor2() {      // probobly not needed to do two void sendsensor, clean later
  float h2 = dht2.readHumidity();
  float t2 = dht2.readTemperature();
   t2 = dht2.readTemperature();
   t2 = t2;
   h2 = dht2.readHumidity();
   h2 = h2;
  
  Blynk.virtualWrite(V13, h2);
  Blynk.virtualWrite(V12, t2);
  }

void setup() {
  Serial.begin(9600); 
  pinMode (Relay1,OUTPUT);
  pinMode (Relay2,OUTPUT);
  pinMode (Relay3,OUTPUT);
  pinMode (Relay4,OUTPUT);
  digitalWrite (Relay1,HIGH);
  digitalWrite (Relay2,HIGH);
  digitalWrite (Relay3,HIGH);
  digitalWrite (Relay4,HIGH);

  Blynk.begin(auth, ssid, pass, "oasiskit.com", 8080);
  dht1.begin();
  dht2.begin(); // this seems to fail in combination with blynk, works just fine "offline" doesnt seam to read sensor 2 with blynk part of the code.. 

  timer.setInterval(2000L, sendsensor1);
  timer.setInterval(2000L, sendsensor2); // probobly not needed twice, timers are wierd things... wiki.. 
}

void loop() {
   //delay(2000);
  float h1 = dht1.readHumidity();
  float t1 = dht1.readTemperature();
  float f1 = dht1.readTemperature(true);
  if (isnan(h1) || isnan(t1) || isnan(f1)) {
  Serial.println(F("Failed to read from DHT sensor!"));
  return;
  }
  //auto
  if ( _button_ma == true){
    if (t1 >= setTemp )
    {
    digitalWrite (Relay1,LOW);
    digitalWrite (Relay2,LOW);
    led1.on();
    led2.on();
    }
  else if (t1 < setTemp)
  {
    digitalWrite (Relay1,HIGH);
    digitalWrite (Relay2,HIGH);
    led1.off();
    led2.off();
  }
  if (h1 <= setHumid)
  {
    digitalWrite (Relay3,LOW);
    digitalWrite (Relay4,LOW);  
    led3.on();
    led4.on();
  }
  else if (h1 > setHumid)
  {
    digitalWrite (Relay3,HIGH);
    digitalWrite (Relay4,HIGH);
    led3.off();
    led4.off();
  }
  }else{   //manual
            if (_button1 == true){
            digitalWrite(Relay1, LOW);
            led1.on();
        }else{
            digitalWrite(Relay1, HIGH);
            led1.off();
        }
                if (_button2 == true){
            digitalWrite(Relay2, LOW);
            led2.on();
        }else{
            digitalWrite(Relay2, HIGH);
            led2.off();
        }
                if (_button3 == true){
            digitalWrite(Relay3, LOW);
            led3.on();
        }else{
            digitalWrite(Relay3, HIGH);
            led3.off();
        }
                if (_button4 == true){
            digitalWrite(Relay4, LOW);
            led4.on();
        }else{
            digitalWrite(Relay4, HIGH);
            led4.off();
        }
  }

//  float hif = dht1.computeHeatIndex(f1, h1);
//  float hic = dht1.computeHeatIndex(t1, h1, false);
//  Serial.print(F("Humidity: "));
//  Serial.print(h1);
//  Serial.print(F("% Temperature: "));
//  Serial.print(t1);
//  Serial.print(F(" C "));
//  Serial.print(f1);
//  Serial.print(F(" F Heat index: "));
//  Serial.print(hic);
//  Serial.print(F(" C "));
//  Serial.print(hif);
//  Serial.println(F(" F"));

//  float h2 = dht2.readHumidity();
//  float t2 = dht2.readTemperature();
//  float f2 = dht2.readTemperature(true);
//  if (isnan(h2) || isnan(t2) || isnan(f2)) {
//  Serial.println(F("Failed to read from DHT sensor!"));
//  return;
//  }
//    if (t2 >= 31 ){
//    digitalWrite (Relay1,LOW);
//    digitalWrite (Relay2,LOW);
//    digitalWrite (Relay3,LOW);
//  }
//  else if (t2 < 31)
//  {
//    digitalWrite (Relay1,HIGH);
//    digitalWrite (Relay2,HIGH);
//    digitalWrite (Relay3,HIGH);
//    Serial.println("Relay 1,2,3 HIGH!");
//  }
//  if (h2 <= 20){
//    digitalWrite (Relay4,LOW);
//
//  }
//  else if (h2 > 20)
//  {
//    digitalWrite (Relay4,HIGH);
//    Serial.println("Relay 4 HIGH!");
//  }
//  float hif1 = dht2.computeHeatIndex(f2, h2);
//  float hic1 = dht2.computeHeatIndex(t2, h2, false);
//  Serial.print(F("Humidity: "));
//  Serial.print(h2);
//  Serial.print(F("% Temperature: "));
//  Serial.print(t2);
//  Serial.print(F(" C "));
//  Serial.print(f2);
//  Serial.print(F(" F Heat index: "));
//  Serial.print(hic);
//  Serial.print(F(" C "));
//  Serial.print(hif);
//  Serial.println(F(" F"));
  Blynk.run();
  timer.run();
  
}

ใช่ครับ สั่ง low ที่นี้เราจะไม่ใช้คำว่า เกรงใจ กันนะครับ

อยากรู้อะไรก็ถามเลย แต่จะตอบช้าเร็ว แค่นั้น

1 Like

ครับ ขอบคุณมากครับ
ถ้ามีปัญหาใหม่ ขออนุญาตตั้งกระทู้ใหม่นะครับ ฮ่าาๆ

จัดไปครับผม ได้เทคนิกอะไรก็อย่าลืมมาแชร์กันครับ