Blynk按钮没有被驱动

发布于 2025-01-22 06:36:57 字数 1886 浏览 0 评论 0原文

我是一个完整的初学者,正在使用ESP8266进行一个项目,在那里我与继电器和两个传感器进行了连接。传感器运行良好,但是当我按下时继电器并没有被Blynk按钮驱动。以下是我为传感器提供适当输出的代码,而不是继电器。 我在Blynk应用程序中获得了所有传感器的值,但没有将其连接到电动机和D2引脚中的继电器致动。提前致谢。:)

#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);
}

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);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文