Arduino-ESP32安防监控:入侵检测与报警

Arduino-ESP32安防监控:入侵检测与报警

【免费下载链接】arduino-esp32 Arduino core for the ESP32 【免费下载链接】arduino-esp32 项目地址: https://gitcode.com/GitHub_Trending/ar/arduino-esp32

痛点场景:传统安防系统的局限性

你是否还在为家庭或办公室的安全担忧?传统安防系统价格昂贵、安装复杂,而且缺乏智能化功能。当入侵发生时,你往往无法及时获知,错过了最佳的应对时机。

本文将为你展示如何利用Arduino-ESP32构建一套低成本、高效率的智能安防监控系统,实现实时入侵检测、即时报警和远程监控功能。

读完本文,你将掌握:

  • ESP32传感器数据采集与处理技术
  • WiFi网络通信与远程报警实现
  • Web服务器搭建与远程监控界面
  • 多传感器融合的智能检测算法
  • 完整的安防系统集成方案

系统架构设计

mermaid

硬件组件清单

组件型号数量功能描述
ESP32开发板ESP32-WROOM-321主控制器,处理传感器数据
PIR运动传感器HC-SR5011检测人体移动
声音传感器LM3931检测异常声响
门磁传感器干簧管若干检测门窗开关状态
蜂鸣器有源5V1发出报警声音
LED指示灯5mm2状态指示(红/绿)
电阻220Ω若干限流保护
杜邦线公对公若干连接线路

核心代码实现

1. 传感器初始化与数据采集

#include <WiFi.h>
#include <WebServer.h>

// 传感器引脚定义
#define PIR_PIN 4      // PIR运动传感器
#define SOUND_PIN 34   // 声音传感器(ADC)
#define DOOR_PIN 5     // 门磁传感器
#define BUZZER_PIN 18  // 蜂鸣器
#define LED_RED 19     // 红色LED
#define LED_GREEN 21   // 绿色LED

// 全局变量
bool motionDetected = false;
bool soundDetected = false;
bool doorOpened = false;
int soundThreshold = 2000; // 声音阈值

void setupSensors() {
  pinMode(PIR_PIN, INPUT);
  pinMode(SOUND_PIN, INPUT);
  pinMode(DOOR_PIN, INPUT_PULLUP);
  pinMode(BUZZER_PIN, OUTPUT);
  pinMode(LED_RED, OUTPUT);
  pinMode(LED_GREEN, OUTPUT);
  
  digitalWrite(BUZZER_PIN, LOW);
  digitalWrite(LED_RED, LOW);
  digitalWrite(LED_GREEN, HIGH); // 系统正常绿灯
}

void readSensors() {
  // 读取PIR传感器
  motionDetected = digitalRead(PIR_PIN) == HIGH;
  
  // 读取声音传感器(ADC值)
  int soundValue = analogRead(SOUND_PIN);
  soundDetected = soundValue > soundThreshold;
  
  // 读取门磁传感器
  doorOpened = digitalRead(DOOR_PIN) == LOW;
}

2. WiFi连接与网络配置

const char* ssid = "Your_WiFi_SSID";
const char* password = "Your_WiFi_Password";

WebServer server(80);

void connectWiFi() {
  Serial.print("Connecting to ");
  Serial.println(ssid);
  
  WiFi.begin(ssid, password);
  
  int attempts = 0;
  while (WiFi.status() != WL_CONNECTED && attempts < 20) {
    delay(500);
    Serial.print(".");
    attempts++;
  }
  
  if (WiFi.status() == WL_CONNECTED) {
    Serial.println("\nWiFi connected");
    Serial.print("IP address: ");
    Serial.println(WiFi.localIP());
  } else {
    Serial.println("\nWiFi connection failed");
  }
}

3. Web服务器与远程监控

void setupWebServer() {
  server.on("/", HTTP_GET, []() {
    String html = "<!DOCTYPE html><html><head><meta charset='UTF-8'>";
    html += "<title>ESP32安防监控系统</title>";
    html += "<meta name='viewport' content='width=device-width, initial-scale=1'>";
    html += "<style>body{font-family:Arial,sans-serif;margin:40px}</style>";
    html += "</head><body>";
    html += "<h1>ESP32安防监控系统</h1>";
    html += "<div id='status'>系统状态: 监控中</div>";
    html += "<div>运动检测: <span id='motion'>" + String(motionDetected ? "是" : "否") + "</span></div>";
    html += "<div>声音检测: <span id='sound'>" + String(soundDetected ? "是" : "否") + "</span></div>";
    html += "<div>门状态: <span id='door'>" + String(doorOpened ? "打开" : "关闭") + "</span></div>";
    html += "<button onclick='refreshData()'>刷新状态</button>";
    html += "<script>function refreshData(){location.reload()}</script>";
    html += "</body></html>";
    
    server.send(200, "text/html", html);
  });
  
  server.on("/api/status", HTTP_GET, []() {
    String json = "{";
    json += "\"motion\":" + String(motionDetected);
    json += ",\"sound\":" + String(soundDetected);
    json += ",\"door\":" + String(doorOpened);
    json += ",\"alarm\":" + String(isAlarmTriggered());
    json += "}";
    
    server.send(200, "application/json", json);
  });
  
  server.begin();
  Serial.println("HTTP server started");
}

4. 智能报警逻辑实现

bool isAlarmTriggered() {
  // 多传感器融合报警算法
  int triggerCount = 0;
  
  if (motionDetected) triggerCount++;
  if (soundDetected) triggerCount++;
  if (doorOpened) triggerCount++;
  
  // 至少两个传感器同时触发才报警,减少误报
  return triggerCount >= 2;
}

void handleAlarm() {
  if (isAlarmTriggered()) {
    // 触发报警
    digitalWrite(LED_RED, HIGH);
    digitalWrite(LED_GREEN, LOW);
    
    // 蜂鸣器报警(间歇性)
    for (int i = 0; i < 5; i++) {
      digitalWrite(BUZZER_PIN, HIGH);
      delay(200);
      digitalWrite(BUZZER_PIN, LOW);
      delay(200);
    }
    
    Serial.println("警报!检测到入侵行为");
    
    // 这里可以添加网络报警推送代码
    // sendPushNotification("检测到入侵!");
    
  } else {
    // 正常状态
    digitalWrite(LED_RED, LOW);
    digitalWrite(LED_GREEN, HIGH);
    digitalWrite(BUZZER_PIN, LOW);
  }
}

5. 主循环与系统集成

void setup() {
  Serial.begin(115200);
  
  setupSensors();
  connectWiFi();
  setupWebServer();
  
  Serial.println("安防监控系统启动完成");
}

void loop() {
  readSensors();
  handleAlarm();
  server.handleClient();
  
  // 每500ms更新一次状态
  delay(500);
  
  // 调试信息输出
  Serial.print("运动:");
  Serial.print(motionDetected ? "是" : "否");
  Serial.print(" 声音:");
  Serial.print(soundDetected ? "是" : "否");
  Serial.print(" 门状态:");
  Serial.print(doorOpened ? "打开" : "关闭");
  Serial.print(" 报警:");
  Serial.println(isAlarmTriggered() ? "触发" : "正常");
}

系统优化与高级功能

1. 误报过滤算法

// 时间窗口内的多次检测才确认为真实报警
unsigned long lastMotionTime = 0;
const unsigned long DEBOUNCE_TIME = 2000; // 2秒去抖

bool debouncedMotionDetection() {
  bool currentState = digitalRead(PIR_PIN) == HIGH;
  
  if (currentState) {
    unsigned long currentTime = millis();
    if (currentTime - lastMotionTime > DEBOUNCE_TIME) {
      lastMotionTime = currentTime;
      return true;
    }
  }
  return false;
}

2. 自适应阈值调整

// 动态调整声音检测阈值
void adjustSoundThreshold() {
  static int sampleCount = 0;
  static long soundSum = 0;
  
  int soundValue = analogRead(SOUND_PIN);
  soundSum += soundValue;
  sampleCount++;
  
  if (sampleCount >= 100) {
    int average = soundSum / sampleCount;
    soundThreshold = average * 1.5; // 平均值的1.5倍作为阈值
    soundSum = 0;
    sampleCount = 0;
    
    Serial.print("调整声音阈值: ");
    Serial.println(soundThreshold);
  }
}

部署与测试指南

硬件连接示意图

mermaid

测试步骤

  1. 硬件连接检查

    • 确认所有传感器正确连接到ESP32
    • 检查电源供应稳定
    • 验证接地连接良好
  2. 软件配置

    • 修改WiFi SSID和密码
    • 上传代码到ESP32
    • 打开串口监视器查看调试信息
  3. 功能测试

    • 测试运动检测:在传感器前移动
    • 测试声音检测:制造声响
    • 测试门磁检测:模拟开门
    • 验证报警触发逻辑
  4. 远程访问测试

    • 通过浏览器访问ESP32的IP地址
    • 检查Web界面状态显示
    • 测试API接口响应

常见问题解决

问题现象可能原因解决方案
WiFi连接失败SSID/密码错误检查WiFi配置信息
传感器无响应引脚连接错误重新检查接线
误报过多阈值设置不当调整检测阈值
Web界面无法访问网络配置问题检查路由器设置
报警不触发传感器故障更换传感器测试

安全注意事项

  1. 电气安全

    • 确保所有连接绝缘良好
    • 避免短路和过载
    • 使用合适的电源适配器
  2. 数据安全

    • 修改默认WiFi密码
    • 定期更新系统固件
    • 避免将系统暴露在公网
  3. 隐私保护

    • 明确告知监控区域
    • 遵守当地隐私法规
    • 定期清理监控数据

总结与展望

通过本文介绍的Arduino-ESP32安防监控系统,你不仅可以构建一个功能完善的入侵检测系统,还能掌握物联网设备开发的核心技术。这个系统具有以下优势:

  • 低成本高效益:相比商业安防系统,成本降低80%以上
  • 灵活可扩展:支持多种传感器和报警方式
  • 智能化程度高:多传感器融合减少误报
  • 远程监控便捷:通过Web界面实时查看状态

未来你可以进一步扩展系统功能,如添加摄像头模块、集成云存储、实现手机APP控制等,打造更加智能化的安防解决方案。

现在就开始动手,用ESP32守护你的安全空间吧!

【免费下载链接】arduino-esp32 Arduino core for the ESP32 【免费下载链接】arduino-esp32 项目地址: https://gitcode.com/GitHub_Trending/ar/arduino-esp32

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值