Code sử dụng ESP8266 điều khiển 3 Relay thông qua Blynk App

Code sử dụng ESP8266 điều khiển 3 relay

  • Relay 1: GPIO5
  • Relay 2: GPIO4
  • Relay 3: GPIO12


/*************************************************************

  Blynk using a LED widget on your phone!

  App dashboard setup:
    LED widget on V1
 *************************************************************/

/* Fill-in information from Blynk Device Info here */

/* Fill-in information from Blynk Device Info here */
#define BLYNK_TEMPLATE_ID           "TMPL672VLZcSa"
#define BLYNK_TEMPLATE_NAME         "ESP32"
#define BLYNK_AUTH_TOKEN            "cbQs_013LMrP5erkSrbUBI-8usFrWQEB"

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial


#include 
#include 

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "tang 3";
char pass[] = "66668888";

//WidgetLED led1(V4);

BlynkTimer timer;

// V1 LED Widget is blinking
int Relay_Pin = 4;  // Connect the relay to GPIO0 
int Relay_Pin1 = 5;
int Relay_Pin2 = 12;

BLYNK_WRITE(V1) // this command is listening when something is written to V1
{
  int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
  
  if (pinValue == 1){
   // do something when button is pressed;
   digitalWrite(Relay_Pin, HIGH); 
  } else if (pinValue == 0) {
    digitalWrite(Relay_Pin, LOW); 
   // do something when button is released;
  }
  
  Serial.print("V1 button value is: "); // printing value to serial monitor
  Serial.println(pinValue);


  }


  BLYNK_WRITE(V2) // this command is listening when something is written to V1
{
  int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
  
  if (pinValue == 1){
   // do something when button is pressed;
   digitalWrite(Relay_Pin1, HIGH); 
  } else if (pinValue == 0) {
    digitalWrite(Relay_Pin1, LOW); 
   // do something when button is released;
  }
  
  Serial.print("V2 button value is: "); // printing value to serial monitor
  Serial.println(pinValue);


  }


////////////

BLYNK_WRITE(V4) // this command is listening when something is written to V1
{
  int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
  
  if (pinValue == 1){
   // do something when button is pressed;
   digitalWrite(Relay_Pin2, HIGH); 
  } else if (pinValue == 0) {
    digitalWrite(Relay_Pin2, LOW); 
   // do something when button is released;
  }
  
  Serial.print("V3 button value is: "); // printing value to serial monitor
  Serial.println(pinValue);


  }

void setup()
{
  // Debug console
  Serial.begin(115200);

  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
  // You can also specify server:
  //Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass, "blynk.cloud", 80);
  //Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass, IPAddress(192,168,1,100), 8080);
pinMode(Relay_Pin, OUTPUT);
pinMode(Relay_Pin1, OUTPUT);
pinMode(Relay_Pin2, OUTPUT);
  //timer.setInterval(1000L, blinkLedWidget);
}

void loop()
{
  Blynk.run();
  timer.run();
}

 

Bài viết liên quan