小智物联网开发:打造专属智能助手,开启智能生活新纪元
在物联网蓬勃发展的今天,小智物联网开发正引领着一股创新浪潮,为我们的生活和工作带来前所未有的便利与智能体验。今天,就让我们一起深入探索小智物联网开发的魅力所在。
项目概述
小智物联网开发是一个专注于打造智能、便捷生活体验的创新项目。它以ESP32开发板为核心,融合了先进的语音识别、人工智能以及物联网技术,旨在为用户提供一个可定制化的智能助手解决方案。通过简单的硬件搭建和软件配置,用户能够轻松实现智能家居控制、语音交互等多种功能,让科技真正融入日常生活。
基于小智当前的代码开发属于自己的小智机器人
第一步:关闭小智的自我更新机制
为了确保我们烧入的自定义代码不会被小智的自我更新机制所覆盖,我们需要先关闭其自我更新功能。具体操作是注释掉xiaozhi-esp32-main\main\application.cc
中的以下代码:
ota_.SetCheckVersionUrl(CONFIG_OTA_VERSION_URL);
ota_.SetHeader("Device-Id", SystemInfo::GetMacAddress().c_str());
ota_.SetHeader("Client-Id", board.GetUuid());
ota_.SetHeader("X-Language", Lang::CODE);
xTaskCreate([](void* arg) { // 创建检查新版本的任务
Application* app = (Application*)arg;
app->CheckNewVersion();
vTaskDelete(NULL);
}, "check_new_version", 4096 * 2, this, 1, nullptr);
完成上述操作后,重新为ESP32S3烧录版本,这样小智就不会再进行自动更新了。
第二步:添加自己的物联网设备——以添加舵机为例
为了让小智具备更多的功能,我们可以为其添加物联网设备。这里以添加舵机作为小智的手臂为例,详细说明操作步骤。
1. 创建舵机控制文件
在路径xiaozhi-esp32-main\main\iot\things
下添加一个名为arm.cc
的文件,其内容如下:
#include "iot/thing.h"
#include "board.h"
#include <driver/gpio.h>
#include <esp_log.h>
#include "esp_err.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
#include "driver/mcpwm_prelude.h"
#define TAG "Arm"
#define SERVO_MIN_PULSEWIDTH_US 500 // Minimum pulse width in microsecond
#define SERVO_MAX_PULSEWIDTH_US 2500 // Maximum pulse width in microsecond
#define SERVO_MIN_DEGREE -90 // Minimum angle
#define SERVO_MAX_DEGREE 90 // Maximum angle
#define SERVO_PULSE_GPIO 19 // GPIO connects to the PWM signal line
#define SERVO_TIMEBASE_RESOLUTION_HZ 1000000 // 1MHz, 1us per tick
#define SERVO_TIMEBASE_PERIOD 20000 // 20000 ticks, 20ms
static inline uint32_t example_angle_to_compare(int angle)
{
return (angle - SERVO_MIN_DEGREE) * (SERVO_MAX_PULSEWIDTH_US - SERVO_MIN_PULSEWIDTH_US) / (SERVO_MAX_DEGREE - SERVO_MIN_DEGREE) + SERVO_MIN_PULSEWIDTH_US;
}
namespace iot {
// 这里仅定义 Arm 的属性和方法,不包含具体的实现
class Arm : public Thing {
private:
bool power_ = false;
mcpwm_cmpr_handle_t comparator = NULL;
void example_ledc_init(void) {
ESP_LOGI(TAG, "Create timer and operator");
mcpwm_timer_handle_t timer = NULL;
mcpwm_timer_config_t timer_config = {
.group_id = 0,
.clk_src = MCPWM_TIMER_CLK_SRC_DEFAULT,
.resolution_hz = SERVO_TIMEBASE_RESOLUTION_HZ,
.count_mode = MCPWM_TIMER_COUNT_MODE_UP,
.period_ticks = SERVO_TIMEBASE_PERIOD,
};
ESP_ERROR_CHECK(mcpwm_new_timer(&timer_config, &timer));
mcpwm_oper_handle_t oper = NULL;
mcpwm_operator_config_t operator_config = {
.group_id = 0, // operator must be in the same group to the timer
};
ESP_ERROR_CHECK(mcpwm_new_operator(&operator_config, &oper));
ESP_LOGI(TAG, "Connect timer and operator");
ESP_ERROR_CHECK(mcpwm_operator_connect_timer(oper, timer));
ESP_LOGI(TAG, "Create comparator and generator from the operator");
mcpwm_comparator_config_t comparator_config = {
0, // intr_priority
{true} // flags.update_cmp_on_tez
};
ESP_ERROR_CHECK(mcpwm_new_comparator(oper, &comparator_config, &comparator));
mcpwm_gen_handle_t generator = NULL;
mcpwm_generator_config_t generator_config = {
.gen_gpio_num = SERVO_PULSE_GPIO,
};
ESP_ERROR_CHECK(mcpwm_new_generator(oper, &generator_config, &generator));
// set the initial compare value, so that the servo will spin to the center position
ESP_ERROR_CHECK(mcpwm_comparator_set_compare_value(comparator, example_angle_to_compare(0)));
ESP_LOGI(TAG, "Set generator action on timer and compare event");
// go high on counter empty
ESP_ERROR_CHECK(mcpwm_generator_set_action_on_timer_event(generator,
MCPWM_GEN_TIMER_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP, MCPWM_TIMER_EVENT_EMPTY, MCPWM_GEN_ACTION_HIGH)));
// go low on compare threshold
ESP_ERROR_CHECK(mcpwm_generator_set_action_on_compare_event(generator,
MCPWM_GEN_COMPARE_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP, comparator, MCPWM_GEN_ACTION_LOW)));
ESP_LOGI(TAG, "Enable and start timer");
ESP_ERROR_CHECK(mcpwm_timer_enable(timer));
ESP_ERROR_CHECK(mcpwm_timer_start_stop(timer, MCPWM_TIMER_START_NO_STOP));
}
public:
Arm() : Thing("Arm", "你的手臂,可以运动"), power_(false) {
// Set the LEDC peripheral configuration
example_ledc_init();
// 定义设备的属性
properties_.AddBooleanProperty("power", "手臂是否运动", [this]() -> bool {
return power_;
});
// 定义设备可以被远程执行的指令
methods_.AddMethod("TurnOn", "手臂运用", ParameterList(), [this](const ParameterList& parameters) {
power_ = true;
int angle = 0;
ESP_LOGI(TAG, "Angle of rotation: %d", angle);
ESP_ERROR_CHECK(mcpwm_comparator_set_compare_value(comparator, example_angle_to_compare(angle)));
//Add delay, since it takes time for servo to rotate, usually 200ms/60degree rotation under 5V power supply
vTaskDelay(pdMS_TO_TICKS(500));
angle = 20;
ESP_LOGI(TAG, "Angle of rotation: %d", angle);
ESP_ERROR_CHECK(mcpwm_comparator_set_compare_value(comparator, example_angle_to_compare(angle)));
vTaskDelay(pdMS_TO_TICKS(500));
angle = 40;
ESP_LOGI(TAG, "Angle of rotation: %d", angle);
ESP_ERROR_CHECK(mcpwm_comparator_set_compare_value(comparator, example_angle_to_compare(angle)));
vTaskDelay(pdMS_TO_TICKS(500));
angle = 60;
ESP_LOGI(TAG, "Angle of rotation: %d", angle);
ESP_ERROR_CHECK(mcpwm_comparator_set_compare_value(comparator, example_angle_to_compare(angle)));
vTaskDelay(pdMS_TO_TICKS(500));
angle = -60;
ESP_LOGI(TAG, "Angle of rotation: %d", angle);
ESP_ERROR_CHECK(mcpwm_comparator_set_compare_value(comparator, example_angle_to_compare(angle)));
});
methods_.AddMethod("TurnOff", "手臂返回原来的位置", ParameterList(), [this](const ParameterList& parameters) {
power_ = false;
// ****
});
}
};
} // namespace iot
DECLARE_THING(Arm);
2. 更新JSON配置文件
在main\iot\sample_interface.json
文件中添加舵机的相关说明:
{
"name": "arm",
"description": "AI机器人的手臂",
"properties": {
"power": {
"type": "boolean",
"description": "当前手臂是否在运动"
}
},
"methods": {
"TurnOn": {
"description": "手臂在运动"
}
}
},
3. 初始化舵机设备
在main\boards\bread-compact-wifi\compact_wifi_board.cc
(根据自己的板子适配的文件)中的物联网初始化函数中添加舵机的初始化函数:
// 物联网初始化,添加对 AI 可见设备
void InitializeIot() {
auto& thing_manager = iot::ThingManager::GetInstance();
thing_manager.AddThing(iot::CreateThing("Speaker"));
thing_manager.AddThing(iot::CreateThing("Lamp"));
thing_manager.AddThing(iot::CreateThing("Arm")); // 添加物联网things中的Arm类(为新加)
}
完成以上步骤后,重新编译工程并为小智刷入新的版本,现在你就可以通过语音指令呼唤小智动手臂了。
小智输出结果展示:
I (573923) LS_learner: ****>> start
I (573923) LS_learner: 麦克风输入>> 动一下手臂。
I (573923) LS_learner: 麦克风输入>> {
"type": "stt",
"text": "动一下手臂。",
"session_id": "b30cf390"
}
I (573943) LS_learner: STATE: speaking
I (574223) LS_learner: 物联网情况<< {
"type": "iot",
"commands": [{
"name": "Arm",
"method": "TurnOn",
"parameters": {
}
}],
"session_id": "b30cf390"
}
I (574233) LS_learner: COMMAND<< [{
"name": "Arm",
"method": "TurnOn",
"parameters": {
}
}]
I (574263) Arm: Angle of rotation: 0
I (574533) LS_learner: 物联网情况<< {
"type": "iot",
"commands": [{
"name": "Arm",
"method": "TurnOff",
"parameters": {
}
}],
"session_id": "b30cf390"
}
I (574543) LS_learner: COMMAND<< [{
"name": "Arm",
"method": "TurnOff",
"parameters": {
}
}]
I (574593) LS_learner: 表情输出情况<< {
"type": "llm",
"text": "😊",
"emotion": "happy",
"session_id": "b30cf390"
}
I (574763) Arm: Angle of rotation: 20
I (574863) LS_learner: 扬声器输出<< 好的,我动了动手臂,感觉挺好玩的!
I (574863) LS_learner: 扬声器输出<< {
"type": "tts",
"state": "sentence_start",
"text": "好的,我动了动手臂,感觉挺好玩的!",
"session_id": "b30cf390"
}
I (575263) Arm: Angle of rotation: 40
I (575763) Arm: Angle of rotation: 60
I (576263) Arm: Angle of rotation: -60
I (579173) LS_learner: 扬声器输出<< 你今天过得怎么样呢?
I (579173) LS_learner: 扬声器输出<< {
"type": "tts",
"state": "sentence_start",
"text": "你今天过得怎么样呢?",
"session_id": "b30cf390"
}
I (582033) LS_learner: ****>> stop
总结与展望
小智物联网开发以其强大的功能、开放的架构和低成本的优势,为物联网爱好者和开发者提供了一个广阔的创作平台。通过简单的硬件搭建和软件配置,我们能够轻松打造出属于自己的智能生活助手,让科技真正融入日常生活。未来,随着技术的不断进步和创新,小智物联网开发将为我们带来更多的惊喜和可能,让我们共同期待它在智能时代的精彩表现!