26-ESP8266 SDK开发基础入门篇--编写WIFI模块 SmartConfig/Airkiss 一键配网
Posted yangfengwu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了26-ESP8266 SDK开发基础入门篇--编写WIFI模块 SmartConfig/Airkiss 一键配网相关的知识,希望对你有一定的参考价值。
https://www.cnblogs.com/yangfengwu/p/11427504.html
SmartConfig/Airkiss 配网需要APP/微信公众号,这节大家先使用我做好的APP/微信公众号
APP下载:
https://www.cnblogs.com/yangfengwu/p/11249674.html
微信公众号: 扫描这个二维码关注我的公众号
其余的步骤等写完8266的配网程序,在下面演示.
如果想自己实现微信绑定可以看↓ (注:配置过程和源码全部是公开的,大家看文章即可实现)
如果你已经有做网页的经验了,可以直接
如果你没有做过网页,你需要先看
然后需要把微信小程序篇的所有章节从头到尾看一遍
现在开始写WIFI的配网程序
其实官方给了例子
咱把例子写个单独的.C和.H文件,方便咱后期使用
smart_config.c
#include "esp_common.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "lwip/sockets.h" #include "lwip/dns.h" #include "lwip/netdb.h" #include "espressif/espconn.h" #include "espressif/airkiss.h" #define server_ip "192.168.101.142" #define server_port 9669 #define DEVICE_TYPE "gh_9e2cff3dfa51" //wechat public number #define DEVICE_ID "122475" //model ID #define DEFAULT_LAN_PORT 12476 LOCAL esp_udp ssdp_udp; LOCAL struct espconn pssdpudpconn; LOCAL os_timer_t ssdp_time_serv; uint8 lan_buf[200]; uint16 lan_buf_len; uint8 udp_sent_cnt = 0; const airkiss_config_t akconf = (airkiss_memset_fn)&memset, (airkiss_memcpy_fn)&memcpy, (airkiss_memcmp_fn)&memcmp, 0, ; LOCAL void ICACHE_FLASH_ATTR airkiss_wifilan_time_callback(void) uint16 i; airkiss_lan_ret_t ret; if ((udp_sent_cnt++) >30) udp_sent_cnt = 0; os_timer_disarm(&ssdp_time_serv);//s //return; ssdp_udp.remote_port = DEFAULT_LAN_PORT; ssdp_udp.remote_ip[0] = 255; ssdp_udp.remote_ip[1] = 255; ssdp_udp.remote_ip[2] = 255; ssdp_udp.remote_ip[3] = 255; lan_buf_len = sizeof(lan_buf); ret = airkiss_lan_pack(AIRKISS_LAN_SSDP_NOTIFY_CMD, DEVICE_TYPE, DEVICE_ID, 0, 0, lan_buf, &lan_buf_len, &akconf); if (ret != AIRKISS_LAN_PAKE_READY) os_printf("Pack lan packet error!"); return; ret = espconn_sendto(&pssdpudpconn, lan_buf, lan_buf_len); if (ret != 0) os_printf("UDP send error!"); os_printf("Finish send notify!\\n"); LOCAL void ICACHE_FLASH_ATTR airkiss_wifilan_recv_callbk(void *arg, char *pdata, unsigned short len) uint16 i; remot_info* pcon_info = NULL; airkiss_lan_ret_t ret = airkiss_lan_recv(pdata, len, &akconf); airkiss_lan_ret_t packret; switch (ret) case AIRKISS_LAN_SSDP_REQ: espconn_get_connection_info(&pssdpudpconn, &pcon_info, 0); os_printf("remote ip: %d.%d.%d.%d \\r\\n",pcon_info->remote_ip[0],pcon_info->remote_ip[1], pcon_info->remote_ip[2],pcon_info->remote_ip[3]); os_printf("remote port: %d \\r\\n",pcon_info->remote_port); pssdpudpconn.proto.udp->remote_port = pcon_info->remote_port; memcpy(pssdpudpconn.proto.udp->remote_ip,pcon_info->remote_ip,4); ssdp_udp.remote_port = DEFAULT_LAN_PORT; lan_buf_len = sizeof(lan_buf); packret = airkiss_lan_pack(AIRKISS_LAN_SSDP_RESP_CMD, DEVICE_TYPE, DEVICE_ID, 0, 0, lan_buf, &lan_buf_len, &akconf); if (packret != AIRKISS_LAN_PAKE_READY) os_printf("Pack lan packet error!"); return; os_printf("\\r\\n\\r\\n"); for (i=0; i<lan_buf_len; i++) os_printf("%c",lan_buf[i]); os_printf("\\r\\n\\r\\n"); packret = espconn_sendto(&pssdpudpconn, lan_buf, lan_buf_len); if (packret != 0) os_printf("LAN UDP Send err!"); break; default: os_printf("Pack is not ssdq req!%d\\r\\n",ret); break; void ICACHE_FLASH_ATTR airkiss_start_discover(void) ssdp_udp.local_port = DEFAULT_LAN_PORT; pssdpudpconn.type = ESPCONN_UDP; pssdpudpconn.proto.udp = &(ssdp_udp); espconn_regist_recvcb(&pssdpudpconn, airkiss_wifilan_recv_callbk); espconn_create(&pssdpudpconn); os_timer_disarm(&ssdp_time_serv); os_timer_setfn(&ssdp_time_serv, (os_timer_func_t *)airkiss_wifilan_time_callback, NULL); os_timer_arm(&ssdp_time_serv, 1000, 1);//1s void ICACHE_FLASH_ATTR smartconfig_done(sc_status status, void *pdata) switch(status) case SC_STATUS_WAIT: printf("SC_STATUS_WAIT\\n"); break; case SC_STATUS_FIND_CHANNEL: printf("SC_STATUS_FIND_CHANNEL\\n"); break; case SC_STATUS_GETTING_SSID_PSWD: printf("SC_STATUS_GETTING_SSID_PSWD\\n"); sc_type *type = pdata; if (*type == SC_TYPE_ESPTOUCH) printf("SC_TYPE:SC_TYPE_ESPTOUCH\\n"); else printf("SC_TYPE:SC_TYPE_AIRKISS\\n"); break; case SC_STATUS_LINK: 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: printf("SC_STATUS_LINK_OVER\\n"); if (pdata != NULL) //SC_TYPE_ESPTOUCH uint8 phone_ip[4] = 0; memcpy(phone_ip, (uint8*)pdata, 4); printf("Phone ip: %d.%d.%d.%d\\n",phone_ip[0],phone_ip[1],phone_ip[2],phone_ip[3]); else //SC_TYPE_AIRKISS - support airkiss v2.0 airkiss_start_discover(); smartconfig_stop(); break; void ICACHE_FLASH_ATTR smartconfig_task(void *pvParameters) smartconfig_start(smartconfig_done); vTaskDelete(NULL);
smart_config.h
#ifndef APP_INCLUDE_SMART_CONFIG_H_ #define APP_INCLUDE_SMART_CONFIG_H_ void smartconfig_task(void *pvParameters); #endif /* APP_INCLUDE_SMART_CONFIG_H_ */
然后主函数
wifi_station_disconnect(); wifi_set_event_handler_cb(wifi_event_monitor_handle_event_cb); wifi_set_opmode(STATION_MODE); smartconfig_set_type(SC_TYPE_ESPTOUCH_AIRKISS);//SmartConfig + AirKiss xTaskCreate(smartconfig_task, "smartconfig_task", 256, NULL, 2, NULL);
现在是模块一启动就进去配网...
编译出错
加上 -lairkiss\\
下载进去,咱先测试下
SmartConfig:
下载完WIFI的程序,复位下WIFI
这个APP是我做的一个面向开发使用的,该APP源码获取方式: https://www.cnblogs.com/yangfengwu/p/11249674.html
AirKiss : 关注我的这个测试用的公众号
复位WIFI模块
好,现在优化下
按下固件按钮(GPIO0)大约3S, 让GPIO2那个灯快闪,进入配网模式,然后60S超时检测.还有就是不让WIFI打印官方内部写的东西(打印的东西太多了...)
以上是关于26-ESP8266 SDK开发基础入门篇--编写WIFI模块 SmartConfig/Airkiss 一键配网的主要内容,如果未能解决你的问题,请参考以下文章
19-ESP8266 SDK开发基础入门篇--C# TCP客户端编写 , 连接和断开
28-ESP8266 SDK开发基础入门篇--编写TCP 客户端程序(官方API版,非RTOS版)
6-ESP8266 SDK开发基础入门篇--操作系统入门使用
12-ESP8266 SDK开发基础入门篇--PWM,呼吸灯