mirror of
https://github.com/espressif/ESP8266_RTOS_SDK.git
synced 2025-07-15 08:32:42 +08:00
Merge branch 'feature/refactor_smartconfig_example' into 'master'
feat(examples): refactor smartconfig example See merge request sdk/ESP8266_RTOS_SDK!291
This commit is contained in:
50
examples/wifi/smart_config/README.md
Normal file
50
examples/wifi/smart_config/README.md
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
# smartconfig Example
|
||||||
|
|
||||||
|
This example shows how ESP8266 connects to AP with ESPTOUCH. Example does the following steps:
|
||||||
|
|
||||||
|
* Download ESPTOUCH APP from app store. [Android source code](https://github.com/EspressifApp/EsptouchForAndroid) and [iOS source code](https://github.com/EspressifApp/EsptouchForIOS) is available.
|
||||||
|
|
||||||
|
* Compile this example and upload it to an ESP8266.
|
||||||
|
|
||||||
|
* Make sure your phone connect to target AP (2.4GHz).
|
||||||
|
|
||||||
|
* Open ESPTOUCH app and input password. There will be success message after few sec.
|
||||||
|
|
||||||
|
### Example output
|
||||||
|
|
||||||
|
Here is an example of smartconfig console output.
|
||||||
|
```
|
||||||
|
I (162) boot: Loaded app from partition at offset 0x10000
|
||||||
|
mode : sta(ec:fa:bc:1d:33:e0)
|
||||||
|
add if0
|
||||||
|
SC version: V2.5.4
|
||||||
|
scandone
|
||||||
|
scandone
|
||||||
|
I (5) sc: SC_STATUS_FINDING_CHANNEL
|
||||||
|
|
||||||
|
TYPE: ESPTOUCH
|
||||||
|
T|PHONE MAC: 90 f0 52 0c 16 2d
|
||||||
|
T|AP MAC : bc 5f f6 1b e8 1c
|
||||||
|
I (8) sc: SC_STATUS_GETTING_SSID_PSWD
|
||||||
|
T|pswd: 1234567890
|
||||||
|
T|ssid: TEST001
|
||||||
|
I (17) sc: SC_STATUS_LINK
|
||||||
|
I (17) sc: SSID:TEST001
|
||||||
|
I (17) sc: PASSWORD:1234567890
|
||||||
|
scandone
|
||||||
|
state: 0 -> 2 (b0)
|
||||||
|
state: 2 -> 3 (0)
|
||||||
|
state: 3 -> 5 (10)
|
||||||
|
add 0
|
||||||
|
aid 2
|
||||||
|
pm open phy_2,type:2 0 0
|
||||||
|
cnt
|
||||||
|
|
||||||
|
connected with TEST001, channel 3
|
||||||
|
I (22) event: sta ip: 192.168.0.108, mask: 255.255.255.0, gw: 192.168.0.1
|
||||||
|
I (22) sc: WiFi Connected to ap
|
||||||
|
I (25) sc: SC_STATUS_LINK_OVER
|
||||||
|
I (25) sc: Phone ip: 192.168.0.108
|
||||||
|
|
||||||
|
I (25) sc: smartconfig over
|
||||||
|
```
|
@ -1,8 +0,0 @@
|
|||||||
if you want to use AIRKISS2.0 LAN discovery, should include airkiss.h and include libairkiss.a in makefile.
|
|
||||||
|
|
||||||
you can follow the steps below to achieve the function of LAN discovery.
|
|
||||||
1.scan the two-dimension code in your wechat.
|
|
||||||
2.running this smartconfig example.
|
|
||||||
3.wait device connect to AP and LAN discovery.
|
|
||||||
|
|
||||||
More detailed introduction refer to wechat.
|
|
125
examples/wifi/smart_config/main/smartconfig_main.c
Normal file
125
examples/wifi/smart_config/main/smartconfig_main.c
Normal file
@ -0,0 +1,125 @@
|
|||||||
|
/* Esptouch example
|
||||||
|
|
||||||
|
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, this
|
||||||
|
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||||
|
CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include "freertos/FreeRTOS.h"
|
||||||
|
#include "freertos/task.h"
|
||||||
|
#include "freertos/event_groups.h"
|
||||||
|
#include "esp_wifi.h"
|
||||||
|
#include "esp_event_loop.h"
|
||||||
|
#include "esp_log.h"
|
||||||
|
#include "esp_system.h"
|
||||||
|
#include "nvs_flash.h"
|
||||||
|
#include "tcpip_adapter.h"
|
||||||
|
#include "esp_smartconfig.h"
|
||||||
|
|
||||||
|
/* FreeRTOS event group to signal when we are connected & ready to make a request */
|
||||||
|
static EventGroupHandle_t wifi_event_group;
|
||||||
|
|
||||||
|
/* The event group allows multiple bits for each event,
|
||||||
|
but we only care about one event - are we connected
|
||||||
|
to the AP with an IP? */
|
||||||
|
static const int CONNECTED_BIT = 1<<0;
|
||||||
|
static const int ESPTOUCH_DONE_BIT = 1<<1;
|
||||||
|
static const char *TAG = "sc";
|
||||||
|
|
||||||
|
void smartconfig_example_task(void * parm);
|
||||||
|
|
||||||
|
static esp_err_t event_handler(void *ctx, system_event_t *event)
|
||||||
|
{
|
||||||
|
switch(event->event_id) {
|
||||||
|
case SYSTEM_EVENT_STA_START:
|
||||||
|
xTaskCreate(smartconfig_example_task, "smartconfig_example_task", 4096, NULL, 3, NULL);
|
||||||
|
break;
|
||||||
|
case SYSTEM_EVENT_STA_GOT_IP:
|
||||||
|
xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
|
||||||
|
break;
|
||||||
|
case SYSTEM_EVENT_STA_DISCONNECTED:
|
||||||
|
esp_wifi_connect();
|
||||||
|
xEventGroupClearBits(wifi_event_group, CONNECTED_BIT);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return ESP_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void initialise_wifi(void)
|
||||||
|
{
|
||||||
|
tcpip_adapter_init();
|
||||||
|
wifi_event_group = xEventGroupCreate();
|
||||||
|
ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) );
|
||||||
|
|
||||||
|
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||||
|
|
||||||
|
ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
|
||||||
|
ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
|
||||||
|
ESP_ERROR_CHECK( esp_wifi_start() );
|
||||||
|
}
|
||||||
|
|
||||||
|
static void sc_callback(smartconfig_status_t status, void *pdata)
|
||||||
|
{
|
||||||
|
switch (status) {
|
||||||
|
case SC_STATUS_WAIT:
|
||||||
|
ESP_LOGI(TAG, "SC_STATUS_WAIT");
|
||||||
|
break;
|
||||||
|
case SC_STATUS_FIND_CHANNEL:
|
||||||
|
ESP_LOGI(TAG, "SC_STATUS_FINDING_CHANNEL");
|
||||||
|
break;
|
||||||
|
case SC_STATUS_GETTING_SSID_PSWD:
|
||||||
|
ESP_LOGI(TAG, "SC_STATUS_GETTING_SSID_PSWD");
|
||||||
|
break;
|
||||||
|
case SC_STATUS_LINK:
|
||||||
|
ESP_LOGI(TAG, "SC_STATUS_LINK");
|
||||||
|
wifi_config_t *wifi_config = pdata;
|
||||||
|
ESP_LOGI(TAG, "SSID:%s", wifi_config->sta.ssid);
|
||||||
|
ESP_LOGI(TAG, "PASSWORD:%s", wifi_config->sta.password);
|
||||||
|
ESP_ERROR_CHECK( esp_wifi_disconnect() );
|
||||||
|
ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, wifi_config) );
|
||||||
|
ESP_ERROR_CHECK( esp_wifi_connect() );
|
||||||
|
break;
|
||||||
|
case SC_STATUS_LINK_OVER:
|
||||||
|
ESP_LOGI(TAG, "SC_STATUS_LINK_OVER");
|
||||||
|
if (pdata != NULL) {
|
||||||
|
uint8_t phone_ip[4] = { 0 };
|
||||||
|
memcpy(phone_ip, (uint8_t* )pdata, 4);
|
||||||
|
ESP_LOGI(TAG, "Phone ip: %d.%d.%d.%d\n", phone_ip[0], phone_ip[1], phone_ip[2], phone_ip[3]);
|
||||||
|
}
|
||||||
|
xEventGroupSetBits(wifi_event_group, ESPTOUCH_DONE_BIT);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void smartconfig_example_task(void * parm)
|
||||||
|
{
|
||||||
|
EventBits_t uxBits;
|
||||||
|
ESP_ERROR_CHECK( esp_smartconfig_set_type(SC_TYPE_ESPTOUCH) );
|
||||||
|
ESP_ERROR_CHECK( esp_smartconfig_start(sc_callback) );
|
||||||
|
while (1) {
|
||||||
|
uxBits = xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT | ESPTOUCH_DONE_BIT, true, false, portMAX_DELAY);
|
||||||
|
if(uxBits & CONNECTED_BIT) {
|
||||||
|
ESP_LOGI(TAG, "WiFi Connected to ap");
|
||||||
|
}
|
||||||
|
if(uxBits & ESPTOUCH_DONE_BIT) {
|
||||||
|
ESP_LOGI(TAG, "smartconfig over");
|
||||||
|
esp_smartconfig_stop();
|
||||||
|
vTaskDelete(NULL);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void app_main()
|
||||||
|
{
|
||||||
|
ESP_ERROR_CHECK( nvs_flash_init() );
|
||||||
|
initialise_wifi();
|
||||||
|
}
|
||||||
|
|
@ -1,215 +0,0 @@
|
|||||||
/* smartconfig example
|
|
||||||
|
|
||||||
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
|
||||||
|
|
||||||
Unless required by applicable law or agreed to in writing, this
|
|
||||||
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
||||||
CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "freertos/FreeRTOS.h"
|
|
||||||
#include "freertos/task.h"
|
|
||||||
|
|
||||||
#include "lwip/sockets.h"
|
|
||||||
#include "lwip/dns.h"
|
|
||||||
#include "lwip/netdb.h"
|
|
||||||
|
|
||||||
#include "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
|
|
||||||
|
|
||||||
static esp_udp ssdp_udp;
|
|
||||||
static struct espconn pssdpudpconn;
|
|
||||||
static os_timer_t ssdp_time_serv;
|
|
||||||
|
|
||||||
uint8_t lan_buf[200];
|
|
||||||
uint16_t lan_buf_len;
|
|
||||||
uint8_t udp_sent_cnt = 0;
|
|
||||||
|
|
||||||
const airkiss_config_t akconf = {
|
|
||||||
(airkiss_memset_fn)& memset,
|
|
||||||
(airkiss_memcpy_fn)& memcpy,
|
|
||||||
(airkiss_memcmp_fn)& memcmp,
|
|
||||||
0,
|
|
||||||
};
|
|
||||||
|
|
||||||
static void airkiss_wifilan_time_callback(void)
|
|
||||||
{
|
|
||||||
uint16_t 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) {
|
|
||||||
printf("Pack lan packet error!");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = espconn_sendto(&pssdpudpconn, lan_buf, lan_buf_len);
|
|
||||||
|
|
||||||
if (ret != 0) {
|
|
||||||
printf("UDP send error!");
|
|
||||||
}
|
|
||||||
|
|
||||||
printf("Finish send notify!\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
static void airkiss_wifilan_recv_callbk(void* arg, char* pdata, unsigned short len)
|
|
||||||
{
|
|
||||||
uint16_t 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);
|
|
||||||
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]);
|
|
||||||
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) {
|
|
||||||
printf("Pack lan packet error!");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
printf("\r\n\r\n");
|
|
||||||
|
|
||||||
for (i = 0; i < lan_buf_len; i++) {
|
|
||||||
printf("%c", lan_buf[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
printf("\r\n\r\n");
|
|
||||||
|
|
||||||
packret = espconn_sendto(&pssdpudpconn, lan_buf, lan_buf_len);
|
|
||||||
|
|
||||||
if (packret != 0) {
|
|
||||||
printf("LAN UDP Send err!");
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
printf("Pack is not ssdq req!%d\r\n", ret);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void 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 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 smartconfig_task(void* pvParameters)
|
|
||||||
{
|
|
||||||
smartconfig_start(smartconfig_done);
|
|
||||||
|
|
||||||
vTaskDelete(NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
/******************************************************************************
|
|
||||||
* FunctionName : user_init
|
|
||||||
* Description : entry of user application, init user function here
|
|
||||||
* Parameters : none
|
|
||||||
* Returns : none
|
|
||||||
*******************************************************************************/
|
|
||||||
void user_init(void)
|
|
||||||
{
|
|
||||||
printf("SDK version:%s\n", esp_get_idf_version());
|
|
||||||
|
|
||||||
/* need to set opmode before you set config */
|
|
||||||
wifi_set_opmode(STATION_MODE);
|
|
||||||
|
|
||||||
xTaskCreate(smartconfig_task, "smartconfig_task", 1024, NULL, 2, NULL);
|
|
||||||
}
|
|
||||||
|
|
Binary file not shown.
@ -1,40 +0,0 @@
|
|||||||
1¡¢compile options
|
|
||||||
|
|
||||||
(1) COMPILE
|
|
||||||
Possible value: xcc
|
|
||||||
Default value:
|
|
||||||
If not set, use gcc by default.
|
|
||||||
|
|
||||||
(2) BOOT
|
|
||||||
Possible value: none/old/new
|
|
||||||
none: no need boot
|
|
||||||
old: use boot_v1.1
|
|
||||||
new: use boot_v1.2
|
|
||||||
Default value: new
|
|
||||||
|
|
||||||
(3) APP
|
|
||||||
Possible value: 0/1/2
|
|
||||||
0: original mode, generate eagle.app.v6.flash.bin and eagle.app.v6.irom0text.bin
|
|
||||||
1: generate user1
|
|
||||||
2: generate user2
|
|
||||||
Default value: 0
|
|
||||||
|
|
||||||
(3) SPI_SPEED
|
|
||||||
Possible value: 20/26.7/40/80
|
|
||||||
Default value: 40
|
|
||||||
|
|
||||||
(4) SPI_MODE
|
|
||||||
Possible value: QIO/QOUT/DIO/DOUT
|
|
||||||
Default value: QIO
|
|
||||||
|
|
||||||
(4) SPI_SIZE_MAP
|
|
||||||
Possible value: 0/2/3/4/5/6
|
|
||||||
Default value: 0
|
|
||||||
|
|
||||||
For example:
|
|
||||||
make COMPILE=gcc BOOT=new APP=1 SPI_SPEED=40 SPI_MODE=QIO SPI_SIZE_MAP=0
|
|
||||||
|
|
||||||
2¡¢You can also use gen_misc to make and generate specific bin you needed.
|
|
||||||
Linux: ./gen_misc.sh
|
|
||||||
Windows: gen_misc.bat
|
|
||||||
Follow the tips and steps.
|
|
Reference in New Issue
Block a user