升级EVB_AIoT快速入门指南中mqtt_config_gen.py工具脚本,使用预定义常量到mqtt连接代码中

This commit is contained in:
HonestQiao
2021-12-27 14:10:50 +08:00
parent ebf7db4948
commit 6a924fc902
2 changed files with 73 additions and 12 deletions

View File

@@ -483,7 +483,7 @@ username = username.encode("utf-8")
passwd = passwd.encode("utf-8") passwd = passwd.encode("utf-8")
sign = hmacsha1(username, passwd) sign = hmacsha1(username, passwd)
template = ('#ifndef TOS_MQTT_CONFIG_H\n' template_header = ('#ifndef TOS_MQTT_CONFIG_H\n'
'#define TOS_MQTT_CONFIG_H\n' '#define TOS_MQTT_CONFIG_H\n'
'\n' '\n'
'#define MQTT_SERVER_IP "111.230.189.156"\n' '#define MQTT_SERVER_IP "111.230.189.156"\n'
@@ -496,26 +496,55 @@ template = ('#ifndef TOS_MQTT_CONFIG_H\n'
'#define MQTT_SUBSCRIBE_TOPIC "$product/$dev/$sub"\n' '#define MQTT_SUBSCRIBE_TOPIC "$product/$dev/$sub"\n'
'#define MQTT_PUBLISH_TOPIC "$product/$dev/$pub"\n' '#define MQTT_PUBLISH_TOPIC "$product/$dev/$pub"\n'
'\n' '\n'
'#define MQTT_SERVER_DOMAIN "$product.iotcloud.tencentdevices.com"\n'
'#define MQTT_TOPIC "$product/$dev"\n'
'#define MQTT_SUBSCRIBE_TOPIC_DOWN "$thing/down/property/$product/$dev"\n'
'#define MQTT_PUBLISH_TOPIC_UP "$thing/up/property/$product/$dev"\n'
'\n'
'#endif\n' '#endif\n'
'\n') '\n')
src = Template(template) template_c = ('#ifndef TOS_MQTT_CONFIG_H\n'
'tos_sal_module_parse_domain(MQTT_SERVER_DOMAIN,host_ip,sizeof(host_ip));\n'
'\n'
'mqtt_set_port(client, MQTT_SERVER_PORT);\n'
'mqtt_set_host(client, host_ip);\n'
'mqtt_set_client_id(client, MQTT_CLIENT_ID);\n'
'mqtt_set_user_name(client, MQTT_USR_NAME);\n'
'mqtt_set_password(client, MQTT_PASSWORD);\n'
'mqtt_set_clean_session(client, 1);\n'
'\n'
'error = mqtt_subscribe(client, MQTT_SUBSCRIBE_TOPIC_DOWN, QOS0, tos_topic_handler);\n'
'\n'
'error = mqtt_publish(client, MQTT_PUBLISH_TOPIC_UP, &msg);\n'
'\n'
'#endif\n'
'\n')
src_header = Template(template_header)
src_c = Template(template_c)
d = { d = {
'product':product_id, 'product':product_id,
'dev':dev_name, 'dev':dev_name,
'sign':sign, 'sign':sign,
'sub':sub, 'sub':sub,
'pub':pub 'pub':pub,
'thing':'$thing'
} }
#do the substitution #do the substitution
dst = src.substitute(d) dst_header = src_header.substitute(d)
dst_c = src_c.substitute(d)
print("===============Generate mqtt_config.h==================") print("===============Generate mqtt_config.h==================")
print(dst) print(dst_header)
with open('mqtt_config.h', 'w') as f: with open('mqtt_config.h', 'w') as f:
f.write(dst) f.write(dst_header)
print("===============Generate mqtt_connect_demo.c==================")
print(dst_c)
with open('mqtt_connect_demo.c', 'w') as f:
f.write(dst_c)
``` ```
@@ -528,6 +557,8 @@ with open('mqtt_config.h', 'w') as f:
![](image/EVB_AIoT_guide/cloud_016.png) ![](image/EVB_AIoT_guide/cloud_016.png)
最新版本的mqtt_config_gen.py将同时生成mqtt_config.h、mqtt_connect_demo.c文件引入mqtt_config.h后可直接复制mqtt_connect_demo.c中的示例代码到mqttclient_task函数之中对应的部分。
#### 4.2.3 编译工程 #### 4.2.3 编译工程
工程上右键,选择构建项目,编译整个工程: 工程上右键,选择构建项目,编译整个工程:

View File

@@ -37,7 +37,7 @@ username = username.encode("utf-8")
passwd = passwd.encode("utf-8") passwd = passwd.encode("utf-8")
sign = hmacsha1(username, passwd) sign = hmacsha1(username, passwd)
template = ('#ifndef TOS_MQTT_CONFIG_H\n' template_header = ('#ifndef TOS_MQTT_CONFIG_H\n'
'#define TOS_MQTT_CONFIG_H\n' '#define TOS_MQTT_CONFIG_H\n'
'\n' '\n'
'#define MQTT_SERVER_IP "111.230.189.156"\n' '#define MQTT_SERVER_IP "111.230.189.156"\n'
@@ -50,22 +50,52 @@ template = ('#ifndef TOS_MQTT_CONFIG_H\n'
'#define MQTT_SUBSCRIBE_TOPIC "$product/$dev/$sub"\n' '#define MQTT_SUBSCRIBE_TOPIC "$product/$dev/$sub"\n'
'#define MQTT_PUBLISH_TOPIC "$product/$dev/$pub"\n' '#define MQTT_PUBLISH_TOPIC "$product/$dev/$pub"\n'
'\n' '\n'
'#define MQTT_SERVER_DOMAIN "$product.iotcloud.tencentdevices.com"\n'
'#define MQTT_TOPIC "$product/$dev"\n'
'#define MQTT_SUBSCRIBE_TOPIC_DOWN "$thing/down/property/$product/$dev"\n'
'#define MQTT_PUBLISH_TOPIC_UP "$thing/up/property/$product/$dev"\n'
'\n'
'#endif\n' '#endif\n'
'\n') '\n')
src = Template(template) template_c = ('#ifndef TOS_MQTT_CONFIG_H\n'
'tos_sal_module_parse_domain(MQTT_SERVER_DOMAIN,host_ip,sizeof(host_ip));\n'
'\n'
'mqtt_set_port(client, MQTT_SERVER_PORT);\n'
'mqtt_set_host(client, host_ip);\n'
'mqtt_set_client_id(client, MQTT_CLIENT_ID);\n'
'mqtt_set_user_name(client, MQTT_USR_NAME);\n'
'mqtt_set_password(client, MQTT_PASSWORD);\n'
'mqtt_set_clean_session(client, 1);\n'
'\n'
'error = mqtt_subscribe(client, MQTT_SUBSCRIBE_TOPIC_DOWN, QOS0, tos_topic_handler);\n'
'\n'
'error = mqtt_publish(client, MQTT_PUBLISH_TOPIC_UP, &msg);\n'
'\n'
'#endif\n'
'\n')
src_header = Template(template_header)
src_c = Template(template_c)
d = { d = {
'product':product_id, 'product':product_id,
'dev':dev_name, 'dev':dev_name,
'sign':sign, 'sign':sign,
'sub':sub, 'sub':sub,
'pub':pub 'pub':pub,
'thing':'$thing'
} }
#do the substitution #do the substitution
dst = src.substitute(d) dst_header = src_header.substitute(d)
dst_c = src_c.substitute(d)
print("===============Generate mqtt_config.h==================") print("===============Generate mqtt_config.h==================")
print(dst) print(dst_header)
with open('mqtt_config.h', 'w') as f: with open('mqtt_config.h', 'w') as f:
f.write(dst) f.write(dst_header)
print("===============Generate mqtt_connect_demo.c==================")
print(dst_c)
with open('mqtt_connect_demo.c', 'w') as f:
f.write(dst_c)