为什么PIR传感器与nodeMCU/ESP8266板连接后保持高电平?

4
我有一个PIR传感器连接到Amica NodeMCU板上,从VIN路由5V,并通过USB端口进行测试。当检测到运动时,它会连接到互联网并将数据发送到IFTTT,我会在手机上收到通知。
当我启动时,PIR暂停校准,然后立即变为高电平并触发检测到运动的调用,我会在手机上收到通知。但是,从那时起,即使没有运动,每5-8分钟它也会再次发送另一个高电平信号。
测试: - 尝试使用独立的5V电源来供电PIR,结果相同。 - 我尝试过重触发模式(H和L),结果相同。 - Adafruit featherwing huzzah板也是如此。 - 我已经在没有微控制器的情况下测试了PIR,并可以确认它“功能正确”(点亮LED)。 - 我已经使用相同的代码测试了Arduino Nano,它“功能正确”(点亮LED)。

我的代码Arduino Playground PIR Project的修改版。

//Sends IFTTT every 5 minutes it detects motion

#include <ESP8266WiFi.h>
//WiFi Settings

// Set up macros for wifi and connection.
#define SSID "my-network"    // SSID
#define PASS "mypassphrase"      // Network Password
#define HOST "maker.ifttt.com"  // Webhost
//-------------------------------
const char* streamId   = "test";
const char* privateKey = "mysecretkey";

//PIR Settings
//the time we give the sensor to calibrate (10-60 secs according to the datasheet)
int calibrationTime = 10;        

//the time when the sensor outputs a low impulse
long unsigned int lowIn;         

//the amount of milliseconds the sensor has to be low 
//before we assume all motion has stopped
long unsigned int pause = 5000;  


int  interval = 1000; // Wait between dumps
boolean lockLow = true;
boolean takeLowTime;  
int ledPin = 1;
int pirPin = 2;    //the digital pin connected to the PIR sensor's output
int nPIR_detect;
int motion = 2;

int minSecsBetweenUpdates = 300; // 5 minutes
long lastSend = -minSecsBetweenUpdates * 1000l;
//-------------------------------

// Begin Setup
void setup(){
  Serial.begin(115200);
  pinMode(pirPin, INPUT);
  digitalWrite(pirPin, LOW);

  //give the sensor some time to calibrate
  Serial.print("calibrating sensor ");
  for(int i = 0; i < calibrationTime; i++){
    Serial.print(".");
    delay(1000);
    }
  Serial.println(" done");
  Serial.println("SENSOR ACTIVE");
  delay(50);
   nPIR_detect = 0;  

  // Test ESP8266 module.
  Serial.println("AT");
  delay(5000);
  if(Serial.find("OK")){
    connectWiFi();
  }
}

void loop(){
 if(digitalRead(pirPin) == HIGH){
       digitalWrite(BUILTIN_LED, HIGH);   //the led visualizes the sensors output pin state
       if(lockLow){  
         //makes sure we wait for a transition to LOW before any further output is made:
         lockLow = false;            
         Serial.println("---");
         Serial.print("motion detected at ");
         Serial.print(millis()/1000);
         Serial.println(" sec"); 
         sendData(String(motion));
         delay(50);
         }         
         takeLowTime = true;
       }

     if(digitalRead(pirPin) == LOW){       
       digitalWrite(BUILTIN_LED, LOW);  //the led visualizes the sensors output pin state

       if(takeLowTime){
        lowIn = millis();          //save the time of the transition from high to LOW
        takeLowTime = false;       //make sure this is only done at the start of a LOW phase
        }
       //if the sensor is low for more than the given pause, 
       //we assume that no more motion is going to happen
       if(!lockLow && millis() - lowIn > pause){  
           //makes sure this block of code is only executed again after 
           //a new motion sequence has been detected
           lockLow = true;                        
           Serial.print("motion ended at ");      //output
           Serial.print((millis() - pause)/1000);
           Serial.println(" sec");
           delay(50);
           }
       }
}

void sendData(String motion){

  //Send the motion to IFTTT value1
  Serial.print("connecting to ");
  Serial.println(HOST);

  // Use WiFiClient class to create TCP connections
  WiFiClient client;
  const int httpPort = 80;
  if (!client.connect(HOST, httpPort)) {
    Serial.println("connection failed");
    return;
  }

  // We now create a URI for the request
  String url = "/trigger/";
  url += streamId;
  url += "/with/key/";
  url += privateKey;
  url += "?value1=";
  url += "motion";

  Serial.print("Requesting URL: ");
  Serial.println(url);

  // This will send the request to the server
  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + HOST + "\r\n" + 
               "Connection: close\r\n\r\n");
  delay(10);

  // Read all the lines of the reply from server and print them to Serial
  while(client.available()){
    String line = client.readStringUntil('\r');
    Serial.print(line);
  }

  Serial.println();
  Serial.println("closing connection");

}

boolean connectWiFi(){
  Serial.println("AT+CWMODE=1");
  delay(2000);
  String cmd="AT+CWJAP=\"";
  cmd+=SSID;
  cmd+="\",\"";
  cmd+=PASS;
  cmd+="\"";
  Serial.println(cmd);
  delay(5000);
  if(Serial.find("OK")){
    Serial.println("Connection");
    return true;
  }
  else{
    Serial.println("No Connection");
    return false;
  }
}

为什么使用nodeMCU/ESP8266板时PIR传感器一直保持高电平?
实质上,我从未看到串行消息“运动结束于…”

你有示波器吗? - Mert Gülsoy
@MertGülsoy 不好意思,不是的。 - Dom
如果在“digitalRead(pirPin) == LOW”测试之后没有任何日志,那么如何知道它从未变为LOW? - Eduardo Yáñez Parareda
2个回答

4

几个月后,但希望能帮助其他遇到同样问题的人。我感到沮丧了几周,尝试了几个PIR模块,但结果令人沮丧。我发现问题是由于我在nodeMCU开发板中刷写固件的方式引起的。

我使用的是ESP-12E,并使用以下命令上传固件:

esptool.py --port /dev/cu.SLAB_USBtoUART --baud 115200 write_flash -fm dio -fs 32m 0x00000 /Users/dev/nodemcu-firmware.bin

问题出在-fm dio(双闪存I/O模式)参数上。一些ESP8266模块,包括某些(不是全部)NodeMCU板载的ESP-12E模块,是双I/O的,而固件只有在使用--flash_mode dio-fm dio刷写时才能启动,但在我的情况下,这正是所有头疼问题的根源。一旦我刷写了没有该选项的固件,一切都开始像魔法般地工作。


1
这基本上是正确的答案。几个月后,我回到了这个项目,并无意中在Arduino IDE中更改了板子配置文件,然后它就开始工作了 - 自那以后,我又再次更改了它,结果它停止工作了...如果我能记得我做了什么就好了。现在我不知道该选择Adafruit Huzzah、GenericNodeMCU还是genericESP8266。 - Dom
我尝试了上面的解决方案,但它没有起作用。我使用的是Amica NodeMCU,每隔X秒它会不断地将GPIO读取触发到1,即使周围没有运动或人。 - Lorensius W. L. T
MCU和PIR是否使用共地?如果接地不同,根据我的经验,这可能会触发幽灵GPIO信号。 - Luis U.

2

只需将Node MCU的V_in插入PIR的V_cc即可解决问题。看起来PIR需要约5V,而只有V_in才能提供这种电压。之前,当插入3.3V时,每隔x秒就会得到一系列的高电平。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接