在使用ESP32 版本的FreeRTOS SDK连接小爱同学时,通过语音让小爱同学对ESP32进行开关灯操作,小爱同学一直提示"要操作的设备好像出问题了等一会再试吧",但是所控制的灯可以被操作。在查看log后,发现系统推送给ESP32的MQTT主题可以被EPS32收到,ESP32也会推送给系统MQTT主题。用arduino开发时就不会出现这个问题。
以下是使用FreeRTOS开发时的代码。
- /* Hello World Example
- This example code is in the Public Domain (or CC0 licensed, at your option.)
- Unless required by applicable law or agreed to in writing, this
- software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
- CONDITIONS OF ANY KIND, either express or implied.
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "blinker_api.h"
- #include "esp_log.h"
- #include "driver/gpio.h"
- static const char *TAG = "blinker";
- #define BUTTON_1 "btn-abc"
- #define NUM_1 "num-abc"
- static int count = 0;
- void button1_callback(const blinker_widget_param_val_t *val)
- {
- ESP_LOGI(TAG, "button state: %s", val->s);
- count++;
- cJSON *param = cJSON_CreateObject();
- blinker_widget_switch(param, val->s);
- blinker_widget_print(BUTTON_1, param);
- cJSON_Delete(param);
- cJSON *num_param = cJSON_CreateObject();
- blinker_widget_color(num_param, "#FF00FF");
- blinker_widget_text(num_param, "按键测试");
- blinker_widget_unit(num_param, "次");
- blinker_widget_value_number(num_param, count);
- blinker_widget_print(NUM_1, num_param);
- cJSON_Delete(num_param);
- printf("gpio stata : %d\r\n", gpio_get_level(12));
- gpio_set_level(12, (~gpio_get_level(12)) & 0x01);
- printf("gpio stata : %d\r\n", gpio_get_level(12));
- }
- static void data_callback(const char *data)
- {
- ESP_LOGI(TAG, "data: %s", data);
- }
- static void blinker_miot_cb_t(const blinker_va_param_cb_t *val)
- {
- cJSON *param = cJSON_CreateObject();
- char buffer[8];
- memset(buffer, 0, 8);
- switch (val->type)
- {
- case BLINKER_PARAM_POWER_STATE:
- {
- if (strcmp(BLINKER_CMD_ON, val->s) == 0)
- {
- blinker_miot_power_state(param, BLINKER_CMD_ON);
- blinker_miot_print(param);
- gpio_set_level(12, 1);
- }
- else if (strcmp(BLINKER_CMD_OFF, val->s) == 0)
- {
- blinker_miot_power_state(param, BLINKER_CMD_TRUE);
- blinker_miot_print(param);
- gpio_set_level(12, 0);
- }
- break;
- }
- default:
- break;
- }
- cJSON_Delete(param);
- }
- void app_main()
- {
- gpio_config_t gpio_cfg = {};
- gpio_cfg.intr_type = GPIO_INTR_DISABLE;
- gpio_cfg.mode = GPIO_MODE_INPUT_OUTPUT;
- gpio_cfg.pin_bit_mask = (1ULL << 12);
- gpio_cfg.pull_up_en = GPIO_PULLUP_ENABLE;
- /// 初始化GPIO口
- gpio_config(&gpio_cfg);
- blinker_init();
- blinker_widget_add(BUTTON_1, BLINKER_BUTTON, button1_callback);
- blinker_data_handler(data_callback);
- blinker_miot_handler_register(blinker_miot_cb_t);
- }
复制代码
|