def run(): productKey = "K3I5ZkgyZjkydGli" productSecret = "VUFyVE92YVJmSUNQ" rand = "q9f5UdAlh687grFl0JVuyY6095302ehd" imei = modem.getDevImei() VER = '1' h1 = uhashlib.md5() h1.update(imei + "," + productSecret + "," + rand) SIGN = ubinascii.hexlify(h1.digest()).decode() print("MD5: %s" % SIGN) # 8ab41bc1e5e74fbf9cec16a6e0990d89 DATA = imei + ";" + rand + ";" + SIGN print("DATA: %s " %DATA) h1 = uhashlib.sha1(productSecret) res = ubinascii.hexlify(h1.digest()).decode() print(res) res = uhashlib.hash_sha1(res, 20) print("SHA1(PS) : %s" % res) aes_key = res[0:16].upper() print("aes_key : %s" % aes_key) res = uhashlib.aes_ecb(aes_key, DATA) print(res) raw = uhashlib.hash_base64(res) print(raw) data = productKey + ";" + raw + ";" + VER url = "220.180.239.212:8415" # url = "192.168.25.64:30884" response = request.post(url, data=data) print(response.json()) # 获取到密钥 # {'RESPCODE': 10200, 'RESPMSG': 'SUCCESS', 'SERVERURI': 'mqtt://220.180.239.212:8420', 'DS': 'd5f01bddfc994c037be90ede69399ac0'} return response.json()
def TrackerMsgFormat(temperature, humidity, light, gps, iccid): ''' 上传数据整合,按照要求格式凭接 :param temperature: :param humidity: :param light: :param gps: :return: ''' global count if count == 256: count = 0 count += 1 num = NumIntTo16(count) # 当前时间戳 time_s = timeIntTo16(utime.mktime(utime.localtime())) # csq csq = NumIntTo16(net.csqQueryPoll()) # 温度 t = t_h_format(temperature) # 湿度 h = t_h_format(humidity) # 光照 l = l_IntTo16(math.ceil(light)) # gps数据 gps_msg = gpsFormat(gps) imei = "3" + "3".join(modem.getDevImei()) iccid = "3" + "3".join(iccid) # 拼接数据包 TrackerMsgFormat = "fa{num}{time}{imei}004d0110001d{iccid}{csq}fcb000b60000003c200004{TH}220002{l}23001101{gps}00000000000013" # 383634343330303130303031303935 3839383631313138323832303039363539343531 TrackerData = TrackerMsgFormat.format(num=num, time=time_s, imei=imei, iccid=iccid, csq=csq, TH=(t + h), l=l, gps=gps_msg) return TrackerData
def main(): global g_connect_status, net, device, deviceSecret, deviceName, productKey, productSecret, device_dyn_resigter_succed #连接网络 connect_network() #获取设备的IMEI 作为deviceName 进行动态注册 deviceName = modem.getDevImei() #初始化物联网平台Device类,获取device实例 device = Device() if deviceName is not None and len(deviceName) > 0: #动态注册一个设备 dyn_register_device(productKey, productSecret, deviceName) else: print("can't get IMEI") while deviceSecret is None: time.sleep(0.2) print('dynamic successed:' + deviceSecret) key_info = { 'region': 'cn-shanghai', 'productKey': productKey, 'deviceName': deviceName, 'deviceSecret': deviceSecret, 'keepaliveSec': 60 } #打印设备信息 print(key_info) #device.ON_CONNECT 是事件,on_connect是事件处理函数/回调函数 device.on(device.ON_CONNECT, on_connect) device.on(device.ON_PROPS, on_props) device.on(device.ON_SERVICE, on_service) device.on(device.ON_ERROR, on_error) device.connect(key_info) write.do_write() _thread.start_new_thread(rfid_read, ())
def mqtt(): from umqtt_v2 import MQTTClient # IMEI+”_”+VER imei = modem.getDevImei() clientid = "{}_1".format(imei) pw = ds c = MQTTClient(clientid, "220.180.239.212", 8420, clientid, pw) c.set_callback(sub_cb) c.connect() sub_topic = "quec/{imei}/down" pub_topic = "quec/{imei}/up" c.subscribe(sub_topic)
def start_modem(): global bs_data_config try: system_log.info("Start to read modem info") modem_info = {} modem_info["imei"] = modem.getDevImei() modem_info["devModel"] = modem.getDevModel() modem_info["sn"] = modem.getDevSN() modem_info["fwVersion"] = modem.getDevFwVersion() modem_info["productId"] = modem.getDevProductId() bs_data_config.update_config('modem', modem_info) except Exception as err: system_log.error("Cannot get modem info, the error is {}".format(err))
def gngga(): while True: #获取当前 RTC 时间 time = utime.localtime() #获取设备 IMET imei = modem.getDevImei() if uart.any() > 0: buf = uart.read(uart.any()) buf = str(buf, "utf8") try: gngga1 = buf.split("$GNGGA,")[1].split("\r\n")[0].split(",") # UTC 时间,hhmmss.sss,时分秒格式 time_gps = gngga1[0] #纬度 ddmm.mmmm,度分格式前导位数不足则补 0 _latitude = float(gngga1[1]) #经度 dddmm.mmmm,度分格式前导位数不足则补 0 _longitude = float(gngga1[3]) # UTC 时间转化 _Clock = int(time[0:2]) _Minute = time[2:4] _Second = time[4:6] _Clock = _Clock + 8 #防止超过 24 小时 if (_Clock >= 24): _Clock = _Clock % 24 #最终获得时间 Effect_time = str(_clock) + ':' + _Minute + ':' + _Second #最终获得纬度 Effect_latitude = int(_latitude / 100) + ( (_latitude % 100) / 60) #最终获得经度 Effect_longitude = int(_longitude / 100) + ( (_longitude % 100) / 60) print('当前时间:', time) print('GPS 时间:', Effect_time) print('设备 IMET', imei) print(gngga1[2], '', str(Effect_latitude)) print(gngga1[4], '', str(Effect_longitude)) utime.sleep(2) except: print('数据格式有误或数据受损') continue
def get_imei(): return modem.getDevImei()