smart-home/trigger_esp32_test_alarm.py
2026-02-26 09:16:34 +08:00

73 lines
2.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
"""
手动触发ESP32测试报警
通过HTTP API触发测试报警验证云端上报功能
"""
import requests
import json
import time
ESP32_IP = "192.168.1.3"
ESP32_PORT = 80
def trigger_test_alarm():
"""触发测试报警"""
print("🚨 尝试触发ESP32测试报警...")
# 方法1: 尝试通过测试API触发
try:
test_data = {
"action": "test_alarm",
"type": "burning",
"message": "手动测试报警",
"temperature": 50.0
}
response = requests.post(f"http://{ESP32_IP}:{ESP32_PORT}/api/test/alarm",
json=test_data, timeout=5)
if response.status_code == 200:
print("✅ 测试报警API触发成功")
return True
except Exception as e:
print(f"⚠️ 测试报警API不可用: {e}")
# 方法2: 检查当前报警状态
try:
response = requests.get(f"http://{ESP32_IP}:{ESP32_PORT}/api/alarm/history", timeout=5)
if response.status_code == 200:
history = response.json()
print(f"📋 当前报警历史: {len(history.get('alarms', []))}")
return True
except Exception as e:
print(f"❌ 获取报警历史失败: {e}")
return False
def main():
"""主函数"""
print("🔥 ESP32测试报警触发器")
print("=" * 30)
# 检查ESP32连接
try:
response = requests.get(f"http://{ESP32_IP}:{ESP32_PORT}/api/status", timeout=5)
if response.status_code == 200:
print("✅ ESP32连接正常")
else:
print("❌ ESP32连接异常")
return
except Exception as e:
print(f"❌ ESP32连接失败: {e}")
return
# 触发测试报警
if trigger_test_alarm():
print("✅ 报警触发成功")
print("💡 请查看模拟云端服务器是否收到报警数据")
else:
print("❌ 报警触发失败")
print("💡 请手动触发报警(接触热成像传感器)")
if __name__ == "__main__":
main()