#!/usr/bin/env python3 """ 手动报警触发指导 指导用户如何手动触发ESP32报警以测试云端上报功能 """ import time import requests def check_esp32_status(): """检查ESP32状态""" try: response = requests.get("http://192.168.1.3:80/api/status", timeout=5) if response.status_code == 200: print("✅ ESP32连接正常") return True else: print(f"❌ ESP32响应异常: {response.status_code}") return False except Exception as e: print(f"❌ ESP32连接失败: {e}") return False def get_current_temperature(): """获取当前温度""" try: response = requests.get("http://192.168.1.3:80/api/room/temperature", timeout=5) if response.status_code == 200: data = response.json() temp = data.get('temperature', 0) print(f"🌡️ 当前温度: {temp}°C") return temp else: print("❌ 获取温度失败") return None except Exception as e: print(f"❌ 获取温度异常: {e}") return None def monitor_alarm_history(): """监控报警历史变化""" try: response = requests.get("http://192.168.1.3:80/api/alarm/history", timeout=5) if response.status_code == 200: history = response.json() alarms = history.get('alarms', []) print(f"📋 当前报警历史: {len(alarms)} 个") if alarms: latest = alarms[0] # 最新的报警 print(f"最新报警: {latest.get('title')} - {latest.get('time')} - {latest.get('temp')}°C") return len(alarms) else: print("❌ 获取报警历史失败") return 0 except Exception as e: print(f"❌ 获取报警历史异常: {e}") return 0 def main(): """主函数""" print("🔥 ESP32手动报警触发指导") print("=" * 40) # 检查ESP32状态 if not check_esp32_status(): print("请确保ESP32正常运行并连接到网络") return # 获取当前温度 current_temp = get_current_temperature() # 获取当前报警数量 initial_alarm_count = monitor_alarm_history() print("\n🚨 手动触发报警指导:") print("=" * 30) print("请执行以下操作之一:") print("1. 用手指直接接触热成像传感器表面") print("2. 用打火机或其他热源靠近传感器") print("3. 快速移动热源触发异常升温报警") print("4. 等待系统自动检测") print() print("预期结果:") print("- 温度应该上升到40°C以上触发燃烧报警") print("- 或者温度变化率超过5°C/秒触发异常升温报警") print("- ESP32应该会自动上报报警到模拟云端服务器") print() print("按 Ctrl+C 停止监控") # 持续监控 try: while True: time.sleep(3) # 每3秒检查一次 # 检查温度变化 new_temp = get_current_temperature() if new_temp and current_temp: temp_change = new_temp - current_temp if abs(temp_change) > 2: # 温度变化超过2度 print(f"🌡️ 温度变化: {current_temp:.1f}°C → {new_temp:.1f}°C (变化: {temp_change:+.1f}°C)") current_temp = new_temp # 检查报警历史变化 new_alarm_count = monitor_alarm_history() if new_alarm_count > initial_alarm_count: print(f"🚨 检测到新报警! 数量从 {initial_alarm_count} 增加到 {new_alarm_count}") print("✅ 报警触发成功! 请检查模拟云端服务器是否收到上报数据") initial_alarm_count = new_alarm_count except KeyboardInterrupt: print(f"\n\n🎯 监控结束") print("如果看到新报警,说明触发成功") print("请检查模拟云端服务器的输出") if __name__ == "__main__": main()