阿里云物联网loT实现树莓派4固件升级(OTA)

首先原理:
以MQTT协议+HTTP协议从阿里云获取固件升级

其次是资源:
Linkkit Core说明文档:https://www.yuque.com/aliyun_iot/product/c-sdk
Linkkit Core仓库地址:https://code.aliyun.com/linkkit/AiotCore-C-SDK.git

先把SDK下载下来

git clone https://code.aliyun.com/linkkit/AiotCore-C-SDK.git

下载后

tree

可见

├── demo
│   ├── bc35
│   │   ├── bc35_sys.c
│   │   ├── nwk_demo.c
│   │   ├── nwk_demo.h
│   │   └── README.md
│   └── linux
│       ├── bind_demo.c
│       ├── bind_reset_demo.c
│       ├── coap_client_demo.c
│       ├── coap_cloud_demo.c
│       ├── coap_server_demo.c
│       ├── dynreg_demo.c
│       ├── http_demo.c
│       ├── linux_sys.c
│       ├── linux_sys.h
│       ├── nwk_demo.c
│       ├── ota_demo.c
│       ├── preauth_demo.c
│       ├── pub_qos0_demo.c
│       ├── pub_qos1_demo.c
│       ├── security_nwk_demo.c
│       ├── sub_demo.c
│       ├── unsub_demo.c
│       ├── wifi_demo.c
│       ├── wifi_smartconfig_bind_demo.c
│       ├── wifi_softap_bind_demo.c
│       ├── wifi_softap_demo.c
│       ├── wifi_softap_dingtalk_demo.c
│       ├── wifi_softap_m2m_demo.c
│       └── wifi_softap_msgroute_demo.c
├── include
│   ├── aiot_bind_api.h
│   ├── aiot_coap_api.h
│   ├── aiot_coap_cloud_api.h
│   ├── aiot_errno.h
│   ├── aiot_log_api.h
│   ├── aiot_mqtt_api.h
│   ├── aiot_ota_api.h
│   ├── aiot_std.h
│   ├── aiot_sys_callback.h
│   └── aiot_wifi_api.h
├── lib
│   ├── aarch64-poky-linux-gcc-5.3.0
│   │   ├── libaiot_sdk.a
│   │   └── libaiot_sdk_wifi.a
│   ├── android
│   │   └── android-ndk-r16b
│   │       └── arm-linux-androideabi-gcc-4.9.x
│   │           ├── libaiot_sdk.a
│   │           └── libaiot_sdk_wifi.a
│   ├── arm-linux-gcc-5.4.0
│   │   ├── libaiot_sdk.a
│   │   └── libaiot_sdk_wifi.a
│   ├── arm-xm-linux-gcc-4.9.2
│   │   ├── libaiot_sdk.a
│   │   └── libaiot_sdk_wifi.a
│   ├── BC35G
│   │   ├── libaiot_sdk.a
│   │   └── libaiot_sdk_wifi.a
│   ├── gcc-linaro-arm-linux-gnueabihf-raspbian
│   │   ├── libaiot_sdk.a
│   │   └── libaiot_sdk_wifi.a
│   ├── ia32-ubuntu-gcc-5.5.0
│   │   ├── libaiot_sdk.a
│   │   └── libaiot_sdk_wifi.a
│   └── ia64-ubuntu-gcc-5.5.0
│       ├── libaiot_sdk.a
│       └── libaiot_sdk_wifi.a
├── nwk-demo
├── ota-demo
├── README.md
└── user_manual.zip

除了最下面两个demo是我自己编译的执行文件,其他应该是都有的。

其中

gcc-linaro-arm-linux-gnueabihf-raspbian

这个是官方人员编译的针对树莓派和linux做的编译库,直接用这个就可以,下面这个也行。我这里就直接用上一个。

arm-linux-gcc-5.4.0

ota_demo.c就是OTA的demo文件,有了库,直接编译demo文件就可以用了,在此之前需要改下demo文件里的三元组(自行百度)。打开ota_demo.c文件,可见

char *product_key       = "shfaufdasnui";
char *product_secret    = "rgfh6kjanksfamhifniasoif";
char *device_name       = "egfwrehh";
char *device_secret     = "fewgrethryyjtukukuyukykuyukyu";

其中第二段不要管,另外3个就是三元组的内容(我写的是乱码),然后查看阿里云上注册的设备。
阿里云物联网loT实现树莓派4固件升级(OTA)_第1张图片
设备注册自己百度,有需求留言看需求再上传。
把实际内容复制到程序里。

然后就是编译文件

gcc -o ota-demo demo/linux/ota_demo.c demo/linux/linux_sys.c lib/gcc-linaro-arm-linux-gnueabihf-raspbian/libaiot_sdk.a -lpthread -Iinclude

运行

./ota-demo

可见MQTT连接成功

[_linux_nwk_tcp_establish:142] establish tcp connection with server(host='a1yP0YEzEId.iot-as-mqtt.cn-shanghai.aliyuncs.com', port=[1883])
[_linux_nwk_tcp_establish:177] success to establish tcp, fd=3
[SDK][_mqtt_sublist_insert:672]topic:/ota/device/upgrade/a1yP0YEzEId/example1, data packet handle insert
提示:做完这一步阿里云上设备应该是在线的。

然后就是再阿里云上操作上传固件

阿里云物联网loT实现树莓派4固件升级(OTA)_第2张图片
选择新增固件然后按图填写阿里云物联网loT实现树莓派4固件升级(OTA)_第3张图片
都会有对应提示,固件有规定格式,我这里把源文件改成.bin作为固件上传了。
确定后,验证固件
在这里插入图片描述
点击批量升级按提示和需要填写
阿里云物联网loT实现树莓派4固件升级(OTA)_第4张图片
确定,树莓派端可见

receive new firmware
url        : https://iotx-ota.oss-cn-shanghai.aliyuncs.com/ota/93612ac9cf19d72cbd01512c23e14e95/ck7aab13200013m7kjgl1y49z.bin?Expires=1583230211&OSSAccessKeyId=cS8uRRy54RszYWna&Signature=fZUu9rpqJe1HSPgj0EYCmkwf%2Faw%3D
size       : 7461
version    : 2.0.0
sign method: Md5
sign       : 0c97865f44cfe62ee24197f8e0ef8cb0
[_linux_nwk_tcp_establish:142] establish tcp connection with server(host='iotx-ota.oss-cn-shanghai.aliyuncs.com', port=[80])
[_linux_nwk_tcp_establish:177] success to establish tcp, fd=4
[SDK][aiot_http_send:318]http request header: 
[SDK][aiot_http_send:319]GET /ota/93612ac9cf19d72cbd01512c23e14e95/ck7aab13200013m7kjgl1y49z.bin?Expires=1583230211&OSSAccessKeyId=cS8uRRy54RszYWna&Signature=fZUu9rpqJe1HSPgj0EYCmkwf%2Faw%3D HTTP/1.1
Host: iotx-ota.oss-cn-shanghai.aliyuncs.com
Range: bytes=0-
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding: gzip, deflate

[SDK][aiot_ota_download:431]firmware download started
ota download start
[SDK][aiot_http_recv:413]HTTP/1.1 206 Partial Content
[SDK][aiot_http_recv:413]Server: AliyunOSS
[SDK][aiot_http_recv:413]Date: Mon, 02 Mar 2020 10:10:11 GMT
[SDK][aiot_http_recv:413]Content-Type: application/octet-stream
[SDK][aiot_http_recv:413]Content-Length: 7461
[SDK][aiot_http_recv:413]Connection: keep-alive
[SDK][aiot_http_recv:413]x-oss-request-id: 5E5CDB8332BE4E38328B97EF
[SDK][aiot_http_recv:413]Content-Range: bytes 0-7460/7461
[SDK][aiot_http_recv:413]Accept-Ranges: bytes
[SDK][aiot_http_recv:413]ETag: "0C97865F44CFE62EE24197F8E0EF8CB0"
[SDK][aiot_http_recv:413]Last-Modified: Mon, 02 Mar 2020 09:52:11 GMT
[SDK][aiot_http_recv:413]x-oss-object-type: Normal
[SDK][aiot_http_recv:413]x-oss-hash-crc64ecma: 273697563426459874
[SDK][aiot_http_recv:413]x-oss-storage-class: Standard
[SDK][aiot_http_recv:413]Content-MD5: DJeGX0TP5i7iQZf44O+MsA==
[SDK][aiot_http_recv:413]x-oss-server-time: 20
download progress: 10%
download progress: 20%
download progress: 30%
download progress: 40%
download progress: 50%
download progress: 60%
download progress: 80%
[SDK][aiot_ota_download:461]firmware download succeed
download progress: 100%
[SDK][aiot_ota_download:490]verify md5 sign: 0c97865f44cfe62ee24197f8e0ef8cb0
[SDK][aiot_ota_download:520]firmware verify succeed
firmware verify success
[SDK][aiot_ota_download:530]firmware download stop
firmware download stop

上传完毕,固件地址

https://iotx-ota.oss-cn-shanghai.aliyuncs.com/ota/93612ac9cf19d72cbd01512c23e14e95/ck7aab13200013m7kjgl1y49z.bin?Expires=1583230211&OSSAccessKeyId=cS8uRRy54RszYWna&Signature=fZUu9rpqJe1HSPgj0EYCmkwf%2Faw%3D

打开就可以看到是刚上传的固件,需要http协议下载下来,然后部署,这就是另外一个问题了。

你可能感兴趣的:(物联网)