Files
ESP32_COMM/ota_upload_test.py
T
小龙虾 2caf10b9de feat(ota): 添加OTA回滚保护和固件完整性校验功能
- 启用CONFIG_BOOTLOADER_APP_ROLLBACK_ENABLE和CONFIG_APP_ROLLBACK_ENABLE配置选项
- 在main.cpp中添加esp_ota_mark_app_valid_cancel_rollback()调用,防止意外回滚
- 实现SHA-256固件完整性校验,包括计算、存储和验证功能
- 添加ota_mutex.h头文件用于ESP32 OTA全局互斥锁管理

feat(health_monitor): 实现线程安全的健康状态管理

- 将HealthMonitor类的健康状态改为私有成员并通过mutex保护
- 添加getState()和setState()方法提供线程安全的状态访问
- 修复多任务并发访问健康状态时的竞争条件问题

feat(ota_relay): 增强中继模块的线程安全性和可靠性

- 为OtaRelay类添加静态mutex保护状态变量访问
- 实现StateLockGuard RAII锁管理器确保异常安全
- 添加全局超时机制防止OTA过程无限等待
- 改进flash写入逻辑支持非4字节对齐数据处理

refactor(app_config): 更新版本信息和配置常量

- 更新BUILD_TIME至最新编译时间
- 更新GIT_HASH至最新提交
- 添加BLE_OTA_CTRL_VERIFY_SHA256命令常量定义
2026-06-14 17:10:24 +08:00

54 lines
2.0 KiB
Python

import urllib.request, json, time, sys
ESP_IP = "192.168.31.127"
# Step 1: Upload ESP32 firmware
bin_path = r"G:\Robot_v2\DCU\ESP32_COMM\build\ESP32_COMM.bin"
with open(bin_path, "rb") as f:
fw_data = f.read()
header = json.dumps({"type": "esp32", "total_size": len(fw_data)}).encode("utf-8")
body = header + fw_data
print(f"[1/3] Uploading {len(fw_data)} bytes...")
req = urllib.request.Request(f"http://{ESP_IP}/ota/upload", data=body, method="POST")
req.add_header("Content-Type", "application/octet-stream")
try:
with urllib.request.urlopen(req, timeout=120) as resp:
result = resp.read().decode()
print(f" Upload OK: {result}")
except urllib.error.HTTPError as e:
body_text = e.read().decode()
print(f" Upload FAILED: HTTP {e.code} - {body_text}")
sys.exit(1)
# Step 2: Trigger update
print("[2/3] Triggering OTA update...")
time.sleep(1)
req2 = urllib.request.Request(f"http://{ESP_IP}/ota/update", data=b"", method="POST")
try:
with urllib.request.urlopen(req2, timeout=10) as resp:
result2 = resp.read().decode()
print(f" Update OK: {result2}")
except (ConnectionResetError, urllib.error.URLError, OSError) as e:
print(f" Update triggered (connection reset expected): {type(e).__name__}")
# Step 3: Wait for ESP32 to come back
print("[3/3] Waiting for ESP32 to reboot with new firmware...")
for attempt in range(1, 31):
time.sleep(5)
try:
req3 = urllib.request.Request(f"http://{ESP_IP}/system/status")
with urllib.request.urlopen(req3, timeout=5) as resp:
data = json.loads(resp.read().decode())
ver = data.get("version", "?")
bld = data.get("build_time", "?")
upt = data.get("uptime_sec", 0)
print(f" Online! version={ver} build={bld} uptime={upt}s")
break
except Exception:
if attempt % 5 == 0:
print(f" Still waiting... ({attempt}/30)")
else:
print(" ESP32 did not come back within 150 seconds")