使用ESP8266、node-red平台、PIR、蜂鸣器、LED等搭建一个家庭安防报警系统

PIR(被动红外)运动传器原理:人体都有恒定的体温,一般在 37度,所以会发出特定波长10UM左右的红外线,被动式红外探头就是靠探测人体发射的10UM左右的红外线而进行工作的。人体发射的10UM左右的红外线通过非泥尔滤光片增强后聚集到红外感应源上。 红外感应源通常采用热释电元件,这种元件在接收到人体红外辐射温度发生变化时就会失去电荷平衡,向外释放电荷,后续经检测到电位变化后就能产生报警信号,进而点亮LED灯并启动蜂鸣器报警。

ESP8266供电电压为3.3V,为了让PIR正常工作,需用5V电压单独供电(图上用的arduino,只要能提供5v稳压就行)。

家庭安防报警系统-编程之家

node-red配置

家庭安防报警系统-编程之家

代码

#include <ESP8266WiFi.h>#include <PubSubClient.h>// Change the credentials below, so your ESP8266 connects to your routerconst char* ssid = "网络名";const char* password = "网络密码";// Change the variable to your Raspberry Pi IP address, so it connects to your MQTT brokerconst char* mqtt_server = "IP地址";// Initializes the espClientWiFiClient espClient;PubSubClient client(espClient);// Control Variablesboolean armMotion = false;boolean motionTriggered = false;// PIR Motion Sensorconst int motionSensor = 14;// Status LEDsconst int motionLED = 12;// Buzzerconst int buzzerPin = 15;// Timers auxiliar variableslong now = millis();long lastMeasure = 0;// Don't change the function below. This functions connects your ESP8266 to your routervoid setup_wifi() {delay(10);// We start by connecting to a WiFi networkSerial.println();Serial.print("Connecting to ");Serial.println(ssid);WiFi.begin(ssid, password);while (WiFi.status() != WL_CONNECTED) {delay(500);Serial.print(".");}Serial.println("");Serial.print("WiFi connected - ESP IP address: ");Serial.println(WiFi.localIP());}// This functions is executed when some device publishes a message to a topic that your ESP8266 is subscribed to// Change the function below to add logic to your program, so when a device publishes a message to a topic that// your ESP8266 is subscribed you can actually do somethingvoid callback(String topic, byte* message, unsigned int length) {Serial.print("Message arrived on topic: ");Serial.print(topic);Serial.print(". Message: ");String messageTemp;for (int i = 0; i < length; i++) {Serial.print((char)message[i]);messageTemp += (char)message[i];}Serial.println();// Feel free to add more if statements to control more GPIOs with MQTTif(topic=="home/office/esp1/motion"){Serial.print("MOTION SENSOR STATUS CHANGE");if(messageTemp == "1"){Serial.print("Motion Sensor Armed");client.publish("home/office/esp1/motion/status", "Armed");client.publish("home/office/esp1/motion/notification", "NO MOTION");armMotion = true;motionTriggered = false;digitalWrite(motionLED, HIGH);}else if(messageTemp == "0"){Serial.print("Motion Sensor Not Armed");client.publish("home/office/esp1/motion/status", "Not Armed");client.publish("home/office/esp1/motion/notification", "NO MOTION");armMotion=false;motionTriggered = false;digitalWrite(motionLED, LOW);}}Serial.println();}// This functions reconnects your ESP8266 to your MQTT broker// Change the function below if you want to subscribe to more topics with your ESP8266void reconnect() {// Loop until we're reconnectedwhile (!client.connected()) {Serial.print("Attempting MQTT connection...");// Attempt to connectif (client.connect("ESP8266Client")) {Serial.println("connected"); // Once connected, publish an announcement...client.publish("home/office/esp1/motion/status", "Not Armed");client.publish("home/office/esp1/motion/notification", "NO MOTION");// Subscribe or resubscribe to a topic// You can subscribe to more topics (to control more LEDs in this example)client.subscribe("home/office/esp1/motion");} else {Serial.print("failed, rc=");Serial.print(client.state());Serial.println(" try again in 5 seconds");// Wait 5 seconds before retryingdelay(5000);}}}// Checks motionICACHE_RAM_ATTR void detectsMovement() {if (armMotion && !motionTriggered) {Serial.println("MOTION DETECTED!!!");motionTriggered = true;client.publish("home/office/esp1/motion/notification", "MOTION DETECTED");}}// The setup function sets your ESP GPIOs to Outputs, starts the serial communication at a baud rate of 115200// Sets your mqtt broker and sets the callback function// The callback function is what receives messages and actually controls the LEDsvoid setup() {pinMode(motionLED, OUTPUT);pinMode(buzzerPin, OUTPUT);pinMode(motionSensor, INPUT_PULLUP);Serial.println(digitalRead( motionSensor));attachInterrupt(digitalPinToInterrupt(motionSensor), detectsMovement, RISING);//attachInterrupt(digitalPinToInterrupt(motionSensor), detectsMovement, FALLING);Serial.begin(115200);setup_wifi();client.setServer(mqtt_server, 1883);client.setCallback(callback);delay(1000);}// For this project, you don't need to change anything in the loop function. Basically it ensures that you ESP is connected to your brokervoid loop() {if (!client.connected()) {reconnect();}client.loop();int a=digitalRead( motionSensor);Serial.println(digitalRead( motionSensor));//delay(1000);if(motionTriggered==true){tone(buzzerPin, 1000, 200);}delay(1000);}