two
This commit is contained in:
@@ -109,6 +109,7 @@
|
||||
from maix import app, uart, pinmap, time
|
||||
import hashlib
|
||||
import hmac
|
||||
import re
|
||||
import ujson
|
||||
|
||||
# ========== 配置 ==========
|
||||
@@ -130,53 +131,109 @@ def generate_token(device_id):
|
||||
return "Arrow_" + hmac.new((SALT + device_id).encode(), SALT2.encode(), hashlib.sha256).hexdigest()
|
||||
|
||||
def send_cmd(cmd_str, timeout_ms=3000):
|
||||
"""发送 AT 指令并等待 OK / ERROR"""
|
||||
"""发送 AT 指令并返回完整响应;超时返回已收到的内容。"""
|
||||
print("[AT] =>", cmd_str)
|
||||
http_serial.write((cmd_str + "\r\n").encode())
|
||||
buffer = b""
|
||||
start = time.ticks_ms()
|
||||
while time.ticks_ms() - start < timeout_ms:
|
||||
while time.ticks_diff(time.ticks_ms(), start) < timeout_ms:
|
||||
data = http_serial.read(128)
|
||||
if data:
|
||||
buffer += data
|
||||
try:
|
||||
decoded = buffer.decode()
|
||||
print("<= ", decoded.strip())
|
||||
if "OK" in decoded:
|
||||
return True
|
||||
if "+CME ERROR" in decoded or "ERROR" in decoded:
|
||||
return False
|
||||
decoded = buffer.decode("utf-8", "ignore")
|
||||
if "OK" in decoded or "+CME ERROR" in decoded or "ERROR" in decoded:
|
||||
print("[AT] <=", decoded.strip())
|
||||
return decoded
|
||||
except:
|
||||
pass
|
||||
time.sleep_ms(10)
|
||||
decoded = buffer.decode("utf-8", "ignore")
|
||||
print("[AT] !! timeout", timeout_ms, "ms, response:", decoded.strip() or "<empty>")
|
||||
return decoded
|
||||
|
||||
|
||||
def response_ok(response):
|
||||
return "OK" in response and "ERROR" not in response
|
||||
|
||||
|
||||
def wait_modem_ready():
|
||||
"""等待模组响应,并确认 PDP 上下文已经获得 IP。"""
|
||||
for attempt in range(15):
|
||||
if response_ok(send_cmd("AT", 1000)):
|
||||
break
|
||||
print("[4G] 等待模组启动", attempt + 1, "/15")
|
||||
time.sleep_ms(1000)
|
||||
else:
|
||||
print("[4G] UART2 无 AT 响应,请检查模组供电、A28/A29 接线和串口占用")
|
||||
return False
|
||||
|
||||
send_cmd("ATE0", 1000)
|
||||
cpin = send_cmd("AT+CPIN?", 3000)
|
||||
if "READY" not in cpin:
|
||||
print("[4G] SIM 卡未就绪:", cpin.strip())
|
||||
return False
|
||||
|
||||
addr = send_cmd("AT+CGPADDR=1", 3000)
|
||||
match = re.search(r'\+CGPADDR:\s*1,"([^\"]+)"', addr)
|
||||
if match and match.group(1) != "0.0.0.0":
|
||||
print("[4G] PDP ready, IP:", match.group(1))
|
||||
return True
|
||||
|
||||
send_cmd("AT+MIPCALL=1,1", 15000)
|
||||
for _ in range(20):
|
||||
addr = send_cmd("AT+CGPADDR=1", 3000)
|
||||
match = re.search(r'\+CGPADDR:\s*1,"([^\"]+)"', addr)
|
||||
if match and match.group(1) != "0.0.0.0":
|
||||
print("[4G] PDP ready, IP:", match.group(1))
|
||||
return True
|
||||
time.sleep_ms(1000)
|
||||
|
||||
print("[4G] PDP 未获得 IP,请检查 SIM 流量、信号和 APN")
|
||||
return False
|
||||
|
||||
|
||||
def clear_http_instances():
|
||||
for instance_id in range(6):
|
||||
send_cmd(f"AT+MHTTPDEL={instance_id}", 1200)
|
||||
|
||||
def create_http_instance(url):
|
||||
cmd = f'AT+MHTTPCREATE="{url}"'
|
||||
if send_cmd(cmd):
|
||||
# 尝试提取 instance ID(如果模块返回)
|
||||
# 注意:部分模块不会返回 ID,可忽略,直接用 0 或 1
|
||||
return True
|
||||
return False
|
||||
response = send_cmd(cmd, 8000)
|
||||
match = re.search(r"\+MHTTPCREATE:\s*(\d+)", response)
|
||||
if not response_ok(response) or not match:
|
||||
print("❌ 创建 HTTP 实例失败,模组响应:", response.strip() or "<empty>")
|
||||
return None
|
||||
return int(match.group(1))
|
||||
|
||||
def send_http_request(url, api_path, token, device_id, json_data):
|
||||
# 1. 创建 HTTP 实例
|
||||
if not create_http_instance(url):
|
||||
print("❌ 创建 HTTP 实例失败")
|
||||
instance_id = create_http_instance(url)
|
||||
if instance_id is None:
|
||||
return False
|
||||
|
||||
# 2. 设置 Headers(假设实例 ID 为 0,或根据模块默认)
|
||||
instance_id = 0 # 大多数模块默认实例为 0;若支持多实例,需解析返回值
|
||||
send_cmd(f'AT+MHTTPCFG="header",{instance_id},"Content-Type: application/json"')
|
||||
send_cmd(f'AT+MHTTPCFG="header",{instance_id},"Authorization: {token}"')
|
||||
send_cmd(f'AT+MHTTPCFG="header",{instance_id},"DeviceId: {device_id}"')
|
||||
# 2. 设置 Headers
|
||||
commands = (
|
||||
f'AT+MHTTPCFG="header",{instance_id},"Content-Type: application/json"',
|
||||
f'AT+MHTTPCFG="header",{instance_id},"Authorization: {token}"',
|
||||
f'AT+MHTTPCFG="header",{instance_id},"DeviceId: {device_id}"',
|
||||
)
|
||||
for command in commands:
|
||||
if not response_ok(send_cmd(command)):
|
||||
print("❌ HTTP Header 配置失败")
|
||||
send_cmd(f"AT+MHTTPDEL={instance_id}", 2000)
|
||||
return False
|
||||
|
||||
# 3. 发送 Body
|
||||
json_str = ujson.dumps(json_data)
|
||||
send_cmd(f'AT+MHTTPCONTENT={instance_id},0,0,"{json_str}"')
|
||||
at_json = json_str.replace("\\", "\\\\").replace('"', '\\"')
|
||||
if not response_ok(send_cmd(f'AT+MHTTPCONTENT={instance_id},0,0,"{at_json}"', 8000)):
|
||||
print("❌ HTTP Body 配置失败")
|
||||
send_cmd(f"AT+MHTTPDEL={instance_id}", 2000)
|
||||
return False
|
||||
|
||||
# 4. 发起 POST 请求
|
||||
if send_cmd(f'AT+MHTTPREQUEST={instance_id},2,0,"{api_path}"'):
|
||||
if response_ok(send_cmd(f'AT+MHTTPREQUEST={instance_id},2,0,"{api_path}"', 15000)):
|
||||
print("✅ HTTP 请求已发送")
|
||||
return True
|
||||
else:
|
||||
@@ -199,7 +256,7 @@ def read_response(timeout_ms=5000):
|
||||
print("🚀 启动直接上传流程...")
|
||||
|
||||
token = generate_token(device_id)
|
||||
print("🔑 Token:", token)
|
||||
print("🔑 Token 已生成:", token[:12] + "...")
|
||||
|
||||
# 构造模拟数据
|
||||
timestamp = int(time.time() * 1000)
|
||||
@@ -216,9 +273,16 @@ json_data = {
|
||||
}
|
||||
|
||||
# 执行上传
|
||||
if send_http_request(url, api_path, token, device_id, json_data):
|
||||
upload_ok = False
|
||||
if not wait_modem_ready():
|
||||
print("💥 4G 模组未就绪")
|
||||
else:
|
||||
clear_http_instances()
|
||||
upload_ok = send_http_request(url, api_path, token, device_id, json_data)
|
||||
|
||||
if upload_ok:
|
||||
read_response()
|
||||
else:
|
||||
print("💥 上传流程失败")
|
||||
|
||||
print("🔚 程序结束")
|
||||
print("🔚 程序结束")
|
||||
|
||||
Reference in New Issue
Block a user