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控制,也可以绑定智能音箱控制。
绑定完智能音箱的账号后,就可以使用我们自己的智能音箱进行控制