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

87 lines
2.7 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
测试ESP32的16宫格热成像数据响应
模拟APP发送GET_THERMAL_GRID命令
"""
import socket
import json
import time
def test_thermal_grid():
# ESP32配置
ESP32_IP = "192.168.1.170"
ESP32_PORT = 8899
print(f"🔥 测试ESP32 16宫格热成像数据")
print(f"📡 目标: {ESP32_IP}:{ESP32_PORT}")
print("=" * 50)
try:
# 创建UDP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.settimeout(5.0) # 5秒超时
# 发送GET_THERMAL_GRID命令
command = "GET_THERMAL_GRID"
print(f"📤 发送命令: {command}")
sock.sendto(command.encode('utf-8'), (ESP32_IP, ESP32_PORT))
# 接收响应
print("⏳ 等待响应...")
data, addr = sock.recvfrom(1024)
response = data.decode('utf-8')
print(f"📥 收到响应 ({len(response)} 字节):")
print(f"📍 来源: {addr}")
print(f"📄 内容: {response}")
print()
# 解析JSON数据
try:
json_data = json.loads(response)
if 'grid' in json_data:
grid_data = json_data.get('grid', [])
print(f"🌡️ 解析成功! 获得 {len(grid_data)} 个温度值:")
# 显示4x4网格
print("🔥 温度分布 (4x4网格):")
for row in range(4):
row_temps = []
for col in range(4):
idx = row * 4 + col
if idx < len(grid_data):
temp = grid_data[idx]
row_temps.append(f"{temp:5.1f}°C")
else:
row_temps.append(" N/A ")
print(" " + " | ".join(row_temps))
if row < 3:
print(" " + "-" * 35)
print(f"\n✅ 测试成功! APP应该能正常接收数据")
else:
print(f"❌ 响应格式错误: {json_data}")
except json.JSONDecodeError as e:
print(f"❌ JSON解析失败: {e}")
print(f"原始响应: {response}")
except socket.timeout:
print("⏰ 超时! ESP32可能未响应")
print("💡 请检查:")
print(" - ESP32是否正常运行")
print(" - IP地址是否正确")
print(" - 网络连接是否正常")
except Exception as e:
print(f"❌ 测试失败: {e}")
finally:
sock.close()
if __name__ == "__main__":
test_thermal_grid()