From 88a0dba02f48eb9132f33625df6616c06b2a79ad Mon Sep 17 00:00:00 2001 From: he1chenglong Date: Wed, 3 Mar 2021 14:35:41 +0800 Subject: [PATCH 1/3] add mqtt to uart demo application (#1287) Signed-off-by: liangzeng.glz Co-authored-by: liangzeng.glz --- application/example/Config.in | 6 + application/example/mqtt_uart/Config.in | 38 ++ application/example/mqtt_uart/README.md | 64 ++++ application/example/mqtt_uart/aos.mk | 28 ++ application/example/mqtt_uart/app_entry.c | 161 +++++++++ application/example/mqtt_uart/app_entry.h | 16 + application/example/mqtt_uart/autobuild.json | 32 ++ application/example/mqtt_uart/k_app_config.h | 7 + application/example/mqtt_uart/maintask.c | 62 ++++ application/example/mqtt_uart/mqtt_uart.c | 347 +++++++++++++++++++ 10 files changed, 761 insertions(+) create mode 100644 application/example/mqtt_uart/Config.in create mode 100644 application/example/mqtt_uart/README.md create mode 100644 application/example/mqtt_uart/aos.mk create mode 100644 application/example/mqtt_uart/app_entry.c create mode 100644 application/example/mqtt_uart/app_entry.h create mode 100644 application/example/mqtt_uart/autobuild.json create mode 100644 application/example/mqtt_uart/k_app_config.h create mode 100644 application/example/mqtt_uart/maintask.c create mode 100644 application/example/mqtt_uart/mqtt_uart.c diff --git a/application/example/Config.in b/application/example/Config.in index d72cdec26d..151a3ca137 100644 --- a/application/example/Config.in +++ b/application/example/Config.in @@ -161,6 +161,12 @@ if AOS_APP_RFID_DEMO default "rfid_demo" endif +source "application/example/mqtt_uart/Config.in" +if AOS_APP_MQTT_UART + config AOS_BUILD_APP + default "mqtt_uart" +endif + source "application/example/auto_demo/Config.in" if AOS_APP_AUTO_DEMO config AOS_BUILD_APP diff --git a/application/example/mqtt_uart/Config.in b/application/example/mqtt_uart/Config.in new file mode 100644 index 0000000000..4997831f74 --- /dev/null +++ b/application/example/mqtt_uart/Config.in @@ -0,0 +1,38 @@ +config AOS_APP_MQTT_UART + bool "MQTT Demo" + select AOS_COMP_NETMGR if !AOS_CREATE_PROJECT + select AOS_COMP_CLI if !AOS_CREATE_PROJECT + select AOS_COMP_CJSON if !AOS_CREATE_PROJECT + select AOS_COMP_SDK_MQTT if !AOS_CREATE_PROJECT + select AOS_COMP_UND if !AOS_CREATE_PROJECT + help + mqtt examples + +if AOS_APP_MQTT_UART +# Configurations for app mqtt_demo + +choice + prompt "Select Case" + default MQTTDEMO_CONFIG_CASE_DEFAULT + help + select case to build + + config MQTTDEMO_CONFIG_CASE_DEFAULT + bool "Default" + help + default case, build mqtt_uart.c + +endchoice + +config MQTTAPP_CONFIG_TEST_LOOP + bool "Test Loop" + default y + help + n: run the full life cycle; y:do publish endless + +config SYSINFO_APP_VERSION + string "Firmware Version" + default "app-1.0.0-20200214.140831" + help + application main firmware version +endif diff --git a/application/example/mqtt_uart/README.md b/application/example/mqtt_uart/README.md new file mode 100644 index 0000000000..3911710f4a --- /dev/null +++ b/application/example/mqtt_uart/README.md @@ -0,0 +1,64 @@ +## Contents + +```sh +. +├── aos.mk +├── app_entry.c +├── app_entry.h +├── Config.in +├── k_app_config.h +├── mqtt_uart.c +└── README.md +``` + +## Introduction + +本应用可以对接各种uart接口的外部设备,将数据解析工作放到了阿里云IOT平台上。 +代码中实现了mqtt和uart协议之间的转换,可以实现设备uart和阿里云IOT平台之间的数据透传。 +当设备uart上收到数据后,通过mqtt协议将数据上传到阿里云IOT平台,用于物模型数据解析。 +当设备接收到阿里云IOT平台通过mqtt协议下发的数据后,通过uart输出给外部设备。 + +### Dependencies + +* yloop +* cli +* linkkit + +### Supported Boards + +- haas100 +- mk3060 +- mk3080 +- esp8266 +- esp32devkitc +- uno-91h +- bk7231 +- stm32f429zi-nucleo + +### Build + +```sh + +aos make mqtt_uart@haas100 -c config + +# or customize config manually +aos make menuconfig + +# build +aos make +``` + +1).mqtt_uart.c (default): + +```sh + aos make +``` + +> if you want to see AliOS-Things supports boards, click [board](../../../board). + + +## Reference + +* [Quick-Start](https://github.com/alibaba/AliOS-Things/wiki/Quick-Start) +* [gateway](https://code.aliyun.com/edward.yangx/public-docs/wikis/user-guide/linkkit/Prog_Guide/MQTT_Connect) +* [物模型数据解析](https://help.aliyun.com/document_detail/68702.html?spm=a2c4g.11186623.6.699.497c678aO5ZCmG) diff --git a/application/example/mqtt_uart/aos.mk b/application/example/mqtt_uart/aos.mk new file mode 100644 index 0000000000..4926907564 --- /dev/null +++ b/application/example/mqtt_uart/aos.mk @@ -0,0 +1,28 @@ +NAME := mqtt_uart + +$(NAME)_MBINS_TYPE := app +$(NAME)_VERSION := 1.0.2 +$(NAME)_SUMMARY := mqtt examples + +$(NAME)_SOURCES := app_entry.c maintask.c +$(NAME)_COMPONENTS := netmgr cjson libiot_mqtt + +ifneq ($(HOST_MCU_FAMILY),mcu_esp8266) +$(NAME)_COMPONENTS += cli +GLOBAL_DEFINES += CLI_CONFIG_STACK_SIZE=3072 +endif + +ifeq ($(AOS_COMP_UND),y) +$(NAME)_COMPONENTS += und +endif + +ifeq ($(MQTTDEMO_CONFIG_CASE_DEFAULT),y) +$(NAME)_SOURCES += mqtt_uart.c +endif + +ifeq ($(MQTTAPP_CONFIG_TEST_LOOP),y) +$(NAME)_DEFINES += TEST_LOOP +endif + +$(NAME)_INCLUDES += ./ + diff --git a/application/example/mqtt_uart/app_entry.c b/application/example/mqtt_uart/app_entry.c new file mode 100644 index 0000000000..97e6a9b75d --- /dev/null +++ b/application/example/mqtt_uart/app_entry.c @@ -0,0 +1,161 @@ +/* + * Copyright (C) 2015-2018 Alibaba Group Holding Limited + */ + +#include +#include +#include +#include + +#include "aos/kernel.h" +#include "ulog/ulog.h" +#include "aos/yloop.h" +#include "linkkit/infra/infra_compat.h" +#include "netmgr.h" +#include "app_entry.h" +#include "aos/cli.h" + +#ifdef CSP_LINUXHOST + #include +#endif + +#ifdef WITH_SAL +#include +#endif + +static char linkkit_started = 0; + +static app_main_paras_t entry_paras; + +typedef void (*task_fun)(void *); + +static void wifi_service_event(input_event_t *event, void *priv_data) +{ + if (event->type != EV_WIFI) { + return; + } + + if (event->code != CODE_WIFI_ON_GOT_IP) { + return; + } + + netmgr_ap_config_t config; + memset(&config, 0, sizeof(netmgr_ap_config_t)); + netmgr_get_ap_config(&config); + LOG("wifi_service_event config.ssid %s", config.ssid); + if (strcmp(config.ssid, "adha") == 0 || strcmp(config.ssid, "aha") == 0) { + //clear_wifi_ssid(); + return; + } + + if (!linkkit_started) { + aos_task_new("iotx_example", (task_fun)linkkit_main, (void *)&entry_paras, 1024 * 6); + linkkit_started = 1; + } +} +#ifdef AOS_COMP_CLI + +static void print_identity() +{ + char _product_key[IOTX_PRODUCT_KEY_LEN + 1] = {0}; + char _device_name[IOTX_DEVICE_NAME_LEN + 1] = {0}; +#ifdef DEMO_DEBUG + char _product_secret[IOTX_PRODUCT_SECRET_LEN + 1] = {0}; + char _device_secret[IOTX_DEVICE_SECRET_LEN + 1] = {0}; +#endif + + HAL_GetProductKey(_product_key); + aos_cli_printf("ProductKey: %s\n", _product_key); +#ifdef DEMO_DEBUG + HAL_GetProductSecret(_product_secret); + aos_cli_printf("ProductSecret: %s\n", _product_secret); +#endif + HAL_GetDeviceName(_device_name); + aos_cli_printf("DeviceName: %s\n", _device_name); +#ifdef DEMO_DEBUG + HAL_GetDeviceSecret(_device_secret); + aos_cli_printf("DeviceSecret: %s\n", _device_secret); +#endif +} + +static void handle_identity_cmd(char *pwbuf, int blen, int argc, char **argv) +{ + const char *rtype = argc > 1 ? argv[1] : ""; + if (strcmp(rtype, "get") == 0) { + print_identity(); + } else if (strcmp(rtype, "set") == 0) { + if (argc == 4) { + HAL_SaveDeviceIdentity(NULL, NULL, argv[2], argv[3]); + } else if (argc == 5) { + HAL_SaveDeviceIdentity(argv[2], argv[3], argv[4], ""); + } else if (argc == 6) { + HAL_SaveDeviceIdentity(argv[2], argv[3], argv[4], argv[5]); + } else { + aos_cli_printf("arg number err! usage:\n"); + aos_cli_printf("identity set {pk} {ps} {dn} [ds] | identity set {dn} {ds}\n"); + } + } else if (strcmp(rtype, "clear") == 0) { + HAL_ClearDeviceIdentity(); + } else { + aos_cli_printf("usage:\n"); + aos_cli_printf("identity [set pk ps dn ds | set dn ds | get | clear]\n"); + } +} + +static struct cli_command identity_cmd = { + .name = "identity", + .help = "identity [set pk ps dn ds | set dn ds | get | clean ]", + .function = handle_identity_cmd +}; +#endif + +#ifdef TEST_LOOP +const char *input_data[2] = {"mqttapp", "loop"}; +#endif + +int application_start(int argc, char **argv) +{ +#ifdef CSP_LINUXHOST + signal(SIGPIPE, SIG_IGN); +#endif + +#ifdef TEST_LOOP + argc = 2; + argv = (char **)input_data; +#endif + entry_paras.argc = argc; + entry_paras.argv = argv; + +#ifdef WITH_SAL + sal_device_config_t data = {0}; + data.uart_dev.port = 1; + data.uart_dev.config.baud_rate = 115200; + data.uart_dev.config.data_width = DATA_WIDTH_8BIT; + data.uart_dev.config.parity = NO_PARITY; + data.uart_dev.config.stop_bits = STOP_BITS_1; + data.uart_dev.config.flow_control = FLOW_CONTROL_DISABLED; + data.uart_dev.config.mode = MODE_TX_RX; + sal_add_dev("mk3060", &data); + sal_init(); +#endif + +#ifdef MDAL_MAL_ICA_TEST + HAL_MDAL_MAL_Init(); +#endif +#ifdef AOS_COMP_CLI + aos_cli_register_command(&identity_cmd); +#endif + aos_set_log_level(AOS_LL_DEBUG); + + netmgr_init(); + + aos_register_event_filter(EV_WIFI, wifi_service_event, NULL); + + netmgr_start(false); +#if defined (CSP_LINUXHOST) && !defined (WITH_SAL) + aos_post_event(EV_WIFI, CODE_WIFI_ON_GOT_IP, 0); +#endif + aos_loop_run(); + + return 0; +} diff --git a/application/example/mqtt_uart/app_entry.h b/application/example/mqtt_uart/app_entry.h new file mode 100644 index 0000000000..7e7fdb0527 --- /dev/null +++ b/application/example/mqtt_uart/app_entry.h @@ -0,0 +1,16 @@ +/* + * Copyright (C) 2015-2018 Alibaba Group Holding Limited + */ + +#ifndef __APP_ENTRY_H__ +#define __APP_ENTRY_H__ + +#include "aos/kernel.h" + +typedef struct { + int argc; + char **argv; +}app_main_paras_t; + +int linkkit_main(void *paras); +#endif \ No newline at end of file diff --git a/application/example/mqtt_uart/autobuild.json b/application/example/mqtt_uart/autobuild.json new file mode 100644 index 0000000000..48093a8b4f --- /dev/null +++ b/application/example/mqtt_uart/autobuild.json @@ -0,0 +1,32 @@ +[ + { + "board": "asr5501", + "os": "linux", + "params": "" + }, + { + "board": "esp8266", + "os": "linux", + "params": "" + }, + { + "board": "linuxhost", + "os": "linux", + "params": "" + }, + { + "board": "mk3072", + "os": "linux", + "params": "" + }, + { + "board": "mk3080", + "os": "linux", + "params": "" + }, + { + "board": "stm32f429zi-nucleo", + "os": "linux", + "params": "" + } +] \ No newline at end of file diff --git a/application/example/mqtt_uart/k_app_config.h b/application/example/mqtt_uart/k_app_config.h new file mode 100644 index 0000000000..0082feb2ec --- /dev/null +++ b/application/example/mqtt_uart/k_app_config.h @@ -0,0 +1,7 @@ + + +/* user space */ +#ifndef RHINO_CONFIG_USER_SPACE +#define RHINO_CONFIG_USER_SPACE 0 +#endif + diff --git a/application/example/mqtt_uart/maintask.c b/application/example/mqtt_uart/maintask.c new file mode 100644 index 0000000000..476f9b649b --- /dev/null +++ b/application/example/mqtt_uart/maintask.c @@ -0,0 +1,62 @@ +/* + * Copyright (C) 2015-2020 Alibaba Group Holding Limited + */ + +#include +#include +#include +#include "aos/init.h" +#include "board.h" +#include + +#ifndef AOS_BINS +extern int application_start(int argc, char *argv[]); +#endif + +/* +If board have no component for example board_xx_init, it indicates that this app does not support this board. +Set the correspondence in file platform\board\aaboard_demo\ucube.py. +*/ +extern void board_tick_init(void); +extern void board_stduart_init(void); +extern void board_dma_init(void); +extern void board_gpio_init(void); +extern void board_network_init(void); +extern void board_kinit_init(kinit_t* init_args); +extern void board_flash_init(void); + +/* For user config + kinit.argc = 0; + kinit.argv = NULL; + kinit.cli_enable = 1; +*/ +static kinit_t kinit = {0, NULL, 1}; + +/** + * @brief Board Initialization Function + * @param None + * @retval None + */ +void board_init(void) +{ + board_tick_init(); + board_stduart_init(); + board_dma_init(); + board_gpio_init(); + board_flash_init(); + + board_network_init(); + /*FOR STM32F429 delete hal_i2c_pre_init \I2C1_init\CAN_init here*/ +} + +void aos_maintask(void* arg) +{ + board_init(); + board_kinit_init(&kinit); + aos_components_init(&kinit); + +#ifndef AOS_BINS + application_start(kinit.argc, kinit.argv); /* jump to app entry */ +#endif +} + diff --git a/application/example/mqtt_uart/mqtt_uart.c b/application/example/mqtt_uart/mqtt_uart.c new file mode 100644 index 0000000000..a16bc542ad --- /dev/null +++ b/application/example/mqtt_uart/mqtt_uart.c @@ -0,0 +1,347 @@ +/* + * Copyright (C) 2015-2018 Alibaba Group Holding Limited + */ + +#include +#include +#include +#include + +#include "linkkit/dev_sign_api.h" +#include "linkkit/mqtt_api.h" +#include "linkkit/wrappers/wrappers.h" +#include "linkkit/infra/infra_compat.h" + +#include "aos/hal/gpio.h" +#include "ulog/ulog.h" +#include "hal_iomux_haas1000.h" +#include "k_api.h" +#include "aos/cli.h" +#include "aos/kernel.h" +#include "aos/hal/uart.h" + +#define PRODUCT_KEY "a1hVZMSs1If" +#define DEVICE_NAME "uart_test" +#define DEVICE_SECRET "ed9bae1fe9a23d279d75e24576920090" + +#define UART_BUF_SIZE 100 +#define UART_RX_TIMEOUT 500 +#define UART_TX_TIMEOUT 100 + +#define TASK_RECVDATA_NAME "revdata" +#define TASK_RECVDATA_STACKSIZE 1024 +#define TASK_RECVDATA_PRI 50 + +static uart_dev_t uart_demo; + +void *pclient = NULL; + +char DEMO_PRODUCT_KEY[IOTX_PRODUCT_KEY_LEN + 1] = {0}; +char DEMO_DEVICE_NAME[IOTX_DEVICE_NAME_LEN + 1] = {0}; +char DEMO_DEVICE_SECRET[IOTX_DEVICE_SECRET_LEN + 1] = {0}; + +#define EXAMPLE_TRACE(fmt, ...) \ + do { \ + HAL_Printf("%s|%03d :: ", __func__, __LINE__); \ + HAL_Printf(fmt, ##__VA_ARGS__); \ + HAL_Printf("%s", "\r\n"); \ + } while(0) + +void example_message_arrive(void *pcontext, void *pclient, iotx_mqtt_event_msg_pt msg) +{ + int res = 0; + iotx_mqtt_topic_info_t *topic_info = (iotx_mqtt_topic_info_pt) msg->msg; + + switch (msg->event_type) { + case IOTX_MQTT_EVENT_PUBLISH_RECEIVED: + /* print topic name and topic message */ + EXAMPLE_TRACE("Message Arrived:"); + EXAMPLE_TRACE("Topic : %.*s", topic_info->topic_len, topic_info->ptopic); + EXAMPLE_TRACE("Payload: %.*s", topic_info->payload_len, topic_info->payload); + EXAMPLE_TRACE("\n"); + res = hal_uart_send(&uart_demo, topic_info->payload, topic_info->payload_len, UART_TX_TIMEOUT); + if (res != 0) { + printf("uart data send error!\n"); + } + break; + default: + break; + } +} + +int example_subscribe(void *handle) +{ + int res = 0; + const char *fmt = "/sys/%s/%s/thing/model/down_raw"; + char *topic = NULL; + int topic_len = 0; + + topic_len = strlen(fmt) + strlen(DEMO_PRODUCT_KEY) + strlen(DEMO_DEVICE_NAME) + 1; + topic = HAL_Malloc(topic_len); + if (topic == NULL) { + EXAMPLE_TRACE("memory not enough"); + return -1; + } + memset(topic, 0, topic_len); + HAL_Snprintf(topic, topic_len, fmt, DEMO_PRODUCT_KEY, DEMO_DEVICE_NAME); + + res = IOT_MQTT_Subscribe(handle, topic, IOTX_MQTT_QOS0, example_message_arrive, NULL); + if (res < 0) { + EXAMPLE_TRACE("subscribe failed"); + HAL_Free(topic); + return -1; + } + + HAL_Free(topic); + return 0; +} + +int example_publish(void *handle, char *payload, int len) +{ + int res = 0; + const char *fmt = "/sys/%s/%s/thing/model/up_raw"; + char *topic = NULL; + int topic_len = 0; + + topic_len = strlen(fmt) + strlen(DEMO_PRODUCT_KEY) + strlen(DEMO_DEVICE_NAME) + 1; + topic = HAL_Malloc(topic_len); + if (topic == NULL) { + EXAMPLE_TRACE("memory not enough"); + return -1; + } + memset(topic, 0, topic_len); + HAL_Snprintf(topic, topic_len, fmt, DEMO_PRODUCT_KEY, DEMO_DEVICE_NAME); + + res = IOT_MQTT_Publish_Simple(0, topic, IOTX_MQTT_QOS0, payload, len); + if (res < 0) { + EXAMPLE_TRACE("publish failed, res = %d", res); + HAL_Free(topic); + return -1; + } + + HAL_Free(topic); + return 0; +} + +void example_event_handle(void *pcontext, void *pclient, iotx_mqtt_event_msg_pt msg) +{ + EXAMPLE_TRACE("msg->event_type : %d", msg->event_type); +} + +/* + * NOTE: About demo topic of /${productKey}/${deviceName}/user/get + * + * The demo device has been configured in IoT console (https://iot.console.aliyun.com) + * so that its /${productKey}/${deviceName}/user/get can both be subscribed and published + * + * We design this to completely demonstrate publish & subscribe process, in this way + * MQTT client can receive original packet sent by itself + * + * For new devices created by yourself, pub/sub privilege also requires being granted + * to its /${productKey}/${deviceName}/user/get for successfully running whole example + */ + +void set_iotx_info() +{ + char _product_key[IOTX_PRODUCT_KEY_LEN + 1] = {0}; + char _device_name[IOTX_DEVICE_NAME_LEN + 1] = {0}; + + HAL_GetProductKey(_product_key); + if (strlen(_product_key) == 0) { + HAL_SetProductKey(PRODUCT_KEY); + } + + HAL_GetDeviceName(_device_name); + if (strlen(_device_name) == 0) { + HAL_SetDeviceName(DEVICE_NAME); + HAL_SetDeviceSecret(DEVICE_SECRET); + } +} + +/* task entry */ +static void task_recvdata_entry(void *arg) +{ + int i; + int ret; + int rev_length; + char rev_buf[UART_BUF_SIZE] = {0}; + + while (1) { + ret = hal_uart_recv_II(&uart_demo, rev_buf, UART_BUF_SIZE, &rev_length, UART_RX_TIMEOUT); + if (ret == 0) { + EXAMPLE_TRACE("uart rev_length:%d\n",rev_length); + for(i = 0;i Date: Thu, 4 Mar 2021 15:18:40 +0800 Subject: [PATCH 2/3] test for push Signed-off-by: caiyan.cai --- test.test | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 test.test diff --git a/test.test b/test.test new file mode 100644 index 0000000000..e69de29bb2 -- Gitee From bf27f52b783e0a84f73319a214edb97d2be003ce Mon Sep 17 00:00:00 2001 From: "caiyan.cai" Date: Fri, 5 Mar 2021 11:39:32 +0800 Subject: [PATCH 3/3] test for creating pull request Signed-off-by: caiyan.cai --- test.skylarCai | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 test.skylarCai diff --git a/test.skylarCai b/test.skylarCai new file mode 100644 index 0000000000..e69de29bb2 -- Gitee