Turn on-off relay with button[esp8266]

Hi! I’m new here. Sorry for bad English.
use esp8266 board and 5VDC relay
Problem: I want to Turn on-off relay with button in Blynk2.0 But when I try It’s nothing happen.

This is my code:

#define BLYNK_TEMPLATE_ID "TMPL1ZBP7FMf"
#define BLYNK_DEVICE_NAME "MUSHROOM"
#define BLYNK_AUTH_TOKEN "zPr0Mmvo91aC1lz2FKtccRIsz6xyfdw-"

#define BLYNK_PRINT Serial


#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>
#include <DHT_U.h>
#include <SimpleTimer.h>

char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "Tawan_2.4G";
char pass[] = "0927015667";

#define DHTPIN D2
#define DHTTYPE DHT11
#define relay D1

DHT dht(DHTPIN, DHTTYPE);
SimpleTimer timer;


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

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  Blynk.virtualWrite(V5, h);
  Blynk.virtualWrite(V6, t);
}

void setup()
{
  // Debug console
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  dht.begin();
  timer.setInterval(1000L, sendSensor);
  pinMode(relay, OUTPUT);
}

void loop()
{
  Blynk.run();
  Serial.println(dht.readHumidity());; 
  timer.run();
}
BLYNK_WRITE(V1)
{
  int button = param.asInt();
  
  if (button == 1){
   digitalWrite(relay, HIGH);
  }
  else if (button == 0){
    digitalWrite(relay, LOW);
  }

}

I assume that button is connected to a datastream linked to virtual pin V1?

How is that datastream configured (Data Type and Min/Max values)?

Is your device showing as Online?

Pete.

I recheck in my app I forget to select datastream.
It’s work now Thanks!

1 Like