[应用方案] ESP8266接入yeelink

[复制链接]
 楼主| cowboy2014 发表于 2016-11-20 20:28 | 显示全部楼层 |阅读模式
搞定了SmartConfig,前头也用cURL玩过了yeelink,今天就编写代码,让ESP8266接入yeelink
一、代码处理。主要是将httpsample加入到SmartConfig_DEMO中。ESP8266的运行流程是SmartConfig->DNS->Connect yeelink->Get /devices
这里的pheadbuffer 格式要注意加上U-ApiKey。我是通过wireshark捕捉了curl Get /devices的动作才知道要怎么写的。






  1. #include "ets_sys.h"  
  2. #include "osapi.h"  
  3.   
  4. #include "user_interface.h"  
  5. #include "smartconfig.h"  
  6.   
  7. #include "espconn.h"  
  8. #include "mem.h"  
  9.   
  10.   
  11. #define NET_DOMAIN "api.yeelink.net"  
  12. #define pheadbuffer "GET /v1.0/devices HTTP/1.1\r\nHost: %s\r\nAccept: */*\r\nU-ApiKey: e510ad132988d34c6fc9c3a9322f6f10\r\n\r\n"  
  13.   
  14. #define packet_size   (2 * 1024)  
  15.   
  16. LOCAL os_timer_t test_timer;  
  17. LOCAL struct espconn user_tcp_conn;  
  18. LOCAL struct _esp_tcp user_tcp;  
  19. ip_addr_t tcp_server_ip;  
  20.   
  21. /******************************************************************************  
  22. * FunctionName : user_tcp_recv_cb  
  23. * Description  : receive callback.  
  24. * Parameters   : arg -- Additional argument to pass to the callback function  
  25. * Returns      : none  
  26. *******************************************************************************/  
  27. LOCAL void ICACHE_FLASH_ATTR  
  28. user_tcp_recv_cb(void *arg, char *pusrdata, unsigned short length)  
  29. {  
  30.    //received some data from tcp connection  
  31.      
  32.     os_printf("tcp recv !!! %s \r\n", pusrdata);  
  33. }  
  34.   
  35. /******************************************************************************
  36. * FunctionName : user_tcp_sent_cb
  37. * Description  : data sent callback.
  38. * Parameters   : arg -- Additional argument to pass to the callback function
  39. * Returns      : none
  40. *******************************************************************************/  
  41. LOCAL void ICACHE_FLASH_ATTR  
  42. user_tcp_sent_cb(void *arg)  
  43. {  
  44.    //data sent successfully  
  45.   
  46.     os_printf("tcp sent succeed !!! \r\n");  
  47. }  
  48. /******************************************************************************
  49. * FunctionName : user_tcp_discon_cb
  50. * Description  : disconnect callback.
  51. * Parameters   : arg -- Additional argument to pass to the callback function
  52. * Returns      : none
  53. *******************************************************************************/  
  54. LOCAL void ICACHE_FLASH_ATTR  
  55. user_tcp_discon_cb(void *arg)  
  56. {  
  57.    //tcp disconnect successfully  
  58.      
  59.     os_printf("tcp disconnect succeed !!! \r\n");  
  60. }  


 楼主| cowboy2014 发表于 2016-11-20 20:28 | 显示全部楼层
  1. /******************************************************************************
  2. * FunctionName : user_esp_platform_sent
  3. * Description  : Processing the application data and sending it to the host
  4. * Parameters   : pespconn -- the espconn used to connetion with the host
  5. * Returns      : none
  6. *******************************************************************************/  
  7. LOCAL void ICACHE_FLASH_ATTR  
  8. user_sent_data(struct espconn *pespconn)  
  9. {  
  10.     char *pbuf = (char *)os_zalloc(packet_size);  
  11.   
  12.     os_sprintf(pbuf, pheadbuffer, NET_DOMAIN);  
  13.   
  14.     espconn_sent(pespconn, pbuf, os_strlen(pbuf));  
  15.      
  16.     os_free(pbuf);  
  17.   
  18. }  
  19.   
  20. /******************************************************************************
  21. * FunctionName : user_tcp_connect_cb
  22. * Description  : A new incoming tcp connection has been connected.
  23. * Parameters   : arg -- Additional argument to pass to the callback function
  24. * Returns      : none
  25. *******************************************************************************/  
  26. LOCAL void ICACHE_FLASH_ATTR  
  27. user_tcp_connect_cb(void *arg)  
  28. {  
  29.     struct espconn *pespconn = arg;  
  30.   
  31.     os_printf("connect succeed !!! \r\n");  
  32.   
  33.     espconn_regist_recvcb(pespconn, user_tcp_recv_cb);  
  34.     espconn_regist_sentcb(pespconn, user_tcp_sent_cb);  
  35.     espconn_regist_disconcb(pespconn, user_tcp_discon_cb);  
  36.      
  37.     user_sent_data(pespconn);  
  38. }  
  39.   
  40. /******************************************************************************
  41. * FunctionName : user_tcp_recon_cb
  42. * Description  : reconnect callback, error occured in TCP connection.
  43. * Parameters   : arg -- Additional argument to pass to the callback function
  44. * Returns      : none
  45. *******************************************************************************/  
  46. LOCAL void ICACHE_FLASH_ATTR  
  47. user_tcp_recon_cb(void *arg, sint8 err)  
  48. {  
  49.    //error occured , tcp connection broke. user can try to reconnect here.   
  50.      
  51.     os_printf("reconnect callback, error code %d !!! \r\n",err);  
  52. }  


 楼主| cowboy2014 发表于 2016-11-20 20:29 | 显示全部楼层
  1. /******************************************************************************
  2. * FunctionName : user_dns_found
  3. * Description  : dns found callback
  4. * Parameters   : name -- pointer to the name that was looked up.
  5. *                ipaddr -- pointer to an ip_addr_t containing the IP address of
  6. *                the hostname, or NULL if the name could not be found (or on any
  7. *                other error).
  8. *                callback_arg -- a user-specified callback argument passed to
  9. *                dns_gethostbyname
  10. * Returns      : none
  11. *******************************************************************************/  
  12. LOCAL void ICACHE_FLASH_ATTR  
  13. user_dns_found(const char *name, ip_addr_t *ipaddr, void *arg)  
  14. {  
  15.     struct espconn *pespconn = (struct espconn *)arg;  
  16.   
  17.     if (ipaddr == NULL) {  
  18.         os_printf("user_dns_found NULL \r\n");  
  19.         return;  
  20.     }  
  21.   
  22.     //dns got ip  
  23.     os_printf("user_dns_found %d.%d.%d.%d \r\n",  
  24.             *((uint8 *)&ipaddr->addr), *((uint8 *)&ipaddr->addr + 1),  
  25.             *((uint8 *)&ipaddr->addr + 2), *((uint8 *)&ipaddr->addr + 3));  
  26.   
  27.     if (tcp_server_ip.addr == 0 && ipaddr->addr != 0) {  
  28.         // dns succeed, create tcp connection  
  29.         os_timer_disarm(&test_timer);  
  30.         tcp_server_ip.addr = ipaddr->addr;  
  31.         os_memcpy(pespconn->proto.tcp->remote_ip, &ipaddr->addr, 4); // remote ip of tcp server which get by dns  
  32.         pespconn->proto.tcp->remote_port = 80; // remote port of tcp server  
  33.         pespconn->proto.tcp->local_port = espconn_port(); //local port of ESP8266  
  34.   
  35.         espconn_regist_connectcb(pespconn, user_tcp_connect_cb); // register connect callback  
  36.         espconn_regist_reconcb(pespconn, user_tcp_recon_cb); // register reconnect callback as error handler  
  37.   
  38.         espconn_connect(pespconn); // tcp connect  
  39.     }  
  40. }  
  41.   
  42. /******************************************************************************
  43. * FunctionName : user_esp_platform_dns_check_cb
  44. * Description  : 1s time callback to check dns found
  45. * Parameters   : arg -- Additional argument to pass to the callback function
  46. * Returns      : none
  47. *******************************************************************************/  
  48. LOCAL void ICACHE_FLASH_ATTR  
  49. user_dns_check_cb(void *arg)  
  50. {  
  51.     struct espconn *pespconn = arg;  
  52.   
  53.     espconn_gethostbyname(pespconn, NET_DOMAIN, &tcp_server_ip, user_dns_found); // recall DNS function  
  54.   
  55.     os_printf("dns_check\n");  
  56.     os_timer_arm(&test_timer, 1000, 0);  
  57. }  
  58.   
  59. /******************************************************************************
  60. * FunctionName : CheckIpStart
  61. * Description  : set the router info which ESP8266 station will connect to  
  62. * Parameters   : none
  63. * Returns      : none
  64. *******************************************************************************/  
  65. void ICACHE_FLASH_ATTR  
  66. CheckIpStart(void)  
  67. {  
  68.     // Connect to tcp server as NET_DOMAIN  
  69.     user_tcp_conn.proto.tcp = &user_tcp;  
  70.     user_tcp_conn.type = ESPCONN_TCP;  
  71.     user_tcp_conn.state = ESPCONN_NONE;  
  72.     tcp_server_ip.addr = 0;  
  73.     espconn_gethostbyname(&user_tcp_conn, NET_DOMAIN, &tcp_server_ip, user_dns_found); // DNS function  
  74.   
  75.     os_timer_disarm(&test_timer);  
  76.     os_timer_setfn(&test_timer, (os_timer_func_t *)user_dns_check_cb, user_tcp_conn);  
  77.     os_timer_arm(&test_timer, 1000, 0);  
  78. }  
  79.   
  80. void ICACHE_FLASH_ATTR  


 楼主| cowboy2014 发表于 2016-11-20 20:31 | 显示全部楼层
  1. smartconfig_done(sc_status status, void *pdata)  
  2. {  
  3.     switch(status) {  
  4.         case SC_STATUS_WAIT:  
  5.             os_printf("SC_STATUS_WAIT\n");  
  6.             break;  
  7.         case SC_STATUS_FIND_CHANNEL:  
  8.             os_printf("SC_STATUS_FIND_CHANNEL\n");  
  9.             break;  
  10.         case SC_STATUS_GETTING_SSID_PSWD:  
  11.             os_printf("SC_STATUS_GETTING_SSID_PSWD\n");  
  12.             sc_type *type = pdata;  
  13.             if (*type == SC_TYPE_ESPTOUCH) {  
  14.                 os_printf("SC_TYPE:SC_TYPE_ESPTOUCH\n");  
  15.             } else {  
  16.                 os_printf("SC_TYPE:SC_TYPE_AIRKISS\n");  
  17.             }  
  18.             break;  
  19.         case SC_STATUS_LINK:  
  20.             os_printf("SC_STATUS_LINK\n");  
  21.             struct station_config *sta_conf = pdata;  
  22.       
  23.             wifi_station_set_config(sta_conf);  
  24.             wifi_station_disconnect();  
  25.             wifi_station_connect();  
  26.             break;  
  27.         case SC_STATUS_LINK_OVER:  
  28.             os_printf("SC_STATUS_LINK_OVER\n");  
  29.             if (pdata != NULL) {  
  30.                 uint8 phone_ip[4] = {0};  
  31.   
  32.                 os_memcpy(phone_ip, (uint8*)pdata, 4);  
  33.                 os_printf("Phone ip: %d.%d.%d.%d\n",phone_ip[0],phone_ip[1],phone_ip[2],phone_ip[3]);  
  34.                 CheckIpStart();  
  35.             }  
  36.             smartconfig_stop();  
  37.             break;  
  38.     }  
  39.       
  40. }  
  41.   
  42. void user_rf_pre_init(void)  
  43. {  
  44. }  
  45.   
  46. void user_init(void)  
  47. {  
  48.     os_printf("SDK version:%s\n", system_get_sdk_version());  
  49.       
  50.     wifi_set_opmode(STATION_MODE);  
  51.     smartconfig_start(smartconfig_done);  
  52. }  


yiyigirl2014 发表于 2016-11-20 23:53 | 显示全部楼层
那个网站都打不开了。
deviceplugs 发表于 2016-11-21 10:51 | 显示全部楼层
APP或者微信是怎么开发的呢?
zhuotuzi 发表于 2016-11-21 20:19 | 显示全部楼层
专门做照明控制的?
稳稳の幸福 发表于 2016-11-22 18:39 | 显示全部楼层
学习了,原来跟服务器这么通信。
500days 发表于 2016-11-24 18:55 | 显示全部楼层
工具用的是什么,windows下可以开发吗
您需要登录后才可以回帖 登录 | 注册

本版积分规则

78

主题

821

帖子

5

粉丝
快速回复 在线客服 返回列表 返回顶部

78

主题

821

帖子

5

粉丝
快速回复 在线客服 返回列表 返回顶部