From 739a20397edc3ffde09dc5712b83d083e917e0c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?esma=28=E9=A9=AC=E7=92=90=29?= Date: Tue, 22 Dec 2020 17:44:13 +0800 Subject: [PATCH] add e53 complete demo --- .../BSP/Hardware/E53_SC1/e53_sc1.c | 201 ++++----- .../BSP/Hardware/E53_SC1/e53_sc1.h | 19 +- .../BSP/Inc/stm32g0xx_hal_conf.h | 2 +- .../mqtt_iot_explorer_e53_light/README.md | 2 - .../TencentOS_tiny_EVB_G0.uvoptx | 12 +- .../TencentOS_tiny_EVB_G0.uvprojx | 12 +- .../demo/mqtt_iot_explorer_e53_light.c | 315 +++++++++----- .../stm32g0xx_hal_conf.h | 310 -------------- .../mqtt_iot_explorer_e53_light/techo.json | 40 -- ...于STM32G0+TencentOS-tiny制作一个智能灯.md | 386 ++++++++++++++++++ .../tencent_firmware_module_wrapper.h | 2 +- 11 files changed, 718 insertions(+), 583 deletions(-) delete mode 100644 board/TencentOS_tiny_EVB_G0/KEIL/mqtt_iot_explorer_e53_light/README.md delete mode 100644 board/TencentOS_tiny_EVB_G0/KEIL/mqtt_iot_explorer_e53_light/stm32g0xx_hal_conf.h delete mode 100644 board/TencentOS_tiny_EVB_G0/KEIL/mqtt_iot_explorer_e53_light/techo.json create mode 100644 board/TencentOS_tiny_EVB_G0/KEIL/mqtt_iot_explorer_e53_light/基于STM32G0+TencentOS-tiny制作一个智能灯.md diff --git a/board/TencentOS_tiny_EVB_G0/BSP/Hardware/E53_SC1/e53_sc1.c b/board/TencentOS_tiny_EVB_G0/BSP/Hardware/E53_SC1/e53_sc1.c index 3a493618..a55ee909 100644 --- a/board/TencentOS_tiny_EVB_G0/BSP/Hardware/E53_SC1/e53_sc1.c +++ b/board/TencentOS_tiny_EVB_G0/BSP/Hardware/E53_SC1/e53_sc1.c @@ -16,36 +16,7 @@ *---------------------------------------------------------------------------*/ #include "e53_sc1.h" #include "i2c.h" - -void iot_explorer_handle_power_switch(int power_switch) -{ - if (0 == power_switch){ - if(LED_IS_OPEN){ - printf("iot-explorer close the light\r\n"); - LED_CLOSE; - }else{ - printf("the light already closed\r\n"); - } - }else{ - if(LED_IS_OPEN){ - printf("iot-explorer already opened\r\n"); - }else{ - printf("the light closed already\r\n"); - LED_OPEN; - } - } -} - -void key1_handle_power_switch(void) -{ - if(LED_IS_OPEN){ - printf("key1 close the light\r\n"); - LED_CLOSE; - }else{ - printf("key1 open the light\r\n"); - LED_OPEN; - } -} +#include "stm32g0xx_hal_tim.h" /*************************************************************** * 函数名称: Init_BH1750 @@ -56,7 +27,7 @@ void key1_handle_power_switch(void) static void Init_BH1750(void) { uint8_t t_Data = 0x01; - HAL_I2C_Master_Transmit(&hi2c2,BH1750_Addr,&t_Data,1,0xff); + HAL_I2C_Master_Transmit(&hi2c2,BH1750_Addr,&t_Data,1,0xff); } /*************************************************************** @@ -67,8 +38,8 @@ static void Init_BH1750(void) ***************************************************************/ static void Start_BH1750(void) { - uint8_t t_Data = 0x10; - HAL_I2C_Master_Transmit(&hi2c2,BH1750_Addr,&t_Data,1,0xff); + uint8_t t_Data = 0x10; + HAL_I2C_Master_Transmit(&hi2c2,BH1750_Addr,&t_Data,1,0xff); } /*************************************************************** @@ -91,16 +62,115 @@ static float Convert_BH1750(void) return result_lx; } +/*************************************************************** +* 函数名称: e53_scl_read_data +* 说 明: 读取e53接口数据 +* 参 数: 无 +* 返 回 值: 无 +***************************************************************/ float e53_scl_read_data(void) { return Convert_BH1750(); } +// 硬件定时器参考代码 + +//定时器句柄 +TIM_HandleTypeDef TIM3_Handler; + +/*************************************************************** +* 函数名称: TIM3_Init +* 说 明: 启动定时器,定时器溢出时间计算方法:Tout=((arr+1)*(psc+1))/Ft us. Ft=定时器工作频率,单位:Mhz +***************************************************************/ +static void TIM3_Init(uint16_t arr,uint16_t psc) //arr:自动重装值 psc:时钟预分频数 +{ + TIM3_Handler.Instance=TIM3; //通用定时器3 + TIM3_Handler.Init.Prescaler=psc; //分频系数 + TIM3_Handler.Init.CounterMode=TIM_COUNTERMODE_UP; //向上计数器 + TIM3_Handler.Init.Period=arr; //自动装载值 + TIM3_Handler.Init.ClockDivision=TIM_CLOCKDIVISION_DIV1; //时钟分频因子 + HAL_TIM_Base_Init(&TIM3_Handler); //时基初始化 + HAL_TIM_Base_Start_IT(&TIM3_Handler); //使能定时器3和定时器3更新中断:TIM_IT_UPDATE +} + +/*************************************************************** +* 函数名称: HAL_TIM_Base_MspInit +* 说 明: 中断初始化 +***************************************************************/ +void HAL_TIM_Base_MspInit(TIM_HandleTypeDef *htim) +{ + if(htim->Instance==TIM3) + { + __HAL_RCC_TIM3_CLK_ENABLE(); //使能TIM3时钟 + HAL_NVIC_SetPriority(TIM3_IRQn,1,3); //设置中断优先级,抢占优先级1,子优先级3 + HAL_NVIC_EnableIRQ(TIM3_IRQn); //开启ITM3中断 + } +} + +/*************************************************************** +* 函数名称: TIM3_IRQHandler +* 说 明: 中断处理函数 +***************************************************************/ +void TIM3_IRQHandler(void) +{ + HAL_TIM_IRQHandler(&TIM3_Handler); +} + +/*************************************************************** +* 函数名称: HAL_TIM_PeriodElapsedCallback +* 说 明: 定时器溢出回调 +***************************************************************/ +static uint8_t light_pwm = 99; +static uint32_t pwm_count = 0; +extern int power_switch_cache; +void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) +{ + if(htim==(&TIM3_Handler)) + { + if (power_switch_cache) { + if(pwm_count < 4 * light_pwm) + { + LED_CLOSE; + } else { + LED_OPEN; + } + if(pwm_count >= 4 * 100) + { + pwm_count = 0; + } + pwm_count++; + } + } +} + +void e53_sc1_set_ligth_level(int light_level) +{ + switch(light_level) { + case LIGHT_LEVEL_LOW: + light_pwm = 99; + break; + case LIGHT_LEVEL_MID: + light_pwm = 95; + break; + case LIGHT_LEVEL_HIGH: + light_pwm = 80; + break; + default: + break; + } +} + +/*************************************************************** +* 函数名称: e53_sc1_init +* 说 明: 初始化e53 +* 参 数: 无 +* 返 回 值: 无 +***************************************************************/ int e53_sc1_init(void) { GPIO_InitTypeDef GPIO_InitStruct = {0}; - - /*Configure GPIO pin : LED_Pin */ + + /*Configure GPIO pin : LED_Pin */ GPIO_InitStruct.Pin = LED_Pin; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; @@ -108,62 +178,7 @@ int e53_sc1_init(void) HAL_GPIO_Init(LED_GPIO_Port, &GPIO_InitStruct); Init_BH1750(); - - return 0; + TIM3_Init(1, 719); + e53_sc1_set_ligth_level(2); + return 0; } - -#if 0 -// 硬件定时器参考代码 -TIM_HandleTypeDef TIM3_Handler; //定时器句柄 -static uint32_t pwm_count = 0; - -//定时器溢出时间计算方法:Tout=((arr+1)*(psc+1))/Ft us. -//Ft=定时器工作频率,单位:Mhz -void TIM3_Init(uint16_t arr,uint16_t psc) //arr:自动重装值 psc:时钟预分频数 -{ - TIM3_Handler.Instance=TIM3; //通用定时器3 - TIM3_Handler.Init.Prescaler=psc; //分频系数 - TIM3_Handler.Init.CounterMode=TIM_COUNTERMODE_UP; //向上计数器 - TIM3_Handler.Init.Period=arr; //自动装载值 - TIM3_Handler.Init.ClockDivision=TIM_CLOCKDIVISION_DIV1;//时钟分频因子 - HAL_TIM_Base_Init(&TIM3_Handler); //时基初始化 - - //HAL_TIM_Base_Start_IT(&TIM3_Handler); //使能定时器3和定时器3更新中断:TIM_IT_UPDATE - //HAL_TIM_Base_Stop_IT(&TIM3_Handler); -} - - //TIM3_Init(1, 719); - -void HAL_TIM_Base_MspInit(TIM_HandleTypeDef *htim) -{ - if(htim->Instance==TIM3) - { - __HAL_RCC_TIM3_CLK_ENABLE(); //使能TIM3时钟 - HAL_NVIC_SetPriority(TIM3_IRQn,1,3); //设置中断优先级,抢占优先级1,子优先级3 - HAL_NVIC_EnableIRQ(TIM3_IRQn); //开启ITM3中断 - } -} - -void TIM3_IRQHandler(void) -{ - HAL_TIM_IRQHandler(&TIM3_Handler); -} - -void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) -{ - if(htim==(&TIM3_Handler)) - { - if(pwm_count < light_pwm) - { - set_light(true); - } else { - set_light(false); - } - if(pwm_count >= 100) - { - pwm_count = 0; - } - pwm_count++; - } -} -#endif diff --git a/board/TencentOS_tiny_EVB_G0/BSP/Hardware/E53_SC1/e53_sc1.h b/board/TencentOS_tiny_EVB_G0/BSP/Hardware/E53_SC1/e53_sc1.h index 9f3e881d..1675bba5 100644 --- a/board/TencentOS_tiny_EVB_G0/BSP/Hardware/E53_SC1/e53_sc1.h +++ b/board/TencentOS_tiny_EVB_G0/BSP/Hardware/E53_SC1/e53_sc1.h @@ -30,20 +30,17 @@ #define BH1750_Addr 0x46 #define LIGHT_MAX_NAME_LEN 32 +typedef enum{ + LIGHT_LEVEL_LOW = 0, + LIGHT_LEVEL_MID = 1, + LIGHT_LEVEL_HIGH = 2, + LIGHT_LEVEL_BUTT, +}E_LIGTH_LEVEL; + int e53_sc1_init(void); float e53_scl_read_data(void); -void iot_explorer_handle_power_switch(int power_switch); +void e53_sc1_set_ligth_level(int light_level); -void key1_handle_power_switch(void); - -#if 0 -typedef enum{ - LIGHT_LEVEL_LOW = 0, - LIGTH_LEVEL_MID = 1, - LIGTH_LEVEL_HIGH = 2, - LIGTH_LEVEL_BUTT, -}E_LIGTH_LEVEL; -#endif #endif /* _E53_SC1_H_ */ diff --git a/board/TencentOS_tiny_EVB_G0/BSP/Inc/stm32g0xx_hal_conf.h b/board/TencentOS_tiny_EVB_G0/BSP/Inc/stm32g0xx_hal_conf.h index 4b661189..d804c9ba 100644 --- a/board/TencentOS_tiny_EVB_G0/BSP/Inc/stm32g0xx_hal_conf.h +++ b/board/TencentOS_tiny_EVB_G0/BSP/Inc/stm32g0xx_hal_conf.h @@ -51,7 +51,7 @@ extern "C" { /* #define HAL_SMARTCARD_MODULE_ENABLED */ /* #define HAL_SMBUS_MODULE_ENABLED */ #define HAL_SPI_MODULE_ENABLED -/* #define HAL_TIM_MODULE_ENABLED */ +#define HAL_TIM_MODULE_ENABLED #define HAL_UART_MODULE_ENABLED /* #define HAL_USART_MODULE_ENABLED */ /* #define HAL_WWDG_MODULE_ENABLED */ diff --git a/board/TencentOS_tiny_EVB_G0/KEIL/mqtt_iot_explorer_e53_light/README.md b/board/TencentOS_tiny_EVB_G0/KEIL/mqtt_iot_explorer_e53_light/README.md deleted file mode 100644 index d4d1d820..00000000 --- a/board/TencentOS_tiny_EVB_G0/KEIL/mqtt_iot_explorer_e53_light/README.md +++ /dev/null @@ -1,2 +0,0 @@ -## 编译说明 -1. 若使用STM32硬件定时器,请拷贝`TencentOS-tiny\board\TencentOS_tiny_EVB_G0\KEIL\mqtt_iot_explorer_e53_light\stm32g0xx_hal_conf.h`到TencentOS-tiny\board\TencentOS_tiny_EVB_G0\BSP\Inc\`目录下。 diff --git a/board/TencentOS_tiny_EVB_G0/KEIL/mqtt_iot_explorer_e53_light/TencentOS_tiny_EVB_G0.uvoptx b/board/TencentOS_tiny_EVB_G0/KEIL/mqtt_iot_explorer_e53_light/TencentOS_tiny_EVB_G0.uvoptx index 9320102e..e9db14af 100644 --- a/board/TencentOS_tiny_EVB_G0/KEIL/mqtt_iot_explorer_e53_light/TencentOS_tiny_EVB_G0.uvoptx +++ b/board/TencentOS_tiny_EVB_G0/KEIL/mqtt_iot_explorer_e53_light/TencentOS_tiny_EVB_G0.uvoptx @@ -1007,7 +1007,7 @@ tos/config - 1 + 0 0 0 0 @@ -1027,7 +1027,7 @@ examples - 1 + 0 0 0 0 @@ -1071,7 +1071,7 @@ hal - 1 + 0 0 0 0 @@ -1091,7 +1091,7 @@ at - 1 + 0 0 0 0 @@ -1123,7 +1123,7 @@ devices - 1 + 0 0 0 0 @@ -1143,7 +1143,7 @@ hardware - 1 + 0 0 0 0 diff --git a/board/TencentOS_tiny_EVB_G0/KEIL/mqtt_iot_explorer_e53_light/TencentOS_tiny_EVB_G0.uvprojx b/board/TencentOS_tiny_EVB_G0/KEIL/mqtt_iot_explorer_e53_light/TencentOS_tiny_EVB_G0.uvprojx index ba773f0e..9d29861e 100644 --- a/board/TencentOS_tiny_EVB_G0/KEIL/mqtt_iot_explorer_e53_light/TencentOS_tiny_EVB_G0.uvprojx +++ b/board/TencentOS_tiny_EVB_G0/KEIL/mqtt_iot_explorer_e53_light/TencentOS_tiny_EVB_G0.uvprojx @@ -352,7 +352,7 @@ 0 0 0 - 4 + 1 @@ -835,14 +835,4 @@ - - - - TencentOS_tiny_EVB_G0 - 0 - 1 - - - - diff --git a/board/TencentOS_tiny_EVB_G0/KEIL/mqtt_iot_explorer_e53_light/demo/mqtt_iot_explorer_e53_light.c b/board/TencentOS_tiny_EVB_G0/KEIL/mqtt_iot_explorer_e53_light/demo/mqtt_iot_explorer_e53_light.c index e0ccdb50..73a5c01b 100644 --- a/board/TencentOS_tiny_EVB_G0/KEIL/mqtt_iot_explorer_e53_light/demo/mqtt_iot_explorer_e53_light.c +++ b/board/TencentOS_tiny_EVB_G0/KEIL/mqtt_iot_explorer_e53_light/demo/mqtt_iot_explorer_e53_light.c @@ -7,33 +7,89 @@ extern void stm32g0xx_key_init(void); -#define PRODUCT_ID "LRKSCAZLF1" -#define DEVICE_NAME "techo_test01" -#define DEVICE_KEY "UOgIWiO8kcVkgif6FO3tJQ==" +// 物联网开发平台设备信息 +#define PRODUCT_ID "3P9346G7NL" +#define DEVICE_NAME "led_01" +#define DEVICE_KEY "uNdwCyxa1Jd6Zch4r3/khw==" -#define YOUR_WIFI_SSID "iot-explorer" -#define YOUR_WIFI_PWD "iot-explorer" +// WIFI信息 +#define YOUR_WIFI_SSID "iot_explorer" +#define YOUR_WIFI_PWD "iot_explorer" -// 数据模板支持数据增量上报 -#define REPORT_LX_DATA_TEMPLATE "{\\\"method\\\":\\\"report\\\"\\,\\\"clientToken\\\":\\\"00000001\\\"\\,\\\"params\\\":{\\\"lx\\\":%.0f}}" -#define REPORT_POWER_SWITCH_DATA_TEMPLATE "{\\\"method\\\":\\\"report\\\"\\,\\\"clientToken\\\":\\\"00000001\\\"\\,\\\"params\\\":{\\\"power_switch\\\":%d}}" +// 数据模板 +#define REPORT_LX_DATA_TEMPLATE "{\\\"method\\\":\\\"report\\\"\\,\\\"clientToken\\\":\\\"00000001\\\"\\,\\\"params\\\":{\\\"lx\\\":%.0f}}" +#define REPORT_POWER_SWITCH_DATA_TEMPLATE "{\\\"method\\\":\\\"report\\\"\\,\\\"clientToken\\\":\\\"00000001\\\"\\,\\\"params\\\":{\\\"power_switch\\\":%d}}" +#define REPORT_BRIGHTNESS_LEVEL_DATA_TEMPLATE "{\\\"method\\\":\\\"report\\\"\\,\\\"clientToken\\\":\\\"00000001\\\"\\,\\\"params\\\":{\\\"brightness_level\\\":%d}}" +#define CONTROL_REPLY_DATA_TEMPLATE "{\\\"method\\\":\\\"control_reply\\\"\\,\\\"clientToken\\\":\\\"%s\\\"\\,\\\"code\\\":0\\,\\\"status\\\":\\\"ok\\\"}" -#define MAIL_SIZE 256 -#define MAIL_MAX 10 -static uint8_t mail_pool[MAIL_MAX * MAIL_SIZE]; -static k_mail_q_t mail_q; +char payload[256] = {0}; +static char report_topic_name[TOPIC_NAME_MAX_SIZE] = {0}; +static char report_reply_topic_name[TOPIC_NAME_MAX_SIZE] = {0}; -static bool key_pressed_flag = false; +static bool is_power_switch_changed = true; +static bool is_brightness_level_changed = true; +static bool is_client_token_received = false; + +int power_switch_cache = 0; +int brightness_level_cache = 2; +char client_token_cache[128] = {0}; + +/*************************************************************** +* 函数名称: key1_handler_callback +* 说 明: 按键处理回调 +***************************************************************/ void key1_handler_callback(void) { - key1_handle_power_switch(); - // 在主循环中进行数据上报 - key_pressed_flag = true; + if(power_switch_cache){ + power_switch_cache = 0; + LED_CLOSE; + }else{ + power_switch_cache = 1; + LED_OPEN; + } + + is_power_switch_changed = true; } +/*************************************************************** +* 函数名称: iot_explorer_handle_power_switch +* 说 明: 根据power switch控制开关 +***************************************************************/ +static void iot_explorer_handle_power_switch(int power_switch) +{ + power_switch_cache = power_switch; + + if (0 == power_switch){ + if(LED_IS_OPEN){ + printf("iot-explorer close the light\r\n"); + LED_CLOSE; + is_power_switch_changed = true; + }else{ + printf("the light already closed\r\n"); + } + }else{ + if(LED_IS_OPEN){ + printf("the already opened\r\n"); + }else{ + printf("iot-explorer open the light\r\n"); + LED_OPEN; + is_power_switch_changed = true; + } + } +} + +/*************************************************************** +* 函数名称: default_message_handler +* 说 明: IoT Explorer下行数据处理 +***************************************************************/ static void default_message_handler(mqtt_message_t* msg) { - int payload_len = 0; + cJSON *root; + cJSON *params; + cJSON *token; + cJSON *method; + cJSON *power_switch; + cJSON *brightness_level; printf("callback:\r\n"); printf("---------------------------------------------------------\r\n"); @@ -41,51 +97,101 @@ static void default_message_handler(mqtt_message_t* msg) printf("\tpayload:%s\r\n", msg->payload); printf("---------------------------------------------------------\r\n"); - payload_len = strlen(msg->payload); - if (payload_len > MAIL_SIZE){ - printf("too long payload\r\n"); - return; + // 1. 解析从云端收到的控制信息,示例控制信息为 + //"{"method":"control","clientToken":"clientToken-Xx-N_enad","params":{"power_switch":0}}" + root = cJSON_Parse(msg->payload +1); + if (!root){ + printf("Invalid json root\r\n"); + return; + } + + // 2. 解析出method + method = cJSON_GetObjectItem(root, "method"); + if (!method){ + printf("Invalid json method\r\n"); + cJSON_Delete(root); + return; + } + + // 3. 仅处理云端下发的 control 数据,report_reply暂不处理 + if (0 != strncmp(method->valuestring, "control", sizeof("control") - 1)){ + cJSON_Delete(root); + return; + } + + // 4. 解析出params + params = cJSON_GetObjectItem(root, "params"); + if (!params){ + printf("Invalid json params\r\n"); + cJSON_Delete(root); + return; + } + + // 1. 解析出 power_switch + power_switch = cJSON_GetObjectItem(params, "power_switch"); + // 2. 根据 power_switch 控制实际硬件开关 + if (power_switch){ + iot_explorer_handle_power_switch(power_switch->valueint); } - - tos_mail_q_post(&mail_q, msg->payload, MAIL_SIZE); + + brightness_level = cJSON_GetObjectItem(params, "brightness_level"); + if (brightness_level){ + e53_sc1_set_ligth_level(brightness_level->valueint); + brightness_level_cache = brightness_level->valueint; + is_brightness_level_changed = true; + } + + // 4. 设置clientToken回复 + token = cJSON_GetObjectItem(root, "clientToken"); + if (token) { + strcpy(client_token_cache, token->valuestring); + is_client_token_received = true; + } + + cJSON_Delete(root); } -static char report_reply_topic_name[TOPIC_NAME_MAX_SIZE] = {0}; -static char report_topic_name[TOPIC_NAME_MAX_SIZE] = {0}; -static char payload[256] = {0}; +/*************************************************************** +* 函数名称: mqtt_demo_task +* 说 明: 主任务,连接平台以及发送上行数据 +***************************************************************/ static void mqtt_demo_task(void) { int ret = 0; int size = 0; mqtt_state_t state; - + char *product_id = PRODUCT_ID; char *device_name = DEVICE_NAME; char *key = DEVICE_KEY; - + device_info_t dev_info; memset(&dev_info, 0, sizeof(device_info_t)); float e53_value = 0; char e53_str[16] = {0}; - + OLED_ShowString(0, 2, (uint8_t*)"connecting...", 16); /** * Please Choose your AT Port first, default is HAL_UART_2(USART2) */ + // 1. 初始化ESP8266 ret = esp8266_tencent_firmware_sal_init(HAL_UART_PORT_2); if (ret < 0) { printf("esp8266 tencent firmware sal init fail, ret is %d\r\n", ret); } - + + // 2. 连接wifi,如果需要自行配置热点或者wifi,请打开注释 esp8266_tencent_firmware_join_ap(YOUR_WIFI_SSID, YOUR_WIFI_PWD); + // 3. 设置设备信息:产品ID,设备名,设备密钥 strncpy(dev_info.product_id, product_id, PRODUCT_ID_MAX_SIZE); strncpy(dev_info.device_name, device_name, DEVICE_NAME_MAX_SIZE); strncpy(dev_info.device_serc, key, DEVICE_SERC_MAX_SIZE); tos_tf_module_info_set(&dev_info, TLS_MODE_PSK); + // 4. 连接IoT Explorer mqtt_param_t init_params = DEFAULT_MQTT_PARAMS; if (tos_tf_module_mqtt_conn(init_params) != 0) { printf("module mqtt conn fail\n"); @@ -96,8 +202,8 @@ static void mqtt_demo_task(void) if (tos_tf_module_mqtt_state_get(&state) != -1) { printf("MQTT: %s\n", state == MQTT_STATE_CONNECTED ? "CONNECTED" : "DISCONNECTED"); } - - // 订阅数据模板 + + // 5. 订阅数据模板 topic size = snprintf(report_reply_topic_name, TOPIC_NAME_MAX_SIZE, "$thing/down/property/%s/%s", product_id, device_name); if (size < 0 || size > sizeof(report_reply_topic_name) - 1) { printf("sub topic content length not enough! content size:%d buf size:%d", size, (int)sizeof(report_reply_topic_name)); @@ -107,30 +213,36 @@ static void mqtt_demo_task(void) } else { printf("module mqtt sub success\n"); } - + memset(report_topic_name, sizeof(report_topic_name), 0); size = snprintf(report_topic_name, TOPIC_NAME_MAX_SIZE, "$thing/up/property/%s/%s", product_id, device_name); if (size < 0 || size > sizeof(report_topic_name) - 1) { printf("pub topic content length not enough! content size:%d buf size:%d", size, (int)sizeof(report_topic_name)); } - - if (e53_sc1_init() == -1) { - printf("e53 init fail\r\n"); - return; - } - + while (1) { + // 判断当前IoT Explorer连接状态 + mqtt_state_t state; + tos_tf_module_mqtt_state_get(&state); + if (state == MQTT_STATE_DISCONNECTED) { + printf("mqtt is disconnected\r\n"); + tos_sleep_ms(5000); + continue; + } + + // 读取光照强度,数据存放在 e53_value e53_value = e53_scl_read_data(); printf("e53 value %.0f lx\r\n", e53_value); - + memset(e53_str, 0, sizeof(e53_str)); sprintf(e53_str, "%.0f lx(lm/m2)", e53_value); OLED_ShowString(0, 2, (uint8_t*)e53_str, 16); - - if (key_pressed_flag){ - key_pressed_flag = false; + + // 如果开关状态改变,需要上报给平台以同步 + if (is_power_switch_changed){ + is_power_switch_changed = false; memset(payload, 0, sizeof(payload)); - snprintf(payload, sizeof(payload), REPORT_POWER_SWITCH_DATA_TEMPLATE, LED_IS_OPEN); + snprintf(payload, sizeof(payload), REPORT_POWER_SWITCH_DATA_TEMPLATE, power_switch_cache); if (tos_tf_module_mqtt_pub(report_topic_name, QOS0, payload) != 0) { printf("module mqtt pub fail\n"); @@ -138,90 +250,77 @@ static void mqtt_demo_task(void) printf("module mqtt pub success\n"); } } + + // 扩展实验2:请在此处添加亮度等级状态上报物联网开发平台的代码 + if (is_brightness_level_changed){ + is_brightness_level_changed = false; + //----请在空白处填写代码 - // 请在此处添加光照强度数据上报物联网开发平台的代码 - - tos_sleep_ms(5000); - } -} - -static void iot_explorer_data_handle(void *arg) -{ - char buf[MAIL_SIZE]; - size_t mail_size = 0; - cJSON *root; - cJSON *params; - //cJSON *token; - cJSON *method; - cJSON *p; - - while(1){ - memset(buf, 0, MAIL_SIZE); - if (K_ERR_NONE == tos_mail_q_pend(&mail_q, buf, &mail_size, TOS_TIME_FOREVER)){ - //printf("mail content: %s\r\n", buf); - //字符串首尾的引号丢弃后才是标准的json数据格式 - buf[strlen(buf) - 1] = '\0'; - root = cJSON_Parse(buf + 1); - if (NULL == root){ - printf("Invalid json root\r\n"); - continue; - } - method = cJSON_GetObjectItem(root, "method"); - if (NULL == method){ - printf("Invalid json method\r\n"); - cJSON_Delete(root); - continue; - } - // 仅处理云端下发的 control 数据 - if (0 != strncmp(method->valuestring, "control", sizeof("control") - 1)){ - //printf("Invalid method\r\n"); - cJSON_Delete(root); - continue; - } - // 若需要回复报文,需要携带数据报文的 clitenToken 字段 - //token = cJSON_GetObjectItem(root, "clientToken"); - params = cJSON_GetObjectItem(root, "params"); - if (NULL == params){ - printf("Invalid json params\r\n"); - cJSON_Delete(root); - continue; - } - - // 请在此处添加物联网开发平台对智能灯的控制代码 - - - cJSON_Delete(root); + //---- } + + // 接受到下行control消息,回复最近一条 + if (is_client_token_received) { + is_client_token_received = false; + memset(payload, 0, sizeof(payload)); + snprintf(payload, sizeof(payload), CONTROL_REPLY_DATA_TEMPLATE, client_token_cache); + if (tos_tf_module_mqtt_pub(report_topic_name, QOS0, payload) != 0) { + printf("module mqtt pub fail\n"); + } else { + printf("module mqtt pub success\n"); + } + } + + // 1. 构造上报的JSON + memset(payload, 0, sizeof(payload)); + snprintf(payload, sizeof(payload), REPORT_LX_DATA_TEMPLATE, e53_value); + // 2. 向数据模板 topic 发布光照强度信息 + if (tos_tf_module_mqtt_pub(report_topic_name, QOS0, payload) != 0) { + printf("module mqtt pub fail\n"); + break; + } else { + printf("module mqtt pub success\n"); + } + + // 2. 向数据模板topic发布光照强度信息 + memset(payload, 0, sizeof(payload)); + snprintf(payload, sizeof(payload), REPORT_BRIGHTNESS_LEVEL_DATA_TEMPLATE, + brightness_level_cache); + if (tos_tf_module_mqtt_pub(report_topic_name, QOS0, payload) != 0) { + printf("module mqtt pub fail\n"); + } else { + printf("module mqtt pub success\n"); + } + + tos_sleep_ms(1000); } } -#define TASK_HANDLE_MAIL_PRIORITY 3 -#define TASK_HANDLE_MAIL_STACK_SIZE 4096 -__STATIC__ k_stack_t stack_task_handle_mail[TASK_HANDLE_MAIL_STACK_SIZE]; -k_task_t task_handle_mail; +/*************************************************************** +* 函数名称: application_entry +* 说 明: 应用入口 +***************************************************************/ void application_entry(void *arg) { char *str = "TencentOS-tiny"; - + // 初始化tencent-os tiny定时器模块 timer_init(); + // 初始化按键检测模块 stm32g0xx_key_init(); + + // 初始化灯以及光照强度检测模块 + e53_sc1_init(); // OLED 显示屏初始化 OLED_Init(); OLED_Clear(); OLED_ShowString(0, 0, (uint8_t*)str, 16); - // 创建tencent-os tiny邮箱,使用邮箱机制进行任务间通信 - tos_mail_q_create(&mail_q, mail_pool, MAIL_MAX, MAIL_SIZE); - // 创建tencent-os tiny任务,任务遍历邮箱,处理iot-explorer下发的数据 - (void)tos_task_create(&task_handle_mail, "iot-explorer", iot_explorer_data_handle, - (void *)NULL, TASK_HANDLE_MAIL_PRIORITY, - stack_task_handle_mail, TASK_HANDLE_MAIL_STACK_SIZE, 0); - mqtt_demo_task(); + while (1) { printf("This is a demo\r\n"); tos_task_delay(10000); diff --git a/board/TencentOS_tiny_EVB_G0/KEIL/mqtt_iot_explorer_e53_light/stm32g0xx_hal_conf.h b/board/TencentOS_tiny_EVB_G0/KEIL/mqtt_iot_explorer_e53_light/stm32g0xx_hal_conf.h deleted file mode 100644 index d804c9ba..00000000 --- a/board/TencentOS_tiny_EVB_G0/KEIL/mqtt_iot_explorer_e53_light/stm32g0xx_hal_conf.h +++ /dev/null @@ -1,310 +0,0 @@ -/** - ****************************************************************************** - * @file stm32g0xx_hal_conf.h - * @author MCD Application Team - * @brief HAL configuration file. - ****************************************************************************** - * @attention - * - *

© Copyright (c) 2018 STMicroelectronics. - * All rights reserved.

- * - * This software component is licensed by ST under BSD 3-Clause license, - * the "License"; You may not use this file except in compliance with the - * License. You may obtain a copy of the License at: - * opensource.org/licenses/BSD-3-Clause - * - ****************************************************************************** - */ - -/* Define to prevent recursive inclusion -------------------------------------*/ -#ifndef STM32G0xx_HAL_CONF_H -#define STM32G0xx_HAL_CONF_H - -#ifdef __cplusplus -extern "C" { -#endif - -/* Exported types ------------------------------------------------------------*/ -/* Exported constants --------------------------------------------------------*/ - -/* ########################## Module Selection ############################## */ -/** - * @brief This is the list of modules to be used in the HAL driver - */ -#define HAL_MODULE_ENABLED - - #define HAL_ADC_MODULE_ENABLED -/* #define HAL_CEC_MODULE_ENABLED */ -/* #define HAL_COMP_MODULE_ENABLED */ -/* #define HAL_CRC_MODULE_ENABLED */ -/* #define HAL_CRYP_MODULE_ENABLED */ -/* #define HAL_DAC_MODULE_ENABLED */ -/* #define HAL_EXTI_MODULE_ENABLED */ -#define HAL_I2C_MODULE_ENABLED -/* #define HAL_I2S_MODULE_ENABLED */ -/* #define HAL_IWDG_MODULE_ENABLED */ -/* #define HAL_IRDA_MODULE_ENABLED */ -/* #define HAL_LPTIM_MODULE_ENABLED */ -/* #define HAL_RNG_MODULE_ENABLED */ -/* #define HAL_RTC_MODULE_ENABLED */ -/* #define HAL_SMARTCARD_MODULE_ENABLED */ -/* #define HAL_SMBUS_MODULE_ENABLED */ -#define HAL_SPI_MODULE_ENABLED -#define HAL_TIM_MODULE_ENABLED -#define HAL_UART_MODULE_ENABLED -/* #define HAL_USART_MODULE_ENABLED */ -/* #define HAL_WWDG_MODULE_ENABLED */ -#define HAL_GPIO_MODULE_ENABLED -#define HAL_EXTI_MODULE_ENABLED -#define HAL_DMA_MODULE_ENABLED -#define HAL_RCC_MODULE_ENABLED -#define HAL_FLASH_MODULE_ENABLED -#define HAL_PWR_MODULE_ENABLED -#define HAL_CORTEX_MODULE_ENABLED - -/* ########################## Register Callbacks selection ############################## */ -/** - * @brief This is the list of modules where register callback can be used - */ -#define USE_HAL_ADC_REGISTER_CALLBACKS 0u -#define USE_HAL_CEC_REGISTER_CALLBACKS 0u -#define USE_HAL_COMP_REGISTER_CALLBACKS 0u -#define USE_HAL_CRYP_REGISTER_CALLBACKS 0u -#define USE_HAL_DAC_REGISTER_CALLBACKS 0u -#define USE_HAL_I2C_REGISTER_CALLBACKS 0u -#define USE_HAL_I2S_REGISTER_CALLBACKS 0u -#define USE_HAL_IRDA_REGISTER_CALLBACKS 0u -#define USE_HAL_LPTIM_REGISTER_CALLBACKS 0u -#define USE_HAL_RNG_REGISTER_CALLBACKS 0u -#define USE_HAL_RTC_REGISTER_CALLBACKS 0u -#define USE_HAL_SMBUS_REGISTER_CALLBACKS 0u -#define USE_HAL_SPI_REGISTER_CALLBACKS 0u -#define USE_HAL_TIM_REGISTER_CALLBACKS 0u -#define USE_HAL_UART_REGISTER_CALLBACKS 0u -#define USE_HAL_USART_REGISTER_CALLBACKS 0u -#define USE_HAL_WWDG_REGISTER_CALLBACKS 0u - -/* ########################## Oscillator Values adaptation ####################*/ -/** - * @brief Adjust the value of External High Speed oscillator (HSE) used in your application. - * This value is used by the RCC HAL module to compute the system frequency - * (when HSE is used as system clock source, directly or through the PLL). - */ -#if !defined (HSE_VALUE) -#define HSE_VALUE 8000000U /*!< Value of the External oscillator in Hz */ -#endif /* HSE_VALUE */ - -#if !defined (HSE_STARTUP_TIMEOUT) -#define HSE_STARTUP_TIMEOUT 100U /*!< Time out for HSE start up, in ms */ -#endif /* HSE_STARTUP_TIMEOUT */ - -/** - * @brief Internal High Speed oscillator (HSI) value. - * This value is used by the RCC HAL module to compute the system frequency - * (when HSI is used as system clock source, directly or through the PLL). - */ -#if !defined (HSI_VALUE) -#define HSI_VALUE 16000000U /*!< Value of the Internal oscillator in Hz*/ -#endif /* HSI_VALUE */ - -/** - * @brief Internal Low Speed oscillator (LSI) value. - */ -#if !defined (LSI_VALUE) -#define LSI_VALUE 32000U /*!< LSI Typical Value in Hz*/ -#endif /* LSI_VALUE */ /*!< Value of the Internal Low Speed oscillator in Hz -The real value may vary depending on the variations -in voltage and temperature.*/ -/** - * @brief External Low Speed oscillator (LSE) value. - * This value is used by the UART, RTC HAL module to compute the system frequency - */ -#if !defined (LSE_VALUE) -#define LSE_VALUE 32768U /*!< Value of the External oscillator in Hz*/ -#endif /* LSE_VALUE */ - -#if !defined (LSE_STARTUP_TIMEOUT) -#define LSE_STARTUP_TIMEOUT 5000U /*!< Time out for LSE start up, in ms */ -#endif /* LSE_STARTUP_TIMEOUT */ - -/** - * @brief External clock source for I2S1 peripheral - * This value is used by the RCC HAL module to compute the I2S1 clock source - * frequency. - */ -#if !defined (EXTERNAL_I2S1_CLOCK_VALUE) -#define EXTERNAL_I2S1_CLOCK_VALUE 48000U /*!< Value of the I2S1 External clock source in Hz*/ -#endif /* EXTERNAL_I2S1_CLOCK_VALUE */ - -/* Tip: To avoid modifying this file each time you need to use different HSE, - === you can define the HSE value in your toolchain compiler preprocessor. */ - -/* ########################### System Configuration ######################### */ -/** - * @brief This is the HAL system configuration section - */ -#define VDD_VALUE 3300U /*!< Value of VDD in mv */ -#define TICK_INT_PRIORITY 0U /*!< tick interrupt priority */ -#define USE_RTOS 0U -#define PREFETCH_ENABLE 1U -#define INSTRUCTION_CACHE_ENABLE 1U - -/* ################## SPI peripheral configuration ########################## */ - -/* CRC FEATURE: Use to activate CRC feature inside HAL SPI Driver -* Activated: CRC code is present inside driver -* Deactivated: CRC code cleaned from driver -*/ - -#define USE_SPI_CRC 0U - -/* ################## CRYP peripheral configuration ########################## */ - -#define USE_HAL_CRYP_SUSPEND_RESUME 1U - -/* ########################## Assert Selection ############################## */ -/** - * @brief Uncomment the line below to expanse the "assert_param" macro in the - * HAL drivers code - */ -/* #define USE_FULL_ASSERT 1U */ - -/* Includes ------------------------------------------------------------------*/ -/** - * @brief Include module's header file - */ - -#ifdef HAL_RCC_MODULE_ENABLED -#include "stm32g0xx_hal_rcc.h" -#endif /* HAL_RCC_MODULE_ENABLED */ - -#ifdef HAL_GPIO_MODULE_ENABLED -#include "stm32g0xx_hal_gpio.h" -#endif /* HAL_GPIO_MODULE_ENABLED */ - -#ifdef HAL_DMA_MODULE_ENABLED -#include "stm32g0xx_hal_dma.h" -#endif /* HAL_DMA_MODULE_ENABLED */ - -#ifdef HAL_CORTEX_MODULE_ENABLED -#include "stm32g0xx_hal_cortex.h" -#endif /* HAL_CORTEX_MODULE_ENABLED */ - -#ifdef HAL_ADC_MODULE_ENABLED -#include "stm32g0xx_hal_adc.h" -#include "stm32g0xx_hal_adc_ex.h" -#endif /* HAL_ADC_MODULE_ENABLED */ - -#ifdef HAL_CEC_MODULE_ENABLED -#include "stm32g0xx_hal_cec.h" -#endif /* HAL_CEC_MODULE_ENABLED */ - -#ifdef HAL_COMP_MODULE_ENABLED -#include "stm32g0xx_hal_comp.h" -#endif /* HAL_COMP_MODULE_ENABLED */ - -#ifdef HAL_CRC_MODULE_ENABLED -#include "stm32g0xx_hal_crc.h" -#endif /* HAL_CRC_MODULE_ENABLED */ - -#ifdef HAL_CRYP_MODULE_ENABLED -#include "stm32g0xx_hal_cryp.h" -#endif /* HAL_CRYP_MODULE_ENABLED */ - -#ifdef HAL_DAC_MODULE_ENABLED -#include "stm32g0xx_hal_dac.h" -#endif /* HAL_DAC_MODULE_ENABLED */ - -#ifdef HAL_EXTI_MODULE_ENABLED -#include "stm32g0xx_hal_exti.h" -#endif /* HAL_EXTI_MODULE_ENABLED */ - -#ifdef HAL_FLASH_MODULE_ENABLED -#include "stm32g0xx_hal_flash.h" -#endif /* HAL_FLASH_MODULE_ENABLED */ - -#ifdef HAL_I2C_MODULE_ENABLED -#include "stm32g0xx_hal_i2c.h" -#endif /* HAL_I2C_MODULE_ENABLED */ - -#ifdef HAL_I2S_MODULE_ENABLED -#include "stm32g0xx_hal_i2s.h" -#endif /* HAL_I2S_MODULE_ENABLED */ - -#ifdef HAL_IRDA_MODULE_ENABLED -#include "stm32g0xx_hal_irda.h" -#endif /* HAL_IRDA_MODULE_ENABLED */ - -#ifdef HAL_IWDG_MODULE_ENABLED -#include "stm32g0xx_hal_iwdg.h" -#endif /* HAL_IWDG_MODULE_ENABLED */ - -#ifdef HAL_LPTIM_MODULE_ENABLED -#include "stm32g0xx_hal_lptim.h" -#endif /* HAL_LPTIM_MODULE_ENABLED */ - -#ifdef HAL_PWR_MODULE_ENABLED -#include "stm32g0xx_hal_pwr.h" -#endif /* HAL_PWR_MODULE_ENABLED */ - -#ifdef HAL_RNG_MODULE_ENABLED -#include "stm32g0xx_hal_rng.h" -#endif /* HAL_RNG_MODULE_ENABLED */ - -#ifdef HAL_RTC_MODULE_ENABLED -#include "stm32g0xx_hal_rtc.h" -#endif /* HAL_RTC_MODULE_ENABLED */ - -#ifdef HAL_SMARTCARD_MODULE_ENABLED -#include "stm32g0xx_hal_smartcard.h" -#endif /* HAL_SMARTCARD_MODULE_ENABLED */ - -#ifdef HAL_SMBUS_MODULE_ENABLED -#include "stm32g0xx_hal_smbus.h" -#endif /* HAL_SMBUS_MODULE_ENABLED */ - -#ifdef HAL_SPI_MODULE_ENABLED -#include "stm32g0xx_hal_spi.h" -#endif /* HAL_SPI_MODULE_ENABLED */ - -#ifdef HAL_TIM_MODULE_ENABLED -#include "stm32g0xx_hal_tim.h" -#endif /* HAL_TIM_MODULE_ENABLED */ - -#ifdef HAL_UART_MODULE_ENABLED -#include "stm32g0xx_hal_uart.h" -#endif /* HAL_UART_MODULE_ENABLED */ - -#ifdef HAL_USART_MODULE_ENABLED -#include "stm32g0xx_hal_usart.h" -#endif /* HAL_USART_MODULE_ENABLED */ - -#ifdef HAL_WWDG_MODULE_ENABLED -#include "stm32g0xx_hal_wwdg.h" -#endif /* HAL_WWDG_MODULE_ENABLED */ - -/* Exported macro ------------------------------------------------------------*/ -#ifdef USE_FULL_ASSERT -/** - * @brief The assert_param macro is used for functions parameters check. - * @param expr If expr is false, it calls assert_failed function - * which reports the name of the source file and the source - * line number of the call that failed. - * If expr is true, it returns no value. - * @retval None - */ -#define assert_param(expr) ((expr) ? (void)0U : assert_failed((uint8_t *)__FILE__, __LINE__)) -/* Exported functions ------------------------------------------------------- */ -void assert_failed(uint8_t *file, uint32_t line); -#else -#define assert_param(expr) ((void)0U) -#endif /* USE_FULL_ASSERT */ - -#ifdef __cplusplus -} -#endif - -#endif /* STM32G0xx_HAL_CONF_H */ - -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/board/TencentOS_tiny_EVB_G0/KEIL/mqtt_iot_explorer_e53_light/techo.json b/board/TencentOS_tiny_EVB_G0/KEIL/mqtt_iot_explorer_e53_light/techo.json deleted file mode 100644 index 041081e2..00000000 --- a/board/TencentOS_tiny_EVB_G0/KEIL/mqtt_iot_explorer_e53_light/techo.json +++ /dev/null @@ -1,40 +0,0 @@ -{ - "version": "1.0", - "profile": { - "ProductId": "LRKSCAZLF1", - "CategoryId": "539" - }, - "properties": [ - { - "id": "power_switch", - "name": "电灯开关", - "desc": "控制电灯开灭", - "required": true, - "mode": "rw", - "define": { - "type": "bool", - "mapping": { - "0": "关", - "1": "开" - } - } - }, - { - "id": "lx", - "name": "光照强度", - "desc": "光照强度", - "mode": "r", - "define": { - "type": "int", - "min": "0", - "max": "20000", - "start": "0", - "step": "1", - "unit": "lx" - }, - "required": false - } - ], - "events": [], - "actions": [] -} \ No newline at end of file diff --git a/board/TencentOS_tiny_EVB_G0/KEIL/mqtt_iot_explorer_e53_light/基于STM32G0+TencentOS-tiny制作一个智能灯.md b/board/TencentOS_tiny_EVB_G0/KEIL/mqtt_iot_explorer_e53_light/基于STM32G0+TencentOS-tiny制作一个智能灯.md new file mode 100644 index 00000000..ee40e9d0 --- /dev/null +++ b/board/TencentOS_tiny_EVB_G0/KEIL/mqtt_iot_explorer_e53_light/基于STM32G0+TencentOS-tiny制作一个智能灯.md @@ -0,0 +1,386 @@ +# 一、智能灯介绍 +传统的家用照明是通过手动拨动物理开关来接通、断开电源进而控制灯泡的亮灭。智能照明则是通过给灯泡增加网络模块这一物理特性,赋予了灯泡连接网络的能力。进而允许用户通过APP、网页、智能接入设备等方式控制灯泡的开关。 + +本项目制作的智能灯可以远程控制开关,调节预设挡位亮度,可以实时检测所处环境的光照强度并在OLED屏幕上显示具体数值。同时还可以将数据实时上报到腾讯云物联网开发平台IoT Explorer,用户可以在“腾讯连连”小程序实时查看数据并控制灯泡。 + +本项目所需要的物品清单有: + +- TOS_EVB_G0开发板 x 1 +- 智慧路灯扩展板E53_SC1 + +![](https://main.qcloudimg.com/raw/a1ace5677ba77d4e5ea42983bd9b0d74.png) + +**成品组装图(安装各接插件时,请务必参考成品图,不要弄错方向,否则可能烧坏MCU)** +![](https://main.qcloudimg.com/raw/503ff0e4e9df234d3d524d0a4d7ed083.jpg) +好啦~接下来开始我们的制作! + +# 二、TOS_EVB_G0开发板 +## 1. 开发板特性 +TOS_EVB_G0 开发板是由腾讯TencentOS-tiny团队设计的一款物联网开发板,板载资源如下: + +![](https://main.qcloudimg.com/raw/45564fdbaf9f5d00ddfda2927c9e6555.jpg) + +- 主控芯片采用STM32G070RB,Flash空间仅有128KB、RAM空间仅有20KB; +- 板载腾讯云定制固件版ESP8266 WIFI模组; +- 板载E53传感器标准接口,方便连接各种E53传感器; +- 板载0.91'OLED显示屏幕; +- 板载8MB SPI Flash,可用于固件升级; +- 板载CH340 转串口连接,可以使用一根USB线连接至电脑,查看串口日志; + +下面是每部分详细的说明。 + +## 2. 开发板硬件电路详解 +### 2.1. 最小系统电路 +最小系统部分由电源电路、时钟电路、Boot电路、复位电路、下载电路组成: +![](https://img-blog.csdnimg.cn/2020091215453594.png) + +### 2.2. WIFI电路 +WIFI部分包括ESP8266 WIFI模组供电电路、串口通信电路: +![](https://img-blog.csdnimg.cn/20200912154639745.png) +### 2.3. E53扩展接口电路 +E53扩展接口供外部传感器连接,包括:一个串口、一个SPI接口、一个IIC接口、四个普通GPIO口、一个ADC模拟采集接口,一个DAC模拟输出接口,如图: +![](https://img-blog.csdnimg.cn/20200912154756810.png) +### 2.4. USB转串口电路 +USB转串口电路将串口信息转换为USB口信号,使得开发板可以直接通过USB线连接至PC,在PC上使用串口助手查看日志: +![](https://img-blog.csdnimg.cn/20200912155018316.png) +### 2.5. OLED显示屏幕电路 +OLED屏幕部分包括OLED供电部分和通信部分: +![](https://img-blog.csdnimg.cn/20200912155131688.png) + +### 2.6. 连接Micor USB线 +![](https://main.qcloudimg.com/raw/a00746e285381da66af9f9eb4b556078.jpg) +### 2.7. 连接ST-Link下载器 +开发板3v3---> STlink 3.3V +开发板DIO---> STlink SWDIO +开发板CLK---> STlink SWCLK +开发板GND---> STlink GND +**请大家参考下图进行连接,千万不要接反接错,以免烧掉MCU** +![](https://main.qcloudimg.com/raw/4ab5937dd0221e4ad1b0109b2f028aa1.jpg) +## 3. 开发板软件环境准备 +### 3.1 MDK软件介绍 + +MDK 即RealView MDK 或MDK-ARM(Microcontroller Development kit),是 ARM 公司收购Keil公司以后,基于uVision界面推出的针对ARM7、ARM9、Cortex-M0、Cortex-M1、Cortex-M2、Cortex-M3、Cortex-R4等ARM处理器的嵌入式软件开发工具。MDK-ARM 集成了业内最领先的技术,包括 uVision4 集成开发环境与 RealView 编译器RVCT。支持 ARM7、ARM9 和最新的Cortex-M3/M1/M0 核处理器,自动配置启动代码,集成 Flash 烧写模块,强大的 Simulation 设备模拟,性能分析等功能,与 ARM 之前的工具包 ADS 等相比,RealView 编译器的最新版本可将性能改善超过 20%。 + +Keil公司开发的ARM开发工具MDK,是用来开发基于ARM核的系列微控制器的嵌入式应用程序。它适合不同层次的开发者使用,包括专业的应用程序开发工程师和嵌入式软件开发的入门者。MDK包含了工业标准的Keil C编译器、宏汇编器、调试器、实时内核等组件,支持所有基于ARM的设备,能帮助工程师按照计划完成项目。 + +### 3.2 MDK安装 + +登录MDK官网,下载MDK5软件,下载地址: +http://www2.keil.com/mdk5 或者 https://www.keil.com/download/product/ + +下载的版本最好在5.24以上,本开发教程以5.24版本为例,双击MDK524应用程序文件,点击next>>。 +![](https://img-blog.csdnimg.cn/20200912160353792.png) + + +打上 I agree前面的勾勾,即是同意一些安装协议。点击next>>。 + +![](https://img-blog.csdnimg.cn/20200912160415757.png) + +选择安装路径,可以默认也可以安装在我们自己建立的文件夹下。点击next>>。 + +![](https://img-blog.csdnimg.cn/20200912160425324.png) + +这里填写的是我们的一些信息,填写完整后,继续next>>。 + +![](https://img-blog.csdnimg.cn/20200912160432240.png) + +然后等待安装完成即可。 + +![](https://img-blog.csdnimg.cn/20200912160448600.png) + +安装完成,点击Finish。 +![](https://img-blog.csdnimg.cn/20200912160455488.png) + +然后会跳出来这个界面,这个我们后面再讲,先点OK,把弹框都叉掉。 +![](https://img-blog.csdnimg.cn/20200912160508347.png) + +激活MDK,导入License,激活MDK后便可使用了。 + +![](https://img-blog.csdnimg.cn/20200912160520293.png) +![](https://img-blog.csdnimg.cn/202009121605276.png) +特别提示:一定要输入License激活MDK软件,建议购买正版License。 + +### 3.3 MDK安装 +安装完MDK后,我们需要安装开发套件中单片机型号对应的Pack。 +**安装方式一** 登录官网:http://www.keil.com/dd2/pack/ + +下载Keil.STM32G0xx_DFP.x.x.x.pack 后安装。 + +**安装方式二** MDK软件上在线安装 + +打开软件,在导航栏打开Pack安装界面,然后选择ok选项。 + +![](https://img-blog.csdnimg.cn/20200912160604878.png) + +![](https://img-blog.csdnimg.cn/20200912160612930.png) + +进入在线安装界面,选着STM32G0XX Pack,点击Install进行安装。 + +![](https://img-blog.csdnimg.cn/20200912160628442.png) +至此,我们开发板的单片机程序开发环境已经搭建完毕,重启MDK软件就可以使用了。 + +### 3.4 ST-Link驱动安装 +前面讲了开发板单片机程序的开发环境的搭建,但是为了将程序烧录到开发板中我们还需要使用仿真器。我们这套开发板选用ST公司的ST-Link V2仿真器进行开发板程序的烧写和仿真,下面介绍ST-Link驱动的安装及环境搭建。 +在ST官网下载ST-Link驱动, +https://www.st.com/content/st_com/zh/products/development-tools/software-development-tools/stm32-software-development-tools/stm32-utilities/stsw-link009.html + +(驱动有2种: 32位电脑系统安装“dpinst_x86”、64位电脑系统安装“dpinst_amd64”)。 + +![](https://img-blog.csdnimg.cn/20200912160942366.png#pic_center) + +运行对应的驱动,安装ST-Link V2驱动程序。安装路径尽量保持默认路径。 + +![](https://img-blog.csdnimg.cn/20200912160951881.png) +安装完成后, 将ST-Link通过USB接口连入电脑。打开“设备管理器”。若看到如下图所示,表示驱动安装成功。 +![](https://img-blog.csdnimg.cn/20200912163834675.png) + + +这里提醒 2 点: +1, 各种 windows 版本设备名称和所在设备管理器栏目可能不一样,例如 WIN10 插上STLINK 后显示的是 STM32 STLINK。 +2, 如果设备名称旁边显示的是黄色的叹号,请直接点击设备名称,然后在弹出的界面点击更新设备驱动 +至此, ST-Link 驱动已经安装完成。接下来大家只需要在 MDK工程里面配置一下 ST-Link即可。 + +### 3.5 CH340串口驱动安装 + +互联网搜索下载CH340 串口芯片的驱动 +安装方法:打开驱动安装程序点击安装即可。 + +![](https://img-blog.csdnimg.cn/20200912161124298.png#pic_center) + +![](https://img-blog.csdnimg.cn/20200912161131269.png) + +注:若安装失败,请先点击卸载,后点击安装。 + +### 3.7 串口调试助手的安装与使用 + +工具下载: http://www.daxia.com/download/sscom.rar + +安装方法:串口调试助手sscom5.13.1是免安装的,解压出压缩包即可直接使用。 +![](https://img-blog.csdnimg.cn/20200912161144942.png) +根据PC和终端之间的连接,选择正确的串行端口。 +打开电脑的设备管理器,在端口列表可以看到PC与开发板连接的端口号。 +![](https://img-blog.csdnimg.cn/20200912163536203.png) + +我这里显示的是COM26,所以要在sscom工具中选择COM26,开发板程序波特率设置为115200,所以我在sscom串口工具中选择115200波特率。 + +# 三、烧写腾讯云定制固件 +## 1. 下载固件 +腾讯云IoT AT指令是一套针对使用通讯模组(2G/4G/NB/WIFI)接入腾讯云物联平台的定制AT指令集,如果通讯模组实现了该指令集,则设备接入和通讯更为简单,所需代码量更少。 ESP8266作为IoT领域使用最广泛的一款WiFi芯片/模组,腾讯云在其通用AT指令基础上,增加了腾讯云IoT AT指令集,形成一个定制的模组固件QCloud_IoT_AT_ESP8266。 + +下载链接:[https://github.com/tencentyun/qcloud-iot-esp-wifi](https://github.com/tencentyun/qcloud-iot-esp-wifi) + +使用git下载的命令如下: +```bash +git clone https://github.com/tencentyun/qcloud-iot-esp-wifi.git +``` +下载之后即可在图中圈出的文件夹中看到定制固件: +![](https://img-blog.csdnimg.cn/20200912164445221.png#pic_center) + +## 2. 下载烧写工具 +使用乐鑫官方下载工具ESPFlashDownloadTool,下载链接如下: + +[https://www.espressif.com/zh-hans/support/download/other-tools](https://www.espressif.com/zh-hans/support/download/other-tools) + +点击下载Flash 下载工具(ESP8266 & ESP32 & ESP32-S2): +![](https://img-blog.csdnimg.cn/20200912170728154.png#pic_center) +下载之后解压即可。 + +## 3. 固件说明 +腾讯云IoT定制的AT模组固件QCloud_IoT_AT_ESP8266,适用于所有FLASH大小为2MB或者2MB以上的ESP8266模组。 + +AT串口使用UART0,默认的Tx为GPIO1,Rx为GPIO3。但因为ESP8266的UART0 默认会在上电启动期间输出一些打印,如果打印信息影响设备功能,可在上电期间将 U0TXD(GPIO1)、U0RXD(GPIO3) 分别与 U0RTS (GPIO15),U0CTS(GPIO13)进行交换,以屏蔽打印。因此提供两个版本的固件: + +- 名称包含UART_1_3的固件串口使用的Tx为GPIO1,Rx为GPIO3。 +- 名称包含UART_15_13的固件串口使用的Tx为GPIO15,Rx为GPIO13。 + +进入QCloud_IoT_AT_ESP8266_FW,本文中我们使用第一个固件: +![](https://img-blog.csdnimg.cn/20200912164714338.png#pic_center) +## 4. 烧写固件 +### 4.1. 切换boot选择端子和串口选择端子 +① 将开发板上的ESP8266启动引脚配置端子切换到低电平; + +② 将开发板上的串口选择端子置于中间,使ESP8266直接与PC串口连接: + +方法参考下图: +![](https://main.qcloudimg.com/raw/733a1c7406bcc2621e751ca07f0be0f7.jpg) + + +### 4.2. 运行Flash烧写工具 +双击工具,运行: +![](https://img-blog.csdnimg.cn/2020091217093379.png) +选择开发者模式: +![](https://img-blog.csdnimg.cn/20200912171110420.png#pic_center) +选择ESP8266下载工具: +![](https://img-blog.csdnimg.cn/20200912171139314.png#pic_center) +### 4.3. 开始下载 +按照图中进行操作: +![](https://img-blog.csdnimg.cn/20200912171454660.png) +点击下载之后,按下开发板上的复位键,Flash下载工具开始下载: +![](https://img-blog.csdnimg.cn/20200912171547158.png) +若没有显示,请检查接线端子是否连接正确。 + +### 4.4. 下载完成 +下载完成之后如图: +![](https://img-blog.csdnimg.cn/20200912171748705.png) +关闭工具,将boot选择引脚拔下,将串口选择端子恢复默认。 + +**注意:完成固件下载后,一定切记将跳线端子恢复到默认状态,默认端子怎么短接可以参考前面完整的组装成品图,否则wifi将没法工作,MCU也没法控制wifi。** +# 四、基于TencentOS tiny接入腾讯云IoT Explorer指引 +基于TencentOS Tiny EVB G0要完成腾讯云IoT Explorer对接,需要完成两个部分的工作。 +一是:腾讯云IoT explorer 上完成项目、产品、设备创建、参数配置 +二是:基于TencentOS Tiny完成应用开发,向腾讯云上报业务数据。 + +# 1. 云端操作步骤 +## 1.1. 新建项目 +登录[腾讯云物联网开发平台](https://cloud.tencent.com/product/iotexplorer),点击新建项目,填写项目名称和简介: + +![](https://main.qcloudimg.com/raw/e8ec3f87b580328d6ef38a64d47ec90b.png) + + +## 2.2. 新建产品 +点击项目名称进入到该项目中,点击新建产品: +![](https://main.qcloudimg.com/raw/fe74a6729cda53a4eab6d82347415e08.png) +产品新建成功后,可在产品列表页查看到: +![](https://main.qcloudimg.com/raw/4f4e045f7d8daebbe7050fcccfdf788f.png) + +## 2.3. 数据模板 +进入产品,点击【数据模板】,点击【导入JSON】,导入下面的JSON代码: +```json +{ + "version": "1.0", + "properties": [ + { + "id": "power_switch", + "name": "电灯开关", + "desc": "控制电灯开灭", + "required": true, + "mode": "rw", + "define": { + "type": "bool", + "mapping": { + "0": "关", + "1": "开" + } + } + }, + { + "id": "lx", + "name": "光照强度", + "desc": "光照强度", + "mode": "r", + "define": { + "type": "int", + "min": "0", + "max": "20000", + "start": "0", + "step": "1", + "unit": "lx" + }, + "required": false + }, + { + "id": "brightness_level", + "name": "亮度等级", + "desc": "亮度等级", + "mode": "rw", + "define": { + "type": "enum", + "mapping": { + "0": "LOW", + "1": "MID", + "2": "HIGH" + } + }, + "required": false + } + ], + "events": [], + "actions": [], + "profile": { + "ProductId": "3P9346G7NL", + "CategoryId": "539" + } +} +``` +导入之后自动根据json文件创建的属性如下: +![](https://main.qcloudimg.com/raw/79499fc777577423d0976f9f4b616e75.png) + +## 2.4. 创建设备 +点击【设备调试】,进入后点击【新建设备】,创建真实设备: +![](https://main.qcloudimg.com/raw/7361a8d69c552ae00931cb5a4ce28254.png) + +创建成功之后进入设备,查看到产品ID、设备名称、设备秘钥: +![](https://main.qcloudimg.com/raw/c90378470875a565a1b3d62941941159.png) + +## 2. 基于TencentOS Tiny 完成设备侧应用开发 +进入 < TencentOS-tiny\board\TencentOS_tiny_EVB_G0\KEIL\\mqtt_iot_explorer_e53_light> 目录,打开TencentOS_tiny.uvprojx工程。 + +### 2.1. 修改WIFI接入信息 +打开`mqtt_iot_explorer_e53_light.c`文件编辑,修改要接入WIFI的名称和密码(建议使用手机热点,不要有特殊字符): +![](https://main.qcloudimg.com/raw/507248c95ae0ebbb3eb0378e95ec6308.png) + +### 2.2. 修改云端对接信息 +同样在此文件内,修改云端配置信息,使用之前在云端查看的信息替换: +![](https://main.qcloudimg.com/raw/51478f697c97b8ffd8311abbdefb5c92.png) + +### 2.3. 编译工程 +点击编译按钮,编译整个工程: +![](https://main.qcloudimg.com/raw/2d3ac56fefd1d6cfa075f3ca121ad93d.png) + +### 2.4. 下载程序 +点击进入工程配置界面按纽,进入工程配置界面: +![](https://main.qcloudimg.com/raw/e896fc448df7635062006dc8d5a8e4aa.png) +选择Debug选项,进入仿真器设置界面。 +![](https://img-blog.csdnimg.cn/20200912163019926.png) + + +下拉仿真器选择列表,选着ST-Link Debugger并勾选右侧Use,点击Settings进入ST-Link V2仿真器配置界面。 +![](https://img-blog.csdnimg.cn/20200912163044635.png) + +开发板设计的程序烧录方式为SW,此处Unit选择ST-Link/V2,且Port选择SW,并确认右侧框内是否检测出SW设备,点击<确认>保存配置。 +![](https://img-blog.csdnimg.cn/20200912163441583.png) +点击确定之后,回到主界面,点击下载按钮: +![](https://main.qcloudimg.com/raw/55a686de1ebf10e101386c62def3dc30.png) + +### 2.5. 使用串口助手查看日志 +运行sscom软件,打开电脑上开发板对应的串口,比如我选择COM5,点击【打开串口】: +![](https://main.qcloudimg.com/raw/27fda47404e3e6001b1aee4fa20344f5.png) +按下开发板上的复位键,程序开始运行,即可在串口助手中看到系统运行打印的日志: +![](https://main.qcloudimg.com/raw/a05e4b4a46f086451fe9c8fadbc7fd67.png) + +## 3. 云端平台查看设备数据 +回到腾讯云物联网开发平台,可以看到设备状态变为【在线】: +![](https://main.qcloudimg.com/raw/6f9d9041aa12013f55ad0c7e548d81ad.png) +点击【设备日志】一栏,可以看到设备上报的光照强度和当前灯光级别数值: +![](https://main.qcloudimg.com/raw/0e4088d1b122cc5f302998cc087adfaa.png) +点击【在线调试】一栏,修改属性【期望值】后点击【发送】,可以对灯泡进行远程控制。 +![](https://main.qcloudimg.com/raw/a77085bdab5a548a0de1edaa87bd001a.png) + +# 五、腾讯连连小程序操作步骤 +## 1. 添加家庭 +手机端在【微信】搜索【腾讯连连】小程序,首次使用需要进入后点击【我的】->【家庭管理】,添加一个你喜欢的名称即可。 + +## 2. 添加调试设备 +返回【首页】,点击右上角“加号”图标: +![](https://img-blog.csdnimg.cn/20200912192517415.png) + +进入后点击右上角扫码图标: +![](https://img-blog.csdnimg.cn/20200912192742454.png) + +在腾讯云物联网开发平台进入【设备调试】,点击对应设备后的【二维码】: +![](https://main.qcloudimg.com/raw/47708ce94ce1b7fd59d1effa168d3dfb.png) + +腾讯连连扫描此二维码即可成功添加设备,添加成功之后如图: +![](https://main.qcloudimg.com/raw/08fdb88f56f524734a3916b8c6fe37e7.png) + +点击此设备即可实时查看数据,并下发控制指令: +![](https://main.qcloudimg.com/raw/4db3af9e6fedc12a158a2a5cb58be2c8.png) + + +## 3. 修改腾讯连连显示面板 +进入【交互开发】,点击【面板配置】一栏: +![](https://img-blog.csdnimg.cn/20200831165713961.png) +在此页面中可以自己根据喜好配置小程序显示面板,如图: +![](https://main.qcloudimg.com/raw/e63ed23e5053852c46e8f2ef12b1ad31.png) + +保存之后,在腾讯连连小程序删除设备,重新扫描绑定设备即可生效。 diff --git a/net/tencent_firmware_module_wrapper/tencent_firmware_module_wrapper.h b/net/tencent_firmware_module_wrapper/tencent_firmware_module_wrapper.h index a8c2a75c..00dfa3d9 100644 --- a/net/tencent_firmware_module_wrapper/tencent_firmware_module_wrapper.h +++ b/net/tencent_firmware_module_wrapper/tencent_firmware_module_wrapper.h @@ -44,7 +44,7 @@ #define PUB_PAYLOAD_MAX_SIZE 200 -#define MQTT_MESSAGE_HANDLE_TASK_STACK_SIZE 512 +#define MQTT_MESSAGE_HANDLE_TASK_STACK_SIZE 2048 #define MQTT_MESSAGE_HANDLE_TASK_PRIO 3 #define MQTT_MESSAGE_NUM_MAX 3 #define MQTT_MESSAGE_POOL_SIZE MQTT_MESSAGE_NUM_MAX*sizeof(mqtt_message_t)