为什么 PIR 传感器在 nodeMCU/ESP8266 板上保持高电平?
Why does PIR sensor stay high with nodeMCU/ESP8266 board?
我有一个 PIR 传感器连接到 Amica nodeMCU 板,从 VIN 路由 5v,并通过 USB 端口连接以进行测试。当检测到运动时,它会连接到互联网并将数据发送到 IFTTT,我在 phone.
上收到通知
当我通电时,PIR 暂停进行校准,然后立即变为高电平并触发我在 phone 上收到的运动检测呼叫。然而从那时起,它永远不会变低,但即使没有运动,每 5-8 分钟也会发送另一个高信号。
测试
- 尝试对 PIR 使用单独的 5v 电源,同样的事情发生了
- 我已经尝试了两种重新触发模式(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 传感器保持高电平?
本质上,我从来没有看到串口消息"motion ended at.."
几个月后,但希望能帮助其他遇到同样问题的人。我沮丧了数周并尝试了几个 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[=20= 闪烁时才会启动] 或 -fm dio 但就我而言,这是我所有头痛的根源。一旦我在没有该选项的情况下刷新固件,一切都开始像魅力一样工作。
通过插入 V_in 的节点 MCU 和 V_cc 的 PIR 就可以解决问题。似乎 PIR 需要 ~5V,只有 V_in 可以提供那种电压。早些时候,当插入 3.3v 时,我每隔 x 秒就会得到一系列高电平。
我有一个 PIR 传感器连接到 Amica nodeMCU 板,从 VIN 路由 5v,并通过 USB 端口连接以进行测试。当检测到运动时,它会连接到互联网并将数据发送到 IFTTT,我在 phone.
上收到通知当我通电时,PIR 暂停进行校准,然后立即变为高电平并触发我在 phone 上收到的运动检测呼叫。然而从那时起,它永远不会变低,但即使没有运动,每 5-8 分钟也会发送另一个高信号。
测试
- 尝试对 PIR 使用单独的 5v 电源,同样的事情发生了
- 我已经尝试了两种重新触发模式(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 传感器保持高电平?
本质上,我从来没有看到串口消息"motion ended at.."
几个月后,但希望能帮助其他遇到同样问题的人。我沮丧了数周并尝试了几个 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[=20= 闪烁时才会启动] 或 -fm dio 但就我而言,这是我所有头痛的根源。一旦我在没有该选项的情况下刷新固件,一切都开始像魅力一样工作。
通过插入 V_in 的节点 MCU 和 V_cc 的 PIR 就可以解决问题。似乎 PIR 需要 ~5V,只有 V_in 可以提供那种电压。早些时候,当插入 3.3v 时,我每隔 x 秒就会得到一系列高电平。