本帖最后由 7788 于 2024-5-19 06:22 编辑
本帖最后由 7788 于 2024-5-11 14:12 编辑
本帖最后由 7788 于 2024-5-10 17:52 编辑
在论坛这么久了,终于有个活动可以让我薅个羊毛了
特别感谢安信可,感谢园长
接下来,现在跟着莫哥的脚步,搭建一个HA点灯了
本篇,先按莫哥教程搭建环境
安装依赖
sudo apt-get install make gcc vim cmake git ninja-build -y
克隆SDK
git clone https://gitee.com/Ai-Thinker-Open/AiPi-Open-Kits.git
拉取子模块
cd AiPi-Open-Kits/
git submodule init
git submodule update
. update_sdk.sh
. export.sh
编译例程
cd examples/blank/
make
例程编译完成,完成了一半了
修改SDK配置文件
修改55行 COM5 改成 : /dev/ttyUSB0
修改88行 flash: 改成 : flash:build
make flash
日志输出,环境搭建没问题了
对接HomeAssistant
下载SDK
cd example
git clone https://gitee.com/Ai-Thinker-Open/HomeAssistant_example.git
拉取子模块
cd HomeAssistant_example/
git submodule init
git submodule update
复制例程
cp HomeAssistant_basic/ -r HomeAssistant_switch
修改配置文件
flash_prog_cfg.ini
修改wifi名称密码
修改MQTT信息
莫哥帖子中没加password,记得加上
应该可以make了
烧录完成
HA显示
修改代码,完成点灯
#include <FreeRTOS.h>
#include "task.h"
#include "queue.h"
#include "board.h"
#include "bluetooth.h"
#include "btble_lib_api.h"
#include "bl616_glb.h"
#include "rfparam_adapter.h"
#include "bflb_mtd.h"
#include "easyflash.h"
#include "wifi_event.h"
#include "log.h"
#include "aiio_os_port.h"
#include "homeAssistantMQTT.h"
struct bflb_device_s* gpio;//全局变量
#define DBG_TAG "MAIN"
void ha_event_cb(ha_event_t event, homeAssisatnt_device_t* ha_dev)
{
switch (event)
{
case HA_EVENT_MQTT_CONNECED:
LOG_I("<<<<<<<<<< HA_EVENT_MQTT_CONNECED");
//一定要加static
static ha_sw_entity_t entity_sw1 = {
.name = "开关1",
.icon = "mdi:power",
.unique_id = "switch_v1",
};
homeAssistant_device_add_entity(CONFIG_HA_ENTITY_SWITCH, &entity_sw1);
homeAssistant_device_send_status(HOMEASSISTANT_STATUS_ONLINE);
homeAssistan_device_send_entity_state(CONFIG_HA_ENTITY_SWITCH, &entity_sw1, 0);
break;
case HA_EVENT_MQTT_DISCONNECT:
LOG_I("<<<<<<<<<< HA_EVENT_MQTT_DISCONNECT");
break;
case HA_EVENT_MQTT_COMMAND_SWITCH:
if (ha_dev->entity_switch->command_switch->switch_state) {
bflb_gpio_set(gpio, GPIO_PIN_15);
}
else bflb_gpio_reset(gpio, GPIO_PIN_15);
LOG_I("<<<<<<<<<< HA_EVENT_MQTT_COMMAND_SWITCH");
// LOG_I("switch addr =%p", ha_dev->entity_switch->command_switch);
LOG_I(" switch %s is %s", ha_dev->entity_switch->command_switch->name, ha_dev->entity_switch->command_switch->switch_state?"true":"flase");
int ret = homeAssistan_device_send_entity_state(CONFIG_HA_ENTITY_SWITCH, ha_dev->entity_switch->command_switch, ha_dev->entity_switch->command_switch->switch_state);
if (ret!=-1)LOG_I("%s send entity suceess,state=%s", ha_dev->entity_switch->command_switch->name, ha_dev->entity_switch->command_switch->switch_state?"true":"flase");
break;
default:
break;
}
}
int main(void)
{
board_init();
bflb_mtd_init();
easyflash_init();
// aiio_log_init();
gpio = bflb_device_get_by_name("gpio");
bflb_gpio_init(gpio, GPIO_PIN_15, GPIO_OUTPUT | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_0);
bflb_gpio_reset(gpio, GPIO_PIN_15);
if (0 != rfparam_init(0, NULL, 0)) {
LOG_E("PHY RF init failed!\r\n");
return 0;
}
staWiFiInit();
// homeassistant_blufi_init();
static homeAssisatnt_device_t ha_device;
ha_device.mqtt_info.mqtt_clientID="Ai-M6X_Kit_Switch";
ha_device.mqtt_info.mqtt_host="";
ha_device.mqtt_info.port=1883;
ha_device.mqtt_info.mqtt_username="";
ha_device.mqtt_info.mqtt_password="";
ha_device.name="7788"; //设备名称
ha_device.model="anxinke";//模块名称
ha_device.manufacturer="anxinke_moge";//产商名称
homeAssistant_device_init(&ha_device, ha_event_cb);
vTaskStartScheduler();
while (1) {
LOG_F("HomeAssistant free heap size=%d", aiio_os_get_free_heap_size());
vTaskDelay(pdMS_TO_TICKS(3000));
}
}
效果图