6.2連線DoHome平臺實現天貓精靈等各種音箱控制
DoHome基本上已經對接了市面上所有的智慧音箱,接入平臺後,我們就可以使用各種智慧音箱來控制開發板。
在doit_config.h中定義了軟體的版本號,開發板產生的熱點名,我們需連線通訊的DoHome伺服器,以及我們udp,tcp通訊使用的埠號:
define FW_VERSION "1.4.0"
define DEF_SSID_PREFIX "DoHome_"
define DEF_HOSTNAME "DoHome"
// #define IS_WYRGB
define IS_WY
// #define IS_STRIP
if defined IS_STRIP
define HARDWARE_TYPE "_STRIPE"
elif defined IS_WYRGB
define HARDWARE_TYPE "_DT-WYRGB"
define PIN_LED_R WM_IO_PA_05
define PIN_LED_G WM_IO_PB_13
define PIN_LED_B WM_IO_PB_15
define PIN_LED_W WM_IO_PB_16
define PIN_LED_Y WM_IO_PB_08
elif defined IS_WY
define HARDWARE_TYPE "_DT-WY"
define PIN_LED_R WM_IO_PA_05
define PIN_LED_G WM_IO_PB_13
define PIN_LED_B WM_IO_PB_15
define PIN_LED_W WM_IO_PB_16
define PIN_LED_Y WM_IO_PB_08
else
define HARDWARE_TYPE "_LED"
endif
define ORGANIZATION "_DOIT"
define CHIP_TYPE "_W800"
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
為了穩定的配網,此方案使用ap配網,通過連線開發板產生的熱點,手機app把路由器wifi名稱密碼傳送給開發板完成配網:
int wifi_setup_ap()
{
u8 ret = 0;
int ssid_len = 0;
struct tls_softap_info_t apinfo;
struct tls_ip_info_t ipinfo;
memset(&apinfo, 0, sizeof(apinfo));
memset(&ipinfo, 0, sizeof(ipinfo));
uint8_t rst_count = flash_get_reset_count();
uint8_t softap_channels[5] = {5, 6, 9, 11, 2};
int channel = softap_channels[rst_count % 5];
char def_ap_ssid[33] = {"W600_AP"};
memset(def_ap_ssid, 0x00, sizeof(def_ap_ssid));
get_default_ssid(def_ap_ssid);
ssid_len = strlen(def_ap_ssid);
MEMCPY(apinfo.ssid, def_ap_ssid, ssid_len);
apinfo.ssid[ssid_len] = '\0';
apinfo.encrypt = IEEE80211_ENCRYT_NONE;
apinfo.channel = channel; /*channel*/
apinfo.keyinfo.format = 1; /*format:0,hex, 1,ascii*/
apinfo.keyinfo.index = 1; /*wep index*/
apinfo.keyinfo.key_len = 0; /*key length*/
MEMCPY(apinfo.keyinfo.key, "", 0);
/*ip information:ip address,mask, DNS name*/
ipinfo.ip_addr[0] = 192;
ipinfo.ip_addr[1] = 168;
ipinfo.ip_addr[2] = 4;
ipinfo.ip_addr[3] = 1;
ipinfo.netmask[0] = 255;
ipinfo.netmask[1] = 255;
ipinfo.netmask[2] = 255;
ipinfo.netmask[3] = 0;
MEMCPY(ipinfo.dnsname, "local.wm", sizeof("local.wm"));
ret = tls_wifi_softap_create((struct tls_softap_info_t * )&apinfo, (struct tls_ip_info_t * )&ipinfo);
printf("ap create %s !", (ret == WM_SUCCESS) ? "Successfully" : "Error");
return ret;
}
Json處理接收到的資料,然後控制開發板的彩燈:
cJSON *pon = cJSON_GetObjectItem(root, "on");
if (pon == NULL) {
// printf("get on field failed, turn on\r\n");
} else {
int on = pon->valueint;
if (on == 1) {
//send_light_onoff(1);
tls_gpio_write(WM_IO_PB_02,0);
tls_gpio_write(WM_IO_PB_07,0);
tls_gpio_write(WM_IO_PB_11,0);
} else {
//send_light_onoff(0);
tls_gpio_write(WM_IO_PB_02,1);
tls_gpio_write(WM_IO_PB_07,1);
tls_gpio_write(WM_IO_PB_11,1);
}
return CMD_RET_OK;
}
程式下載到開發板,手機掃碼下載app,新增裝置:按提示配網即可。
配網成功後,可使用app控制,也可以繫結智慧音箱控制。
繫結完智慧音箱的賬號後,就可以使用我們自己的智慧音箱進行控制