From: azao <821478748@qq.com> Date: Thu, 8 Jan 2026 13:57:10 +0000 (+0800) Subject: 成功连接iot3,有回应 X-Git-Url: http://git.snicker.com.cn/?a=commitdiff_plain;h=7128353f6ae9d9a05267ebea79a4df8e0bd2e0ac;p=mirrors%2FASR1603_G5.git 成功连接iot3,有回应 但是小程序未显示信息 --- diff --git a/2.sdk/project/K8/sdk/services/microPython/ports/quectel/core/ai_VC.py b/2.sdk/project/K8/sdk/services/microPython/ports/quectel/core/ai_VC.py index 6fa1035f..a0866145 100644 --- a/2.sdk/project/K8/sdk/services/microPython/ports/quectel/core/ai_VC.py +++ b/2.sdk/project/K8/sdk/services/microPython/ports/quectel/core/ai_VC.py @@ -1388,7 +1388,7 @@ class AI_VC(Abstract): EventMesh.publish("connect_iot_socket")#连接IOT print("IOT连接成功") - EventMesh.publish("login_IOT")#登录IOT + # EventMesh.publish("login_IOT")#登录IOT EventMesh.publish("send_unbind_dev_to_iot")#开启IOT心跳包 self.server_ip = EventMesh.publish("get_storage_config","server_ip")# if self.server_ip == None: diff --git a/2.sdk/project/K8/sdk/services/microPython/ports/quectel/core/protocol.py b/2.sdk/project/K8/sdk/services/microPython/ports/quectel/core/protocol.py index d23df450..a3fc3382 100644 --- a/2.sdk/project/K8/sdk/services/microPython/ports/quectel/core/protocol.py +++ b/2.sdk/project/K8/sdk/services/microPython/ports/quectel/core/protocol.py @@ -354,23 +354,28 @@ class IOT_socket_SendCmdManager(Abstract): VC_TTS_volume =100 sourceVersion =versions.Build_Time Timeversion ="JSWM-"+sourceVersion + ts = utime.ticks_ms() infor_send_data = { "data":{ "version":Timeversion,#版本号 "charge":battery_state,#0表示未充电,1表示充电中 - "current_role_name":"",#当前音色的name + "currentRoleName":"",#当前音色的name "volume":str(VC_TTS_volume),#百分比数值 - "wifi_rssi":str(net.csqQueryPoll()//6),#wifi强弱:0,1,2,3,4,5。转成这几个梯度 + "wifiRssi":str(net.csqQueryPoll()//6),#wifi强弱:0,1,2,3,4,5。转成这几个梯度 "battery":str(battery),#电量 - "device_id":modem.getDevImei(),#设备号 + "bargein": "打断模式:0",#不能手动设置打断模式 + "deviceId":modem.getDevImei(),#设备号 }, - "cmd":"device_info" + "cmd":"device_info", + "msg": "上报设备信息", + "ts": ts } return infor_send_data #设备上报VC状态 def device_report_state_send_data(self): ai_status = EventMesh.publish("screen_get_ai") + ts = utime.ticks_ms() if EventMesh.publish("get_VC_satrt_flag") == 1:#获取vc是否在运行 if ai_status < 6 : target_status = "listening" @@ -383,10 +388,12 @@ class IOT_socket_SendCmdManager(Abstract): device_report_data ={ "data":{ - "device_id":modem.getDevImei(), + "deviceId":modem.getDevImei(), "state":target_status }, - "cmd":"chat_state" + "cmd":"chat_state", + "msg": "当前服务状态上传", + "ts": ts } return device_report_data def extract_target_number(json_str): @@ -424,13 +431,13 @@ def playload_data(data): if data[0] != 0xff or data[1] !=0xfe: print("data格式错误,重新登录") - EventMesh.publish("login_IOT")#登录IOT + # EventMesh.publish("login_IOT")#登录IOT return - payload_len_bytes = data [3:5] - payload_len = ustruct.unpack ('H', payload_len_bytes)[0] - # print ("解析到 Payload 长度:{} 字节".format(payload_len)) + # payload_len_bytes = data [7:5] + # payload_len = ustruct.unpack ('H', payload_len_bytes)[0] + # # print ("解析到 Payload 长度:{} 字节".format(payload_len)) - payload = data [5 : 5+ payload_len] + payload = data [8 : ] try: json_str = payload.decode ('utf-8') except UnicodeDecodeError: @@ -440,6 +447,7 @@ def playload_data(data): except UnicodeDecodeError: print ("Payload 解码失败") return + print ("JSON 字符串:{}".format(json_str)) # json_str = "\"" + json_str + "\"" if type(json_str) is str and len(json_str)>5: print ("解析出的 JSON 字符串:{}".format(json_str) + "-"*50) @@ -551,7 +559,7 @@ class SockMangager(Abstract): EventMesh.subscribe("CHANNEL_IOT_SEND",self.user_send)#发送数据的函数!!重要 EventMesh.subscribe("GET_IOT_PROTOCOL_CILENT",self.get_IOT_sock) EventMesh.subscribe("close_socket_timer",self.close_socket_timer) - EventMesh.subscribe("connect_iot_socket",self.connect_iot_socket_cb)#使用socket连接iot平台 + EventMesh.subscribe("connect_iot_websocket",self.connect_iot_websocket_cb)#使用socket连接iot平台 EventMesh.subscribe("login_IOT",self.login_IOT)#登录iot平台 EventMesh.subscribe("heart_IOT",self.heart_IOt)#心跳包 EventMesh.subscribe("send_device_information",self.send_device_information)#上报设备信息 @@ -578,8 +586,8 @@ class SockMangager(Abstract): def initialization(self, *args): pass - def connect_iot_socket_cb(self,event,msg): - self.connect_iot_socket() + def connect_iot_websocket_cb(self,event,msg): + self.connect_iot_websocket() def add_support_resolver(self, resolver): self.resolver = resolver def hr_report_event(self, *args, **kwargs): @@ -612,12 +620,12 @@ class SockMangager(Abstract): EventMesh.publish("publish_AI_btnInfo",{"func":self.send_packet_iot,"data":WEB_CMD_ENUM.POST_DEVICE_INFOR_S_CMD})#上报设备信息 def iot_connect_timerCb(self,*argc): - self.connect_iot_socket() + self.connect_iot_websocket() - def connect_iot_socket(self): + def connect_iot_websocket(self): if EventMesh.publish("get_voice_record_flag") == 1: EventMesh.publish("stop_voice_record") - print("connect_iot_socket !!!!") + print("connect_iot_websocket !!!!") stagecode, subcode = checknet.wait_network_connected(30) if stagecode == 3 and subcode == 1: @@ -677,7 +685,7 @@ class SockMangager(Abstract): self.start_voice_flag = 1 else: self.start_voice_flag = 0 - + print("iot没连上1111!!!!") print("self.iot_protocol_client {} self.start_voice_flag:{}".format(self.iot_protocol_client,self.start_voice_flag)) if self.iot_protocol_client != None: if self.timer_id == 0: @@ -692,21 +700,39 @@ class SockMangager(Abstract): pass except Exception as e: self.iot_protocol_client = None - print("connect_iot_socket error") + print("connect_iot_websocket error") def heart_IOt(self,event,msg): print("heart_IOT.....") #Header header = ustruct.pack('>H', 0xFFFE) # 使用大端模式 #Type packet_type = ustruct.pack('B', 0x02) + #心跳帧 + json_str = { + "cmd":"post_heart_beat", + "ts":1738283829, + "msg":"心跳", + "data":{ + "battery": 20 + } + } + json_str = ujson.dumps(json_str) + #字符串转为二进制数据Payload + payload = json_str.encode('utf-8') + payloadLen = ustruct.pack('H', len(payload)) #拼接 - heart_packet = header + packet_type + heart_packet = header + packet_type + payloadLen + payload#+ crc EventMesh.publish("CHANNEL_IOT_SEND", heart_packet) # print("心跳发送数据包:{}".format(heart_packet)) def send_packet_iot(self,data):#发送数据给iot,data为接口json数据 - if sim.getStatus() != 1 or self.start_voice_flag == 1:#网络正常 + if sim.getStatus() != 1 :#网络正常 + print("网络不正常") return + if self.start_voice_flag == 0: + print("iot不在线") + EventMesh.publish("publish_AI_btnInfo",{"func":self.connect_iot_websocket}) + return #Header header = ustruct.pack('>H', 0xFFFE) # 使用大端模式 #Type @@ -722,7 +748,7 @@ class SockMangager(Abstract): crc = self.crc16(payload) print("crc的长度为{}".format(len(crc))) #拼接 - full_packet = header + packet_type + payloadLen + payload+ crc + full_packet = header + packet_type + payloadLen + payload#+ crc EventMesh.publish("CHANNEL_IOT_SEND", full_packet) print("发送更新数据包:{}".format(full_packet)) @@ -743,7 +769,7 @@ class SockMangager(Abstract): #CRC16(2字节,基于上述所有字段计算) crc = self.crc16(body) # 完整数据包 - full_packet = body + crc + full_packet = body #+ crc EventMesh.publish("CHANNEL_IOT_SEND", full_packet) print("登录发送数据包:{}".format(full_packet)) @@ -779,7 +805,7 @@ class SockMangager(Abstract): # print("iot断线重连") # self.login_statu = 0 # self.iot_close_therad() - # EventMesh.publish("publish_AI_btnInfo",{"func":self.connect_iot_socket}) + # EventMesh.publish("publish_AI_btnInfo",{"func":self.connect_iot_websocket}) # EventMesh.publish("login_IOT")#登录IOT # else: # if self.login_statu == 0: @@ -789,8 +815,8 @@ class SockMangager(Abstract): print("iot断线重连") self.login_statu = 0 self.iot_close_therad() - EventMesh.publish("publish_AI_btnInfo",{"func":self.connect_iot_socket}) - EventMesh.publish("login_IOT")#登录IOT + EventMesh.publish("publish_AI_btnInfo",{"func":self.connect_iot_websocket}) + # EventMesh.publish("login_IOT")#登录IOT self._1scnt = 0 @@ -810,58 +836,40 @@ class SockMangager(Abstract): # EventMesh.debug_log("发送线程") self.reconnect_count = 0 else: - EventMesh.publish("publish_AI_btnInfo",{"func":self.connect_iot_socket}) + EventMesh.publish("publish_AI_btnInfo",{"func":self.connect_iot_websocket}) print("socket关闭退出send线程") return - EventMesh.debug_log("socket:send data2 len:{}".format(len(data),self.iot_protocol_client)) + EventMesh.debug_log("websocket:send data2 len:{}".format(len(data),self.iot_protocol_client)) except Exception as e: - EventMesh.debug_log("socket:send error") - EventMesh.publish("publish_AI_btnInfo",{"func":self.connect_iot_socket}) + EventMesh.debug_log("websocket:send error") + EventMesh.publish("publish_AI_btnInfo",{"func":self.connect_iot_websocket}) break def iot_protocol_recv(self): - # 配置超时参数,单位:秒 - RECV_TIMEOUT = 30*1000 # 30秒未收到数据则重连 - last_recv_time = utime.time() # 初始化最后一次收到数据的时间 while True: # 死循环接收数据 - # with self.sock_lock: # 警告:不能加锁,不然会导致线程超长延时 - try: - data = self.iot_protocol_client.recv() - if isinstance(data, bytes) and len(data)>10: - playload_data(data) - #fffe00010001xxxx - if isinstance(data, bytes) and len(data) == 8:#登录成功 - target_bytes = b'\xff\xfe\x00\x01\x00\x01' - if data[:6] == target_bytes: - self.login_statu = 1#登录成功标志位修改 - socket_log.info("IOT_recv = {} ,len = {}".format(self.bytes_to_hex(data),len(data))) - #检查socket是否已关闭 - if self.iot_protocol_client == None: - print("socket关闭开启重连") - EventMesh.publish("publish_AI_btnInfo",{"func":self.connect_iot_socket}) - return - #超时检查:如果超过设定时间未收到数据,触发重连 - # print("接收时间差 = {}".format(utime.time() - last_recv_time)) - if utime.time() - last_recv_time > RECV_TIMEOUT: - print("超过{}秒未收到数据,触发重连".format(RECV_TIMEOUT)) - # 先关闭现有连接 - if self.iot_protocol_client: - self.iot_protocol_client.close() - # 触发重连 - EventMesh.publish("publish_AI_btnInfo", {"func": self.connect_iot_socket}) - # 重连后短暂延迟再继续循环 - utime.sleep(1) - # 重置超时计时器 - last_recv_time = utime.time() - except Exception as e: - print("cli break") - print("异常是:{}".format(e)) + try: + recv_data = self.iot_protocol_client.recv() + print("recv_data = {}".format(recv_data)) + if recv_data == "websocket close": + # 服务器关闭连接或客户端关闭连接 + print("对端 服务器强制关闭 cli close") # EventMesh.publish("connect_iot_websocket_cb") - last_recv_time = utime.time() - EventMesh.publish("publish_AI_btnInfo",{"func":self.connect_iot_socket}) + EventMesh.publish("publish_AI_btnInfo",{"func":self.connect_iot_websocket}) break + elif recv_data == "": + print("下发的空数据 不做处理") + else: + if len(recv_data) != 0: + self.reconnect_count = 0 + playload_data(recv_data) + + except Exception as e: + print("cli break") + # EventMesh.publish("connect_iot_websocket_cb") + EventMesh.publish("publish_AI_btnInfo",{"func":self.connect_iot_websocket}) + break self.iot_protoclo_recv_id =None def decode_with_multiple_encodings(self, data): #尝试多种常见编码格式解码,返回第一个成功的结果 @@ -927,6 +935,7 @@ class SockMangager(Abstract): except Exception as e: print("iot_protoclo_recv_close_therad error!!!!!!!!") self.start_voice_flag = 0 + print("iot没连上222!!!!") print("iot_close_therad") def get_IOT_sock(self,event,msg): @@ -941,7 +950,7 @@ class SockMangager(Abstract): else : try: if self.start_voice_flag == 0: #如果关闭了就需要重新再连接 - EventMesh.publish("publish_AI_btnInfo",{"func":self.connect_iot_socket}) + EventMesh.publish("publish_AI_btnInfo",{"func":self.connect_iot_websocket}) return if data != None: if len(data) != 0: @@ -961,29 +970,29 @@ class SockMangager(Abstract): EventMesh.publish("publish_AI_btnInfo",{"func":self.iot_protocol_send_cb,"data":data}) else: print("reconnect1 {}".format(data)) - EventMesh.publish("publish_AI_btnInfo",{"func":self.connect_iot_socket}) + EventMesh.publish("publish_AI_btnInfo",{"func":self.connect_iot_websocket}) except Exception as e: print("senc data err reconnect{}".format(data)) - EventMesh.publish("publish_AI_btnInfo",{"func":self.connect_iot_socket}) + EventMesh.publish("publish_AI_btnInfo",{"func":self.connect_iot_websocket}) def iot_protocol_send_cb(self,data): try: if data != None: - print("socket connect success:{} self.iot_protocol_client:{}".format(data,self.iot_protocol_client)) + print("websocket connect success:{} self.iot_protocol_client:{}".format(data,self.iot_protocol_client)) if self.iot_protocol_client != None: self.iot_protocol_client.send(data) self.reconnect_count = 0 - print("socket send success") + print("websocket send success") else: print("reconnect2 {}".format(data)) # EventMesh.publish("connect_iot_socket_cb") - EventMesh.publish("publish_AI_btnInfo",{"func":self.connect_iot_socket}) + EventMesh.publish("publish_AI_btnInfo",{"func":self.connect_iot_websocket}) except Exception as e: print("发送失败 重新连接") self.reconnect_count+=1 # EventMesh.publish("connect_iot_socket_cb") - EventMesh.publish("publish_AI_btnInfo",{"func":self.connect_iot_socket}) + EventMesh.publish("publish_AI_btnInfo",{"func":self.connect_iot_websocket}) def bytes_ljust(data, length, fill_byte=b'\x00'): """ @@ -3896,8 +3905,9 @@ class ProtoColResolver(object): # EventMesh.publish(constants.PROTOCOL.H04_RECV, data) try: if len(data) > 0: + pass #EventMesh.debug_log("handler:{}".format(len(data))) - playload_data(data) + # playload_data(data) except Exception as e: EventMesh.debug_log("handler:ERROR:") usys.print_exception(e)