开始前 先强烈建议 登录控制台走一遍亚马逊的引导流程,并用nodejs版 在你ubuntu上跑一边,把策略文件生成了。并了解如何创建事物(设备)
文档:https://docs.aws.amazon.com/iot/latest/developerguide/iot-gs.html
当前版本:
aws-iot-device-sdk-cpp: Release version 1.3.0
Openssl 版本: 1.0.2d
gcc:4.8.3(需支持c++11)
项目AWS IOT C++SDK git:
https://github.com/aws/aws-iot-device-sdk-cpp/tree/release
https://github.com/aws/aws-iot-device-sdk-cpp/tree/release
vi ToolchainFile.cmake
2.1修改TOOLCHAIN_DIR(交叉工具链目录)
2.2修改TARGET_CROSS(交叉工具链前缀)
3.1 指定安装目录
./config no-asmshared–prefix= dir
说明:
no-asm 在交叉编译过程中不使用汇编代码代码加速编译过程。
shared 生成动态链接库。
./config no-asmshared –prefix=/home/xx.xx/code/openssl
目录参数是–
3.2 修改openssl源码目录下的makefile文件。
(1)在Makefile中删除-m64(两处)
(2)在makefile中修改编译器
CC=/home/xx.xx/work/toolchain-arm_cortex-a7+vfp_gcc-4.8-linaro_uClibc-0.9.33.2_eabi/bin/arm-openwrt-linux-gcc
AR= /home/xx.xx/work/toolchain-arm_cortex-a7+vfp_gcc-4.8-linaro_uClibc-0.9.33.2_eabi/bin/arm-openwrt-linux-ar $(ARFLAGS) r
RANLIB= /home/jw.li/work/toolchain-arm_cortex-a7+vfp_gcc-4.8-linaro_uClibc-0.9.33.2_eabi/bin/arm-openwrt-linux-ranlib
/home/xx.xx/code/pcre/bin/pcretest
/home/xx.xx/work/toolchain-arm_cortex-a7+vfp_gcc-4.8-linaro_uClibc-0.9.33.2_eabi/bin/arm-openwrt-linux-g++
3.3 Make & make install
vi network/CMakeLists.txt.in 修改OpenSSL库位置并将其更改为交叉编译的OpenSSL
前面屏蔽掉find_package(OpenSSL REQUIRED)
然后打开屏蔽
#set(OPENSSL_INCLUDE_DIR "YOUR_OPENSSL_PATH/include")
#set(OPENSSL_LIBRARIES "YOUR_OPENSSL_PATH/lib/libssl.a;YOUR_OPENSSL_PATH/lib/libcrypto.a")
并修改openssl位置和lib
set(OPENSSL_INCLUDE_DIR "/home/xx.xx/code/save/v50openwrt/openssl/include")
set(OPENSSL_LIBRARIES "/home/xx.xx/code/save/v50openwrt/openssl/lib/libssl.a;/home/xx.xx/code/save/v50openwrt/openssl/lib/libcrypto.a")
5.1 在IOT控制台点击管理 事物 选择你刚才创建的设备
5.2 在后台选择安全性,点击创建证书
5.3得到3个证书和根证书
PS: 重点一定要点激活 点激活 点激活
5.4 将证书拷贝到sdk 根目录下的certs/下(即aws-iot-device-sdk-cpp/certs)
Mkdir build
Cd build
cmake ../. -DCMAKE_TOOLCHAIN_FILE=../ToolchainFile.cmakemake
6.1 在gcc 4.8.3中出现这个错误
to_string' was not declared in this scope
string f_str = to_string(f);
交叉编译链 Gcc 4.8.3 加上-D_GLIBCXX_USE_C99=1才能使用to_string
修改vi ToolchainFile.cmake 在g++ 设置后加上-D_GLIBCXX_USE_C99=1
SET(CMAKE_CXX_COMPILER TOOLCHAINDIR/ {TARGET_CROSS}g++ -D_GLIBCXX_USE_C99=1)
6.2报错
error: ‘SIGPIPE’ was not declared in this scope
signal(SIGPIPE, SIG_IGN);
解决修改头文件
vi ../network/OpenSSL/OpenSSLConnection.cpp
在 #ifdef WIN32 #else分支内加入
#include
在archive下可以找到生成的lib ,在bin下可以找到生成bin, 在bin下可以找到cert 和config文件。
aws-iot-cplus-sdk提供了测试程序
8.1 在bulid/bin/config/SampleConfig.json
内容如下:
{
"endpoint": "",
"mqtt_port": 8883,
"https_port": 443,
"greengrass_discovery_port": 8443,
"root_ca_relative_path": "certs/rootCA.crt",
"device_certificate_relative_path": "",
"device_private_key_relative_path": "",
"tls_handshake_timeout_msecs": 60000,
"tls_read_timeout_msecs": 2000,
"tls_write_timeout_msecs": 2000,
"aws_region": "",
"aws_access_key_id": "",
"aws_secret_access_key": "",
"aws_session_token": "",
"client_id": "",
"thing_name": "",
"is_clean_session": true,
"mqtt_command_timeout_msecs": 20000,
"keepalive_interval_secs": 600,
"minimum_reconnect_interval_secs": 1,
"maximum_reconnect_interval_secs": 128,
"maximum_acks_to_wait_for": 32,
"action_processing_rate_hz": 5,
"maximum_outgoing_action_queue_length": 32,
"discover_action_timeout_msecs": 300000
}
有5个参数需要自己填
8.2 这样直接连接mqtt 会报错如下:
MQTT Connect failed. The MQTT request timed out : SDK Code -703.
控制台-安全-证书 选择你刚刚生成的设备证书
把引导教程里面生成事物的策略加进去,应该就可以连上了。
{
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"iot:Publish",
"iot:Subscribe",
"iot:Connect",
"iot:Receive"
],
"Effect": "Allow",
"Resource": [
"*"
]
}
]
}
8.3 把你交叉编译生成的bin下的pub-sub-sample config和certs 复制到嵌入式平台同一目录下执行。
我们开始测试./pub-sub-sample