Blynk Button is not getting actuated

393 Views Asked by At

I, a complete beginner, was doing a project with ESP8266 where I interfaced with a relay and two sensors. The sensors are working fine but the relay is not getting actuated by the Blynk button when I press it. Below is the code where I got proper output for the sensors but not the relay. I get all the values of sensors in the Blynk app but not the relay actuation where I connected it to a motor and in the D2 pin. Thanks in advance.:)

#define BLYNK_PRINT Serial
#define BLYNK_TEMPLATE_ID "my_template"
#define BLYNK_DEVICE_NAME "my_device name"
#define BLYNK_AUTH_TOKEN "Auth token"
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>
#define BLYNK_PRINT Serial
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS D2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

char auth[] = "Authtoken";
char ssid[] = "my_ssid";
char pass[] = "my_pass";

#define sensorPin D3
int sensorState = 0;
int lastState = 0;
#define DHTPIN 2
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;

void sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  Blynk.virtualWrite(V5, h);  //hum
  Blynk.virtualWrite(V6, t);  //temp
}

void setup()
{
  pinMode(D2,OUTPUT); //these two lines are the one which use for actuating the relay
  digitalWrite(D2, HIGH);//
  Blynk.begin(auth, ssid, pass);
  pinMode(sensorPin, INPUT);
  dht.begin();
  timer.setInterval(1000L, sendSensor);
  Blynk.begin(auth, ssid, pass);
  sensors.begin();
}
int sensor = 0;
void sendTemps()
{
  sensor = analogRead(A0);
  sensors.requestTemperatures();
  float temp = sensors.getTempCByIndex(0);
  Blynk.virtualWrite(V1, temp);
  Blynk.virtualWrite(V2, sensor);
  delay(1000);
}
void loop()
{
  Blynk.run();
  timer.run();
  sendTemps();
  sensorState = digitalRead(sensorPin);
  if (sensorState == 1 && lastState == 0) {
    lastState = 1;

    delay(1000);
  }
  else if (sensorState == 1 && lastState == 1) {
    delay(1000);
  }
  else {
    lastState = 0;
    delay(1000);
  }
  delay(100);
}
0

There are 0 best solutions below