通過BL602/BL604芯片作為系統主控,實現智能全彩燈的控制,可通過wifi接入Dohome平台,同時DoHome APP已經對接了各大智能音箱(小米小愛,百度小度,阿里天貓精靈,Amazon,Google,京東叮咚)。可根據需求通過BL602模組制作智能燈具和其他相關產品。
項目地址:
https://github.com/SmartArduino/Doiting_BL/tree/master/examples/bl602_Dohome_Light_RGB
在app_config.h中定義了軟件的版本號,開發板產生的熱點名,我們需連接通信的DoHome服務器,和我們udp,tcp通信使用的端口號,以及使用到的IO口:
/* * Log config */ #define USE_UART_LOG 1 #define LOG_COLOR_ENABLE 0 #define USE_UDP_LOG 0 #if USE_UART_LOG #define LOG_MIAN_EN 1 #define LOG_WIFI_EN 1 #define LOG_FLASH_EN 1 #define LOG_UTILS_EN 0 #define LOG_PROTOCOL_EN 1 #define LOG_DEVICE_EN 1 #define LOG_UDP_EN 1 #define LOG_LIGHT_EN 0 #define LOG_TCP_EN 0 #define LOG_TCP_CONFIG_EN 1 #define LOG_HTTP_EN 1 #define LOG_UPLOAD_EN 1 #define LOG_SNTP_EN 1 #define LOG_TIMER_EN 1 #define LOG_PROCES_EN 0 #define LOG_LED_DRIVE_EN 0 #define LOG_LED_BOARD_EN 0 #define LOG_OTA_EN 1 #define LOG_UDPLOG_EN 0 #define LOG_FACTORY_EN 1 #define LOG_KEY_EN 1 #define LOG_LOG_EN 1 #define LOG_IR_EN 1 #define LOG_PRODUCT_EN 1 #endif /* * product config */ #define IS_WYRGB // #define IS_WY // #define IS_STRIP #if defined IS_STRIP #define HARDWARE_TYPE "_STRIPE" #elif defined IS_WYRGB #define DEVIVE_NAME_PREFIX "Light_" #define HARDWARE_TYPE "_DT-WYRGB" #define PIN_LED_R 27 #define PIN_LED_G 13 #define PIN_LED_B 26 #define PIN_LED_W 4 #define PIN_LED_Y 16 #elif defined IS_WY #define HARDWARE_TYPE "_DT-WY" #else #define HARDWARE_TYPE "_LED" #endif /* * doit config */ #define FW_VERSION "1.1.0" #define DEF_SSID_PREFIX "DoHome_" #define DEF_HOSTNAME "DoHome" #define ORGANIZATION "_DOIT" #define CHIP_TYPE "_ESP32" #define REMOTE_SERVER_IP "115.28.78.23" #define REMOTE_SRV_HOST "led_iot.doit.am" #define REMOTE_SRV_PORT 8899//6007 #define UPLOAD_SRV_HOST "dohome.doit.am" #define UPLOAD_SRV_PORT 8008 #define NTP_SERVER_HOST "xinfeng.doit.am" #define NTP_SERVER_PORT 80 #define UPLOAD_SRV_URL "http://dohome.doit.am:8008" #define NTP_SERVER_URL "http://xinfeng.doit.am/iot_api/get_iot_time.php" #define MAX_STA_NUM 3 #define TCP_SRV_PORT 5555 #define SPI_FLASH_SEC_SIZE 4096 #define UDP_BROADCAST_INFO_PORT 6095 #define UDP_SRV_PORT 6091 #define WEB_SRV_PORT 80 #define DNS_SRV_PORT 53 #define OTA_TCP_PORT 6093 #define OTA_TCP_ECHO_PORT 6094 #define HTTP_UPO #define WIFI_DEF_MAGIC 0x03 #define KEEPALIVE_TIME 60*1000 #define RESTART_TRIGGER_COUNT 3 #define EXT_UPLOAD_PERIOD (60) //second #define BASE_UPLOAD_PERIOD (50) //second #define SYNC_TIME_PERIOD (30*60) //second #define MIN_TIMEZONE_OFFSET 0 #define MAX_TIMEZONE_OFFSET 23 #define TIMER_NUM 7 // you can setup at most TIMER_NUM timer #define SEQ_SIZE (16 * 5) #define POWERUP_COLOR_MAX_NUM 16 #define MAX_CUSTOM_COLOR 16 #define OTA_PKG_SIZE 240 #define HB_SEND_MAX_COUNT 8 #define SSID_PASS_LEN 64 #define SSID_SSID_LEN 32 #define COLOR_MAX 1000 #define LIGHT_GRA_STEP 10 #define USE_WM_HTTP 0 #ifndef IS_STRIP_WS2812 #define USE_RTC_TIME 1 #endif #define RESTART_REASON_RESET_NULL 0 #define RESTART_REASON_RESET_DEV 1 #define RESTART_REASON_REBOOT_REQ 2
為了穩定的配網,此方案使用ap配網,通過連接開發板產生的熱點,手機app把路由器wifi名稱密碼發送給開發板完成配網:
int wifi_setup_ap(void) { LOGI(TAG, "wifi_setup_ap"); char def_ap_ssid[33] = {"BL602_AP"}; memset(def_ap_ssid, 0x00, sizeof(def_ap_ssid)); get_default_ssid(def_ap_ssid); wifi_interface_t wifi_interface_ap = wifi_mgmr_ap_enable(); dhcpd_set_server_gw("192.168.4.1"); wifi_mgmr_ap_start(wifi_interface_ap, def_ap_ssid, 0, NULL, 1); return 1; }
連上服務器后開始發送心跳包:
static void vTimerCallback(TimerHandle_t timer) { //服務器連接上了,開始發送心跳 if (b_start_keep_alive) { char keep_msg[256]; memset(keep_msg, 0x00, sizeof(keep_msg)); sprintf(keep_msg, "cmd=keep&device_id=%s&device_key=%s\r\n", dev_id, dev_key); send(g_client_fd, keep_msg, strlen(keep_msg), 0); LOGI(TAG, "keepalive buff: %s", keep_msg); } else { // LOGI(TAG, "tcp client disconnect====="); } }
處理下發的數據進行設備控制:
/* 處理tcp服務下發的數據 */ static void parse(int socket, char *buff, int len) { // LOGI(TAG, "tcp_package: %s", buff); if (strstr(buff, "cmd=subscribe") != NULL) { char *pstart = strstr(buff, "res="); if (pstart != NULL) { pstart += strlen("res="); int r = *pstart - '0'; if (r == 1) { LOGI(TAG, "register device on remote server OK"); } else { LOGI(TAG, "register device failed!"); } } else { LOGI(TAG, "result not found!"); } } else if (strstr(buff, "cmd=publish") != NULL) { char *pstart = strstr(buff, "message="); if (pstart != NULL) { pstart += strlen("message="); char ret_buf[256]; parse_dohome_protocol(pstart, len - (pstart - buff), ret_buf); } else { LOGI(TAG, "message not found!"); } } else if (strstr(buff, "cmd=keep") != NULL) { LOGI(TAG, "send heartbeat OK"); } else { LOGI(TAG, "no such command"); } }
將程序編譯好后燒錄到芯片中,手機下載Dohome APP,添加設備,按提示進行設備的配網
1.掃碼下載APP
2.進入APP點擊右上角添加設備,選擇彩色燈
3.按APP提示進行配網
4.添加設備成功,能從Dohome APP控制設備
配網成功后,可使用app控制,也可以綁定智能音箱控制。