搞定了SmartConfig,前头也用cURL玩过了yeelink,今天就编写代码,让ESP8266接入yeelink。
转载请注明:http://blog.csdn.net/sadshen/article/details/46897479
主要是将http的sample加入到SmartConfig_DEMO中。ESP8266的运行流程是SmartConfig->DNS->Connect yeelink->Get /devices
这里的pheadbuffer 格式要注意加上U-ApiKey。我是通过wireshark捕捉了curl Get /devices的动作才知道要怎么写的。
#include "ets_sys.h" #include "osapi.h" #include "user_interface.h" #include "smartconfig.h" #include "espconn.h" #include "mem.h" #define NET_DOMAIN "api.yeelink.net" #define pheadbuffer "GET /v1.0/devices HTTP/1.1\r\nHost: %s\r\nAccept: */*\r\nU-ApiKey: e510ad132988d34c6fc9c3a9322f6f10\r\n\r\n" #define packet_size (2 * 1024) LOCAL os_timer_t test_timer; LOCAL struct espconn user_tcp_conn; LOCAL struct _esp_tcp user_tcp; ip_addr_t tcp_server_ip; /****************************************************************************** * FunctionName : user_tcp_recv_cb * Description : receive callback. * Parameters : arg -- Additional argument to pass to the callback function * Returns : none *******************************************************************************/ LOCAL void ICACHE_FLASH_ATTR user_tcp_recv_cb(void *arg, char *pusrdata, unsigned short length) { //received some data from tcp connection os_printf("tcp recv !!! %s \r\n", pusrdata); } /****************************************************************************** * FunctionName : user_tcp_sent_cb * Description : data sent callback. * Parameters : arg -- Additional argument to pass to the callback function * Returns : none *******************************************************************************/ LOCAL void ICACHE_FLASH_ATTR user_tcp_sent_cb(void *arg) { //data sent successfully os_printf("tcp sent succeed !!! \r\n"); } /****************************************************************************** * FunctionName : user_tcp_discon_cb * Description : disconnect callback. * Parameters : arg -- Additional argument to pass to the callback function * Returns : none *******************************************************************************/ LOCAL void ICACHE_FLASH_ATTR user_tcp_discon_cb(void *arg) { //tcp disconnect successfully os_printf("tcp disconnect succeed !!! \r\n"); } /****************************************************************************** * FunctionName : user_esp_platform_sent * Description : Processing the application data and sending it to the host * Parameters : pespconn -- the espconn used to connetion with the host * Returns : none *******************************************************************************/ LOCAL void ICACHE_FLASH_ATTR user_sent_data(struct espconn *pespconn) { char *pbuf = (char *)os_zalloc(packet_size); os_sprintf(pbuf, pheadbuffer, NET_DOMAIN); espconn_sent(pespconn, pbuf, os_strlen(pbuf)); os_free(pbuf); } /****************************************************************************** * FunctionName : user_tcp_connect_cb * Description : A new incoming tcp connection has been connected. * Parameters : arg -- Additional argument to pass to the callback function * Returns : none *******************************************************************************/ LOCAL void ICACHE_FLASH_ATTR user_tcp_connect_cb(void *arg) { struct espconn *pespconn = arg; os_printf("connect succeed !!! \r\n"); espconn_regist_recvcb(pespconn, user_tcp_recv_cb); espconn_regist_sentcb(pespconn, user_tcp_sent_cb); espconn_regist_disconcb(pespconn, user_tcp_discon_cb); user_sent_data(pespconn); } /****************************************************************************** * FunctionName : user_tcp_recon_cb * Description : reconnect callback, error occured in TCP connection. * Parameters : arg -- Additional argument to pass to the callback function * Returns : none *******************************************************************************/ LOCAL void ICACHE_FLASH_ATTR user_tcp_recon_cb(void *arg, sint8 err) { //error occured , tcp connection broke. user can try to reconnect here. os_printf("reconnect callback, error code %d !!! \r\n",err); } /****************************************************************************** * FunctionName : user_dns_found * Description : dns found callback * Parameters : name -- pointer to the name that was looked up. * ipaddr -- pointer to an ip_addr_t containing the IP address of * the hostname, or NULL if the name could not be found (or on any * other error). * callback_arg -- a user-specified callback argument passed to * dns_gethostbyname * Returns : none *******************************************************************************/ LOCAL void ICACHE_FLASH_ATTR user_dns_found(const char *name, ip_addr_t *ipaddr, void *arg) { struct espconn *pespconn = (struct espconn *)arg; if (ipaddr == NULL) { os_printf("user_dns_found NULL \r\n"); return; } //dns got ip os_printf("user_dns_found %d.%d.%d.%d \r\n", *((uint8 *)&ipaddr->addr), *((uint8 *)&ipaddr->addr + 1), *((uint8 *)&ipaddr->addr + 2), *((uint8 *)&ipaddr->addr + 3)); if (tcp_server_ip.addr == 0 && ipaddr->addr != 0) { // dns succeed, create tcp connection os_timer_disarm(&test_timer); tcp_server_ip.addr = ipaddr->addr; os_memcpy(pespconn->proto.tcp->remote_ip, &ipaddr->addr, 4); // remote ip of tcp server which get by dns pespconn->proto.tcp->remote_port = 80; // remote port of tcp server pespconn->proto.tcp->local_port = espconn_port(); //local port of ESP8266 espconn_regist_connectcb(pespconn, user_tcp_connect_cb); // register connect callback espconn_regist_reconcb(pespconn, user_tcp_recon_cb); // register reconnect callback as error handler espconn_connect(pespconn); // tcp connect } } /****************************************************************************** * FunctionName : user_esp_platform_dns_check_cb * Description : 1s time callback to check dns found * Parameters : arg -- Additional argument to pass to the callback function * Returns : none *******************************************************************************/ LOCAL void ICACHE_FLASH_ATTR user_dns_check_cb(void *arg) { struct espconn *pespconn = arg; espconn_gethostbyname(pespconn, NET_DOMAIN, &tcp_server_ip, user_dns_found); // recall DNS function os_printf("dns_check\n"); os_timer_arm(&test_timer, 1000, 0); } /****************************************************************************** * FunctionName : CheckIpStart * Description : set the router info which ESP8266 station will connect to * Parameters : none * Returns : none *******************************************************************************/ void ICACHE_FLASH_ATTR CheckIpStart(void) { // Connect to tcp server as NET_DOMAIN user_tcp_conn.proto.tcp = &user_tcp; user_tcp_conn.type = ESPCONN_TCP; user_tcp_conn.state = ESPCONN_NONE; tcp_server_ip.addr = 0; espconn_gethostbyname(&user_tcp_conn, NET_DOMAIN, &tcp_server_ip, user_dns_found); // DNS function os_timer_disarm(&test_timer); os_timer_setfn(&test_timer, (os_timer_func_t *)user_dns_check_cb, user_tcp_conn); os_timer_arm(&test_timer, 1000, 0); } void ICACHE_FLASH_ATTR smartconfig_done(sc_status status, void *pdata) { switch(status) { case SC_STATUS_WAIT: os_printf("SC_STATUS_WAIT\n"); break; case SC_STATUS_FIND_CHANNEL: os_printf("SC_STATUS_FIND_CHANNEL\n"); break; case SC_STATUS_GETTING_SSID_PSWD: os_printf("SC_STATUS_GETTING_SSID_PSWD\n"); sc_type *type = pdata; if (*type == SC_TYPE_ESPTOUCH) { os_printf("SC_TYPE:SC_TYPE_ESPTOUCH\n"); } else { os_printf("SC_TYPE:SC_TYPE_AIRKISS\n"); } break; case SC_STATUS_LINK: os_printf("SC_STATUS_LINK\n"); struct station_config *sta_conf = pdata; wifi_station_set_config(sta_conf); wifi_station_disconnect(); wifi_station_connect(); break; case SC_STATUS_LINK_OVER: os_printf("SC_STATUS_LINK_OVER\n"); if (pdata != NULL) { uint8 phone_ip[4] = {0}; os_memcpy(phone_ip, (uint8*)pdata, 4); os_printf("Phone ip: %d.%d.%d.%d\n",phone_ip[0],phone_ip[1],phone_ip[2],phone_ip[3]); CheckIpStart(); } smartconfig_stop(); break; } } void user_rf_pre_init(void) { } void user_init(void) { os_printf("SDK version:%s\n", system_get_sdk_version()); wifi_set_opmode(STATION_MODE); smartconfig_start(smartconfig_done); }
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/sadshen/article/details/46897479