前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ESP8266学习笔记5:ESP8266接入yeelink

ESP8266学习笔记5:ESP8266接入yeelink

作者头像
twowinter
发布2020-04-17 14:35:44
8880
发布2020-04-17 14:35:44
举报
文章被收录于专栏:twowintertwowinter

我整理了从2015年至今关于ESP8266的学习笔记,梳理出来了开发环境、基础功能、进阶学习三大部分,方便自己和他人。可点此查看,欢迎交流。

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

本文作者twowinter,转载请注明作者:http://blog.csdn.net/iotisan/

一、代码处理。

主要是将http的sample加入到SmartConfig_DEMO中。ESP8266的运行流程是SmartConfig->DNS->Connect yeelink->Get /devices

这里的pheadbuffer 格式要注意加上U-ApiKey。我是通过wireshark捕捉了curl Get /devices的动作才知道要怎么写的。

代码语言:javascript
复制
#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);
}

二、测试


本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、代码处理。
  • 二、测试
相关产品与服务
命令行工具
腾讯云命令行工具 TCCLI 是管理腾讯云资源的统一工具。使用腾讯云命令行工具,您可以快速调用腾讯云 API 来管理您的腾讯云资源。此外,您还可以基于腾讯云的命令行工具来做自动化和脚本处理,以更多样的方式进行组合和重用。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档