智能家居的話,如果只能手動控制會覺得不太智能,如果能用說的或者自動的話體驗可能會更好一些。
智能家居其實就是為了給生活添加一些便利,懶人推送科技發(fā)展是沒錯的。
對于個人而言接入智能家居無非有以下幾個原因:
1、動動手動動嘴不動腿就是懶恨不能意念控制
2、開關(guān)對小朋友不友好,個頭不夠摸不到開關(guān)
3、全屋聯(lián)動回家模式離家模式等等
基于以上幾個原因
通過嘗試。實現(xiàn)了語音控制 HA 設(shè)備的基本功能。
主要思路如圖:
開始想著增加機械開關(guān)手動去操作,后來想了以下都語音控制了還要物理開關(guān)干嘛呢。
小朋友和老人,直接語音控制開關(guān)設(shè)備。就很舒服呢。
對于紅外設(shè)備,還沒研究透徹怎么去獲取遙控器的紅外編碼,然后發(fā)射出來。這樣就可以實現(xiàn)更多的功能。
可以語音控制空調(diào)、電視等紅外設(shè)備。
[智能家居]MQTT 控制 HomeAssistant 設(shè)備
https://bbs.ai-thinker.com/forum.php?mod=viewthread&tid=44644&fromuid=15918
(出處: 物聯(lián)網(wǎng)開發(fā)者社區(qū)-安信可論壇)
這里主要還是用到了 HomeAssistant 的自動化功能,這種設(shè)置不需要對 HomeAssistant 有很深的了解,簡單配置即可。
對于小白用戶體驗更好一些,上手就用。對于大佬的話,可以延伸出更多的功能。
下面介紹一下主要功能模塊
一、離線語音模組系列
VC 系列模組是我司開發(fā)的一款 AI 離線語音識別的產(chǎn)品,主芯片是云知聲推出的離線語音識別芯片鋒鳥 M(US516P6),具有高可靠性,通用性強的特點。在語音識別技術(shù)上實現(xiàn)了高可靠的喚醒識別率、更遠距離的喚醒、更低誤喚醒率、更強的抗噪音能力、更快的響應(yīng)識別時間,免聯(lián)網(wǎng)的純離線識別。
VC 系列模組采用了 32bit RISC 架構(gòu)內(nèi)核,并加入了專門針對信號處理和語音識別所需要的 DSP 指令集,支持浮點運算的 FPU 運算單元,以及 FFT 加速器。支持最高 150 條本地指令離線識別,支持 RTOS 輕量級系統(tǒng),以及簡單友好的客制化工具。
VC 系列模組具有豐富的外圍接口,包括 UART/I2C/PWM/SPI,以及簡單友好的二次開發(fā)工具,方便客戶實現(xiàn)單模組的語音控制應(yīng)用場景方案。
VC-02 模組
揚聲器功率盡量不要太大,如果用 M61-32S 供電揚聲器功率過高會導(dǎo)致掉電。
揚聲器:
SPK-8Ω2W
SPK+8Ω2W
官方資料
VC-02 模組規(guī)格書:
中文: https://docs.ai-thinker.com/_media/vc-02_v1.0.0%E8%A7%84%E6%A0%BC%E4%B9%A6_516.pdf
EN:https://docs.ai-thinker.com/_media/vc-02_v1.0.0_specification_516.pdf
VC-02 原理圖:https://docs.ai-thinker.com/_media/vc-02_v1.0_%E5%8E%9F%E7%90%86%E5%9B%BE-20220531.pdf
VC-02 封裝: https://mp.weixin.qq.com/s?__biz=MzIzODA0NDgxNg==&mid=2650092833&idx=5&sn=b8dfe99e2b08c31a704256f79c351b47&chksm=f13ec6dec6494fc88d033ff313ab824fc41695f4a188a9e98b237bf563e0c6fc07a1c5f4fabd&token=2098411327&lang=zh_CN#rd
串口燒錄工具
定制語音
安信可語音開放平臺:http://voice.ai-thinker.com/
在安信可語音開放平臺創(chuàng)建產(chǎn)品,選擇 VC 系列模組。
然后修改喚醒詞
設(shè)置 Pin 引腳功能
波特率設(shè)置 115200,引腳 B6 設(shè)置為 UART1_RX 引腳 B7 設(shè)置為 UART1_TX,用來發(fā)送接收數(shù)據(jù)。注意不要配置多個 UART1,否則不生效。
添加語音指令
然后添加控制詳情
這里主要用到的就是串口數(shù)據(jù),這里發(fā)送指令 第一位是指令開頭默認 A1,第二位定義的是設(shè)備位置,第三位定義的是設(shè)備類型,第四位定義的是開關(guān)狀態(tài),第五位結(jié)束位默認 FF。根據(jù)自己的需要設(shè)置解析即可。
還有免喚醒命令,不用喚醒詞直接就可以用,比如開燈、關(guān)燈等等。
免喚醒命令詞最多 10 個選擇自己需要的添加,添多了也是默認取前 10 條。
二、Ai-M61-32S Wi-Fi 模組
Ai-M61 系列模組(下稱模組)是由深圳市安信可科技有限公司開發(fā)的 Wi-Fi6 & 藍牙雙模模組,搭載 BL618 芯片作為處理器,支持 Wi-Fi 802.11b/g/n/ax 協(xié)議和 BLE 5.3 協(xié)議。BL618 芯片內(nèi)置低功耗的 32 位 RISC-V CPU,最高主頻可達 320M. 豐富的外圍接口,包括 DVP、MJPEG、Dispaly、Audio Codec、USB2.0、SDU、以太網(wǎng)(EMAC)、SD/MMC(SDH)、SPI、UART、I2C、I2S、PWM、GPDAC、GPADC、ACOMP 和 GPIO 等。可廣泛應(yīng)用于音視頻多媒體、物聯(lián)網(wǎng)(IoT)、移動設(shè)備、可穿戴電子設(shè)備、智能家居等領(lǐng)域。
M61-32S 模組
官方資料
Ai-M61-32S-Kit 開發(fā)板規(guī)格書:
中文:https://docs.ai-thinker.com/_media/ai-m61-32s-kit_v1.1.2_product_specification_cn.pdf
EN:https://docs.ai-thinker.com/_media/ai-m61-32s-kit_v1.1.2_product_specification_en.pdf
Ai-M61-32S 開發(fā)板原理圖:https://docs.ai-thinker.com/_media/nodemcu-ai-m61-32s-kit_v1.1.pdf
Ai-M61-32S 模組規(guī)格書:
中文 :https://docs.ai-thinker.com/_media/ai-m61-32s_v1.3.0_product_specification_cn.pdf
EN:https://docs.ai-thinker.com/_media/ai-m61-32s_v1.3.0_product_specification_en.pdf
Ai-M61 系列出廠固件
固件下載工具:點擊下載
固件燒錄指南:M61/M62 系列燒錄指導(dǎo)(包含模組&開發(fā)板)https://blog.csdn.net/Boantong_/article/details/140183535?
三、代碼部分
主要用 wifi、mqtt、uart、flash
uart 代碼主要參考
【完全開源】智能桌面助手——AiPi-DSL_Dashboard
主程序代碼
#include
#include
#include
#include
#include
#include
#include "log.h"
#include "board.h"
#include "mem.h"
//easy flash
#include "easyflash.h"
#include "bflb_mtd.h"
#include "bl_fw_api.h"
#include "wifi_mgmr_ext.h"
#include "wifi_mgmr.h"
#include "ha_task.h"
#include "wifi_event.h"
#include "voice_uart.h"
int main(void)
{
board_init();
wifi_start_firmware_task();
//init easyflash
bflb_mtd_init();
easyflash_init();
xTaskCreate(voice_uart_task, "uart task", 1024, NULL, 10, NULL);
xTaskCreate(queue_receive_task, "queue_receive_task", 1024*3, NULL, 3, NULL);
vTaskStartScheduler();
}
VC02 模組交互代碼
#include
#include
#include
#include
#include
#include
#include
#include "ha_task.h"
#include "bflb_uart.h"
#include "bflb_gpio.h"
#include "voice_uart.h"
#include "user_mqtt.h"
#include "log.h"
#define GBD_TAG "UART"
static void uart_init(void);
extern xQueueHandle queue;
uart_rx_cmd_t uart_cmd;
static struct bflb_device_s* uart1;
static char uart_buff[5] = { 0 };
static const uart_data_t g_uart_buf[] = {
{{0xA1, 0x00, 0x01, 0x00, 0xFF}, 5}, //wakeup_uni
{{0xA1, 0x00, 0x01, 0x01, 0xFF}, 5}, //openL
{{0xA1, 0x00, 0x01, 0x00, 0xFF}, 5}, //closeL
};
void voice_uart_task(void* arg)
{
uart_init();
while (1) {
vTaskDelay(1000/portTICK_PERIOD_MS);
}
}
static int voice_cmd_check(char* buff, int len)
{
int data_cnt = 0, j = 0;
// char* uart_txbuf = pvPortMalloc(4);
// memset(uart_txbuf, 0, 4);
while (1) {
for (size_t i = 0; i < 5; i++)
{
if (buff[i]==g_uart_buf[j].data[i]) data_cnt++;
else break;
}
if (data_cnt==g_uart_buf-?>len) break;
data_cnt = 0;
j++;
if (j>23) return -1;
}
LOG_I("check uart cmd=%d", j);
return j;
}
static void uart_isr(int irq, void* arg)
{
uint32_t intstatus = bflb_uart_get_intstatus(uart1);
uint32_t rx_data_len = 0;
char data_type[1] = { 0 };
if (intstatus & UART_INTSTS_RX_FIFO) {
int index = 0;
memset(uart_buff, 0, 5);
LOG_I("rx fiforn");
while (bflb_uart_rxavailable(uart1)) {
uart_buff[index++] = bflb_uart_getchar(uart1);
}
LOG_I("uart recv:%02X %02X %02X %02X %02X", uart_buff[0], uart_buff[1], uart_buff[2], uart_buff[3], uart_buff[4]);
}
}
/**
* @brief
*/
static void uart_init(void)
{
struct bflb_device_s* gpio;
struct bflb_uart_config_s cfg = {
.baudrate = 115200,
.data_bits = UART_DATA_BITS_8,
.stop_bits = UART_STOP_BITS_1,
.parity = UART_PARITY_NONE,
.flow_ctrl = 0,
.tx_fifo_threshold = 4,
.rx_fifo_threshold = 4,
};
gpio = bflb_device_get_by_name("gpio");
uart1 = bflb_device_get_by_name("uart1");
bflb_gpio_uart_init(gpio, GPIO_PIN_25, GPIO_UART_FUNC_UART1_TX);
bflb_gpio_uart_init(gpio, GPIO_PIN_26, GPIO_UART_FUNC_UART1_RX);
bflb_uart_init(uart1, &cfg);
bflb_uart_txint_mask(uart1, false);
bflb_uart_rxint_mask(uart1, false);
bflb_irq_attach(uart1->irq_num, uart_isr, NULL);
bflb_irq_enable(uart1->irq_num);
}
MQTT 與 WIFI
wifi
#include "FreeRTOS.h"
#include "task.h"
#include "timers.h"
#include
#include
#include
#include "bl_fw_api.h"
#include "wifi_mgmr_ext.h"
#include "wifi_mgmr.h"
#include "bflb_irq.h"
#include "bflb_uart.h"
#include "bflb_l1c.h"
#include "bflb_mtimer.h"
#include "bl616_glb.h"
#include "rfparam_adapter.h"
#include "board.h"
#include "log.h"
#include "ha_task.h"
#include "config.h"
#define DBG_TAG "WIFI EVENT"
#define WIFI_STACK_SIZE (1024*4)
#define TASK_PRIORITY_FW (16)
static wifi_conf_t conf =
{
.country_code = "CN",
};
static TaskHandle_t wifi_fw_task;
static uint32_t sta_ConnectStatus = 0;
extern TaskHandle_t https_Handle;
/**
* @brief WiFi 任務(wù)
* @return int
*/
int wifi_start_firmware_task(void)
{
LOG_I("Starting wifi ...");
/* enable wifi clock */
GLB_PER_Clock_UnGate(GLB_AHB_CLOCK_IP_WIFI_PHY | GLB_AHB_CLOCK_IP_WIFI_MAC_PHY | GLB_AHB_CLOCK_IP_WIFI_PLATFORM);
GLB_AHB_MCU_Software_Reset(GLB_AHB_MCU_SW_WIFI);
/* set ble controller EM Size */
GLB_Set_EM_Sel(GLB_WRAM160KB_EM0KB);
if (0 != rfparam_init(0, NULL, 0)) {
LOG_I("PHY RF init failed!");
return 0;
}
LOG_I("PHY RF init success!");
/* Enable wifi irq */
extern void interrupt0_handler(void);
bflb_irq_attach(WIFI_IRQn, (irq_callback)interrupt0_handler, NULL);
bflb_irq_enable(WIFI_IRQn);
xTaskCreate(wifi_main, (char*)"fw", WIFI_STACK_SIZE, NULL, TASK_PRIORITY_FW, &wifi_fw_task);
return 0;
}
/**
* @brief wifi event handler
* WiFi 事件回調(diào)
* @param code
*/
void wifi_event_handler(uint32_t code)
{
sta_ConnectStatus = code;
switch (code) {
case CODE_WIFI_ON_INIT_DONE:
{
LOG_I("[APP] [EVT] %s, CODE_WIFI_ON_INIT_DONE", __func__);
wifi_mgmr_init(&conf);
}
break;
case CODE_WIFI_ON_MGMR_DONE:
{
LOG_I("[APP] [EVT] %s, CODE_WIFI_ON_MGMR_DONE", __func__);
}
break;
case CODE_WIFI_ON_SCAN_DONE:
{
char* scan_msg = pvPortMalloc(128);
wifi_mgmr_sta_scanlist();
LOG_I("[APP] [EVT] %s, CODE_WIFI_ON_SCAN_DONE SSID numbles:%d", __func__, wifi_mgmr_sta_scanlist_nums_get());
sprintf(scan_msg, "{"wifi_scan":{"status":0}}");
vPortFree(scan_msg);
}
break;
case CODE_WIFI_ON_CONNECTED:
{
LOG_I("[APP] [EVT] %s, CODE_WIFI_ON_CONNECTED", __func__);
void mm_sec_keydump();
mm_sec_keydump();
}
break;
case CODE_WIFI_ON_GOT_IP:
{
LOG_I("[APP] [EVT] %s, CODE_WIFI_ON_GOT_IP", __func__);
mqtt_client_init(MQTT_HOST, MQTT_PORT);
mqtt_client_register_event();
vTaskDelay(500/portTICK_PERIOD_MS);
mqtt_start_connect(MQTT_HOST, MQTT_PORT, MQTT_USERNAME, MQTT_PASSWORD);
mqtt_app_subscribe("sub", 0);
}
break;
case CODE_WIFI_ON_DISCONNECT:
{
LOG_I("[APP] [EVT] %s, CODE_WIFI_ON_DISCONNECT", __func__);
}
break;
case CODE_WIFI_ON_AP_STARTED:
{
LOG_I("[APP] [EVT] %s, CODE_WIFI_ON_AP_STARTED", __func__);
}
break;
case CODE_WIFI_ON_AP_STOPPED:
{
LOG_I("[APP] [EVT] %s, CODE_WIFI_ON_AP_STOPPED", __func__);
}
break;
case CODE_WIFI_ON_AP_STA_ADD:
{
LOG_I("[APP] [EVT] [AP] [ADD] %lld", xTaskGetTickCount());
}
break;
case CODE_WIFI_ON_AP_STA_DEL:
{
LOG_I("[APP] [EVT] [AP] [DEL] %lld", xTaskGetTickCount());
}
break;
default:
{
LOG_I("[APP] [EVT] Unknown code %u ", code);
}
}
}
uint8_t wifi_connect(char* ssid, char* passwd)
{
int ret = 255;
// struct fhost_vif_ip_addr_cfg ip_cfg = { 0 };
uint32_t ipv4_addr = 0;
char* queue_buff = pvPortMalloc(128);
memset(queue_buff, 0, 128);
if (NULL==ssid || 0==strlen(ssid)) {
return 1;
}
//先斷開WiFi
if (wifi_mgmr_sta_state_get() == 1) {
wifi_sta_disconnect();
}
LOG_I("WiFi STA connect .....");
if (wifi_sta_connect(ssid, passwd, NULL, NULL, 0, 0, 0, 1)0) {
vPortFree(queue_buff);
return 4;
}
LOG_I("Wating wifi connet");
//等待連接成功
sta_ConnectStatus = 0;
for (int i = 0;i10*30;i++) {
vTaskDelay(100/portTICK_PERIOD_MS);
switch (sta_ConnectStatus) {
case CODE_WIFI_ON_MGMR_DONE:
// vTaskDelay(2000);
// LOG_I("wifi_mgmr_sta_scan:%d", wifi_mgmr_sta_scan(wifi_scan_config));
vPortFree(queue_buff);
return 3;
case CODE_WIFI_ON_SCAN_DONE:
// LOG_I("WIFI STA SCAN DONE %s", wifi_scan_config[0].ssid_array);
vPortFree(queue_buff);
return 2;
case CODE_WIFI_ON_DISCONNECT: //連接失敗(超過了重連次數(shù)還沒有連接成功的狀態(tài))
// wifi_sta_disconnect();
// vPortFree(queue_buff);
return 4;
case CODE_WIFI_ON_CONNECTED: //連接成功(表示wifi sta狀態(tài)的時候表示同時獲取IP(DHCP)成功,或者使用靜態(tài)IP)
// LOG_I("Wating wifi connet OK");
break;
case CODE_WIFI_ON_GOT_IP:
wifi_sta_ip4_addr_get(&ipv4_addr, NULL, NULL, NULL);
LOG_I("wifi connened %s,IP:%s", ssid, inet_ntoa(ipv4_addr));
sprintf(queue_buff, "{"ip":{"IP":"%s"}}", inet_ntoa(ipv4_addr));
flash_erase_set(SSID_KEY, ssid);
flash_erase_set(PASS_KEY, passwd);
LOG_I("Wating wifi connet OK and get ip OK");
vPortFree(queue_buff);
return 0;
default:
//等待連接成功
break;
}
}
vPortFree(queue_buff);
return 14; //連接超時
}
mqtt
#include
#include
#include
#include "FreeRTOS.h"
#include "log.h"
#include
#include "user_mqtt.h"
#include "ha_task.h"
#include
#include "bflb_uart.h"
#include
#include
#include
#include
#include "bflb_uart.h"
#include "voice_uart.h"
#include "utils_getopt.h"
#include "config.h"
#define DBG_TAG "MQTT"
// #define USER_MQTT
int test_sockfd;
struct mqtt_client client;
static mqtt_event_t event;
uint8_t sendbuf[2048]; /* sendbuf should be large enough to hold multiple whole mqtt messages */
uint8_t recvbuf[1024]; /* recvbuf should be large enough any whole mqtt message expected to be received */
static TaskHandle_t client_daemon;
static int open_socket(const char* host, const char* port);
static int open_socket(const char* host, const char* port)
{
struct addrinfo hints = { 0 };
hints.ai_family = AF_UNSPEC; /* IPv4 or IPv6 */
hints.ai_socktype = SOCK_STREAM; /* Must be TCP */
int sockfd = -1;
int rv;
struct addrinfo* p, * servinfo;
/* get address information */
LOG_I("host:%s, port=%s", host, port);
rv = getaddrinfo(host, port, &hints, &servinfo);
if (rv != 0) {
LOG_E("Failed to open socket (getaddrinfo): %d", rv);
return -1;
}
/* open the first possible socket */
for (p = servinfo; p != NULL; p = p->ai_next) {
sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
if (sockfd == -1) continue;
/* connect to server */
rv = connect(sockfd, p->ai_addr, p->ai_addrlen);
if (rv == -1) {
close(sockfd);
sockfd = -1;
continue;
}
break;
}
/* free servinfo */
freeaddrinfo(servinfo);
/* make non-blocking */
if (sockfd != -1) {
int iMode = 1;
ioctlsocket(sockfd, FIONBIO, &iMode);
}
return sockfd;
}
/**
* @brief
* @param sig
*/
static void test_close(void)
{
if (test_sockfd)
{
close(test_sockfd);
}
vTaskDelete(client_daemon);
}
/**
* @brief
* @param unused
* @param published
*/
static void publish_callback_1(void** unused, struct mqtt_response_publish* published)
{
/* not used in this example */
char* topic_name = (char*)malloc(published->topic_name_size + 1);
memcpy(topic_name, published->topic_name, published->topic_name_size);
topic_name[published->topic_name_size] = '';
char* topic_msg = (char*)malloc(published->application_message_size + 1);
memcpy(topic_msg, published->application_message, published->application_message_size);
topic_msg[published->application_message_size] = '';
printf("%s:[%s]rn", topic_name, topic_msg);
char* queue_buff = pvPortMalloc(512);
memset(queue_buff, 0, 512);
sprintf(queue_buff, "{"mqtt_msg":{"topic":"%s","data":%.*s}}", topic_name, published->application_message_size, topic_msg);
vPortFree(queue_buff);
free(topic_name);
free(topic_msg);
}
/**
* @brief _inspector_callback
* @param client_arg
* @return enum MQTTErrors
*/
enum MQTTErrors _inspector_callback(struct mqtt_client* client_arg)
{
if (client_arg->mq.queue_tail->state!=MQTT_QUEUED_UNSENT) return MQTT_OK; //識別消息狀態(tài)是否有更新,沒有更新就返回
event = client_arg->mq.queue_tail->control_type;
static struct bflb_device_s* uartx;
uartx = bflb_device_get_by_name("uart1");
switch (event) {
case MQTT_EVENT_CONNECT:
{
LOG_I("MQTT_EVENT_CONNECT");
}
break;
case MQTT_EVENT_CONNACK:
{
LOG_I("MQTT_EVENT_CONNACK");
}
break;
case MQTT_EVENT_PUBLISH:
{
LOG_I("MQTT_EVENT_PUBLISH");
}
break;
case MQTT_EVENT_PUBACK:
{
LOG_I("MQTT_EVENT_PUBACK");
}
break;
case MQTT_EVENT_PUBREC:
{
LOG_I("MQTT_EVENT_PUBREC");
}
break;
case MQTT_EVENT_PUBREL:
{
LOG_I("MQTT_EVENT_PUBREL");
}
break;
case MQTT_EVENT_PUBCOMP:
{
LOG_I("MQTT_EVENT_PUBCOMP");
}
break;
case MQTT_EVENT_SUBSCRIBE:
{
LOG_I("MQTT_EVENT_SUBSCRIBE");
}
break;
case MQTT_EVENT_SUBACK:
{
LOG_I("MQTT_EVENT_SUBACK");
}
break;
case MQTT_EVENT_UNSUBSCRIBE:
{
LOG_I("MQTT_EVENT_UNSUBSCRIBE");
}
break;
case MQTT_EVENT_UNSUBACK:
{
LOG_I("MQTT_EVENT_UNSUBACK");
}
break;
case MQTT_EVENT_PINGREQ:
{
LOG_I("MQTT_EVENT_PINGREQ");
}
break;
case MQTT_EVENT_PINGRESP:
{
LOG_I("MQTT_EVENT_PINGRESP");
}
break;
case MQTT_EVENT_DISCONNECT:
{
LOG_I("MQTT_EVENT_DISCONNECT");
}
break;
default:
break;
}
return MQTT_OK;
}
/**
* @brief
* @param host
* @param port
*/
void mqtt_client_init(const char* host, int port)
{
LOG_I("MQTT init start");
test_sockfd = open_socket(host, "1883");
if (test_sockfd < 0) {
LOG_E("Failed to open socket: %d", test_sockfd);
test_close();
return;
}
LOG_I(" test_sockfd crater OK id=%d", test_sockfd);
mqtt_init(&client, test_sockfd, sendbuf, sizeof(sendbuf), recvbuf, sizeof(recvbuf), publish_callback_1);
}
/**
* @brief
* @param client
*/
static void client_refresher(void* arg)
{
struct mqtt_client* client = (struct mqtt_client*)arg;
while (1)
{
mqtt_sync(client);
vTaskDelay(200/portTICK_PERIOD_MS);
}
}
/**
* @brief mqtt_start_connect
* MQTT啟動連接
* @param host
* @param port
* @param user_name
* @param pass
* @return int
*/
int mqtt_start_connect(char* host, uint16_t port, char* user_name, char* pass)
{
int ret = 0;
/* Ensure we have a clean session */
uint8_t connect_flags = MQTT_CONNECT_CLEAN_SESSION;
/* Send connection request to the broker. */
ret = mqtt_connect(&client, MQTT_CLIENT_ID, NULL, NULL, 0, user_name, pass, connect_flags, 400);
if (ret != MQTT_OK)
{
LOG_E("MQTT init fail");
}
/* check that we don't have any errors */
if (client.error != MQTT_OK) {
LOG_E("error: %s", mqtt_error_str(client.error));
test_close();
}
xTaskCreate(client_refresher, (char*)"client_ref", 1024, &client, 10, &client_daemon);
return 0;
}
void mqtt_app_diconnect(void)
{
test_close();
}
/**
* @brief mqtt_app_publish
* @param topic
* @param payload
* @param qos
* @return int
*/
int mqtt_app_publish(const char* topic, const char* payload, int qos)
{
static enum MQTTPublishFlags qos_flags;
if (qos==0)qos_flags = MQTT_PUBLISH_QOS_0;
else if (qos==1)qos_flags = MQTT_PUBLISH_QOS_1;
else qos_flags = MQTT_PUBLISH_QOS_2;
mqtt_publish(&client, topic, payload, strlen(payload), qos_flags);
if (client.error != MQTT_OK) {
LOG_E("error: %s", mqtt_error_str(client.error));
test_close();
return -1;
}
return 0;
}
/**
* @brief mqtt_app_subscribe
* @param topic
* @param qos
* @return int
*/
int mqtt_app_subscribe(char* topic, int qos)
{
mqtt_subscribe(&client, topic, 0);
return 0;
}
void mqtt_client_register_event(void)
{
client.inspector_callback = _inspector_callback;
}
配置文件
#define SSID_KEY "SSID"
#define PASS_KEY "PASS"
#define USER_SSID "******"
#define USER_PASSWORD "******"
#define MQTT_HOST "192.168.5*.*"
#define MQTT_PORT 1883
#define MQTT_USERNAME "******"
#define MQTT_PASSWORD "******"
#define MQTT_TOPIC "/HA/Ai/topic"
#define MQTT_MSG_OPEN_L "{status:true}"
#define MQTT_MSG_CLOSE_L "{status:false}"
如果感興趣的話可以看下
[智能家居]小安派 DSL 通過 MQTT 控制 Home Assistant 燈
https://bbs.ai-thinker.com/forum.php?mod=viewthread&tid=44671&fromuid=15918
(出處: 物聯(lián)網(wǎng)開發(fā)者社區(qū)-安信可論壇)
【完全開源】智能桌面助手——AiPi-DSL_Dashboard
https://bbs.ai-thinker.com/forum.php?mod=viewthread&tid=42026&fromuid=15918
自動化方案低配置,低代碼。操作簡單。
現(xiàn)在手里只有 M61 開發(fā)板,不知道有沒有小一些功耗低一些的模組。希望一塊小電池能堅持一個月那種。
-
智能家居
+關(guān)注
關(guān)注
1926文章
9471瀏覽量
183864 -
語音控制
+關(guān)注
關(guān)注
5文章
481瀏覽量
28213 -
藍牙模組
+關(guān)注
關(guān)注
0文章
39瀏覽量
4059 -
安信可
+關(guān)注
關(guān)注
0文章
142瀏覽量
3936 -
wifi6
+關(guān)注
關(guān)注
4文章
500瀏覽量
38017
發(fā)布評論請先 登錄
相關(guān)推薦
評論