与NODEMCU和Blynk App连接时,多个超声波传感器(HC-SR04)不起作用

时间:2020-10-05 09:18:25

标签: wifi sensors nodemcu blynk

我遇到了这个问题,当我仅将一个超声波传感器连接到NodeMCU和Blynk应用程序时,当有人经过传感器时,Blynk应用程序确实会通知我。当我在NodeMCU上添加另一个超声波传感器时,当我中断两个传感器时,blynk应用程序不会通知我。 下面随附的代码是超声波传感器与NodeMCU和blynk应用程序的接口。请帮帮我...谢谢!

代码:

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#define echoPin1 D1
#define trigPin1 D2
#define echoPin2 D3
#define trigPin2 D4

char auth[] = "***";
char ssid[] = "***";
char pass[] = "***";

long duration1;
float distance1;
long duration2;
float distance2;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Blynk.begin(auth,ssid,pass);
  pinMode(trigPin1,OUTPUT);
  pinMode(echoPin1,INPUT);
  pinMode(trigPin2,OUTPUT);
  pinMode(echoPin2,INPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  digitalWrite(trigPin1,LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin1,HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin1,LOW);
  duration1 = pulseIn(echoPin1,HIGH);
  distance1 = duration1 *0.34/2;
  
  digitalWrite(trigPin2,LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin2,HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin2,LOW);
  duration2 = pulseIn(echoPin2,HIGH);
  distance2 = duration2 *0.34/2;
  
  if (distance1 <= 1000){
    Blynk.virtualWrite(V1,255);
    Blynk.notify("Intruder passing by sensor1 !");
  }
  else{
    Blynk.virtualWrite(V1,0);
  }

  if (distance2 <= 1000){
    Blynk.virtualWrite(V2,255);
    Blynk.notify("Intruder passing by sensor2 !");
  }
  else{
    Blynk.virtualWrite(V2,0);
  }
  Blynk.run();
}

0 个答案:

没有答案