• haas506 2.0开发教程 - 阿里云ota - pac 固件升级(仅支持2.2以上版本)


    ota - pac固件升级

    案例说明

    • 本案例用于升级开发板固件版本,例如:将haas506 - 2.0版本升级到2.2版本。
    • 案例通过阿里云OTA升级,上传 < .bin > 格式文件,实现远程硬件更新。
    • 注意:升级前需要确认开发板内存充足。

    1.确定开发板内存

    1、查看开发板内存

    • 打开设备管理器,找到USB port 0对应COM口,
      在这里插入图片描述

    • 用串口工具打开串口,输入指令 ’ AT+FSLSTPART ’ ,查看内存大小

      1、显示的数字为字节大小,例如剩余空间 924500 换算为内存大小  924500/1024  →  902.83 KB
      2、剩余空间要大于硬件升级包,否则会导致升级失败
      
      • 1
      • 2

    在这里插入图片描述

    • 如果内存不足使用指令’ AT+FSLSTFILE=“/data/pyamp” '查看内部文件;
    • 选择多余的文件使用指令’
      AT+FSDELFILE=“文件路径” '删除。例如( AT+FSDELFILE=“/data/pyamp/abc.zip”)
      注意:删除内容请谨慎,删除确定可以删除的。

    在这里插入图片描述

    2.物联网平台开发

    第一次使用物联网平台的读者,需要开通实例后使用物联网平台功能。也可以使用免费的公共实例进行开发,在阿里云物联网平台中,左上角选择‘华东2-上海’,点击‘公共实例’,即可开通。

    1、平台产品创建可参考haas506 2.0开发教程-aliyunIoT

    3、设备端开发

    • 第一次使用开发板的读者可以按照haas5062.0开发教程-导学篇搭建开发环境。
    • 搭建完后复制以下代码到Visual Studio Code,复制产品证书到代码相应位置。
      在这里插入图片描述

    main.py

    # coding=utf-8
    
    from driver import GPIO
    import network
    import ujson
    import utime as time
    import modem
    from modem import info as infos
    import modem 
    from  aliyunIoT import Device
    import ota
    import kv
    
    # 定义升级包的下载和安装路径,其中url,hash_type和hash 会通过服务端推送被保存下来
    info = {
        'url': '',
        'store_path': '/data/pyamp/app.zip',
        'install_path': '/data/pyamp/',
        'length': 0,
        'hash_type': '',
        'hash': ''
    }
    
     # ota 消息推送的接受函数
    def on_trigger(data):
        global info
     # 保存服务端推送的ota信息
        info['url'] = data['url']
        info['length'] = data['length']
        info['module_name'] = data['module_name']
        info['version'] = data['version']
        info['hash'] = data['hash']
        info['hash_type'] = data['hash_type']
        # 开始ota 包下载
        dl_data = {}
        dl_data['url'] = info['url']
        dl_data['store_path'] = info['store_path']
        ota.download(dl_data)
    
    
     # ota 升级包下载结果回调函数
    def on_download(data):
        global info
        if data >= 0:
            print('Ota download succeed')
         # 开始ota包校验
            param = {}
            param['length'] = info['length']
            param['store_path'] = info['store_path']
            param['hash_type'] = info['hash_type']
            param['hash'] = info['hash']
            ota.verify(param)
    
     # ota 升级包校验结果回调函数
    def on_verify(data):
        global info
        print(data)
        if data >= 0 :
            print('Ota verify succeed')
            print('Start Upgrade')
         # 开始ota升级
            param = {}
            param['length'] = info['length']
            param['store_path'] = info['store_path']
            param['install_path'] = info['install_path']
            ota.upgrade(param)
    
     # ota 升级包结果回调函数
    def on_upgrade(data):
        if data >= 0 :
            print('Ota succeed')
            #ota升完级后 重启设备
            reboot()
     
    #当iot设备连接到物联网平台的时候触发'connect' 事件
    def on_connect(data):
        global module_name,default_ver,productKey,deviceName,deviceSecret,on_trigger,on_download,on_verify,on_upgrade
        print('***** connect lp succeed****')
        data_handle = {}
        data_handle['device_handle'] = device.getDeviceHandle()
    
     # 初始化ota服务
        ota.init(data_handle)
    
     # ota 回调函数注册
        ota.on(1,on_trigger)
        ota.on(2,on_download)
        ota.on(3,on_verify)
        ota.on(4,on_upgrade)  
    
    #当连接断开时,触发'disconnect'事件
    def on_disconnect():
        print('linkkit is disconnected')
    
    #当iot云端下发属性设置时,触发'props'事件
    def on_props(request):
        print('clound req data is {}'.format(request))
    
    #当iot云端调用设备service时,触发'service'事件
    def on_service(id,request):
        print('clound req id  is {} , req is {}'.format(id,request))
    #当设备跟iot平台通信过程中遇到错误时,触发'error'事件
    def on_error(err):
        print('err msg is {} '.format(err))
    
    #网络连接的回调函数
    def on_4g_cb(args):
        global g_connect_status
        pdp = args[0]
        netwk_sta = args[1]
        if netwk_sta == 1:
            g_connect_status = True
        else:
            g_connect_status = False
    
    #网络连接
    def connect_network():
        global net,on_4g_cb,g_connect_status
         #NetWorkClient该类是一个单例类,实现网络管理相关的功能,包括初始化,联网,状态信息等.
        net = network.NetWorkClient()
        g_register_network = False
        if net._stagecode is not None and net._stagecode == 3 and net._subcode == 1:
            g_register_network = True
        else:
            g_register_network = False
        if g_register_network:
        #注册网络连接的回调函数on(self,id,func);  1代表连接,func 回调函数  ;return 0 成功
            net.on(1,on_4g_cb)    
            net.connect(None)
            net.connect({
                            'username' : '' ,
                            'password' : '',
                            'profileidx' :1,
                            'ipType' : 0,
                            'apn' :"CMNET",
                            'authType' : 1
                        })
        else:
            print('network reg failed')
        while True:
            if g_connect_status:
                print('network connect success')
                break
            time.sleep_ms(20)
    
    #动态注册回调函数
    def on_dynreg_cb(data):
        global deviceSecret,device_dyn_resigter_succed
        deviceSecret = data
        device_dyn_resigter_succed = True
    
     # 连接物联网平台
    def dyn_register_device(productKey,productSecret,deviceName):
        global on_dynreg_cb,device,deviceSecret,device_dyn_resigter_succed
        key = '_amp_customer_devicesecret'
        deviceSecretdict = kv.get(key)
        print("deviceSecretdict:",deviceSecretdict)
        if isinstance(deviceSecretdict,str):    
            deviceSecret = deviceSecretdict 
    
        if deviceSecretdict is None or deviceSecret is None:
            key_info = {
                'productKey': productKey  ,
                'productSecret': productSecret ,
                'deviceName': deviceName
                }
            # 动态注册一个设备,获取设备的deviceSecret
            #下面的if防止多次注册,当前若是注册过一次了,重启设备再次注册就会卡住,
            if not device_dyn_resigter_succed:
                device.register(key_info,on_dynreg_cb)   
    
    if __name__ == '__main__':
        ICCID=None
        g_connect_status = False
        net = None
        device = None
        deviceSecret = None
        deviceName = None
        #更改 productKey 和 productSecret
        ##############################
        productKey = "a1laDtv9VrO"
        productSecret = "bPbyllJ80mRX5PPy"
        ##############################
        device_dyn_resigter_succed = False
        # 定义需要升级的模块和版本号
        module_name = 'default'
        default_ver = 'app-1.0.1'
    
        # 连接网络
        connect_network()
         # 获取设备的IMEI 作为deviceName 进行动态注册
        deviceName = infos.getDevImei()
        #获取设备的ICCID
        ICCID=modem.sim.getIccid()
        #初始化物联网平台Device类,获取device实例
        device = Device()
        if deviceName is not None and len(deviceName) > 0 :
         #动态注册一个设备
            dyn_register_device(productKey,productSecret,deviceName)
        else:
            print("can not dynamic reg")
        while deviceSecret is None:
            time.sleep(0.2)
        print('dynamic reg success:' + 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_DISCONNECT,on_disconnect)
        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)
    
    
        for num in range(1, 5):
            report_info = {
                "device_handle":  device.getDeviceHandle(),
                "product_key": productKey,
                "device_name": deviceName,
                "module_name": 'default',
                "version": default_ver
            }
            ret = ota.report(report_info)
            time.sleep(1)
    
        sys_version=infos.getDevFwVersion()
     
    
        while True:
            # print('等待Ota升级包.....')
            print('app_version:{} ;sys_version:{}'.format(default_ver,sys_version))
            time.sleep_ms(1000)
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243

    board.json

    
    {
        "name": "haas506",
        "version": "1.0.0",
        "io": {
            "cloud_led":{
              "type":"GPIO",
              "port": 1,
              "dir": "output",
              "pull":"pulldown"
            },    
          "serial1":{
            "type":"UART",
            "port":0,
            "dataWidth":8,
            "baudRate":115200,
            "stopBits":1,
            "flowControl":"disable",
            "parity":"none"
          },
          "serial2":{
            "type":"UART",
            "port":1,
            "dataWidth":8,
            "baudRate":115200,
            "stopBits":1,
            "flowControl":"disable",
            "parity":"none"
          },
          "serial3":{
            "type":"UART",
            "port":2,
            "dataWidth":8,
            "baudRate":115200,
            "stopBits":1,
            "flowControl":"disable",
            "parity":"none"
          }
        },
        "debugLevel": "ERROR"
      }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

    调试,确定开发板驻网成功

    在这里插入图片描述

    4.ota - 硬件升级

    1、平台端找到 监控运维→OTA升级→添加升级包,填写数据,点击确定
    在这里插入图片描述

    • 没有模块可以进行添加
      在这里插入图片描述
      2、点击 验证 → 选择需要升级的设备。
      在这里插入图片描述
      3、点击 查看,等待升级完成在这里插入图片描述
      在这里插入图片描述
      4、升级完成后开发板会重启,串口工具会打印相应的log,版本变为2.03
      在这里插入图片描述
  • 相关阅读:
    一文了解 Go 标准库 strings 常用函数和方法
    【RocketMQ】Dledger日志复制源码分析
    VBA技术资料MF50:VBA_在Excel中突出显示前3个值
    VEX —— Quaternion|Euler Angle
    水体中磷赋存形态
    简单介绍一下c++正则表达式
    php7.3 centos7.9安装sqlserver扩展
    Win10系统下提示“系统组策略禁止安装此设备”的解决方案(家庭版无组策略)
    算法基础课——第一章 基础算法(二)
    pmp新考纲全真模拟题,提分敏捷+情景!
  • 原文地址:https://blog.csdn.net/w_hizyf_m/article/details/125621189