ESP8266学习笔记5:ESP8266接入yeelink

搞定了SmartConfig,前头也用cURL玩过了yeelink,今天就编写代码,让ESP8266接入yeelink

转载请注明:http://blog.csdn.NET/sadshen/article/details/46897479

一、代码处理。

主要是将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. }  
  61. /****************************************************************************** 
  62.  * FunctionName : user_esp_platform_sent 
  63.  * Description  : Processing the application data and sending it to the host 
  64.  * Parameters   : pespconn -- the espconn used to connetion with the host 
  65.  * Returns      : none 
  66. *******************************************************************************/  
  67. LOCAL void ICACHE_FLASH_ATTR  
  68. user_sent_data(struct espconn *pespconn)  
  69. {  
  70.     char *pbuf = (char *)os_zalloc(packet_size);  
  71.   
  72.     os_sprintf(pbuf, pheadbuffer, NET_DOMAIN);  
  73.   
  74.     espconn_sent(pespconn, pbuf, os_strlen(pbuf));  
  75.      
  76.     os_free(pbuf);  
  77.   
  78. }  
  79.   
  80. /****************************************************************************** 
  81.  * FunctionName : user_tcp_connect_cb 
  82.  * Description  : A new incoming tcp connection has been connected. 
  83.  * Parameters   : arg -- Additional argument to pass to the callback function 
  84.  * Returns      : none 
  85. *******************************************************************************/  
  86. LOCAL void ICACHE_FLASH_ATTR  
  87. user_tcp_connect_cb(void *arg)  
  88. {  
  89.     struct espconn *pespconn = arg;  
  90.   
  91.     os_printf("connect succeed !!! \r\n");  
  92.   
  93.     espconn_regist_recvcb(pespconn, user_tcp_recv_cb);  
  94.     espconn_regist_sentcb(pespconn, user_tcp_sent_cb);  
  95.     espconn_regist_disconcb(pespconn, user_tcp_discon_cb);  
  96.      
  97.     user_sent_data(pespconn);  
  98. }  
  99.   
  100. /****************************************************************************** 
  101.  * FunctionName : user_tcp_recon_cb 
  102.  * Description  : reconnect callback, error occured in TCP connection. 
  103.  * Parameters   : arg -- Additional argument to pass to the callback function 
  104.  * Returns      : none 
  105. *******************************************************************************/  
  106. LOCAL void ICACHE_FLASH_ATTR  
  107. user_tcp_recon_cb(void *arg, sint8 err)  
  108. {  
  109.    //error occured , tcp connection broke. user can try to reconnect here.   
  110.      
  111.     os_printf("reconnect callback, error code %d !!! \r\n",err);  
  112. }  
  113.   
  114. /****************************************************************************** 
  115.  * FunctionName : user_dns_found 
  116.  * Description  : dns found callback 
  117.  * Parameters   : name -- pointer to the name that was looked up. 
  118.  *                ipaddr -- pointer to an ip_addr_t containing the IP address of 
  119.  *                the hostname, or NULL if the name could not be found (or on any 
  120.  *                other error). 
  121.  *                callback_arg -- a user-specified callback argument passed to 
  122.  *                dns_gethostbyname 
  123.  * Returns      : none 
  124. *******************************************************************************/  
  125. LOCAL void ICACHE_FLASH_ATTR  
  126. user_dns_found(const char *name, ip_addr_t *ipaddr, void *arg)  
  127. {  
  128.     struct espconn *pespconn = (struct espconn *)arg;  
  129.   
  130.     if (ipaddr == NULL) {  
  131.         os_printf("user_dns_found NULL \r\n");  
  132.         return;  
  133.     }  
  134.   
  135.     //dns got ip  
  136.     os_printf("user_dns_found %d.%d.%d.%d \r\n",  
  137.             *((uint8 *)&ipaddr->addr), *((uint8 *)&ipaddr->addr + 1),  
  138.             *((uint8 *)&ipaddr->addr + 2), *((uint8 *)&ipaddr->addr + 3));  
  139.   
  140.     if (tcp_server_ip.addr == 0 && ipaddr->addr != 0) {  
  141.         // dns succeed, create tcp connection  
  142.         os_timer_disarm(&test_timer);  
  143.         tcp_server_ip.addr = ipaddr->addr;  
  144.         os_memcpy(pespconn->proto.tcp->remote_ip, &ipaddr->addr, 4); // remote ip of tcp server which get by dns  
  145.         pespconn->proto.tcp->remote_port = 80; // remote port of tcp server  
  146.         pespconn->proto.tcp->local_port = espconn_port(); //local port of ESP8266  
  147.   
  148.         espconn_regist_connectcb(pespconn, user_tcp_connect_cb); // register connect callback  
  149.         espconn_regist_reconcb(pespconn, user_tcp_recon_cb); // register reconnect callback as error handler  
  150.   
  151.         espconn_connect(pespconn); // tcp connect  
  152.     }  
  153. }  
  154.   
  155. /****************************************************************************** 
  156.  * FunctionName : user_esp_platform_dns_check_cb 
  157.  * Description  : 1s time callback to check dns found 
  158.  * Parameters   : arg -- Additional argument to pass to the callback function 
  159.  * Returns      : none 
  160. *******************************************************************************/  
  161. LOCAL void ICACHE_FLASH_ATTR  
  162. user_dns_check_cb(void *arg)  
  163. {  
  164.     struct espconn *pespconn = arg;  
  165.   
  166.     espconn_gethostbyname(pespconn, NET_DOMAIN, &tcp_server_ip, user_dns_found); // recall DNS function  
  167.   
  168.     os_printf("dns_check\n");  
  169.     os_timer_arm(&test_timer, 1000, 0);  
  170. }  
  171.   
  172. /****************************************************************************** 
  173.  * FunctionName : CheckIpStart 
  174.  * Description  : set the router info which ESP8266 station will connect to  
  175.  * Parameters   : none 
  176.  * Returns      : none 
  177. *******************************************************************************/  
  178. void ICACHE_FLASH_ATTR  
  179. CheckIpStart(void)  
  180. {  
  181.     // Connect to tcp server as NET_DOMAIN  
  182.     user_tcp_conn.proto.tcp = &user_tcp;  
  183.     user_tcp_conn.type = ESPCONN_TCP;  
  184.     user_tcp_conn.state = ESPCONN_NONE;  
  185.     tcp_server_ip.addr = 0;  
  186.     espconn_gethostbyname(&user_tcp_conn, NET_DOMAIN, &tcp_server_ip, user_dns_found); // DNS function  
  187.   
  188.     os_timer_disarm(&test_timer);  
  189.     os_timer_setfn(&test_timer, (os_timer_func_t *)user_dns_check_cb, user_tcp_conn);  
  190.     os_timer_arm(&test_timer, 1000, 0);  
  191. }  
  192.   
  193. void ICACHE_FLASH_ATTR  
  194. smartconfig_done(sc_status status, void *pdata)  
  195. {  
  196.     switch(status) {  
  197.         case SC_STATUS_WAIT:  
  198.             os_printf("SC_STATUS_WAIT\n");  
  199.             break;  
  200.         case SC_STATUS_FIND_CHANNEL:  
  201.             os_printf("SC_STATUS_FIND_CHANNEL\n");  
  202.             break;  
  203.         case SC_STATUS_GETTING_SSID_PSWD:  
  204.             os_printf("SC_STATUS_GETTING_SSID_PSWD\n");  
  205.             sc_type *type = pdata;  
  206.             if (*type == SC_TYPE_ESPTOUCH) {  
  207.                 os_printf("SC_TYPE:SC_TYPE_ESPTOUCH\n");  
  208.             } else {  
  209.                 os_printf("SC_TYPE:SC_TYPE_AIRKISS\n");  
  210.             }  
  211.             break;  
  212.         case SC_STATUS_LINK:  
  213.             os_printf("SC_STATUS_LINK\n");  
  214.             struct station_config *sta_conf = pdata;  
  215.       
  216.             wifi_station_set_config(sta_conf);  
  217.             wifi_station_disconnect();  
  218.             wifi_station_connect();  
  219.             break;  
  220.         case SC_STATUS_LINK_OVER:  
  221.             os_printf("SC_STATUS_LINK_OVER\n");  
  222.             if (pdata != NULL) {  
  223.                 uint8 phone_ip[4] = {0};  
  224.   
  225.                 os_memcpy(phone_ip, (uint8*)pdata, 4);  
  226.                 os_printf("Phone ip: %d.%d.%d.%d\n",phone_ip[0],phone_ip[1],phone_ip[2],phone_ip[3]);  
  227.                 CheckIpStart();  
  228.             }  
  229.             smartconfig_stop();  
  230.             break;  
  231.     }  
  232.       
  233. }  
  234.   
  235. void user_rf_pre_init(void)  
  236. {  
  237. }  
  238.   
  239. void user_init(void)  
  240. {  
  241.     os_printf("SDK version:%s\n", system_get_sdk_version());  
  242.       
  243.     wifi_set_opmode(STATION_MODE);  
  244.     smartconfig_start(smartconfig_done);  
  245. }  

二、测试






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值