From 4dee04c03b4d6f3f437d327cf09efac14326c13f Mon Sep 17 00:00:00 2001 From: Dong Heng Date: Wed, 15 Jan 2020 16:26:34 +0800 Subject: [PATCH] feat(esp_event): examples use esp_event instead of old event process --- .../protocol_examples_common/connect.c | 115 +++++++------ .../include/protocol_examples_common.h | 5 + examples/protocols/coap_client/CMakeLists.txt | 4 + examples/protocols/coap_client/Makefile | 2 + .../coap_client/main/Kconfig.projbuild | 12 -- .../main/coap_client_example_main.c | 86 ++-------- examples/protocols/coap_server/CMakeLists.txt | 4 + examples/protocols/coap_server/Makefile | 2 + .../coap_server/main/Kconfig.projbuild | 15 -- .../main/coap_server_example_main.c | 85 ++-------- .../protocols/esp_http_client/CMakeLists.txt | 4 + examples/protocols/esp_http_client/Makefile | 2 + .../esp_http_client/main/CMakeLists.txt | 5 +- .../esp_http_client/main/Kconfig.projbuild | 17 -- .../protocols/esp_http_client/main/app_wifi.c | 81 ---------- .../protocols/esp_http_client/main/app_wifi.h | 17 -- .../main/esp_http_client_example.c | 22 +-- .../protocols/http_request/CMakeLists.txt | 4 + examples/protocols/http_request/Makefile | 2 + .../http_request/main/Kconfig.projbuild | 17 -- .../main/http_request_example_main.c | 90 ++--------- .../http_server/advanced_tests/CMakeLists.txt | 4 + .../http_server/advanced_tests/Makefile | 2 + .../advanced_tests/main/Kconfig.projbuild | 16 -- .../http_server/advanced_tests/main/main.c | 103 ++++-------- .../persistent_sockets/CMakeLists.txt | 4 + .../http_server/persistent_sockets/Makefile | 2 + .../persistent_sockets/main/Kconfig.projbuild | 16 -- .../persistent_sockets/main/main.c | 104 ++++-------- .../http_server/simple/CMakeLists.txt | 4 + .../protocols/http_server/simple/Makefile | 2 + .../http_server/simple/main/Kconfig.projbuild | 16 -- .../protocols/http_server/simple/main/main.c | 109 +++++-------- .../protocols/https_mbedtls/CMakeLists.txt | 4 + examples/protocols/https_mbedtls/Makefile | 2 + .../https_mbedtls/main/Kconfig.projbuild | 17 -- .../main/https_mbedtls_example_main.c | 90 ++--------- .../protocols/https_request/CMakeLists.txt | 4 + examples/protocols/https_request/Makefile | 2 + .../https_request/main/Kconfig.projbuild | 17 -- .../main/https_request_example_main.c | 88 ++-------- examples/protocols/mdns/CMakeLists.txt | 4 + examples/protocols/mdns/Makefile | 2 + .../protocols/mdns/main/Kconfig.projbuild | 14 -- .../protocols/mdns/main/mdns_example_main.c | 109 +++---------- examples/protocols/mqtt/ssl/CMakeLists.txt | 4 + examples/protocols/mqtt/ssl/Makefile | 2 + .../protocols/mqtt/ssl/main/Kconfig.projbuild | 12 -- examples/protocols/mqtt/ssl/main/app_main.c | 79 ++------- examples/protocols/mqtt/ssl/sdkconfig.ci | 1 + .../protocols/mqtt/ssl/sdkconfig.defaults | 1 + .../mqtt/ssl_mutual_auth/CMakeLists.txt | 4 + .../protocols/mqtt/ssl_mutual_auth/Makefile | 2 + .../ssl_mutual_auth/main/Kconfig.projbuild | 15 -- .../mqtt/ssl_mutual_auth/main/app_main.c | 79 ++------- .../mqtt/ssl_mutual_auth/sdkconfig.defaults | 1 + examples/protocols/mqtt/tcp/CMakeLists.txt | 4 + examples/protocols/mqtt/tcp/Makefile | 2 + .../protocols/mqtt/tcp/main/Kconfig.projbuild | 12 -- examples/protocols/mqtt/tcp/main/app_main.c | 77 ++------- examples/protocols/mqtt/tcp/sdkconfig.ci | 1 + .../protocols/mqtt/tcp/sdkconfig.defaults | 1 + examples/protocols/mqtt/ws/CMakeLists.txt | 4 + examples/protocols/mqtt/ws/Makefile | 2 + .../protocols/mqtt/ws/main/Kconfig.projbuild | 12 -- examples/protocols/mqtt/ws/main/app_main.c | 78 ++------- examples/protocols/mqtt/ws/sdkconfig.ci | 1 + examples/protocols/mqtt/ws/sdkconfig.defaults | 1 + examples/protocols/mqtt/wss/CMakeLists.txt | 4 + examples/protocols/mqtt/wss/Makefile | 2 + .../protocols/mqtt/wss/main/Kconfig.projbuild | 12 -- examples/protocols/mqtt/wss/main/app_main.c | 79 ++------- examples/protocols/mqtt/wss/sdkconfig.ci | 1 + .../protocols/mqtt/wss/sdkconfig.defaults | 1 + .../protocols/openssl_client/CMakeLists.txt | 4 + examples/protocols/openssl_client/Makefile | 2 + .../openssl_client/main/Kconfig.projbuild | 14 -- .../main/openssl_client_example_main.c | 92 ++--------- .../protocols/openssl_demo/CMakeLists.txt | 4 + examples/protocols/openssl_demo/Makefile | 2 + .../openssl_demo/main/Kconfig.projbuild | 17 -- .../main/openssl_demo_example_main.c | 100 ++---------- .../protocols/openssl_server/CMakeLists.txt | 4 + examples/protocols/openssl_server/Makefile | 2 + .../openssl_server/main/Kconfig.projbuild | 17 -- .../main/openssl_server_example_main.c | 95 ++--------- examples/protocols/sntp/CMakeLists.txt | 4 + examples/protocols/sntp/Makefile | 2 + .../protocols/sntp/main/Kconfig.projbuild | 17 -- .../protocols/sntp/main/sntp_example_main.c | 94 ++--------- .../sockets/tcp_client/CMakeLists.txt | 4 + .../protocols/sockets/tcp_client/Makefile | 2 + .../sockets/tcp_client/main/Kconfig.projbuild | 15 +- .../sockets/tcp_client/main/tcp_client.c | 112 ++----------- .../sockets/tcp_server/CMakeLists.txt | 4 + .../protocols/sockets/tcp_server/Makefile | 2 + .../sockets/tcp_server/main/Kconfig.projbuild | 15 +- .../sockets/tcp_server/main/tcp_server.c | 112 ++----------- .../sockets/udp_client/CMakeLists.txt | 4 + .../protocols/sockets/udp_client/Makefile | 2 + .../sockets/udp_client/main/Kconfig.projbuild | 15 +- .../sockets/udp_client/main/udp_client.c | 112 ++----------- .../sockets/udp_multicast/CMakeLists.txt | 4 + .../protocols/sockets/udp_multicast/Makefile | 2 + .../udp_multicast/main/Kconfig.projbuild | 16 +- .../main/udp_multicast_example_main.c | 101 ++---------- .../sockets/udp_server/CMakeLists.txt | 4 + .../protocols/sockets/udp_server/Makefile | 2 + .../sockets/udp_server/main/Kconfig.projbuild | 15 +- .../sockets/udp_server/main/udp_server.c | 112 ++----------- .../provisioning/custom_config/CMakeLists.txt | 4 + examples/provisioning/custom_config/Makefile | 2 + .../custom_config/main/Kconfig.projbuild | 6 + .../custom_config/main/app_main.c | 82 +++++----- .../provisioning/softap_prov/CMakeLists.txt | 4 + examples/provisioning/softap_prov/Makefile | 2 + .../softap_prov/main/Kconfig.projbuild | 6 + .../provisioning/softap_prov/main/app_main.c | 63 ++++---- .../console/main/console_example_main.c | 2 +- examples/system/factory-test/main/main.c | 5 +- .../new_to_new_no_old/CMakeLists.txt | 4 + .../1MB_flash/new_to_new_no_old/Makefile | 2 + .../new_to_new_no_old/main/Kconfig.projbuild | 14 -- .../new_to_new_no_old/main/ota_example_main.c | 90 ++--------- .../new_to_new_no_old_copy/CMakeLists.txt | 4 + .../1MB_flash/new_to_new_no_old_copy/Makefile | 2 + .../main/Kconfig.projbuild | 14 -- .../main/ota_example_main.c | 90 ++--------- .../new_to_new_with_old/CMakeLists.txt | 4 + .../1MB_flash/new_to_new_with_old/Makefile | 2 + .../main/Kconfig.projbuild | 16 -- .../main/ota_example_main.c | 101 +++--------- .../new_to_new_no_old/CMakeLists.txt | 4 + .../2+MB_flash/new_to_new_no_old/Makefile | 2 + .../new_to_new_no_old/main/Kconfig.projbuild | 14 -- .../new_to_new_no_old/main/ota_example_main.c | 90 ++--------- .../new_to_new_with_old/CMakeLists.txt | 4 + .../2+MB_flash/new_to_new_with_old/Makefile | 2 + .../main/Kconfig.projbuild | 16 -- .../main/ota_example_main.c | 101 +++--------- .../ota/simple_ota_example/CMakeLists.txt | 4 + .../system/ota/simple_ota_example/Makefile | 2 + .../simple_ota_example/main/Kconfig.projbuild | 12 -- .../main/simple_ota_example.c | 92 ++--------- .../wifi/espnow/main/espnow_example_main.c | 16 +- .../getting_started/softAP/CMakeLists.txt | 6 + .../softAP}/Makefile | 2 +- .../wifi/getting_started/softAP/README.md | 42 +++++ .../softAP/main/CMakeLists.txt | 2 + .../softAP/main/Kconfig.projbuild | 20 +++ .../getting_started/softAP/main/component.mk | 8 + .../softAP/main/softap_example_main.c | 83 ++++++++++ .../getting_started/station/CMakeLists.txt | 6 + .../wifi/getting_started/station/Makefile | 9 ++ .../wifi/getting_started/station/README.md | 109 +++++++++++++ .../station/main/CMakeLists.txt | 2 + .../station/main/Kconfig.projbuild | 20 +++ .../getting_started/station/main/component.mk | 8 + .../station/main/station_example_main.c | 126 +++++++++++++++ examples/wifi/power_save/CMakeLists.txt | 4 + examples/wifi/power_save/Makefile | 2 + .../wifi/power_save/main/Kconfig.projbuild | 12 -- examples/wifi/power_save/main/power_save.c | 99 ++---------- examples/wifi/simple_wifi/CMakeLists.txt | 6 - examples/wifi/simple_wifi/main/CMakeLists.txt | 3 - .../wifi/simple_wifi/main/Kconfig.projbuild | 37 ----- examples/wifi/simple_wifi/main/component.mk | 4 - examples/wifi/simple_wifi/main/simple_wifi.c | 152 ------------------ .../wifi/smart_config/main/smartconfig_main.c | 46 +++--- examples/wifi/sniffer/main/sniffer_main.c | 24 +-- .../main/wpa2_enterprise_main.c | 60 ++----- examples/wifi/wps/main/wps.c | 75 ++++----- 172 files changed, 1347 insertions(+), 3443 deletions(-) delete mode 100644 examples/protocols/coap_server/main/Kconfig.projbuild delete mode 100644 examples/protocols/esp_http_client/main/Kconfig.projbuild delete mode 100644 examples/protocols/esp_http_client/main/app_wifi.c delete mode 100644 examples/protocols/esp_http_client/main/app_wifi.h delete mode 100644 examples/protocols/http_request/main/Kconfig.projbuild delete mode 100644 examples/protocols/http_server/advanced_tests/main/Kconfig.projbuild delete mode 100644 examples/protocols/http_server/persistent_sockets/main/Kconfig.projbuild delete mode 100644 examples/protocols/http_server/simple/main/Kconfig.projbuild delete mode 100644 examples/protocols/https_mbedtls/main/Kconfig.projbuild delete mode 100644 examples/protocols/https_request/main/Kconfig.projbuild create mode 100644 examples/protocols/mqtt/ssl/sdkconfig.defaults delete mode 100644 examples/protocols/mqtt/ssl_mutual_auth/main/Kconfig.projbuild create mode 100644 examples/protocols/mqtt/ssl_mutual_auth/sdkconfig.defaults create mode 100644 examples/protocols/mqtt/tcp/sdkconfig.defaults create mode 100644 examples/protocols/mqtt/ws/sdkconfig.defaults create mode 100644 examples/protocols/mqtt/wss/sdkconfig.defaults delete mode 100644 examples/protocols/openssl_demo/main/Kconfig.projbuild delete mode 100644 examples/protocols/openssl_server/main/Kconfig.projbuild delete mode 100644 examples/protocols/sntp/main/Kconfig.projbuild create mode 100644 examples/wifi/getting_started/softAP/CMakeLists.txt rename examples/wifi/{simple_wifi => getting_started/softAP}/Makefile (84%) create mode 100644 examples/wifi/getting_started/softAP/README.md create mode 100644 examples/wifi/getting_started/softAP/main/CMakeLists.txt create mode 100644 examples/wifi/getting_started/softAP/main/Kconfig.projbuild create mode 100644 examples/wifi/getting_started/softAP/main/component.mk create mode 100644 examples/wifi/getting_started/softAP/main/softap_example_main.c create mode 100644 examples/wifi/getting_started/station/CMakeLists.txt create mode 100644 examples/wifi/getting_started/station/Makefile create mode 100644 examples/wifi/getting_started/station/README.md create mode 100644 examples/wifi/getting_started/station/main/CMakeLists.txt create mode 100644 examples/wifi/getting_started/station/main/Kconfig.projbuild create mode 100644 examples/wifi/getting_started/station/main/component.mk create mode 100644 examples/wifi/getting_started/station/main/station_example_main.c delete mode 100644 examples/wifi/simple_wifi/CMakeLists.txt delete mode 100644 examples/wifi/simple_wifi/main/CMakeLists.txt delete mode 100644 examples/wifi/simple_wifi/main/Kconfig.projbuild delete mode 100644 examples/wifi/simple_wifi/main/component.mk delete mode 100644 examples/wifi/simple_wifi/main/simple_wifi.c diff --git a/examples/common_components/protocol_examples_common/connect.c b/examples/common_components/protocol_examples_common/connect.c index 5b6b2814..fd348c8a 100644 --- a/examples/common_components/protocol_examples_common/connect.c +++ b/examples/common_components/protocol_examples_common/connect.c @@ -8,6 +8,7 @@ */ #include + #include "protocol_examples_common.h" #include "sdkconfig.h" #include "esp_event.h" @@ -32,7 +33,8 @@ static EventGroupHandle_t s_connect_event_group; static ip4_addr_t s_ip_addr; -static const char *s_connection_name; +static char s_connection_name[32] = CONFIG_EXAMPLE_WIFI_SSID; +static char s_connection_passwd[32] = CONFIG_EXAMPLE_WIFI_PASSWORD; #ifdef CONFIG_EXAMPLE_CONNECT_IPV6 static ip6_addr_t s_ipv6_addr; @@ -40,65 +42,70 @@ static ip6_addr_t s_ipv6_addr; static const char *TAG = "example_connect"; -static esp_err_t event_handler(void *ctx, system_event_t *event) +static void on_wifi_disconnect(void *arg, esp_event_base_t event_base, + int32_t event_id, void *event_data) { - /* For accessing reason codes in case of disconnection */ - system_event_info_t *info = &event->event_info; + system_event_sta_disconnected_t *event = (system_event_sta_disconnected_t *)event_data; - switch (event->event_id) { - case SYSTEM_EVENT_STA_START: - esp_wifi_connect(); - break; - case SYSTEM_EVENT_STA_GOT_IP: - memcpy(&s_ip_addr, &event->event_info.got_ip.ip_info.ip, sizeof(s_ip_addr)); - xEventGroupSetBits(s_connect_event_group, GOT_IPV4_BIT);; - break; -#ifdef CONFIG_EXAMPLE_CONNECT_IPV6 - case SYSTEM_EVENT_STA_CONNECTED: - tcpip_adapter_create_ip6_linklocal(TCPIP_ADAPTER_IF_STA); - break; - case SYSTEM_EVENT_AP_STA_GOT_IP6: - memcpy(&s_ipv6_addr, &event->event_info.got_ip6.ip6_info, sizeof(s_ipv6_addr)); - xEventGroupSetBits(s_connect_event_group, GOT_IPV6_BIT); - break; -#endif - case SYSTEM_EVENT_STA_DISCONNECTED: - ESP_LOGE(TAG, "Disconnect reason : %d", info->disconnected.reason); - if (info->disconnected.reason == WIFI_REASON_BASIC_RATE_NOT_SUPPORT) { - /*Switch to 802.11 bgn mode */ - esp_wifi_set_protocol(ESP_IF_WIFI_STA, WIFI_PROTOCAL_11B | WIFI_PROTOCAL_11G | WIFI_PROTOCAL_11N); - } - esp_wifi_connect(); - xEventGroupClearBits(s_connect_event_group, GOT_IPV4_BIT); -#ifdef CONFIG_EXAMPLE_CONNECT_IPV6 - xEventGroupClearBits(s_connect_event_group, GOT_IPV6_BIT); -#endif - break; - default: - break; + ESP_LOGI(TAG, "Wi-Fi disconnected, trying to reconnect..."); + if (event->reason == WIFI_REASON_BASIC_RATE_NOT_SUPPORT) { + /*Switch to 802.11 bgn mode */ + esp_wifi_set_protocol(ESP_IF_WIFI_STA, WIFI_PROTOCAL_11B | WIFI_PROTOCAL_11G | WIFI_PROTOCAL_11N); } - return ESP_OK; + ESP_ERROR_CHECK(esp_wifi_connect()); } +#ifdef CONFIG_EXAMPLE_CONNECT_IPV6 +static void on_wifi_connect(void *arg, esp_event_base_t event_base, + int32_t event_id, void *event_data) +{ + tcpip_adapter_create_ip6_linklocal(TCPIP_ADAPTER_IF_STA); +} +#endif + +static void on_got_ip(void *arg, esp_event_base_t event_base, + int32_t event_id, void *event_data) +{ + ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data; + memcpy(&s_ip_addr, &event->ip_info.ip, sizeof(s_ip_addr)); + xEventGroupSetBits(s_connect_event_group, GOT_IPV4_BIT); +} + +#ifdef CONFIG_EXAMPLE_CONNECT_IPV6 + +static void on_got_ipv6(void *arg, esp_event_base_t event_base, + int32_t event_id, void *event_data) +{ + ip_event_got_ip6_t *event = (ip_event_got_ip6_t *)event_data; + memcpy(&s_ipv6_addr, &event->ip6_info.ip, sizeof(s_ipv6_addr)); + xEventGroupSetBits(s_connect_event_group, GOT_IPV6_BIT); +} + +#endif // CONFIG_EXAMPLE_CONNECT_IPV6 + static void start(void) { - 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_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &on_wifi_disconnect, NULL)); + ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &on_got_ip, NULL)); +#ifdef CONFIG_EXAMPLE_CONNECT_IPV6 + ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_CONNECTED, &on_wifi_connect, NULL)); + ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_GOT_IP6, &on_got_ipv6, NULL)); +#endif + ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM)); - wifi_config_t wifi_config = { - .sta = { - .ssid = CONFIG_EXAMPLE_WIFI_SSID, - .password = CONFIG_EXAMPLE_WIFI_PASSWORD, - }, - }; + wifi_config_t wifi_config = { 0 }; + + strncpy((char *)&wifi_config.sta.ssid, s_connection_name, 32); + strncpy((char *)&wifi_config.sta.password, s_connection_passwd, 32); + ESP_LOGI(TAG, "Connecting to %s...", wifi_config.sta.ssid); ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config)); ESP_ERROR_CHECK(esp_wifi_start()); - s_connection_name = CONFIG_EXAMPLE_WIFI_SSID; + ESP_ERROR_CHECK(esp_wifi_connect()); } static void stop(void) @@ -108,6 +115,14 @@ static void stop(void) return; } ESP_ERROR_CHECK(err); + + ESP_ERROR_CHECK(esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &on_wifi_disconnect)); + ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, &on_got_ip)); +#ifdef CONFIG_EXAMPLE_CONNECT_IPV6 + ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_GOT_IP6, &on_got_ipv6)); + ESP_ERROR_CHECK(esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_STA_CONNECTED, &on_wifi_connect)); +#endif + ESP_ERROR_CHECK(esp_wifi_deinit()); } @@ -137,6 +152,14 @@ esp_err_t example_disconnect(void) s_connect_event_group = NULL; stop(); ESP_LOGI(TAG, "Disconnected from %s", s_connection_name); - s_connection_name = NULL; + s_connection_name[0] = '\0'; + return ESP_OK; +} + +esp_err_t example_set_connection_info(const char *ssid, const char *passwd) +{ + strncpy(s_connection_name, ssid, sizeof(s_connection_name)); + strncpy(s_connection_passwd, passwd, sizeof(s_connection_passwd)); + return ESP_OK; } diff --git a/examples/common_components/protocol_examples_common/include/protocol_examples_common.h b/examples/common_components/protocol_examples_common/include/protocol_examples_common.h index 222ab6c1..3eb1ff42 100644 --- a/examples/common_components/protocol_examples_common/include/protocol_examples_common.h +++ b/examples/common_components/protocol_examples_common/include/protocol_examples_common.h @@ -48,6 +48,11 @@ esp_err_t example_disconnect(void); */ esp_err_t example_configure_stdin_stdout(void); +/** + * @brief Configure SSID and password + */ +esp_err_t example_set_connection_info(const char *ssid, const char *passwd); + #ifdef __cplusplus } #endif diff --git a/examples/protocols/coap_client/CMakeLists.txt b/examples/protocols/coap_client/CMakeLists.txt index 671988b7..77a85f8d 100644 --- a/examples/protocols/coap_client/CMakeLists.txt +++ b/examples/protocols/coap_client/CMakeLists.txt @@ -2,5 +2,9 @@ # in this exact order for cmake to work correctly cmake_minimum_required(VERSION 3.5) +# (Not part of the boilerplate) +# This example uses an extra component for common functions such as Wi-Fi and Ethernet connection. +set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common) + include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(coap_client) diff --git a/examples/protocols/coap_client/Makefile b/examples/protocols/coap_client/Makefile index c9a25d11..84a2905f 100644 --- a/examples/protocols/coap_client/Makefile +++ b/examples/protocols/coap_client/Makefile @@ -5,5 +5,7 @@ PROJECT_NAME := coap_client +EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/protocol_examples_common + include $(IDF_PATH)/make/project.mk diff --git a/examples/protocols/coap_client/main/Kconfig.projbuild b/examples/protocols/coap_client/main/Kconfig.projbuild index 045137a0..78de5fac 100644 --- a/examples/protocols/coap_client/main/Kconfig.projbuild +++ b/examples/protocols/coap_client/main/Kconfig.projbuild @@ -6,16 +6,4 @@ config TARGET_DOMAIN_URI help Target uri for the example to use. -config WIFI_SSID - string "WiFi SSID" - default "myssid" - help - SSID (network name) for the example to connect to. - -config WIFI_PASSWORD - string "WiFi Password" - default "mypassword" - help - WiFi password (WPA or WPA2) for the example to use. - endmenu \ No newline at end of file diff --git a/examples/protocols/coap_client/main/coap_client_example_main.c b/examples/protocols/coap_client/main/coap_client_example_main.c index f28df68d..9ee11d06 100644 --- a/examples/protocols/coap_client/main/coap_client_example_main.c +++ b/examples/protocols/coap_client/main/coap_client_example_main.c @@ -13,25 +13,16 @@ #include "freertos/FreeRTOS.h" #include "freertos/task.h" -#include "freertos/event_groups.h" - +#include "esp_system.h" #include "esp_log.h" -#include "esp_wifi.h" -#include "esp_event_loop.h" +#include "esp_netif.h" +#include "esp_event.h" +#include "protocol_examples_common.h" #include "nvs_flash.h" #include "coap.h" -/* The examples use simple WiFi configuration that you can set via - 'make menuconfig'. - - If you'd rather not, just change the below entries to strings with - the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid" -*/ -#define EXAMPLE_WIFI_SSID CONFIG_WIFI_SSID -#define EXAMPLE_WIFI_PASS CONFIG_WIFI_PASSWORD - #define COAP_DEFAULT_TIME_SEC 5 #define COAP_DEFAULT_TIME_USEC 0 @@ -43,13 +34,6 @@ */ #define COAP_DEFAULT_DEMO_URI CONFIG_TARGET_DOMAIN_URI -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? */ -const static int CONNECTED_BIT = BIT0; - const static char *TAG = "CoAP_client"; static void message_handler(struct coap_context_t *ctx, const coap_endpoint_t *local_interface, const coap_address_t *remote, @@ -81,13 +65,6 @@ static void coap_example_task(void *p) uint8_t get_method = 1; while (1) { - /* Wait for the callback to set the CONNECTED_BIT in the - event group. - */ - xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT, - false, true, portMAX_DELAY); - ESP_LOGI(TAG, "Connected to AP"); - if (coap_split_uri((const uint8_t *)server_uri, strlen(server_uri), &uri) == -1) { ESP_LOGE(TAG, "CoAP server uri error"); break; @@ -157,55 +134,14 @@ static void coap_example_task(void *p) vTaskDelete(NULL); } -static esp_err_t wifi_event_handler(void *ctx, system_event_t *event) -{ - /* For accessing reason codes in case of disconnection */ - system_event_info_t *info = &event->event_info; - - switch(event->event_id) { - case SYSTEM_EVENT_STA_START: - esp_wifi_connect(); - break; - case SYSTEM_EVENT_STA_GOT_IP: - xEventGroupSetBits(wifi_event_group, CONNECTED_BIT); - break; - case SYSTEM_EVENT_STA_DISCONNECTED: - ESP_LOGE(TAG, "Disconnect reason : %d", info->disconnected.reason); - if (info->disconnected.reason == WIFI_REASON_BASIC_RATE_NOT_SUPPORT) { - /*Switch to 802.11 bgn mode */ - esp_wifi_set_protocol(ESP_IF_WIFI_STA, WIFI_PROTOCAL_11B | WIFI_PROTOCAL_11G | WIFI_PROTOCAL_11N); - } - esp_wifi_connect(); - xEventGroupClearBits(wifi_event_group, CONNECTED_BIT); - break; - default: - break; - } - return ESP_OK; -} - -static void wifi_conn_init(void) -{ - tcpip_adapter_init(); - wifi_event_group = xEventGroupCreate(); - ESP_ERROR_CHECK( esp_event_loop_init(wifi_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_storage(WIFI_STORAGE_RAM) ); - wifi_config_t wifi_config = { - .sta = { - .ssid = EXAMPLE_WIFI_SSID, - .password = EXAMPLE_WIFI_PASS, - }, - }; - ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) ); - ESP_ERROR_CHECK( esp_wifi_set_config(WIFI_IF_STA, &wifi_config) ); - ESP_ERROR_CHECK( esp_wifi_start() ); -} - void app_main(void) { - ESP_ERROR_CHECK( nvs_flash_init() ); - wifi_conn_init(); + ESP_ERROR_CHECK(nvs_flash_init()); + ESP_ERROR_CHECK(esp_netif_init()); + ESP_ERROR_CHECK(esp_event_loop_create_default()); + + + ESP_ERROR_CHECK(example_connect()); + xTaskCreate(coap_example_task, "coap", 2048, NULL, 5, NULL); } diff --git a/examples/protocols/coap_server/CMakeLists.txt b/examples/protocols/coap_server/CMakeLists.txt index 28389253..7a5876cb 100644 --- a/examples/protocols/coap_server/CMakeLists.txt +++ b/examples/protocols/coap_server/CMakeLists.txt @@ -2,5 +2,9 @@ # in this exact order for cmake to work correctly cmake_minimum_required(VERSION 3.5) +# (Not part of the boilerplate) +# This example uses an extra component for common functions such as Wi-Fi and Ethernet connection. +set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common) + include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(coap_server) diff --git a/examples/protocols/coap_server/Makefile b/examples/protocols/coap_server/Makefile index f82e5fc2..63af79f7 100644 --- a/examples/protocols/coap_server/Makefile +++ b/examples/protocols/coap_server/Makefile @@ -5,5 +5,7 @@ PROJECT_NAME := coap_server +EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/protocol_examples_common + include $(IDF_PATH)/make/project.mk diff --git a/examples/protocols/coap_server/main/Kconfig.projbuild b/examples/protocols/coap_server/main/Kconfig.projbuild deleted file mode 100644 index 7a9cb97a..00000000 --- a/examples/protocols/coap_server/main/Kconfig.projbuild +++ /dev/null @@ -1,15 +0,0 @@ -menu "Example Configuration" - -config WIFI_SSID - string "WiFi SSID" - default "myssid" - help - SSID (network name) for the example to connect to. - -config WIFI_PASSWORD - string "WiFi Password" - default "mypassword" - help - WiFi password (WPA or WPA2) for the example to use. - -endmenu \ No newline at end of file diff --git a/examples/protocols/coap_server/main/coap_server_example_main.c b/examples/protocols/coap_server/main/coap_server_example_main.c index 9ec2be39..fa5401e5 100644 --- a/examples/protocols/coap_server/main/coap_server_example_main.c +++ b/examples/protocols/coap_server/main/coap_server_example_main.c @@ -12,35 +12,19 @@ #include "freertos/FreeRTOS.h" #include "freertos/task.h" -#include "freertos/event_groups.h" - +#include "esp_system.h" #include "esp_log.h" -#include "esp_wifi.h" -#include "esp_event_loop.h" +#include "esp_netif.h" +#include "esp_event.h" +#include "protocol_examples_common.h" #include "nvs_flash.h" #include "coap.h" -/* The examples use simple WiFi configuration that you can set via - 'make menuconfig'. - - If you'd rather not, just change the below entries to strings with - the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid" -*/ -#define EXAMPLE_WIFI_SSID CONFIG_WIFI_SSID -#define EXAMPLE_WIFI_PASS CONFIG_WIFI_PASSWORD - #define COAP_DEFAULT_TIME_SEC 5 #define COAP_DEFAULT_TIME_USEC 0 -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? */ -const static int CONNECTED_BIT = BIT0; - const static char *TAG = "CoAP_server"; static coap_async_state_t *async = NULL; @@ -90,13 +74,6 @@ static void coap_example_thread(void *p) int flags = 0; while (1) { - /* Wait for the callback to set the CONNECTED_BIT in the - event group. - */ - xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT, - false, true, portMAX_DELAY); - ESP_LOGI(TAG, "Connected to AP"); - /* Prepare the CoAP server socket */ coap_address_init(&serv_addr); serv_addr.addr.sin.sin_family = AF_INET; @@ -143,56 +120,14 @@ static void coap_example_thread(void *p) vTaskDelete(NULL); } -static esp_err_t wifi_event_handler(void *ctx, system_event_t *event) -{ - /* For accessing reason codes in case of disconnection */ - system_event_info_t *info = &event->event_info; - - switch(event->event_id) { - case SYSTEM_EVENT_STA_START: - esp_wifi_connect(); - break; - case SYSTEM_EVENT_STA_GOT_IP: - xEventGroupSetBits(wifi_event_group, CONNECTED_BIT); - break; - case SYSTEM_EVENT_STA_DISCONNECTED: - ESP_LOGE(TAG, "Disconnect reason : %d", info->disconnected.reason); - if (info->disconnected.reason == WIFI_REASON_BASIC_RATE_NOT_SUPPORT) { - /*Switch to 802.11 bgn mode */ - esp_wifi_set_protocol(ESP_IF_WIFI_STA, WIFI_PROTOCAL_11B | WIFI_PROTOCAL_11G | WIFI_PROTOCAL_11N); - } - esp_wifi_connect(); - xEventGroupClearBits(wifi_event_group, CONNECTED_BIT); - break; - default: - break; - } - return ESP_OK; -} - -static void wifi_conn_init(void) -{ - tcpip_adapter_init(); - wifi_event_group = xEventGroupCreate(); - ESP_ERROR_CHECK( esp_event_loop_init(wifi_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_storage(WIFI_STORAGE_RAM) ); - wifi_config_t wifi_config = { - .sta = { - .ssid = EXAMPLE_WIFI_SSID, - .password = EXAMPLE_WIFI_PASS, - }, - }; - ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) ); - ESP_ERROR_CHECK( esp_wifi_set_config(WIFI_IF_STA, &wifi_config) ); - ESP_ERROR_CHECK( esp_wifi_start() ); -} - void app_main(void) { - ESP_ERROR_CHECK( nvs_flash_init() ); - wifi_conn_init(); + ESP_ERROR_CHECK(nvs_flash_init()); + ESP_ERROR_CHECK(esp_netif_init()); + ESP_ERROR_CHECK(esp_event_loop_create_default()); + + + ESP_ERROR_CHECK(example_connect()); xTaskCreate(coap_example_thread, "coap", 2048, NULL, 5, NULL); } diff --git a/examples/protocols/esp_http_client/CMakeLists.txt b/examples/protocols/esp_http_client/CMakeLists.txt index 6896d5ac..cce8eb1e 100644 --- a/examples/protocols/esp_http_client/CMakeLists.txt +++ b/examples/protocols/esp_http_client/CMakeLists.txt @@ -2,5 +2,9 @@ # CMakeLists in this exact order for cmake to work correctly cmake_minimum_required(VERSION 3.5) +# (Not part of the boilerplate) +# This example uses an extra component for common functions such as Wi-Fi and Ethernet connection. +set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common) + include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(esp-http-client-example) diff --git a/examples/protocols/esp_http_client/Makefile b/examples/protocols/esp_http_client/Makefile index 851c3475..50d3b5f3 100644 --- a/examples/protocols/esp_http_client/Makefile +++ b/examples/protocols/esp_http_client/Makefile @@ -5,5 +5,7 @@ PROJECT_NAME := esp-http-client-example +EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/protocol_examples_common + include $(IDF_PATH)/make/project.mk diff --git a/examples/protocols/esp_http_client/main/CMakeLists.txt b/examples/protocols/esp_http_client/main/CMakeLists.txt index 093811e6..48f4e831 100644 --- a/examples/protocols/esp_http_client/main/CMakeLists.txt +++ b/examples/protocols/esp_http_client/main/CMakeLists.txt @@ -1,7 +1,4 @@ -set(COMPONENT_SRCS "app_wifi.c" - "esp_http_client_example.c") -set(COMPONENT_ADD_INCLUDEDIRS ".") - +set(COMPONENT_SRCS "esp_http_client_example.c") # Embed the server root certificate into the final binary # diff --git a/examples/protocols/esp_http_client/main/Kconfig.projbuild b/examples/protocols/esp_http_client/main/Kconfig.projbuild deleted file mode 100644 index 1c7241da..00000000 --- a/examples/protocols/esp_http_client/main/Kconfig.projbuild +++ /dev/null @@ -1,17 +0,0 @@ -menu "Example Configuration" - -config WIFI_SSID - string "WiFi SSID" - default "myssid" - help - SSID (network name) for the example to connect to. - -config WIFI_PASSWORD - string "WiFi Password" - default "mypassword" - help - WiFi password (WPA or WPA2) for the example to use. - - Can be left blank if the network has no security set. - -endmenu diff --git a/examples/protocols/esp_http_client/main/app_wifi.c b/examples/protocols/esp_http_client/main/app_wifi.c deleted file mode 100644 index 7d8d8d97..00000000 --- a/examples/protocols/esp_http_client/main/app_wifi.c +++ /dev/null @@ -1,81 +0,0 @@ -/* ESP HTTP Client 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 "esp_wifi.h" -#include "esp_event_loop.h" -#include "esp_log.h" -#include "esp_system.h" -#include "freertos/event_groups.h" -#include "esp_wifi.h" -#include "esp_event_loop.h" -#include "esp_log.h" -#include "app_wifi.h" - -static const char *TAG = "WIFI"; - -/* 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? */ -const int CONNECTED_BIT = BIT0; - -static esp_err_t event_handler(void *ctx, system_event_t *event) -{ - /* For accessing reason codes in case of disconnection */ - system_event_info_t *info = &event->event_info; - - switch (event->event_id) { - case SYSTEM_EVENT_STA_START: - esp_wifi_connect(); - break; - case SYSTEM_EVENT_STA_GOT_IP: - xEventGroupSetBits(wifi_event_group, CONNECTED_BIT); - break; - case SYSTEM_EVENT_STA_DISCONNECTED: - ESP_LOGE(TAG, "Disconnect reason : %d", info->disconnected.reason); - if (info->disconnected.reason == WIFI_REASON_BASIC_RATE_NOT_SUPPORT) { - /*Switch to 802.11 bgn mode */ - esp_wifi_set_protocol(ESP_IF_WIFI_STA, WIFI_PROTOCAL_11B | WIFI_PROTOCAL_11G | WIFI_PROTOCAL_11N); - } - esp_wifi_connect(); - xEventGroupClearBits(wifi_event_group, CONNECTED_BIT); - break; - default: - break; - } - return ESP_OK; -} - -void app_wifi_initialise(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_storage(WIFI_STORAGE_RAM)); - wifi_config_t wifi_config = { - .sta = { - .ssid = CONFIG_WIFI_SSID, - .password = CONFIG_WIFI_PASSWORD, - }, - }; - ESP_LOGI(TAG, "Setting WiFi configuration SSID %s...", wifi_config.sta.ssid); - ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); - ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config)); - ESP_ERROR_CHECK(esp_wifi_start()); - -} - -void app_wifi_wait_connected() -{ - xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT, false, true, portMAX_DELAY); -} diff --git a/examples/protocols/esp_http_client/main/app_wifi.h b/examples/protocols/esp_http_client/main/app_wifi.h deleted file mode 100644 index 91c886d5..00000000 --- a/examples/protocols/esp_http_client/main/app_wifi.h +++ /dev/null @@ -1,17 +0,0 @@ -/* ESP HTTP Client 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. -*/ - -#ifndef _APP_WIFI_H_ -#define _APP_WIFI_H_ - -void app_wifi_initialise(void); -void app_wifi_wait_connected(); - - -#endif diff --git a/examples/protocols/esp_http_client/main/esp_http_client_example.c b/examples/protocols/esp_http_client/main/esp_http_client_example.c index 9d564277..4c5a751c 100644 --- a/examples/protocols/esp_http_client/main/esp_http_client_example.c +++ b/examples/protocols/esp_http_client/main/esp_http_client_example.c @@ -9,12 +9,16 @@ #include #include + #include "freertos/FreeRTOS.h" #include "freertos/task.h" -#include "esp_log.h" #include "esp_system.h" +#include "esp_log.h" +#include "esp_netif.h" +#include "esp_event.h" +#include "protocol_examples_common.h" +#include "nvs.h" #include "nvs_flash.h" -#include "app_wifi.h" #include "esp_http_client.h" @@ -375,7 +379,6 @@ static void https_async() static void http_test_task(void *pvParameters) { - app_wifi_wait_connected(); ESP_LOGI(TAG, "Connected to AP, begin http example"); http_rest(); http_auth_basic(); @@ -394,13 +397,12 @@ static void http_test_task(void *pvParameters) void app_main() { - esp_err_t ret = nvs_flash_init(); - if (ret == ESP_ERR_NVS_NO_FREE_PAGES) { - ESP_ERROR_CHECK(nvs_flash_erase()); - ret = nvs_flash_init(); - } - ESP_ERROR_CHECK(ret); - app_wifi_initialise(); + ESP_ERROR_CHECK(nvs_flash_init()); + ESP_ERROR_CHECK(esp_netif_init()); + ESP_ERROR_CHECK(esp_event_loop_create_default()); + + + ESP_ERROR_CHECK(example_connect()); xTaskCreate(&http_test_task, "http_test_task", 8192, NULL, 5, NULL); } diff --git a/examples/protocols/http_request/CMakeLists.txt b/examples/protocols/http_request/CMakeLists.txt index 173ebfa7..a4c540a0 100644 --- a/examples/protocols/http_request/CMakeLists.txt +++ b/examples/protocols/http_request/CMakeLists.txt @@ -2,5 +2,9 @@ # CMakeLists in this exact order for cmake to work correctly cmake_minimum_required(VERSION 3.5) +# (Not part of the boilerplate) +# This example uses an extra component for common functions such as Wi-Fi and Ethernet connection. +set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common) + include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(http_request) diff --git a/examples/protocols/http_request/Makefile b/examples/protocols/http_request/Makefile index 26250750..3d31a530 100644 --- a/examples/protocols/http_request/Makefile +++ b/examples/protocols/http_request/Makefile @@ -5,5 +5,7 @@ PROJECT_NAME := http-request +EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/protocol_examples_common + include $(IDF_PATH)/make/project.mk diff --git a/examples/protocols/http_request/main/Kconfig.projbuild b/examples/protocols/http_request/main/Kconfig.projbuild deleted file mode 100644 index 92a75195..00000000 --- a/examples/protocols/http_request/main/Kconfig.projbuild +++ /dev/null @@ -1,17 +0,0 @@ -menu "Example Configuration" - -config WIFI_SSID - string "WiFi SSID" - default "myssid" - help - SSID (network name) for the example to connect to. - -config WIFI_PASSWORD - string "WiFi Password" - default "mypassword" - help - WiFi password (WPA or WPA2) for the example to use. - - Can be left blank if the network has no security set. - -endmenu diff --git a/examples/protocols/http_request/main/http_request_example_main.c b/examples/protocols/http_request/main/http_request_example_main.c index 23266c33..220d5fe1 100644 --- a/examples/protocols/http_request/main/http_request_example_main.c +++ b/examples/protocols/http_request/main/http_request_example_main.c @@ -11,34 +11,17 @@ #include "freertos/FreeRTOS.h" #include "freertos/task.h" -#include "freertos/event_groups.h" - -#include "esp_wifi.h" -#include "esp_event_loop.h" +#include "esp_system.h" #include "esp_log.h" - +#include "esp_netif.h" +#include "esp_event.h" +#include "protocol_examples_common.h" +#include "nvs.h" #include "nvs_flash.h" #include #include -/* The examples use simple WiFi configuration that you can set via - 'make menuconfig'. - - If you'd rather not, just change the below entries to strings with - the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid" -*/ -#define EXAMPLE_WIFI_SSID CONFIG_WIFI_SSID -#define EXAMPLE_WIFI_PASS CONFIG_WIFI_PASSWORD - -/* 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? */ -const int CONNECTED_BIT = BIT0; - /* Constants that aren't configurable in menuconfig */ #define WEB_SERVER "example.com" #define WEB_PORT 80 @@ -51,53 +34,6 @@ static const char *REQUEST = "GET " WEB_URL " HTTP/1.0\r\n" "User-Agent: esp-idf/1.0 esp32\r\n" "\r\n"; -static esp_err_t event_handler(void *ctx, system_event_t *event) -{ - /* For accessing reason codes in case of disconnection */ - system_event_info_t *info = &event->event_info; - - switch(event->event_id) { - case SYSTEM_EVENT_STA_START: - esp_wifi_connect(); - break; - case SYSTEM_EVENT_STA_GOT_IP: - xEventGroupSetBits(wifi_event_group, CONNECTED_BIT); - break; - case SYSTEM_EVENT_STA_DISCONNECTED: - ESP_LOGE(TAG, "Disconnect reason : %d", info->disconnected.reason); - if (info->disconnected.reason == WIFI_REASON_BASIC_RATE_NOT_SUPPORT) { - /*Switch to 802.11 bgn mode */ - esp_wifi_set_protocol(ESP_IF_WIFI_STA, WIFI_PROTOCAL_11B | WIFI_PROTOCAL_11G | WIFI_PROTOCAL_11N); - } - 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_storage(WIFI_STORAGE_RAM) ); - wifi_config_t wifi_config = { - .sta = { - .ssid = EXAMPLE_WIFI_SSID, - .password = EXAMPLE_WIFI_PASS, - }, - }; - ESP_LOGI(TAG, "Setting WiFi configuration SSID %s...", wifi_config.sta.ssid); - ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) ); - ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) ); - ESP_ERROR_CHECK( esp_wifi_start() ); -} - static void http_get_task(void *pvParameters) { const struct addrinfo hints = { @@ -110,13 +46,6 @@ static void http_get_task(void *pvParameters) char recv_buf[64]; while(1) { - /* Wait for the callback to set the CONNECTED_BIT in the - event group. - */ - xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT, - false, true, portMAX_DELAY); - ESP_LOGI(TAG, "Connected to AP"); - int err = getaddrinfo(WEB_SERVER, "80", &hints, &res); if(err != 0 || res == NULL) { @@ -192,7 +121,12 @@ static void http_get_task(void *pvParameters) void app_main() { - ESP_ERROR_CHECK( nvs_flash_init() ); - initialise_wifi(); + ESP_ERROR_CHECK(nvs_flash_init()); + ESP_ERROR_CHECK(esp_netif_init()); + ESP_ERROR_CHECK(esp_event_loop_create_default()); + + + ESP_ERROR_CHECK(example_connect()); + xTaskCreate(&http_get_task, "http_get_task", 16384, NULL, 5, NULL); } diff --git a/examples/protocols/http_server/advanced_tests/CMakeLists.txt b/examples/protocols/http_server/advanced_tests/CMakeLists.txt index b6f65f8f..1af5093c 100644 --- a/examples/protocols/http_server/advanced_tests/CMakeLists.txt +++ b/examples/protocols/http_server/advanced_tests/CMakeLists.txt @@ -2,6 +2,10 @@ # in this exact order for cmake to work correctly cmake_minimum_required(VERSION 3.5) +# (Not part of the boilerplate) +# This example uses an extra component for common functions such as Wi-Fi and Ethernet connection. +set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common) + include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(tests) diff --git a/examples/protocols/http_server/advanced_tests/Makefile b/examples/protocols/http_server/advanced_tests/Makefile index 178ddf69..07699abc 100644 --- a/examples/protocols/http_server/advanced_tests/Makefile +++ b/examples/protocols/http_server/advanced_tests/Makefile @@ -5,5 +5,7 @@ PROJECT_NAME := tests +EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/protocol_examples_common + include $(IDF_PATH)/make/project.mk diff --git a/examples/protocols/http_server/advanced_tests/main/Kconfig.projbuild b/examples/protocols/http_server/advanced_tests/main/Kconfig.projbuild deleted file mode 100644 index 9e2813c6..00000000 --- a/examples/protocols/http_server/advanced_tests/main/Kconfig.projbuild +++ /dev/null @@ -1,16 +0,0 @@ -menu "Example Configuration" - -config WIFI_SSID - string "WiFi SSID" - default "myssid" - help - SSID (network name) for the example to connect to. - -config WIFI_PASSWORD - string "WiFi Password" - default "mypassword" - help - WiFi password (WPA or WPA2) for the example to use. - Can be left blank if the network has no security set. - -endmenu diff --git a/examples/protocols/http_server/advanced_tests/main/main.c b/examples/protocols/http_server/advanced_tests/main/main.c index 48dead08..05a5786c 100644 --- a/examples/protocols/http_server/advanced_tests/main/main.c +++ b/examples/protocols/http_server/advanced_tests/main/main.c @@ -1,88 +1,51 @@ -#include "esp_wifi.h" -#include "esp_event_loop.h" -#include "esp_log.h" +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" #include "esp_system.h" +#include "esp_log.h" +#include "esp_netif.h" +#include "esp_event.h" +#include "protocol_examples_common.h" +#include "nvs.h" #include "nvs_flash.h" #include "tests.h" -/* The examples use simple WiFi configuration that you can set via - 'make menuconfig'. +static const char *TAG = "example"; - If you'd rather not, just change the below entries to strings with - the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid" -*/ -#define EXAMPLE_WIFI_SSID CONFIG_WIFI_SSID -#define EXAMPLE_WIFI_PASS CONFIG_WIFI_PASSWORD +static httpd_handle_t server = NULL; -static const char *TAG="TEST_WIFI"; - -static esp_err_t event_handler(void *ctx, system_event_t *event) +static void disconnect_handler(void* arg, esp_event_base_t event_base, + int32_t event_id, void* event_data) { - httpd_handle_t *hd = (httpd_handle_t *) ctx; - /* For accessing reason codes in case of disconnection */ - system_event_info_t *info = &event->event_info; - - switch(event->event_id) { - case SYSTEM_EVENT_STA_START: - ESP_LOGI(TAG, "SYSTEM_EVENT_STA_START"); - ESP_ERROR_CHECK(esp_wifi_connect()); - break; - case SYSTEM_EVENT_STA_GOT_IP: - ESP_LOGI(TAG, "SYSTEM_EVENT_STA_GOT_IP"); - ESP_LOGI(TAG, "Got IP: '%s'", - ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip)); - - // Start webserver tests - if (*hd == NULL) { - *hd = start_tests(); - } - - break; - case SYSTEM_EVENT_STA_DISCONNECTED: - ESP_LOGI(TAG, "SYSTEM_EVENT_STA_DISCONNECTED"); - ESP_LOGE(TAG, "Disconnect reason : %d", info->disconnected.reason); - if (info->disconnected.reason == WIFI_REASON_BASIC_RATE_NOT_SUPPORT) { - /*Switch to 802.11 bgn mode */ - esp_wifi_set_protocol(ESP_IF_WIFI_STA, WIFI_PROTOCAL_11B | WIFI_PROTOCAL_11G | WIFI_PROTOCAL_11N); - } - ESP_ERROR_CHECK(esp_wifi_connect()); - - // Stop webserver tests - if (*hd) { - stop_tests(*hd); - *hd = NULL; - } - - break; - default: - break; + httpd_handle_t* server = (httpd_handle_t*) arg; + if (*server) { + ESP_LOGI(TAG, "Stopping webserver"); + stop_tests(*server); + *server = NULL; } - return ESP_OK; } -static void initialise_wifi(void) +static void connect_handler(void* arg, esp_event_base_t event_base, + int32_t event_id, void* event_data) { - tcpip_adapter_init(); - static httpd_handle_t hd = NULL; - ESP_ERROR_CHECK(esp_event_loop_init(event_handler, &hd)); - wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); - ESP_ERROR_CHECK(esp_wifi_init(&cfg)); - ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM)); - wifi_config_t wifi_config = { - .sta = { - .ssid = EXAMPLE_WIFI_SSID, - .password = EXAMPLE_WIFI_PASS, - }, - }; - ESP_LOGI(TAG, "Setting WiFi configuration SSID %s...", wifi_config.sta.ssid); - ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); - ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config)); - ESP_ERROR_CHECK(esp_wifi_start()); + httpd_handle_t* server = (httpd_handle_t*) arg; + if (*server == NULL) { + ESP_LOGI(TAG, "Starting webserver"); + *server = start_tests(); + } } void app_main() { ESP_ERROR_CHECK(nvs_flash_init()); - initialise_wifi(); + ESP_ERROR_CHECK(esp_netif_init()); + ESP_ERROR_CHECK(esp_event_loop_create_default()); + + + ESP_ERROR_CHECK(example_connect()); + + ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &connect_handler, &server)); + ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &disconnect_handler, &server)); + + server = start_tests(); } diff --git a/examples/protocols/http_server/persistent_sockets/CMakeLists.txt b/examples/protocols/http_server/persistent_sockets/CMakeLists.txt index 2d453b67..b6d34fb2 100644 --- a/examples/protocols/http_server/persistent_sockets/CMakeLists.txt +++ b/examples/protocols/http_server/persistent_sockets/CMakeLists.txt @@ -2,6 +2,10 @@ # in this exact order for cmake to work correctly cmake_minimum_required(VERSION 3.5) +# (Not part of the boilerplate) +# This example uses an extra component for common functions such as Wi-Fi and Ethernet connection. +set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common) + include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(persistent_sockets) diff --git a/examples/protocols/http_server/persistent_sockets/Makefile b/examples/protocols/http_server/persistent_sockets/Makefile index 9c178022..f20d51a6 100644 --- a/examples/protocols/http_server/persistent_sockets/Makefile +++ b/examples/protocols/http_server/persistent_sockets/Makefile @@ -5,5 +5,7 @@ PROJECT_NAME := persistent_sockets +EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/protocol_examples_common + include $(IDF_PATH)/make/project.mk diff --git a/examples/protocols/http_server/persistent_sockets/main/Kconfig.projbuild b/examples/protocols/http_server/persistent_sockets/main/Kconfig.projbuild deleted file mode 100644 index 9e2813c6..00000000 --- a/examples/protocols/http_server/persistent_sockets/main/Kconfig.projbuild +++ /dev/null @@ -1,16 +0,0 @@ -menu "Example Configuration" - -config WIFI_SSID - string "WiFi SSID" - default "myssid" - help - SSID (network name) for the example to connect to. - -config WIFI_PASSWORD - string "WiFi Password" - default "mypassword" - help - WiFi password (WPA or WPA2) for the example to use. - Can be left blank if the network has no security set. - -endmenu diff --git a/examples/protocols/http_server/persistent_sockets/main/main.c b/examples/protocols/http_server/persistent_sockets/main/main.c index 4bebbed8..6790d388 100644 --- a/examples/protocols/http_server/persistent_sockets/main/main.c +++ b/examples/protocols/http_server/persistent_sockets/main/main.c @@ -7,23 +7,18 @@ CONDITIONS OF ANY KIND, either express or implied. */ -#include -#include -#include -#include -#include +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "esp_system.h" +#include "esp_log.h" +#include "esp_netif.h" +#include "esp_event.h" +#include "protocol_examples_common.h" +#include "nvs.h" +#include "nvs_flash.h" #include -/* An example to demonstrate persistent sockets, with context maintained across - * multiple requests on that socket. - * The examples use simple WiFi configuration that you can set via 'make menuconfig'. - * If you'd rather not, just change the below entries to strings with - * the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid" - */ -#define EXAMPLE_WIFI_SSID CONFIG_WIFI_SSID -#define EXAMPLE_WIFI_PASS CONFIG_WIFI_PASSWORD - static const char *TAG="APP"; /* Function to free context */ @@ -190,70 +185,39 @@ void stop_webserver(httpd_handle_t server) httpd_stop(server); } -static esp_err_t event_handler(void *ctx, system_event_t *event) +static httpd_handle_t server = NULL; + +static void disconnect_handler(void* arg, esp_event_base_t event_base, + int32_t event_id, void* event_data) { - httpd_handle_t *server = (httpd_handle_t *) ctx; - /* For accessing reason codes in case of disconnection */ - system_event_info_t *info = &event->event_info; - - switch(event->event_id) { - case SYSTEM_EVENT_STA_START: - ESP_LOGI(TAG, "SYSTEM_EVENT_STA_START"); - ESP_ERROR_CHECK(esp_wifi_connect()); - break; - case SYSTEM_EVENT_STA_GOT_IP: - ESP_LOGI(TAG, "SYSTEM_EVENT_STA_GOT_IP"); - ESP_LOGI(TAG, "Got IP: '%s'", - ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip)); - - /* Start the web server */ - if (*server == NULL) { - *server = start_webserver(); - } - break; - case SYSTEM_EVENT_STA_DISCONNECTED: - ESP_LOGI(TAG, "SYSTEM_EVENT_STA_DISCONNECTED"); - ESP_LOGE(TAG, "Disconnect reason : %d", info->disconnected.reason); - if (info->disconnected.reason == WIFI_REASON_BASIC_RATE_NOT_SUPPORT) { - /*Switch to 802.11 bgn mode */ - esp_wifi_set_protocol(ESP_IF_WIFI_STA, WIFI_PROTOCAL_11B | WIFI_PROTOCAL_11G | WIFI_PROTOCAL_11N); - } - ESP_ERROR_CHECK(esp_wifi_connect()); - - /* Stop the webserver */ - if (*server) { - stop_webserver(*server); - *server = NULL; - } - break; - default: - break; + httpd_handle_t* server = (httpd_handle_t*) arg; + if (*server) { + ESP_LOGI(TAG, "Stopping webserver"); + stop_webserver(*server); + *server = NULL; } - return ESP_OK; } -static void initialise_wifi(void *arg) +static void connect_handler(void* arg, esp_event_base_t event_base, + int32_t event_id, void* event_data) { - tcpip_adapter_init(); - ESP_ERROR_CHECK(esp_event_loop_init(event_handler, arg)); - wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); - ESP_ERROR_CHECK(esp_wifi_init(&cfg)); - ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM)); - wifi_config_t wifi_config = { - .sta = { - .ssid = EXAMPLE_WIFI_SSID, - .password = EXAMPLE_WIFI_PASS, - }, - }; - ESP_LOGI(TAG, "Setting WiFi configuration SSID %s...", wifi_config.sta.ssid); - ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); - ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config)); - ESP_ERROR_CHECK(esp_wifi_start()); + httpd_handle_t* server = (httpd_handle_t*) arg; + if (*server == NULL) { + ESP_LOGI(TAG, "Starting webserver"); + *server = start_webserver(); + } } void app_main() { - static httpd_handle_t server = NULL; ESP_ERROR_CHECK(nvs_flash_init()); - initialise_wifi(&server); + ESP_ERROR_CHECK(esp_netif_init()); + ESP_ERROR_CHECK(esp_event_loop_create_default()); + + ESP_ERROR_CHECK(example_connect()); + + ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &connect_handler, &server)); + ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &disconnect_handler, &server)); + + server = start_webserver(); } diff --git a/examples/protocols/http_server/simple/CMakeLists.txt b/examples/protocols/http_server/simple/CMakeLists.txt index cc9d4fd8..db2ef2fa 100644 --- a/examples/protocols/http_server/simple/CMakeLists.txt +++ b/examples/protocols/http_server/simple/CMakeLists.txt @@ -2,5 +2,9 @@ # in this exact order for cmake to work correctly cmake_minimum_required(VERSION 3.5) +# (Not part of the boilerplate) +# This example uses an extra component for common functions such as Wi-Fi and Ethernet connection. +set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common) + include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(simple) diff --git a/examples/protocols/http_server/simple/Makefile b/examples/protocols/http_server/simple/Makefile index 48f628a6..85660a20 100644 --- a/examples/protocols/http_server/simple/Makefile +++ b/examples/protocols/http_server/simple/Makefile @@ -5,5 +5,7 @@ PROJECT_NAME := simple +EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/protocol_examples_common + include $(IDF_PATH)/make/project.mk diff --git a/examples/protocols/http_server/simple/main/Kconfig.projbuild b/examples/protocols/http_server/simple/main/Kconfig.projbuild deleted file mode 100644 index 9e2813c6..00000000 --- a/examples/protocols/http_server/simple/main/Kconfig.projbuild +++ /dev/null @@ -1,16 +0,0 @@ -menu "Example Configuration" - -config WIFI_SSID - string "WiFi SSID" - default "myssid" - help - SSID (network name) for the example to connect to. - -config WIFI_PASSWORD - string "WiFi Password" - default "mypassword" - help - WiFi password (WPA or WPA2) for the example to use. - Can be left blank if the network has no security set. - -endmenu diff --git a/examples/protocols/http_server/simple/main/main.c b/examples/protocols/http_server/simple/main/main.c index d29ccaac..1c532b8d 100644 --- a/examples/protocols/http_server/simple/main/main.c +++ b/examples/protocols/http_server/simple/main/main.c @@ -6,26 +6,19 @@ software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */ - -#include -#include -#include -#include -#include #include -#include +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "esp_system.h" +#include "esp_log.h" +#include "esp_netif.h" +#include "esp_event.h" +#include "protocol_examples_common.h" +#include "nvs.h" +#include "nvs_flash.h" -/* A simple example that demonstrates how to create GET and POST - * handlers for the web server. - * The examples use simple WiFi configuration that you can set via - * 'make menuconfig'. - * If you'd rather not, just change the below entries to strings - * with the config you want - - * ie. #define EXAMPLE_WIFI_SSID "mywifissid" -*/ -#define EXAMPLE_WIFI_SSID CONFIG_WIFI_SSID -#define EXAMPLE_WIFI_PASS CONFIG_WIFI_PASSWORD +#include static const char *TAG="APP"; @@ -217,70 +210,40 @@ void stop_webserver(httpd_handle_t server) httpd_stop(server); } -static esp_err_t event_handler(void *ctx, system_event_t *event) +static httpd_handle_t server = NULL; + +static void disconnect_handler(void* arg, esp_event_base_t event_base, + int32_t event_id, void* event_data) { - httpd_handle_t *server = (httpd_handle_t *) ctx; - /* For accessing reason codes in case of disconnection */ - system_event_info_t *info = &event->event_info; - - switch(event->event_id) { - case SYSTEM_EVENT_STA_START: - ESP_LOGI(TAG, "SYSTEM_EVENT_STA_START"); - ESP_ERROR_CHECK(esp_wifi_connect()); - break; - case SYSTEM_EVENT_STA_GOT_IP: - ESP_LOGI(TAG, "SYSTEM_EVENT_STA_GOT_IP"); - ESP_LOGI(TAG, "Got IP: '%s'", - ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip)); - - /* Start the web server */ - if (*server == NULL) { - *server = start_webserver(); - } - break; - case SYSTEM_EVENT_STA_DISCONNECTED: - ESP_LOGI(TAG, "SYSTEM_EVENT_STA_DISCONNECTED"); - ESP_LOGE(TAG, "Disconnect reason : %d", info->disconnected.reason); - if (info->disconnected.reason == WIFI_REASON_BASIC_RATE_NOT_SUPPORT) { - /*Switch to 802.11 bgn mode */ - esp_wifi_set_protocol(ESP_IF_WIFI_STA, WIFI_PROTOCAL_11B | WIFI_PROTOCAL_11G | WIFI_PROTOCAL_11N); - } - ESP_ERROR_CHECK(esp_wifi_connect()); - - /* Stop the web server */ - if (*server) { - stop_webserver(*server); - *server = NULL; - } - break; - default: - break; + httpd_handle_t* server = (httpd_handle_t*) arg; + if (*server) { + ESP_LOGI(TAG, "Stopping webserver"); + stop_webserver(*server); + *server = NULL; } - return ESP_OK; } -static void initialise_wifi(void *arg) +static void connect_handler(void* arg, esp_event_base_t event_base, + int32_t event_id, void* event_data) { - tcpip_adapter_init(); - ESP_ERROR_CHECK(esp_event_loop_init(event_handler, arg)); - wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); - ESP_ERROR_CHECK(esp_wifi_init(&cfg)); - ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM)); - wifi_config_t wifi_config = { - .sta = { - .ssid = EXAMPLE_WIFI_SSID, - .password = EXAMPLE_WIFI_PASS, - }, - }; - ESP_LOGI(TAG, "Setting WiFi configuration SSID %s...", wifi_config.sta.ssid); - ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); - ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config)); - ESP_ERROR_CHECK(esp_wifi_start()); + httpd_handle_t* server = (httpd_handle_t*) arg; + if (*server == NULL) { + ESP_LOGI(TAG, "Starting webserver"); + *server = start_webserver(); + } } void app_main() { - static httpd_handle_t server = NULL; ESP_ERROR_CHECK(nvs_flash_init()); - initialise_wifi(&server); + ESP_ERROR_CHECK(esp_netif_init()); + ESP_ERROR_CHECK(esp_event_loop_create_default()); + + + ESP_ERROR_CHECK(example_connect()); + + ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &connect_handler, &server)); + ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &disconnect_handler, &server)); + + server = start_webserver(); } diff --git a/examples/protocols/https_mbedtls/CMakeLists.txt b/examples/protocols/https_mbedtls/CMakeLists.txt index 1990bc1a..e969fdca 100644 --- a/examples/protocols/https_mbedtls/CMakeLists.txt +++ b/examples/protocols/https_mbedtls/CMakeLists.txt @@ -2,5 +2,9 @@ # CMakeLists in this exact order for cmake to work correctly cmake_minimum_required(VERSION 3.5) +# (Not part of the boilerplate) +# This example uses an extra component for common functions such as Wi-Fi and Ethernet connection. +set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common) + include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(https_mbedtls) diff --git a/examples/protocols/https_mbedtls/Makefile b/examples/protocols/https_mbedtls/Makefile index a6777c41..538b584f 100644 --- a/examples/protocols/https_mbedtls/Makefile +++ b/examples/protocols/https_mbedtls/Makefile @@ -5,4 +5,6 @@ PROJECT_NAME := https-mbedtls +EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/protocol_examples_common + include $(IDF_PATH)/make/project.mk \ No newline at end of file diff --git a/examples/protocols/https_mbedtls/main/Kconfig.projbuild b/examples/protocols/https_mbedtls/main/Kconfig.projbuild deleted file mode 100644 index 92a75195..00000000 --- a/examples/protocols/https_mbedtls/main/Kconfig.projbuild +++ /dev/null @@ -1,17 +0,0 @@ -menu "Example Configuration" - -config WIFI_SSID - string "WiFi SSID" - default "myssid" - help - SSID (network name) for the example to connect to. - -config WIFI_PASSWORD - string "WiFi Password" - default "mypassword" - help - WiFi password (WPA or WPA2) for the example to use. - - Can be left blank if the network has no security set. - -endmenu diff --git a/examples/protocols/https_mbedtls/main/https_mbedtls_example_main.c b/examples/protocols/https_mbedtls/main/https_mbedtls_example_main.c index c2879f55..0378fef2 100644 --- a/examples/protocols/https_mbedtls/main/https_mbedtls_example_main.c +++ b/examples/protocols/https_mbedtls/main/https_mbedtls_example_main.c @@ -26,12 +26,12 @@ #include "freertos/FreeRTOS.h" #include "freertos/task.h" -#include "freertos/event_groups.h" - -#include "esp_wifi.h" -#include "esp_event_loop.h" +#include "esp_system.h" #include "esp_log.h" - +#include "esp_netif.h" +#include "esp_event.h" +#include "protocol_examples_common.h" +#include "nvs.h" #include "nvs_flash.h" #include @@ -46,23 +46,6 @@ #include "mbedtls/error.h" #include "mbedtls/certs.h" -/* The examples use simple WiFi configuration that you can set via - 'make menuconfig'. - - If you'd rather not, just change the below entries to strings with - the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid" -*/ -#define EXAMPLE_WIFI_SSID CONFIG_WIFI_SSID -#define EXAMPLE_WIFI_PASS CONFIG_WIFI_PASSWORD - -/* 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? */ -const int CONNECTED_BIT = BIT0; - /* Constants that aren't configurable in menuconfig */ #define WEB_SERVER "www.howsmyssl.com" #define WEB_PORT "443" @@ -88,53 +71,6 @@ static const char *REQUEST = "GET " WEB_URL " HTTP/1.0\r\n" extern const uint8_t server_root_cert_pem_start[] asm("_binary_server_root_cert_pem_start"); extern const uint8_t server_root_cert_pem_end[] asm("_binary_server_root_cert_pem_end"); -static esp_err_t event_handler(void *ctx, system_event_t *event) -{ - /* For accessing reason codes in case of disconnection */ - system_event_info_t *info = &event->event_info; - - switch(event->event_id) { - case SYSTEM_EVENT_STA_START: - esp_wifi_connect(); - break; - case SYSTEM_EVENT_STA_GOT_IP: - xEventGroupSetBits(wifi_event_group, CONNECTED_BIT); - break; - case SYSTEM_EVENT_STA_DISCONNECTED: - ESP_LOGE(TAG, "Disconnect reason : %d", info->disconnected.reason); - if (info->disconnected.reason == WIFI_REASON_BASIC_RATE_NOT_SUPPORT) { - /*Switch to 802.11 bgn mode */ - esp_wifi_set_protocol(ESP_IF_WIFI_STA, WIFI_PROTOCAL_11B | WIFI_PROTOCAL_11G | WIFI_PROTOCAL_11N); - } - 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_storage(WIFI_STORAGE_RAM) ); - wifi_config_t wifi_config = { - .sta = { - .ssid = EXAMPLE_WIFI_SSID, - .password = EXAMPLE_WIFI_PASS, - }, - }; - ESP_LOGI(TAG, "Setting WiFi configuration SSID %s...", wifi_config.sta.ssid); - ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) ); - ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) ); - ESP_ERROR_CHECK( esp_wifi_start() ); -} - static void https_get_task(void *pvParameters) { char buf[512]; @@ -212,13 +148,6 @@ static void https_get_task(void *pvParameters) } while(1) { - /* Wait for the callback to set the CONNECTED_BIT in the - event group. - */ - xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT, - false, true, portMAX_DELAY); - ESP_LOGI(TAG, "Connected to AP"); - mbedtls_net_init(&server_fd); ESP_LOGI(TAG, "Connecting to %s:%s...", WEB_SERVER, WEB_PORT); @@ -340,7 +269,12 @@ static void https_get_task(void *pvParameters) void app_main(void) { - ESP_ERROR_CHECK( nvs_flash_init() ); - initialise_wifi(); + ESP_ERROR_CHECK(nvs_flash_init()); + ESP_ERROR_CHECK(esp_netif_init()); + ESP_ERROR_CHECK(esp_event_loop_create_default()); + + + ESP_ERROR_CHECK(example_connect()); + xTaskCreate(&https_get_task, "https_get_task", 8192, NULL, 5, NULL); } diff --git a/examples/protocols/https_request/CMakeLists.txt b/examples/protocols/https_request/CMakeLists.txt index f77bc661..6e3f1265 100644 --- a/examples/protocols/https_request/CMakeLists.txt +++ b/examples/protocols/https_request/CMakeLists.txt @@ -2,5 +2,9 @@ # CMakeLists in this exact order for cmake to work correctly cmake_minimum_required(VERSION 3.5) +# (Not part of the boilerplate) +# This example uses an extra component for common functions such as Wi-Fi and Ethernet connection. +set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common) + include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(https_request) diff --git a/examples/protocols/https_request/Makefile b/examples/protocols/https_request/Makefile index 55c5f943..f81163b0 100644 --- a/examples/protocols/https_request/Makefile +++ b/examples/protocols/https_request/Makefile @@ -5,5 +5,7 @@ PROJECT_NAME := https_request +EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/protocol_examples_common + include $(IDF_PATH)/make/project.mk diff --git a/examples/protocols/https_request/main/Kconfig.projbuild b/examples/protocols/https_request/main/Kconfig.projbuild deleted file mode 100644 index 1c7241da..00000000 --- a/examples/protocols/https_request/main/Kconfig.projbuild +++ /dev/null @@ -1,17 +0,0 @@ -menu "Example Configuration" - -config WIFI_SSID - string "WiFi SSID" - default "myssid" - help - SSID (network name) for the example to connect to. - -config WIFI_PASSWORD - string "WiFi Password" - default "mypassword" - help - WiFi password (WPA or WPA2) for the example to use. - - Can be left blank if the network has no security set. - -endmenu diff --git a/examples/protocols/https_request/main/https_request_example_main.c b/examples/protocols/https_request/main/https_request_example_main.c index 628410ce..dea2aaed 100644 --- a/examples/protocols/https_request/main/https_request_example_main.c +++ b/examples/protocols/https_request/main/https_request_example_main.c @@ -23,13 +23,15 @@ */ #include #include + #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 "esp_log.h" +#include "esp_netif.h" +#include "esp_event.h" +#include "protocol_examples_common.h" +#include "nvs.h" #include "nvs_flash.h" #include "lwip/err.h" @@ -40,23 +42,6 @@ #include "esp_tls.h" -/* The examples use simple WiFi configuration that you can set via - 'make menuconfig'. - - If you'd rather not, just change the below entries to strings with - the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid" -*/ -#define EXAMPLE_WIFI_SSID CONFIG_WIFI_SSID -#define EXAMPLE_WIFI_PASS CONFIG_WIFI_PASSWORD - -/* 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? */ -const int CONNECTED_BIT = BIT0; - /* Constants that aren't configurable in menuconfig */ #define WEB_SERVER "www.howsmyssl.com" #define WEB_PORT 443 @@ -81,53 +66,6 @@ static const char *REQUEST = "GET " WEB_URL " HTTP/1.0\r\n" */ extern const uint8_t server_root_cert_pem_start[] asm("_binary_server_root_cert_pem_start"); extern const uint8_t server_root_cert_pem_end[] asm("_binary_server_root_cert_pem_end"); - -static esp_err_t event_handler(void *ctx, system_event_t *event) -{ - /* For accessing reason codes in case of disconnection */ - system_event_info_t *info = &event->event_info; - - switch(event->event_id) { - case SYSTEM_EVENT_STA_START: - esp_wifi_connect(); - break; - case SYSTEM_EVENT_STA_GOT_IP: - xEventGroupSetBits(wifi_event_group, CONNECTED_BIT); - break; - case SYSTEM_EVENT_STA_DISCONNECTED: - ESP_LOGE(TAG, "Disconnect reason : %d", info->disconnected.reason); - if (info->disconnected.reason == WIFI_REASON_BASIC_RATE_NOT_SUPPORT) { - /*Switch to 802.11 bgn mode */ - esp_wifi_set_protocol(ESP_IF_WIFI_STA, WIFI_PROTOCAL_11B | WIFI_PROTOCAL_11G | WIFI_PROTOCAL_11N); - } - 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_storage(WIFI_STORAGE_RAM) ); - wifi_config_t wifi_config = { - .sta = { - .ssid = EXAMPLE_WIFI_SSID, - .password = EXAMPLE_WIFI_PASS, - }, - }; - ESP_LOGI(TAG, "Setting WiFi configuration SSID %s...", wifi_config.sta.ssid); - ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) ); - ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) ); - ESP_ERROR_CHECK( esp_wifi_start() ); -} static void https_get_task(void *pvParameters) { @@ -135,12 +73,6 @@ static void https_get_task(void *pvParameters) int ret, len; while(1) { - /* Wait for the callback to set the CONNECTED_BIT in the - event group. - */ - xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT, - false, true, portMAX_DELAY); - ESP_LOGI(TAG, "Connected to AP"); esp_tls_cfg_t cfg = { .cacert_pem_buf = server_root_cert_pem_start, .cacert_pem_bytes = server_root_cert_pem_end - server_root_cert_pem_start, @@ -223,7 +155,11 @@ static void https_get_task(void *pvParameters) void app_main() { - ESP_ERROR_CHECK( nvs_flash_init() ); - initialise_wifi(); + ESP_ERROR_CHECK(nvs_flash_init()); + ESP_ERROR_CHECK(esp_netif_init()); + ESP_ERROR_CHECK(esp_event_loop_create_default()); + + ESP_ERROR_CHECK(example_connect()); + xTaskCreate(&https_get_task, "https_get_task", 8192, NULL, 5, NULL); } diff --git a/examples/protocols/mdns/CMakeLists.txt b/examples/protocols/mdns/CMakeLists.txt index 88b7c1e3..ddd7263c 100644 --- a/examples/protocols/mdns/CMakeLists.txt +++ b/examples/protocols/mdns/CMakeLists.txt @@ -2,5 +2,9 @@ # in this exact order for cmake to work correctly cmake_minimum_required(VERSION 3.5) +# (Not part of the boilerplate) +# This example uses an extra component for common functions such as Wi-Fi and Ethernet connection. +set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common) + include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(mdns-test) diff --git a/examples/protocols/mdns/Makefile b/examples/protocols/mdns/Makefile index 0353c51c..91a417c7 100644 --- a/examples/protocols/mdns/Makefile +++ b/examples/protocols/mdns/Makefile @@ -5,5 +5,7 @@ PROJECT_NAME := mdns-test +EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/protocol_examples_common + include $(IDF_PATH)/make/project.mk diff --git a/examples/protocols/mdns/main/Kconfig.projbuild b/examples/protocols/mdns/main/Kconfig.projbuild index a55b0bf4..5fb52b28 100644 --- a/examples/protocols/mdns/main/Kconfig.projbuild +++ b/examples/protocols/mdns/main/Kconfig.projbuild @@ -1,19 +1,5 @@ menu "Example Configuration" -config WIFI_SSID - string "WiFi SSID" - default "myssid" - help - SSID (network name) for the example to connect to. - -config WIFI_PASSWORD - string "WiFi Password" - default "mypassword" - help - WiFi password (WPA or WPA2) for the example to use. - - Can be left blank if the network has no security set. - config MDNS_HOSTNAME string "mDNS Hostname" default "esp32-mdns" diff --git a/examples/protocols/mdns/main/mdns_example_main.c b/examples/protocols/mdns/main/mdns_example_main.c index bac352da..7f17e961 100644 --- a/examples/protocols/mdns/main/mdns_example_main.c +++ b/examples/protocols/mdns/main/mdns_example_main.c @@ -7,101 +7,26 @@ CONDITIONS OF ANY KIND, either express or implied. */ #include -#include "freertos/FreeRTOS.h" -#include "freertos/task.h" -#include "freertos/event_groups.h" -#include "esp_system.h" -#include "esp_wifi.h" -#include "esp_event_loop.h" -#include "esp_log.h" -#include "nvs_flash.h" -#include "mdns.h" -#include "driver/gpio.h" #include #include -/* The examples use simple WiFi configuration that you can set via - 'make menuconfig'. +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "esp_system.h" +#include "esp_log.h" +#include "esp_netif.h" +#include "esp_event.h" +#include "protocol_examples_common.h" +#include "nvs.h" +#include "nvs_flash.h" +#include "driver/gpio.h" - If you'd rather not, just change the below entries to strings with - the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid" -*/ -#define EXAMPLE_WIFI_SSID CONFIG_WIFI_SSID -#define EXAMPLE_WIFI_PASS CONFIG_WIFI_PASSWORD +#include "mdns.h" #define EXAMPLE_MDNS_HOSTNAME CONFIG_MDNS_HOSTNAME #define EXAMPLE_MDNS_INSTANCE CONFIG_MDNS_INSTANCE -/* 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? */ -const int IP4_CONNECTED_BIT = BIT0; -const int IP6_CONNECTED_BIT = BIT1; - static const char *TAG = "mdns-test"; -static bool auto_reconnect = true; - -static esp_err_t event_handler(void *ctx, system_event_t *event) -{ - /* For accessing reason codes in case of disconnection */ - system_event_info_t *info = &event->event_info; - - switch(event->event_id) { - case SYSTEM_EVENT_STA_START: - esp_wifi_connect(); - break; - case SYSTEM_EVENT_STA_CONNECTED: - /* enable ipv6 */ - tcpip_adapter_create_ip6_linklocal(TCPIP_ADAPTER_IF_STA); - break; - case SYSTEM_EVENT_STA_GOT_IP: - xEventGroupSetBits(wifi_event_group, IP4_CONNECTED_BIT); - break; - case SYSTEM_EVENT_AP_STA_GOT_IP6: - xEventGroupSetBits(wifi_event_group, IP6_CONNECTED_BIT); - break; - case SYSTEM_EVENT_STA_DISCONNECTED: - /* This is a workaround as ESP32 WiFi libs don't currently - auto-reassociate. */ - if (auto_reconnect) { - ESP_LOGE(TAG, "Disconnect reason : %d", info->disconnected.reason); - if (info->disconnected.reason == WIFI_REASON_BASIC_RATE_NOT_SUPPORT) { - /*Switch to 802.11 bgn mode */ - esp_wifi_set_protocol(ESP_IF_WIFI_STA, WIFI_PROTOCAL_11B | WIFI_PROTOCAL_11G | WIFI_PROTOCAL_11N); - } - esp_wifi_connect(); - } - xEventGroupClearBits(wifi_event_group, IP4_CONNECTED_BIT | IP6_CONNECTED_BIT); - break; - default: - break; - } - mdns_handle_system_event(ctx, event); - 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_storage(WIFI_STORAGE_RAM) ); - wifi_config_t wifi_config = { - .sta = { - .ssid = EXAMPLE_WIFI_SSID, - .password = EXAMPLE_WIFI_PASS, - }, - }; - ESP_LOGI(TAG, "Setting WiFi configuration SSID %s...", wifi_config.sta.ssid); - ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) ); - ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) ); - ESP_ERROR_CHECK( esp_wifi_start() ); -} static void initialise_mdns(void) { @@ -233,9 +158,6 @@ static void check_button(void) static void mdns_example_task(void *pvParameters) { - /* Wait for the callback to set the CONNECTED_BIT in the event group. */ - xEventGroupWaitBits(wifi_event_group, IP4_CONNECTED_BIT | IP6_CONNECTED_BIT, - false, true, portMAX_DELAY); while(1) { check_button(); vTaskDelay(50 / portTICK_PERIOD_MS); @@ -244,9 +166,14 @@ static void mdns_example_task(void *pvParameters) void app_main() { - ESP_ERROR_CHECK( nvs_flash_init() ); + ESP_ERROR_CHECK(nvs_flash_init()); + ESP_ERROR_CHECK(esp_netif_init()); + ESP_ERROR_CHECK(esp_event_loop_create_default()); + + ESP_ERROR_CHECK(example_connect()); + initialise_mdns(); - initialise_wifi(); initialise_button(); + xTaskCreate(&mdns_example_task, "mdns_example_task", 2048, NULL, 5, NULL); } diff --git a/examples/protocols/mqtt/ssl/CMakeLists.txt b/examples/protocols/mqtt/ssl/CMakeLists.txt index 13bdd8c2..63ce6063 100644 --- a/examples/protocols/mqtt/ssl/CMakeLists.txt +++ b/examples/protocols/mqtt/ssl/CMakeLists.txt @@ -2,6 +2,10 @@ # in this exact order for cmake to work correctly cmake_minimum_required(VERSION 3.5) +# (Not part of the boilerplate) +# This example uses an extra component for common functions such as Wi-Fi and Ethernet connection. +set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common) + include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(mqtt_ssl) diff --git a/examples/protocols/mqtt/ssl/Makefile b/examples/protocols/mqtt/ssl/Makefile index bae0d73b..efe7f7d5 100644 --- a/examples/protocols/mqtt/ssl/Makefile +++ b/examples/protocols/mqtt/ssl/Makefile @@ -4,4 +4,6 @@ # PROJECT_NAME := mqtt_ssl +EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/protocol_examples_common + include $(IDF_PATH)/make/project.mk diff --git a/examples/protocols/mqtt/ssl/main/Kconfig.projbuild b/examples/protocols/mqtt/ssl/main/Kconfig.projbuild index 8b4eda41..6aa3e08f 100644 --- a/examples/protocols/mqtt/ssl/main/Kconfig.projbuild +++ b/examples/protocols/mqtt/ssl/main/Kconfig.projbuild @@ -1,17 +1,5 @@ menu "Example Configuration" -config WIFI_SSID - string "WiFi SSID" - default "myssid" - help - SSID (network name) for the example to connect to. - -config WIFI_PASSWORD - string "WiFi Password" - default "mypassword" - help - WiFi password (WPA or WPA2) for the example to use. - config BROKER_URI string "Broker URL" default "mqtts://iot.eclipse.org:8883" diff --git a/examples/protocols/mqtt/ssl/main/app_main.c b/examples/protocols/mqtt/ssl/main/app_main.c index 17818e4e..e1a70b55 100644 --- a/examples/protocols/mqtt/ssl/main/app_main.c +++ b/examples/protocols/mqtt/ssl/main/app_main.c @@ -2,81 +2,25 @@ #include #include #include -#include "esp_wifi.h" -#include "esp_system.h" -#include "nvs_flash.h" -#include "esp_event_loop.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" -#include "freertos/semphr.h" -#include "freertos/queue.h" -#include "freertos/event_groups.h" +#include "esp_system.h" +#include "esp_log.h" +#include "esp_netif.h" +#include "esp_event.h" +#include "protocol_examples_common.h" +#include "nvs.h" +#include "nvs_flash.h" #include "lwip/sockets.h" #include "lwip/dns.h" #include "lwip/netdb.h" -#include "esp_log.h" #include "mqtt_client.h" static const char *TAG = "MQTTS_EXAMPLE"; -static EventGroupHandle_t wifi_event_group; -const static int CONNECTED_BIT = BIT0; - - - -static esp_err_t wifi_event_handler(void *ctx, system_event_t *event) -{ - /* For accessing reason codes in case of disconnection */ - system_event_info_t *info = &event->event_info; - - switch (event->event_id) { - case SYSTEM_EVENT_STA_START: - esp_wifi_connect(); - break; - case SYSTEM_EVENT_STA_GOT_IP: - xEventGroupSetBits(wifi_event_group, CONNECTED_BIT); - - break; - case SYSTEM_EVENT_STA_DISCONNECTED: - ESP_LOGE(TAG, "Disconnect reason : %d", info->disconnected.reason); - if (info->disconnected.reason == WIFI_REASON_BASIC_RATE_NOT_SUPPORT) { - /*Switch to 802.11 bgn mode */ - esp_wifi_set_protocol(ESP_IF_WIFI_STA, WIFI_PROTOCAL_11B | WIFI_PROTOCAL_11G | WIFI_PROTOCAL_11N); - } - esp_wifi_connect(); - xEventGroupClearBits(wifi_event_group, CONNECTED_BIT); - break; - default: - break; - } - return ESP_OK; -} - -static void wifi_init(void) -{ - tcpip_adapter_init(); - wifi_event_group = xEventGroupCreate(); - ESP_ERROR_CHECK(esp_event_loop_init(wifi_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_storage(WIFI_STORAGE_RAM)); - wifi_config_t wifi_config = { - .sta = { - .ssid = CONFIG_WIFI_SSID, - .password = CONFIG_WIFI_PASSWORD, - }, - }; - ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); - ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config)); - ESP_LOGI(TAG, "start the WIFI SSID:[%s]", CONFIG_WIFI_SSID); - ESP_ERROR_CHECK(esp_wifi_start()); - ESP_LOGI(TAG, "Waiting for wifi"); - xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT, false, true, portMAX_DELAY); -} - #if CONFIG_BROKER_CERTIFICATE_OVERRIDDEN == 1 static const uint8_t iot_eclipse_org_pem_start[] = "-----BEGIN CERTIFICATE-----\n" CONFIG_BROKER_CERTIFICATE_OVERRIDE "\n-----END CERTIFICATE-----"; #else @@ -144,6 +88,12 @@ static void mqtt_app_start(void) void app_main() { + ESP_ERROR_CHECK(nvs_flash_init()); + ESP_ERROR_CHECK(esp_netif_init()); + ESP_ERROR_CHECK(esp_event_loop_create_default()); + + ESP_ERROR_CHECK(example_connect()); + ESP_LOGI(TAG, "[APP] Startup.."); ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size()); ESP_LOGI(TAG, "[APP] IDF version: %s", esp_get_idf_version()); @@ -155,8 +105,5 @@ void app_main() esp_log_level_set("TRANSPORT", ESP_LOG_VERBOSE); esp_log_level_set("OUTBOX", ESP_LOG_VERBOSE); - nvs_flash_init(); - wifi_init(); mqtt_app_start(); - } diff --git a/examples/protocols/mqtt/ssl/sdkconfig.ci b/examples/protocols/mqtt/ssl/sdkconfig.ci index ce328a6b..27a129be 100644 --- a/examples/protocols/mqtt/ssl/sdkconfig.ci +++ b/examples/protocols/mqtt/ssl/sdkconfig.ci @@ -1,2 +1,3 @@ CONFIG_BROKER_URI="mqtts://${EXAMPLE_MQTT_BROKER_SSL}" CONFIG_BROKER_CERTIFICATE_OVERRIDE="${EXAMPLE_MQTT_BROKER_CERTIFICATE}" +CONFIG_MQTT_USING_ESP=y diff --git a/examples/protocols/mqtt/ssl/sdkconfig.defaults b/examples/protocols/mqtt/ssl/sdkconfig.defaults new file mode 100644 index 00000000..31a9759a --- /dev/null +++ b/examples/protocols/mqtt/ssl/sdkconfig.defaults @@ -0,0 +1 @@ +CONFIG_MQTT_USING_ESP=y diff --git a/examples/protocols/mqtt/ssl_mutual_auth/CMakeLists.txt b/examples/protocols/mqtt/ssl_mutual_auth/CMakeLists.txt index 84bf3752..e99ccf5f 100644 --- a/examples/protocols/mqtt/ssl_mutual_auth/CMakeLists.txt +++ b/examples/protocols/mqtt/ssl_mutual_auth/CMakeLists.txt @@ -2,6 +2,10 @@ # in this exact order for cmake to work correctly cmake_minimum_required(VERSION 3.5) +# (Not part of the boilerplate) +# This example uses an extra component for common functions such as Wi-Fi and Ethernet connection. +set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common) + include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(mqtt_ssl_mutual_auth) diff --git a/examples/protocols/mqtt/ssl_mutual_auth/Makefile b/examples/protocols/mqtt/ssl_mutual_auth/Makefile index cfc04f81..1394f99a 100644 --- a/examples/protocols/mqtt/ssl_mutual_auth/Makefile +++ b/examples/protocols/mqtt/ssl_mutual_auth/Makefile @@ -4,4 +4,6 @@ # PROJECT_NAME := mqtt_ssl_mutual_auth +EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/protocol_examples_common + include $(IDF_PATH)/make/project.mk diff --git a/examples/protocols/mqtt/ssl_mutual_auth/main/Kconfig.projbuild b/examples/protocols/mqtt/ssl_mutual_auth/main/Kconfig.projbuild deleted file mode 100644 index 176d8fb3..00000000 --- a/examples/protocols/mqtt/ssl_mutual_auth/main/Kconfig.projbuild +++ /dev/null @@ -1,15 +0,0 @@ -menu "Example Configuration" - -config WIFI_SSID - string "WiFi SSID" - default "myssid" - help - SSID (network name) for the example to connect to. - -config WIFI_PASSWORD - string "WiFi Password" - default "mypassword" - help - WiFi password (WPA or WPA2) for the example to use. - -endmenu diff --git a/examples/protocols/mqtt/ssl_mutual_auth/main/app_main.c b/examples/protocols/mqtt/ssl_mutual_auth/main/app_main.c index adf5db9a..c1815a53 100644 --- a/examples/protocols/mqtt/ssl_mutual_auth/main/app_main.c +++ b/examples/protocols/mqtt/ssl_mutual_auth/main/app_main.c @@ -2,81 +2,25 @@ #include #include #include -#include "esp_wifi.h" -#include "esp_system.h" -#include "nvs_flash.h" -#include "esp_event_loop.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" -#include "freertos/semphr.h" -#include "freertos/queue.h" -#include "freertos/event_groups.h" +#include "esp_system.h" +#include "esp_log.h" +#include "esp_netif.h" +#include "esp_event.h" +#include "protocol_examples_common.h" +#include "nvs.h" +#include "nvs_flash.h" #include "lwip/sockets.h" #include "lwip/dns.h" #include "lwip/netdb.h" -#include "esp_log.h" #include "mqtt_client.h" static const char *TAG = "MQTTS_EXAMPLE"; -static EventGroupHandle_t wifi_event_group; -const static int CONNECTED_BIT = BIT0; - - - -static esp_err_t wifi_event_handler(void *ctx, system_event_t *event) -{ - /* For accessing reason codes in case of disconnection */ - system_event_info_t *info = &event->event_info; - - switch (event->event_id) { - case SYSTEM_EVENT_STA_START: - esp_wifi_connect(); - break; - case SYSTEM_EVENT_STA_GOT_IP: - xEventGroupSetBits(wifi_event_group, CONNECTED_BIT); - - break; - case SYSTEM_EVENT_STA_DISCONNECTED: - ESP_LOGE(TAG, "Disconnect reason : %d", info->disconnected.reason); - if (info->disconnected.reason == WIFI_REASON_BASIC_RATE_NOT_SUPPORT) { - /*Switch to 802.11 bgn mode */ - esp_wifi_set_protocol(ESP_IF_WIFI_STA, WIFI_PROTOCAL_11B | WIFI_PROTOCAL_11G | WIFI_PROTOCAL_11N); - } - esp_wifi_connect(); - xEventGroupClearBits(wifi_event_group, CONNECTED_BIT); - break; - default: - break; - } - return ESP_OK; -} - -static void wifi_init(void) -{ - tcpip_adapter_init(); - wifi_event_group = xEventGroupCreate(); - ESP_ERROR_CHECK(esp_event_loop_init(wifi_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_storage(WIFI_STORAGE_RAM)); - wifi_config_t wifi_config = { - .sta = { - .ssid = CONFIG_WIFI_SSID, - .password = CONFIG_WIFI_PASSWORD, - }, - }; - ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); - ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config)); - ESP_LOGI(TAG, "start the WIFI SSID:[%s]", CONFIG_WIFI_SSID); - ESP_ERROR_CHECK(esp_wifi_start()); - ESP_LOGI(TAG, "Waiting for wifi"); - xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT, false, true, portMAX_DELAY); -} - extern const uint8_t client_cert_pem_start[] asm("_binary_client_crt_start"); extern const uint8_t client_cert_pem_end[] asm("_binary_client_crt_end"); extern const uint8_t client_key_pem_start[] asm("_binary_client_key_start"); @@ -142,6 +86,12 @@ static void mqtt_app_start(void) void app_main() { + ESP_ERROR_CHECK(nvs_flash_init()); + ESP_ERROR_CHECK(esp_netif_init()); + ESP_ERROR_CHECK(esp_event_loop_create_default()); + + ESP_ERROR_CHECK(example_connect()); + ESP_LOGI(TAG, "[APP] Startup.."); ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size()); ESP_LOGI(TAG, "[APP] IDF version: %s", esp_get_idf_version()); @@ -153,8 +103,5 @@ void app_main() esp_log_level_set("TRANSPORT", ESP_LOG_VERBOSE); esp_log_level_set("OUTBOX", ESP_LOG_VERBOSE); - nvs_flash_init(); - wifi_init(); mqtt_app_start(); - } diff --git a/examples/protocols/mqtt/ssl_mutual_auth/sdkconfig.defaults b/examples/protocols/mqtt/ssl_mutual_auth/sdkconfig.defaults new file mode 100644 index 00000000..31a9759a --- /dev/null +++ b/examples/protocols/mqtt/ssl_mutual_auth/sdkconfig.defaults @@ -0,0 +1 @@ +CONFIG_MQTT_USING_ESP=y diff --git a/examples/protocols/mqtt/tcp/CMakeLists.txt b/examples/protocols/mqtt/tcp/CMakeLists.txt index 678d787a..dd954008 100644 --- a/examples/protocols/mqtt/tcp/CMakeLists.txt +++ b/examples/protocols/mqtt/tcp/CMakeLists.txt @@ -2,6 +2,10 @@ # in this exact order for cmake to work correctly cmake_minimum_required(VERSION 3.5) +# (Not part of the boilerplate) +# This example uses an extra component for common functions such as Wi-Fi and Ethernet connection. +set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common) + include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(mqtt_tcp) \ No newline at end of file diff --git a/examples/protocols/mqtt/tcp/Makefile b/examples/protocols/mqtt/tcp/Makefile index cd53fdbf..53027650 100644 --- a/examples/protocols/mqtt/tcp/Makefile +++ b/examples/protocols/mqtt/tcp/Makefile @@ -4,4 +4,6 @@ # PROJECT_NAME := mqtt_tcp +EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/protocol_examples_common + include $(IDF_PATH)/make/project.mk diff --git a/examples/protocols/mqtt/tcp/main/Kconfig.projbuild b/examples/protocols/mqtt/tcp/main/Kconfig.projbuild index 71f95ea8..0b0f9f8f 100644 --- a/examples/protocols/mqtt/tcp/main/Kconfig.projbuild +++ b/examples/protocols/mqtt/tcp/main/Kconfig.projbuild @@ -1,17 +1,5 @@ menu "Example Configuration" -config WIFI_SSID - string "WiFi SSID" - default "myssid" - help - SSID (network name) for the example to connect to. - -config WIFI_PASSWORD - string "WiFi Password" - default "mypassword" - help - WiFi password (WPA or WPA2) for the example to use. - config BROKER_URL string "Broker URL" default "mqtt://iot.eclipse.org" diff --git a/examples/protocols/mqtt/tcp/main/app_main.c b/examples/protocols/mqtt/tcp/main/app_main.c index ea37a740..72a16b3e 100644 --- a/examples/protocols/mqtt/tcp/main/app_main.c +++ b/examples/protocols/mqtt/tcp/main/app_main.c @@ -2,30 +2,25 @@ #include #include #include -#include "esp_wifi.h" -#include "esp_system.h" -#include "nvs_flash.h" -#include "esp_event_loop.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" -#include "freertos/semphr.h" -#include "freertos/queue.h" -#include "freertos/event_groups.h" +#include "esp_system.h" +#include "esp_log.h" +#include "esp_netif.h" +#include "esp_event.h" +#include "protocol_examples_common.h" +#include "nvs.h" +#include "nvs_flash.h" #include "lwip/sockets.h" #include "lwip/dns.h" #include "lwip/netdb.h" -#include "esp_log.h" #include "mqtt_client.h" static const char *TAG = "MQTT_EXAMPLE"; -static EventGroupHandle_t wifi_event_group; -const static int CONNECTED_BIT = BIT0; - - static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event) { esp_mqtt_client_handle_t client = event->client; @@ -73,56 +68,6 @@ static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event) return ESP_OK; } -static esp_err_t wifi_event_handler(void *ctx, system_event_t *event) -{ - /* For accessing reason codes in case of disconnection */ - system_event_info_t *info = &event->event_info; - - switch (event->event_id) { - case SYSTEM_EVENT_STA_START: - esp_wifi_connect(); - break; - case SYSTEM_EVENT_STA_GOT_IP: - xEventGroupSetBits(wifi_event_group, CONNECTED_BIT); - - break; - case SYSTEM_EVENT_STA_DISCONNECTED: - ESP_LOGE(TAG, "Disconnect reason : %d", info->disconnected.reason); - if (info->disconnected.reason == WIFI_REASON_BASIC_RATE_NOT_SUPPORT) { - /*Switch to 802.11 bgn mode */ - esp_wifi_set_protocol(ESP_IF_WIFI_STA, WIFI_PROTOCAL_11B | WIFI_PROTOCAL_11G | WIFI_PROTOCAL_11N); - } - esp_wifi_connect(); - xEventGroupClearBits(wifi_event_group, CONNECTED_BIT); - break; - default: - break; - } - return ESP_OK; -} - -static void wifi_init(void) -{ - tcpip_adapter_init(); - wifi_event_group = xEventGroupCreate(); - ESP_ERROR_CHECK(esp_event_loop_init(wifi_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_storage(WIFI_STORAGE_RAM)); - wifi_config_t wifi_config = { - .sta = { - .ssid = CONFIG_WIFI_SSID, - .password = CONFIG_WIFI_PASSWORD, - }, - }; - ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); - ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config)); - ESP_LOGI(TAG, "start the WIFI SSID:[%s]", CONFIG_WIFI_SSID); - ESP_ERROR_CHECK(esp_wifi_start()); - ESP_LOGI(TAG, "Waiting for wifi"); - xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT, false, true, portMAX_DELAY); -} - static void mqtt_app_start(void) { esp_mqtt_client_config_t mqtt_cfg = { @@ -162,6 +107,12 @@ static void mqtt_app_start(void) void app_main() { + ESP_ERROR_CHECK(nvs_flash_init()); + ESP_ERROR_CHECK(esp_netif_init()); + ESP_ERROR_CHECK(esp_event_loop_create_default()); + + ESP_ERROR_CHECK(example_connect()); + ESP_LOGI(TAG, "[APP] Startup.."); ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size()); ESP_LOGI(TAG, "[APP] IDF version: %s", esp_get_idf_version()); @@ -173,7 +124,5 @@ void app_main() esp_log_level_set("TRANSPORT", ESP_LOG_VERBOSE); esp_log_level_set("OUTBOX", ESP_LOG_VERBOSE); - nvs_flash_init(); - wifi_init(); mqtt_app_start(); } diff --git a/examples/protocols/mqtt/tcp/sdkconfig.ci b/examples/protocols/mqtt/tcp/sdkconfig.ci index 09ca8f37..f0b34b79 100644 --- a/examples/protocols/mqtt/tcp/sdkconfig.ci +++ b/examples/protocols/mqtt/tcp/sdkconfig.ci @@ -1,2 +1,3 @@ CONFIG_LOG_DEFAULT_LEVEL_DEBUG=y CONFIG_BROKER_URL="FROM_STDIN" +CONFIG_MQTT_USING_ESP=y diff --git a/examples/protocols/mqtt/tcp/sdkconfig.defaults b/examples/protocols/mqtt/tcp/sdkconfig.defaults new file mode 100644 index 00000000..31a9759a --- /dev/null +++ b/examples/protocols/mqtt/tcp/sdkconfig.defaults @@ -0,0 +1 @@ +CONFIG_MQTT_USING_ESP=y diff --git a/examples/protocols/mqtt/ws/CMakeLists.txt b/examples/protocols/mqtt/ws/CMakeLists.txt index 58a2135d..c272385f 100644 --- a/examples/protocols/mqtt/ws/CMakeLists.txt +++ b/examples/protocols/mqtt/ws/CMakeLists.txt @@ -2,6 +2,10 @@ # in this exact order for cmake to work correctly cmake_minimum_required(VERSION 3.5) +# (Not part of the boilerplate) +# This example uses an extra component for common functions such as Wi-Fi and Ethernet connection. +set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common) + include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(mqtt_websocket) \ No newline at end of file diff --git a/examples/protocols/mqtt/ws/Makefile b/examples/protocols/mqtt/ws/Makefile index 668719bf..2b97fa44 100644 --- a/examples/protocols/mqtt/ws/Makefile +++ b/examples/protocols/mqtt/ws/Makefile @@ -4,4 +4,6 @@ # PROJECT_NAME := mqtt_websocket +EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/protocol_examples_common + include $(IDF_PATH)/make/project.mk diff --git a/examples/protocols/mqtt/ws/main/Kconfig.projbuild b/examples/protocols/mqtt/ws/main/Kconfig.projbuild index 5223e885..add2256b 100644 --- a/examples/protocols/mqtt/ws/main/Kconfig.projbuild +++ b/examples/protocols/mqtt/ws/main/Kconfig.projbuild @@ -1,17 +1,5 @@ menu "Example Configuration" -config WIFI_SSID - string "WiFi SSID" - default "myssid" - help - SSID (network name) for the example to connect to. - -config WIFI_PASSWORD - string "WiFi Password" - default "mypassword" - help - WiFi password (WPA or WPA2) for the example to use. - config BROKER_URI string "Broker URL" default "ws://iot.eclipse.org:80/ws" diff --git a/examples/protocols/mqtt/ws/main/app_main.c b/examples/protocols/mqtt/ws/main/app_main.c index 6b1e18c8..7b86b462 100644 --- a/examples/protocols/mqtt/ws/main/app_main.c +++ b/examples/protocols/mqtt/ws/main/app_main.c @@ -2,30 +2,25 @@ #include #include #include -#include "esp_wifi.h" -#include "esp_system.h" -#include "nvs_flash.h" -#include "esp_event_loop.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" -#include "freertos/semphr.h" -#include "freertos/queue.h" -#include "freertos/event_groups.h" +#include "esp_system.h" +#include "esp_log.h" +#include "esp_netif.h" +#include "esp_event.h" +#include "protocol_examples_common.h" +#include "nvs.h" +#include "nvs_flash.h" #include "lwip/sockets.h" #include "lwip/dns.h" #include "lwip/netdb.h" -#include "esp_log.h" #include "mqtt_client.h" static const char *TAG = "MQTTWS_EXAMPLE"; -static EventGroupHandle_t wifi_event_group; -const static int CONNECTED_BIT = BIT0; - - static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event) { esp_mqtt_client_handle_t client = event->client; @@ -70,56 +65,6 @@ static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event) return ESP_OK; } -static esp_err_t wifi_event_handler(void *ctx, system_event_t *event) -{ - /* For accessing reason codes in case of disconnection */ - system_event_info_t *info = &event->event_info; - - switch (event->event_id) { - case SYSTEM_EVENT_STA_START: - esp_wifi_connect(); - break; - case SYSTEM_EVENT_STA_GOT_IP: - xEventGroupSetBits(wifi_event_group, CONNECTED_BIT); - - break; - case SYSTEM_EVENT_STA_DISCONNECTED: - ESP_LOGE(TAG, "Disconnect reason : %d", info->disconnected.reason); - if (info->disconnected.reason == WIFI_REASON_BASIC_RATE_NOT_SUPPORT) { - /*Switch to 802.11 bgn mode */ - esp_wifi_set_protocol(ESP_IF_WIFI_STA, WIFI_PROTOCAL_11B | WIFI_PROTOCAL_11G | WIFI_PROTOCAL_11N); - } - esp_wifi_connect(); - xEventGroupClearBits(wifi_event_group, CONNECTED_BIT); - break; - default: - break; - } - return ESP_OK; -} - -static void wifi_init(void) -{ - tcpip_adapter_init(); - wifi_event_group = xEventGroupCreate(); - ESP_ERROR_CHECK(esp_event_loop_init(wifi_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_storage(WIFI_STORAGE_RAM)); - wifi_config_t wifi_config = { - .sta = { - .ssid = CONFIG_WIFI_SSID, - .password = CONFIG_WIFI_PASSWORD, - }, - }; - ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); - ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config)); - ESP_LOGI(TAG, "start the WIFI SSID:[%s]", CONFIG_WIFI_SSID); - ESP_ERROR_CHECK(esp_wifi_start()); - ESP_LOGI(TAG, "Waiting for wifi"); - xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT, false, true, portMAX_DELAY); -} - static void mqtt_app_start(void) { const esp_mqtt_client_config_t mqtt_cfg = { @@ -134,6 +79,13 @@ static void mqtt_app_start(void) void app_main() { + ESP_ERROR_CHECK(nvs_flash_init()); + ESP_ERROR_CHECK(esp_netif_init()); + ESP_ERROR_CHECK(esp_event_loop_create_default()); + + + ESP_ERROR_CHECK(example_connect()); + ESP_LOGI(TAG, "[APP] Startup.."); ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size()); ESP_LOGI(TAG, "[APP] IDF version: %s", esp_get_idf_version()); @@ -146,7 +98,5 @@ void app_main() esp_log_level_set("TRANSPORT", ESP_LOG_VERBOSE); esp_log_level_set("OUTBOX", ESP_LOG_VERBOSE); - nvs_flash_init(); - wifi_init(); mqtt_app_start(); } diff --git a/examples/protocols/mqtt/ws/sdkconfig.ci b/examples/protocols/mqtt/ws/sdkconfig.ci index 4f14eef9..f024d643 100644 --- a/examples/protocols/mqtt/ws/sdkconfig.ci +++ b/examples/protocols/mqtt/ws/sdkconfig.ci @@ -1 +1,2 @@ CONFIG_BROKER_URI="ws://${EXAMPLE_MQTT_BROKER_WS}/ws" +CONFIG_MQTT_USING_ESP=y diff --git a/examples/protocols/mqtt/ws/sdkconfig.defaults b/examples/protocols/mqtt/ws/sdkconfig.defaults new file mode 100644 index 00000000..31a9759a --- /dev/null +++ b/examples/protocols/mqtt/ws/sdkconfig.defaults @@ -0,0 +1 @@ +CONFIG_MQTT_USING_ESP=y diff --git a/examples/protocols/mqtt/wss/CMakeLists.txt b/examples/protocols/mqtt/wss/CMakeLists.txt index 7ba5e629..ca6651bd 100644 --- a/examples/protocols/mqtt/wss/CMakeLists.txt +++ b/examples/protocols/mqtt/wss/CMakeLists.txt @@ -2,6 +2,10 @@ # in this exact order for cmake to work correctly cmake_minimum_required(VERSION 3.5) +# (Not part of the boilerplate) +# This example uses an extra component for common functions such as Wi-Fi and Ethernet connection. +set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common) + include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(mqtt_websocket_secure) diff --git a/examples/protocols/mqtt/wss/Makefile b/examples/protocols/mqtt/wss/Makefile index 27047d04..50ed13dd 100644 --- a/examples/protocols/mqtt/wss/Makefile +++ b/examples/protocols/mqtt/wss/Makefile @@ -4,4 +4,6 @@ # PROJECT_NAME := mqtt_websocket_secure +EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/protocol_examples_common + include $(IDF_PATH)/make/project.mk diff --git a/examples/protocols/mqtt/wss/main/Kconfig.projbuild b/examples/protocols/mqtt/wss/main/Kconfig.projbuild index 964d436f..7a15e2b6 100644 --- a/examples/protocols/mqtt/wss/main/Kconfig.projbuild +++ b/examples/protocols/mqtt/wss/main/Kconfig.projbuild @@ -1,17 +1,5 @@ menu "Example Configuration" -config WIFI_SSID - string "WiFi SSID" - default "myssid" - help - SSID (network name) for the example to connect to. - -config WIFI_PASSWORD - string "WiFi Password" - default "mypassword" - help - WiFi password (WPA or WPA2) for the example to use. - config BROKER_URI string "Broker URL" default "wss://iot.eclipse.org:443/ws" diff --git a/examples/protocols/mqtt/wss/main/app_main.c b/examples/protocols/mqtt/wss/main/app_main.c index 06f6307d..c9c658a8 100644 --- a/examples/protocols/mqtt/wss/main/app_main.c +++ b/examples/protocols/mqtt/wss/main/app_main.c @@ -2,81 +2,25 @@ #include #include #include -#include "esp_wifi.h" -#include "esp_system.h" -#include "nvs_flash.h" -#include "esp_event_loop.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" -#include "freertos/semphr.h" -#include "freertos/queue.h" -#include "freertos/event_groups.h" +#include "esp_system.h" +#include "esp_log.h" +#include "esp_netif.h" +#include "esp_event.h" +#include "protocol_examples_common.h" +#include "nvs.h" +#include "nvs_flash.h" #include "lwip/sockets.h" #include "lwip/dns.h" #include "lwip/netdb.h" -#include "esp_log.h" #include "mqtt_client.h" static const char *TAG = "MQTTWSS_EXAMPLE"; -static EventGroupHandle_t wifi_event_group; -const static int CONNECTED_BIT = BIT0; - - - -static esp_err_t wifi_event_handler(void *ctx, system_event_t *event) -{ - /* For accessing reason codes in case of disconnection */ - system_event_info_t *info = &event->event_info; - - switch (event->event_id) { - case SYSTEM_EVENT_STA_START: - esp_wifi_connect(); - break; - case SYSTEM_EVENT_STA_GOT_IP: - xEventGroupSetBits(wifi_event_group, CONNECTED_BIT); - - break; - case SYSTEM_EVENT_STA_DISCONNECTED: - ESP_LOGE(TAG, "Disconnect reason : %d", info->disconnected.reason); - if (info->disconnected.reason == WIFI_REASON_BASIC_RATE_NOT_SUPPORT) { - /*Switch to 802.11 bgn mode */ - esp_wifi_set_protocol(ESP_IF_WIFI_STA, WIFI_PROTOCAL_11B | WIFI_PROTOCAL_11G | WIFI_PROTOCAL_11N); - } - esp_wifi_connect(); - xEventGroupClearBits(wifi_event_group, CONNECTED_BIT); - break; - default: - break; - } - return ESP_OK; -} - -static void wifi_init(void) -{ - tcpip_adapter_init(); - wifi_event_group = xEventGroupCreate(); - ESP_ERROR_CHECK(esp_event_loop_init(wifi_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_storage(WIFI_STORAGE_RAM)); - wifi_config_t wifi_config = { - .sta = { - .ssid = CONFIG_WIFI_SSID, - .password = CONFIG_WIFI_PASSWORD, - }, - }; - ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); - ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config)); - ESP_LOGI(TAG, "start the WIFI SSID:[%s]", CONFIG_WIFI_SSID); - ESP_ERROR_CHECK(esp_wifi_start()); - ESP_LOGI(TAG, "Waiting for wifi"); - xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT, false, true, portMAX_DELAY); -} - #if CONFIG_BROKER_CERTIFICATE_OVERRIDDEN == 1 static const uint8_t iot_eclipse_org_pem_start[] = "-----BEGIN CERTIFICATE-----\n" CONFIG_BROKER_CERTIFICATE_OVERRIDE "\n-----END CERTIFICATE-----"; #else @@ -143,6 +87,13 @@ static void mqtt_app_start(void) void app_main() { + ESP_ERROR_CHECK(nvs_flash_init()); + ESP_ERROR_CHECK(esp_netif_init()); + ESP_ERROR_CHECK(esp_event_loop_create_default()); + + + ESP_ERROR_CHECK(example_connect()); + ESP_LOGI(TAG, "[APP] Startup.."); ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size()); ESP_LOGI(TAG, "[APP] IDF version: %s", esp_get_idf_version()); @@ -154,7 +105,5 @@ void app_main() esp_log_level_set("TRANSPORT", ESP_LOG_VERBOSE); esp_log_level_set("OUTBOX", ESP_LOG_VERBOSE); - nvs_flash_init(); - wifi_init(); mqtt_app_start(); } diff --git a/examples/protocols/mqtt/wss/sdkconfig.ci b/examples/protocols/mqtt/wss/sdkconfig.ci index d4af6205..ed4ef640 100644 --- a/examples/protocols/mqtt/wss/sdkconfig.ci +++ b/examples/protocols/mqtt/wss/sdkconfig.ci @@ -1,2 +1,3 @@ CONFIG_BROKER_URI="wss://${EXAMPLE_MQTT_BROKER_WSS}/ws" CONFIG_BROKER_CERTIFICATE_OVERRIDE="${EXAMPLE_MQTT_BROKER_CERTIFICATE}" +CONFIG_MQTT_USING_ESP=y diff --git a/examples/protocols/mqtt/wss/sdkconfig.defaults b/examples/protocols/mqtt/wss/sdkconfig.defaults new file mode 100644 index 00000000..31a9759a --- /dev/null +++ b/examples/protocols/mqtt/wss/sdkconfig.defaults @@ -0,0 +1 @@ +CONFIG_MQTT_USING_ESP=y diff --git a/examples/protocols/openssl_client/CMakeLists.txt b/examples/protocols/openssl_client/CMakeLists.txt index 3b366a05..83830446 100644 --- a/examples/protocols/openssl_client/CMakeLists.txt +++ b/examples/protocols/openssl_client/CMakeLists.txt @@ -2,5 +2,9 @@ # CMakeLists in this exact order for cmake to work correctly cmake_minimum_required(VERSION 3.5) +# (Not part of the boilerplate) +# This example uses an extra component for common functions such as Wi-Fi and Ethernet connection. +set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common) + include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(openssl_client) diff --git a/examples/protocols/openssl_client/Makefile b/examples/protocols/openssl_client/Makefile index 7e2f4fe7..1ae39bf1 100644 --- a/examples/protocols/openssl_client/Makefile +++ b/examples/protocols/openssl_client/Makefile @@ -5,5 +5,7 @@ PROJECT_NAME := openssl_client +EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/protocol_examples_common + include $(IDF_PATH)/make/project.mk diff --git a/examples/protocols/openssl_client/main/Kconfig.projbuild b/examples/protocols/openssl_client/main/Kconfig.projbuild index 13605964..d7934480 100644 --- a/examples/protocols/openssl_client/main/Kconfig.projbuild +++ b/examples/protocols/openssl_client/main/Kconfig.projbuild @@ -13,18 +13,4 @@ config TARGET_PORT_NUMBER help Target port number for the example to connect to. -config WIFI_SSID - string "WiFi SSID" - default "myssid" - help - SSID (network name) for the example to connect to. - -config WIFI_PASSWORD - string "WiFi Password" - default "mypassword" - help - WiFi password (WPA or WPA2) for the example to use. - - Can be left blank if the network has no security set. - endmenu diff --git a/examples/protocols/openssl_client/main/openssl_client_example_main.c b/examples/protocols/openssl_client/main/openssl_client_example_main.c index 29c6f494..564f3397 100644 --- a/examples/protocols/openssl_client/main/openssl_client_example_main.c +++ b/examples/protocols/openssl_client/main/openssl_client_example_main.c @@ -10,40 +10,21 @@ #include #include -#include "esp_system.h" -#include "esp_wifi.h" -#include "esp_event_loop.h" -#include "esp_log.h" -#include "nvs_flash.h" - #include "freertos/FreeRTOS.h" #include "freertos/task.h" -#include "freertos/event_groups.h" +#include "esp_system.h" +#include "esp_log.h" +#include "esp_netif.h" +#include "esp_event.h" +#include "protocol_examples_common.h" +#include "nvs.h" +#include "nvs_flash.h" #include #include #include "openssl/ssl.h" -/* The examples use simple WiFi configuration that you can set via - 'make menuconfig'. - - If you'd rather not, just change the below entries to strings with - the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid" -*/ -#define EXAMPLE_WIFI_SSID CONFIG_WIFI_SSID -#define EXAMPLE_WIFI_PASS CONFIG_WIFI_PASSWORD - -/* FreeRTOS event group to signal when we are connected & ready to make a request */ -static EventGroupHandle_t wifi_event_group; - -static const char *TAG = "example"; - -/* 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? */ -const int CONNECTED_BIT = BIT0; - extern const uint8_t ca_pem_start[] asm("_binary_ca_pem_start"); extern const uint8_t ca_pem_end[] asm("_binary_ca_pem_end"); extern const uint8_t client_pem_start[] asm("_binary_client_pem_start"); @@ -73,53 +54,6 @@ static int send_bytes = sizeof(send_data); static char recv_buf[OPENSSL_CLIENT_RECV_BUF_LEN]; -static esp_err_t event_handler(void *ctx, system_event_t *event) -{ - /* For accessing reason codes in case of disconnection */ - system_event_info_t *info = &event->event_info; - - switch(event->event_id) { - case SYSTEM_EVENT_STA_START: - esp_wifi_connect(); - break; - case SYSTEM_EVENT_STA_GOT_IP: - xEventGroupSetBits(wifi_event_group, CONNECTED_BIT); - break; - case SYSTEM_EVENT_STA_DISCONNECTED: - ESP_LOGE(TAG, "Disconnect reason : %d", info->disconnected.reason); - if (info->disconnected.reason == WIFI_REASON_BASIC_RATE_NOT_SUPPORT) { - /*Switch to 802.11 bgn mode */ - esp_wifi_set_protocol(ESP_IF_WIFI_STA, WIFI_PROTOCAL_11B | WIFI_PROTOCAL_11G | WIFI_PROTOCAL_11N); - } - 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_storage(WIFI_STORAGE_RAM) ); - wifi_config_t wifi_config = { - .sta = { - .ssid = EXAMPLE_WIFI_SSID, - .password = EXAMPLE_WIFI_PASS, - }, - }; - ESP_LOGI(TAG, "Setting WiFi configuration SSID %s...", wifi_config.sta.ssid); - ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) ); - ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) ); - ESP_ERROR_CHECK( esp_wifi_start() ); -} - static void openssl_client_task(void* p) { int ret; @@ -134,10 +68,6 @@ static void openssl_client_task(void* p) printf("OpenSSL client thread start...\n"); - xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT, - false, true, portMAX_DELAY); - ESP_LOGI(TAG, "Connected to AP"); - /*get addr info for hostname*/ do { entry = gethostbyname(CONFIG_TARGET_DOMAIN); @@ -282,7 +212,11 @@ failed1: void app_main(void) { - ESP_ERROR_CHECK( nvs_flash_init() ); - initialise_wifi(); + ESP_ERROR_CHECK(nvs_flash_init()); + ESP_ERROR_CHECK(esp_netif_init()); + ESP_ERROR_CHECK(esp_event_loop_create_default()); + + ESP_ERROR_CHECK(example_connect()); + xTaskCreate(&openssl_client_task, "openssl_client", 8192, NULL, 6, NULL); } diff --git a/examples/protocols/openssl_demo/CMakeLists.txt b/examples/protocols/openssl_demo/CMakeLists.txt index 477e79ae..3af20bda 100644 --- a/examples/protocols/openssl_demo/CMakeLists.txt +++ b/examples/protocols/openssl_demo/CMakeLists.txt @@ -2,5 +2,9 @@ # in this exact order for cmake to work correctly cmake_minimum_required(VERSION 3.5) +# (Not part of the boilerplate) +# This example uses an extra component for common functions such as Wi-Fi and Ethernet connection. +set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common) + include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(openssl_demo) diff --git a/examples/protocols/openssl_demo/Makefile b/examples/protocols/openssl_demo/Makefile index d5f30fcd..21e33142 100644 --- a/examples/protocols/openssl_demo/Makefile +++ b/examples/protocols/openssl_demo/Makefile @@ -5,5 +5,7 @@ PROJECT_NAME := openssl_demo +EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/protocol_examples_common + include $(IDF_PATH)/make/project.mk diff --git a/examples/protocols/openssl_demo/main/Kconfig.projbuild b/examples/protocols/openssl_demo/main/Kconfig.projbuild deleted file mode 100644 index 92a75195..00000000 --- a/examples/protocols/openssl_demo/main/Kconfig.projbuild +++ /dev/null @@ -1,17 +0,0 @@ -menu "Example Configuration" - -config WIFI_SSID - string "WiFi SSID" - default "myssid" - help - SSID (network name) for the example to connect to. - -config WIFI_PASSWORD - string "WiFi Password" - default "mypassword" - help - WiFi password (WPA or WPA2) for the example to use. - - Can be left blank if the network has no security set. - -endmenu diff --git a/examples/protocols/openssl_demo/main/openssl_demo_example_main.c b/examples/protocols/openssl_demo/main/openssl_demo_example_main.c index a5242542..837b9e20 100644 --- a/examples/protocols/openssl_demo/main/openssl_demo_example_main.c +++ b/examples/protocols/openssl_demo/main/openssl_demo_example_main.c @@ -10,37 +10,18 @@ #include #include #include - -#include "esp_wifi.h" -#include "esp_event_loop.h" -#include "esp_log.h" -#include "nvs_flash.h" - -#include "freertos/FreeRTOS.h" -#include "freertos/task.h" -#include "freertos/event_groups.h" - #include #include +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" + +#include "esp_log.h" +#include "nvs_flash.h" +#include "esp_netif.h" +#include "esp_event.h" #include "openssl/ssl.h" - -/* The examples use simple WiFi configuration that you can set via - 'make menuconfig'. - - If you'd rather not, just change the below entries to strings with - the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid" -*/ -#define EXAMPLE_WIFI_SSID CONFIG_WIFI_SSID -#define EXAMPLE_WIFI_PASS CONFIG_WIFI_PASSWORD - -/* 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? */ -const int CONNECTED_BIT = BIT0; +#include "protocol_examples_common.h" static const char *TAG = "example"; @@ -58,57 +39,6 @@ static int send_bytes = sizeof(send_data); static char recv_buf[OPENSSL_DEMO_RECV_BUF_LEN]; -static esp_err_t event_handler(void *ctx, system_event_t *event) -{ - /* For accessing reason codes in case of disconnection */ - system_event_info_t *info = &event->event_info; - - switch (event->event_id) { - case SYSTEM_EVENT_STA_START: - esp_wifi_connect(); - break; - - case SYSTEM_EVENT_STA_GOT_IP: - xEventGroupSetBits(wifi_event_group, CONNECTED_BIT); - break; - - case SYSTEM_EVENT_STA_DISCONNECTED: - ESP_LOGE(TAG, "Disconnect reason : %d", info->disconnected.reason); - if (info->disconnected.reason == WIFI_REASON_BASIC_RATE_NOT_SUPPORT) { - /*Switch to 802.11 bgn mode */ - esp_wifi_set_protocol(ESP_IF_WIFI_STA, WIFI_PROTOCAL_11B | WIFI_PROTOCAL_11G | WIFI_PROTOCAL_11N); - } - 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_storage(WIFI_STORAGE_RAM)); - wifi_config_t wifi_config = { - .sta = { - .ssid = EXAMPLE_WIFI_SSID, - .password = EXAMPLE_WIFI_PASS, - }, - }; - ESP_LOGI(TAG, "Setting WiFi configuration SSID %s...", wifi_config.sta.ssid); - ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); - ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config)); - ESP_ERROR_CHECK(esp_wifi_start()); -} - static void openssl_task(void *p) { int ret; @@ -124,13 +54,6 @@ static void openssl_task(void *p) ESP_LOGI(TAG, "OpenSSL demo thread start..."); - /* Wait for the callback to set the CONNECTED_BIT in the - event group. - */ - xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT, - false, true, portMAX_DELAY); - ESP_LOGI(TAG, "Connected to AP"); - /*get addr info for hostname*/ do { entry = gethostbyname(OPENSSL_DEMO_TARGET_NAME); @@ -260,6 +183,11 @@ failed1: void app_main(void) { ESP_ERROR_CHECK(nvs_flash_init()); - initialise_wifi(); + ESP_ERROR_CHECK(esp_netif_init()); + ESP_ERROR_CHECK(esp_event_loop_create_default()); + + + ESP_ERROR_CHECK(example_connect()); + xTaskCreate(&openssl_task, "openssl_task", 8192, NULL, 5, NULL); } diff --git a/examples/protocols/openssl_server/CMakeLists.txt b/examples/protocols/openssl_server/CMakeLists.txt index d0a68b27..22094167 100644 --- a/examples/protocols/openssl_server/CMakeLists.txt +++ b/examples/protocols/openssl_server/CMakeLists.txt @@ -2,5 +2,9 @@ # CMakeLists in this exact order for cmake to work correctly cmake_minimum_required(VERSION 3.5) +# (Not part of the boilerplate) +# This example uses an extra component for common functions such as Wi-Fi and Ethernet connection. +set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common) + include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(openssl_server) diff --git a/examples/protocols/openssl_server/Makefile b/examples/protocols/openssl_server/Makefile index 8f781a8c..87d6e942 100644 --- a/examples/protocols/openssl_server/Makefile +++ b/examples/protocols/openssl_server/Makefile @@ -5,5 +5,7 @@ PROJECT_NAME := openssl-server +EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/protocol_examples_common + include $(IDF_PATH)/make/project.mk diff --git a/examples/protocols/openssl_server/main/Kconfig.projbuild b/examples/protocols/openssl_server/main/Kconfig.projbuild deleted file mode 100644 index 92a75195..00000000 --- a/examples/protocols/openssl_server/main/Kconfig.projbuild +++ /dev/null @@ -1,17 +0,0 @@ -menu "Example Configuration" - -config WIFI_SSID - string "WiFi SSID" - default "myssid" - help - SSID (network name) for the example to connect to. - -config WIFI_PASSWORD - string "WiFi Password" - default "mypassword" - help - WiFi password (WPA or WPA2) for the example to use. - - Can be left blank if the network has no security set. - -endmenu diff --git a/examples/protocols/openssl_server/main/openssl_server_example_main.c b/examples/protocols/openssl_server/main/openssl_server_example_main.c index 7431e8a4..20a2d9e5 100644 --- a/examples/protocols/openssl_server/main/openssl_server_example_main.c +++ b/examples/protocols/openssl_server/main/openssl_server_example_main.c @@ -11,39 +11,20 @@ #include #include -#include "esp_system.h" -#include "esp_wifi.h" -#include "esp_event_loop.h" -#include "esp_log.h" -#include "nvs_flash.h" - #include "freertos/FreeRTOS.h" #include "freertos/task.h" -#include "freertos/event_groups.h" +#include "esp_system.h" +#include "esp_log.h" +#include "esp_netif.h" +#include "esp_event.h" +#include "protocol_examples_common.h" +#include "nvs.h" +#include "nvs_flash.h" #include #include "openssl/ssl.h" -/* The examples use simple WiFi configuration that you can set via - 'make menuconfig'. - - If you'd rather not, just change the below entries to strings with - the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid" -*/ -#define EXAMPLE_WIFI_SSID CONFIG_WIFI_SSID -#define EXAMPLE_WIFI_PASS CONFIG_WIFI_PASSWORD - -/* FreeRTOS event group to signal when we are connected & ready to make a request */ -static EventGroupHandle_t wifi_event_group; - -static const char *TAG = "example"; - -/* 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? */ -const int CONNECTED_BIT = BIT0; - extern const uint8_t ca_pem_start[] asm("_binary_ca_pem_start"); extern const uint8_t ca_pem_end[] asm("_binary_ca_pem_end"); extern const uint8_t server_pem_start[] asm("_binary_server_pem_start"); @@ -73,53 +54,6 @@ static int send_bytes = sizeof(send_data); static char recv_buf[OPENSSL_SERVER_RECV_BUF_LEN]; -static esp_err_t event_handler(void *ctx, system_event_t *event) -{ - /* For accessing reason codes in case of disconnection */ - system_event_info_t *info = &event->event_info; - - switch(event->event_id) { - case SYSTEM_EVENT_STA_START: - esp_wifi_connect(); - break; - case SYSTEM_EVENT_STA_GOT_IP: - xEventGroupSetBits(wifi_event_group, CONNECTED_BIT); - break; - case SYSTEM_EVENT_STA_DISCONNECTED: - ESP_LOGE(TAG, "Disconnect reason : %d", info->disconnected.reason); - if (info->disconnected.reason == WIFI_REASON_BASIC_RATE_NOT_SUPPORT) { - /*Switch to 802.11 bgn mode */ - esp_wifi_set_protocol(ESP_IF_WIFI_STA, WIFI_PROTOCAL_11B | WIFI_PROTOCAL_11G | WIFI_PROTOCAL_11N); - } - 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_storage(WIFI_STORAGE_RAM) ); - wifi_config_t wifi_config = { - .sta = { - .ssid = EXAMPLE_WIFI_SSID, - .password = EXAMPLE_WIFI_PASS, - }, - }; - ESP_LOGI(TAG, "Setting WiFi configuration SSID %s...", wifi_config.sta.ssid); - ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) ); - ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) ); - ESP_ERROR_CHECK( esp_wifi_start() ); -} - static void openssl_server_task(void* p) { int ret; @@ -134,13 +68,6 @@ static void openssl_server_task(void* p) printf("OpenSSL server thread start...\n"); - /* Wait for the callback to set the CONNECTED_BIT in the - event group. - */ - xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT, - false, true, portMAX_DELAY); - ESP_LOGI(TAG, "Connected to AP"); - printf("create SSL context ......"); ctx = SSL_CTX_new(TLSv1_2_server_method()); @@ -287,7 +214,11 @@ failed1: void app_main(void) { - ESP_ERROR_CHECK( nvs_flash_init() ); - initialise_wifi(); + ESP_ERROR_CHECK(nvs_flash_init()); + ESP_ERROR_CHECK(esp_netif_init()); + ESP_ERROR_CHECK(esp_event_loop_create_default()); + + ESP_ERROR_CHECK(example_connect()); + xTaskCreate(&openssl_server_task, "openssl_server", 8192, NULL, 6, NULL); } diff --git a/examples/protocols/sntp/CMakeLists.txt b/examples/protocols/sntp/CMakeLists.txt index 872321a8..99c51e87 100644 --- a/examples/protocols/sntp/CMakeLists.txt +++ b/examples/protocols/sntp/CMakeLists.txt @@ -2,5 +2,9 @@ # CMakeLists in this exact order for cmake to work correctly cmake_minimum_required(VERSION 3.5) +# (Not part of the boilerplate) +# This example uses an extra component for common functions such as Wi-Fi and Ethernet connection. +set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common) + include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(sntp) diff --git a/examples/protocols/sntp/Makefile b/examples/protocols/sntp/Makefile index e6ef17be..d3e9a53f 100644 --- a/examples/protocols/sntp/Makefile +++ b/examples/protocols/sntp/Makefile @@ -5,5 +5,7 @@ PROJECT_NAME := sntp +EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/protocol_examples_common + include $(IDF_PATH)/make/project.mk diff --git a/examples/protocols/sntp/main/Kconfig.projbuild b/examples/protocols/sntp/main/Kconfig.projbuild deleted file mode 100644 index 92a75195..00000000 --- a/examples/protocols/sntp/main/Kconfig.projbuild +++ /dev/null @@ -1,17 +0,0 @@ -menu "Example Configuration" - -config WIFI_SSID - string "WiFi SSID" - default "myssid" - help - SSID (network name) for the example to connect to. - -config WIFI_PASSWORD - string "WiFi Password" - default "mypassword" - help - WiFi password (WPA or WPA2) for the example to use. - - Can be left blank if the network has no security set. - -endmenu diff --git a/examples/protocols/sntp/main/sntp_example_main.c b/examples/protocols/sntp/main/sntp_example_main.c index 50687a26..7683f21d 100644 --- a/examples/protocols/sntp/main/sntp_example_main.c +++ b/examples/protocols/sntp/main/sntp_example_main.c @@ -12,34 +12,17 @@ #include "freertos/FreeRTOS.h" #include "freertos/task.h" -#include "freertos/event_groups.h" - #include "esp_system.h" -#include "esp_wifi.h" -#include "esp_event_loop.h" #include "esp_log.h" - +#include "esp_netif.h" +#include "esp_event.h" +#include "protocol_examples_common.h" +#include "nvs.h" #include "nvs_flash.h" +#include "driver/gpio.h" #include "lwip/apps/sntp.h" -/* The examples use simple WiFi configuration that you can set via - 'make menuconfig'. - - If you'd rather not, just change the below entries to strings with - the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid" -*/ -#define EXAMPLE_WIFI_SSID CONFIG_WIFI_SSID -#define EXAMPLE_WIFI_PASS CONFIG_WIFI_PASSWORD - -/* 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? */ -const int CONNECTED_BIT = BIT0; - static const char *TAG = "sntp_example"; static void initialize_sntp(void) @@ -52,8 +35,6 @@ static void initialize_sntp(void) static void obtain_time(void) { - xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT, - false, true, portMAX_DELAY); initialize_sntp(); // wait for time to be set @@ -70,37 +51,6 @@ static void obtain_time(void) } } -static esp_err_t event_handler(void *ctx, system_event_t *event) -{ - /* For accessing reason codes in case of disconnection */ - system_event_info_t *info = &event->event_info; - - switch (event->event_id) { - case SYSTEM_EVENT_STA_START: - esp_wifi_connect(); - break; - - case SYSTEM_EVENT_STA_GOT_IP: - xEventGroupSetBits(wifi_event_group, CONNECTED_BIT); - break; - - case SYSTEM_EVENT_STA_DISCONNECTED: - ESP_LOGE(TAG, "Disconnect reason : %d", info->disconnected.reason); - if (info->disconnected.reason == WIFI_REASON_BASIC_RATE_NOT_SUPPORT) { - /*Switch to 802.11 bgn mode */ - esp_wifi_set_protocol(ESP_IF_WIFI_STA, WIFI_PROTOCAL_11B | WIFI_PROTOCAL_11G | WIFI_PROTOCAL_11N); - } - esp_wifi_connect(); - xEventGroupClearBits(wifi_event_group, CONNECTED_BIT); - break; - - default: - break; - } - - return ESP_OK; -} - static void sntp_example_task(void *arg) { time_t now; @@ -141,39 +91,13 @@ static void sntp_example_task(void *arg) } } -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_storage(WIFI_STORAGE_RAM)); - wifi_config_t wifi_config = { - .sta = { - .ssid = EXAMPLE_WIFI_SSID, - .password = EXAMPLE_WIFI_PASS, - }, - }; - ESP_LOGI(TAG, "Setting WiFi configuration SSID %s...", wifi_config.sta.ssid); - ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); - ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config)); - ESP_ERROR_CHECK(esp_wifi_start()); -} - void app_main() { - //Initialize NVS - esp_err_t ret = nvs_flash_init(); + ESP_ERROR_CHECK(nvs_flash_init()); + ESP_ERROR_CHECK(esp_netif_init()); + ESP_ERROR_CHECK(esp_event_loop_create_default()); - if (ret == ESP_ERR_NVS_NO_FREE_PAGES) { - ESP_ERROR_CHECK(nvs_flash_erase()); - ret = nvs_flash_init(); - } - - ESP_ERROR_CHECK(ret); - - initialise_wifi(); + ESP_ERROR_CHECK(example_connect()); // SNTP service uses LwIP, please allocate large stack space. xTaskCreate(sntp_example_task, "sntp_example_task", 2048, NULL, 10, NULL); diff --git a/examples/protocols/sockets/tcp_client/CMakeLists.txt b/examples/protocols/sockets/tcp_client/CMakeLists.txt index 22aa37f1..aee70cad 100644 --- a/examples/protocols/sockets/tcp_client/CMakeLists.txt +++ b/examples/protocols/sockets/tcp_client/CMakeLists.txt @@ -2,5 +2,9 @@ # CMakeLists in this exact order for cmake to work correctly cmake_minimum_required(VERSION 3.5) +# (Not part of the boilerplate) +# This example uses an extra component for common functions such as Wi-Fi and Ethernet connection. +set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common) + include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(tcp_client) diff --git a/examples/protocols/sockets/tcp_client/Makefile b/examples/protocols/sockets/tcp_client/Makefile index b390190f..eee056b3 100644 --- a/examples/protocols/sockets/tcp_client/Makefile +++ b/examples/protocols/sockets/tcp_client/Makefile @@ -5,5 +5,7 @@ PROJECT_NAME := tcp_client +EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/protocol_examples_common + include $(IDF_PATH)/make/project.mk diff --git a/examples/protocols/sockets/tcp_client/main/Kconfig.projbuild b/examples/protocols/sockets/tcp_client/main/Kconfig.projbuild index 1beefd49..b5f67bec 100644 --- a/examples/protocols/sockets/tcp_client/main/Kconfig.projbuild +++ b/examples/protocols/sockets/tcp_client/main/Kconfig.projbuild @@ -1,18 +1,5 @@ menu "Example Configuration" -config WIFI_SSID - string "WiFi SSID" - default "myssid" - help - SSID (network name) for the example to connect to. - -config WIFI_PASSWORD - string "WiFi Password" - default "mypassword" - help - WiFi password (WPA or WPA2) for the example to use. - Can be left blank if the network has no security set. - choice EXAMPLE_IP_MODE prompt "IP Version" help @@ -22,7 +9,7 @@ config EXAMPLE_IPV4 bool "IPV4" config EXAMPLE_IPV6 - select LWIP_IPV6 + select EXAMPLE_CONNECT_IPV6 bool "IPV6" endchoice diff --git a/examples/protocols/sockets/tcp_client/main/tcp_client.c b/examples/protocols/sockets/tcp_client/main/tcp_client.c index cd546ddb..f7bc6eab 100644 --- a/examples/protocols/sockets/tcp_client/main/tcp_client.c +++ b/examples/protocols/sockets/tcp_client/main/tcp_client.c @@ -8,13 +8,15 @@ */ #include #include + #include "freertos/FreeRTOS.h" #include "freertos/task.h" -#include "freertos/event_groups.h" #include "esp_system.h" -#include "esp_wifi.h" -#include "esp_event_loop.h" #include "esp_log.h" +#include "esp_netif.h" +#include "esp_event.h" +#include "protocol_examples_common.h" +#include "nvs.h" #include "nvs_flash.h" #include "lwip/err.h" @@ -22,15 +24,6 @@ #include "lwip/sys.h" #include - -/* The examples use simple WiFi configuration that you can set via - 'make menuconfig'. - If you'd rather not, just change the below entries to strings with - the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid" -*/ -#define EXAMPLE_WIFI_SSID CONFIG_WIFI_SSID -#define EXAMPLE_WIFI_PASS CONFIG_WIFI_PASSWORD - #ifdef CONFIG_EXAMPLE_IPV4 #define HOST_IP_ADDR CONFIG_EXAMPLE_IPV4_ADDR #else @@ -39,96 +32,9 @@ #define PORT CONFIG_EXAMPLE_PORT -/* FreeRTOS event group to signal when we are connected & ready to make a request */ -static EventGroupHandle_t wifi_event_group; - -const int IPV4_GOTIP_BIT = BIT0; -#ifdef CONFIG_EXAMPLE_IPV6 -const int IPV6_GOTIP_BIT = BIT1; -#endif - static const char *TAG = "example"; static const char *payload = "Message from ESP32 "; -static esp_err_t event_handler(void *ctx, system_event_t *event) -{ - /* For accessing reason codes in case of disconnection */ - system_event_info_t *info = &event->event_info; - - switch (event->event_id) { - case SYSTEM_EVENT_STA_START: - esp_wifi_connect(); - ESP_LOGI(TAG, "SYSTEM_EVENT_STA_START"); - break; - case SYSTEM_EVENT_STA_CONNECTED: -#ifdef CONFIG_EXAMPLE_IPV6 - /* enable ipv6 */ - tcpip_adapter_create_ip6_linklocal(TCPIP_ADAPTER_IF_STA); -#endif - break; - case SYSTEM_EVENT_STA_GOT_IP: - xEventGroupSetBits(wifi_event_group, IPV4_GOTIP_BIT); - ESP_LOGI(TAG, "SYSTEM_EVENT_STA_GOT_IP"); - break; - case SYSTEM_EVENT_STA_DISCONNECTED: - ESP_LOGE(TAG, "Disconnect reason : %d", info->disconnected.reason); - if (info->disconnected.reason == WIFI_REASON_BASIC_RATE_NOT_SUPPORT) { - /*Switch to 802.11 bgn mode */ - esp_wifi_set_protocol(ESP_IF_WIFI_STA, WIFI_PROTOCAL_11B | WIFI_PROTOCAL_11G | WIFI_PROTOCAL_11N); - } - esp_wifi_connect(); - xEventGroupClearBits(wifi_event_group, IPV4_GOTIP_BIT); -#ifdef CONFIG_EXAMPLE_IPV6 - xEventGroupClearBits(wifi_event_group, IPV6_GOTIP_BIT); -#endif - break; - case SYSTEM_EVENT_AP_STA_GOT_IP6: -#ifdef CONFIG_EXAMPLE_IPV6 - xEventGroupSetBits(wifi_event_group, IPV6_GOTIP_BIT); - ESP_LOGI(TAG, "SYSTEM_EVENT_STA_GOT_IP6"); - - char *ip6 = ip6addr_ntoa(&event->event_info.got_ip6.ip6_info.ip); - ESP_LOGI(TAG, "IPv6: %s", ip6); -#endif - 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_storage(WIFI_STORAGE_RAM) ); - wifi_config_t wifi_config = { - .sta = { - .ssid = EXAMPLE_WIFI_SSID, - .password = EXAMPLE_WIFI_PASS, - }, - }; - ESP_LOGI(TAG, "Setting WiFi configuration SSID %s...", wifi_config.sta.ssid); - ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) ); - ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) ); - ESP_ERROR_CHECK( esp_wifi_start() ); -} - -static void wait_for_ip() -{ -#ifdef CONFIG_EXAMPLE_IPV6 - uint32_t bits = IPV4_GOTIP_BIT | IPV6_GOTIP_BIT; -#else - uint32_t bits = IPV4_GOTIP_BIT; -#endif - - ESP_LOGI(TAG, "Waiting for AP connection..."); - xEventGroupWaitBits(wifi_event_group, bits, false, true, portMAX_DELAY); - ESP_LOGI(TAG, "Connected to AP"); -} - static void tcp_client_task(void *pvParameters) { char rx_buffer[128]; @@ -203,9 +109,11 @@ static void tcp_client_task(void *pvParameters) void app_main() { - ESP_ERROR_CHECK( nvs_flash_init() ); - initialise_wifi(); - wait_for_ip(); + ESP_ERROR_CHECK(nvs_flash_init()); + ESP_ERROR_CHECK(esp_netif_init()); + ESP_ERROR_CHECK(esp_event_loop_create_default()); + + ESP_ERROR_CHECK(example_connect()); xTaskCreate(tcp_client_task, "tcp_client", 4096, NULL, 5, NULL); } diff --git a/examples/protocols/sockets/tcp_server/CMakeLists.txt b/examples/protocols/sockets/tcp_server/CMakeLists.txt index 3514e60f..9865464b 100644 --- a/examples/protocols/sockets/tcp_server/CMakeLists.txt +++ b/examples/protocols/sockets/tcp_server/CMakeLists.txt @@ -2,5 +2,9 @@ # CMakeLists in this exact order for cmake to work correctly cmake_minimum_required(VERSION 3.5) +# (Not part of the boilerplate) +# This example uses an extra component for common functions such as Wi-Fi and Ethernet connection. +set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common) + include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(tcp_server) diff --git a/examples/protocols/sockets/tcp_server/Makefile b/examples/protocols/sockets/tcp_server/Makefile index e965b909..ccdc6d54 100644 --- a/examples/protocols/sockets/tcp_server/Makefile +++ b/examples/protocols/sockets/tcp_server/Makefile @@ -5,5 +5,7 @@ PROJECT_NAME := tcp_server +EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/protocol_examples_common + include $(IDF_PATH)/make/project.mk diff --git a/examples/protocols/sockets/tcp_server/main/Kconfig.projbuild b/examples/protocols/sockets/tcp_server/main/Kconfig.projbuild index c1209ac8..8d1d772c 100644 --- a/examples/protocols/sockets/tcp_server/main/Kconfig.projbuild +++ b/examples/protocols/sockets/tcp_server/main/Kconfig.projbuild @@ -1,18 +1,5 @@ menu "Example Configuration" -config WIFI_SSID - string "WiFi SSID" - default "myssid" - help - SSID (network name) for the example to connect to. - -config WIFI_PASSWORD - string "WiFi Password" - default "mypassword" - help - WiFi password (WPA or WPA2) for the example to use. - Can be left blank if the network has no security set. - choice EXAMPLE_IP_MODE prompt "IP Version" help @@ -23,7 +10,7 @@ config EXAMPLE_IPV4 config EXAMPLE_IPV6 bool "IPV6" - select LWIP_IPV6 + select EXAMPLE_CONNECT_IPV6 endchoice diff --git a/examples/protocols/sockets/tcp_server/main/tcp_server.c b/examples/protocols/sockets/tcp_server/main/tcp_server.c index d7c6472a..4b1c2d93 100644 --- a/examples/protocols/sockets/tcp_server/main/tcp_server.c +++ b/examples/protocols/sockets/tcp_server/main/tcp_server.c @@ -8,13 +8,15 @@ */ #include #include + #include "freertos/FreeRTOS.h" #include "freertos/task.h" -#include "freertos/event_groups.h" #include "esp_system.h" -#include "esp_wifi.h" -#include "esp_event_loop.h" #include "esp_log.h" +#include "esp_netif.h" +#include "esp_event.h" +#include "protocol_examples_common.h" +#include "nvs.h" #include "nvs_flash.h" #include "lwip/err.h" @@ -22,106 +24,10 @@ #include "lwip/sys.h" #include - -/* The examples use simple WiFi configuration that you can set via - 'make menuconfig'. - If you'd rather not, just change the below entries to strings with - the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid" -*/ -#define EXAMPLE_WIFI_SSID CONFIG_WIFI_SSID -#define EXAMPLE_WIFI_PASS CONFIG_WIFI_PASSWORD - #define PORT CONFIG_EXAMPLE_PORT -/* FreeRTOS event group to signal when we are connected & ready to make a request */ -static EventGroupHandle_t wifi_event_group; - -const int IPV4_GOTIP_BIT = BIT0; -#ifdef CONFIG_EXAMPLE_IPV6 -const int IPV6_GOTIP_BIT = BIT1; -#endif - static const char *TAG = "example"; -static esp_err_t event_handler(void *ctx, system_event_t *event) -{ - /* For accessing reason codes in case of disconnection */ - system_event_info_t *info = &event->event_info; - - switch (event->event_id) { - case SYSTEM_EVENT_STA_START: - esp_wifi_connect(); - ESP_LOGI(TAG, "SYSTEM_EVENT_STA_START"); - break; - case SYSTEM_EVENT_STA_CONNECTED: -#ifdef CONFIG_EXAMPLE_IPV6 - /* enable ipv6 */ - tcpip_adapter_create_ip6_linklocal(TCPIP_ADAPTER_IF_STA); -#endif - break; - case SYSTEM_EVENT_STA_GOT_IP: - xEventGroupSetBits(wifi_event_group, IPV4_GOTIP_BIT); - ESP_LOGI(TAG, "SYSTEM_EVENT_STA_GOT_IP"); - break; - case SYSTEM_EVENT_STA_DISCONNECTED: - ESP_LOGE(TAG, "Disconnect reason : %d", info->disconnected.reason); - if (info->disconnected.reason == WIFI_REASON_BASIC_RATE_NOT_SUPPORT) { - /*Switch to 802.11 bgn mode */ - esp_wifi_set_protocol(ESP_IF_WIFI_STA, WIFI_PROTOCAL_11B | WIFI_PROTOCAL_11G | WIFI_PROTOCAL_11N); - } - esp_wifi_connect(); - xEventGroupClearBits(wifi_event_group, IPV4_GOTIP_BIT); -#ifdef CONFIG_EXAMPLE_IPV6 - xEventGroupClearBits(wifi_event_group, IPV6_GOTIP_BIT); -#endif - break; - case SYSTEM_EVENT_AP_STA_GOT_IP6: -#ifdef CONFIG_EXAMPLE_IPV6 - xEventGroupSetBits(wifi_event_group, IPV6_GOTIP_BIT); - ESP_LOGI(TAG, "SYSTEM_EVENT_STA_GOT_IP6"); - - char *ip6 = ip6addr_ntoa(&event->event_info.got_ip6.ip6_info.ip); - ESP_LOGI(TAG, "IPv6: %s", ip6); -#endif - 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_storage(WIFI_STORAGE_RAM) ); - wifi_config_t wifi_config = { - .sta = { - .ssid = EXAMPLE_WIFI_SSID, - .password = EXAMPLE_WIFI_PASS, - }, - }; - ESP_LOGI(TAG, "Setting WiFi configuration SSID %s...", wifi_config.sta.ssid); - ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) ); - ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) ); - ESP_ERROR_CHECK( esp_wifi_start() ); -} - -static void wait_for_ip() -{ -#ifdef CONFIG_EXAMPLE_IPV6 - uint32_t bits = IPV4_GOTIP_BIT | IPV6_GOTIP_BIT; -#else - uint32_t bits = IPV4_GOTIP_BIT; -#endif - - ESP_LOGI(TAG, "Waiting for AP connection..."); - xEventGroupWaitBits(wifi_event_group, bits, false, true, portMAX_DELAY); - ESP_LOGI(TAG, "Connected to AP"); -} - static void tcp_server_task(void *pvParameters) { char rx_buffer[128]; @@ -231,9 +137,11 @@ static void tcp_server_task(void *pvParameters) void app_main() { - ESP_ERROR_CHECK( nvs_flash_init() ); - initialise_wifi(); - wait_for_ip(); + ESP_ERROR_CHECK(nvs_flash_init()); + ESP_ERROR_CHECK(esp_netif_init()); + ESP_ERROR_CHECK(esp_event_loop_create_default()); + + ESP_ERROR_CHECK(example_connect()); xTaskCreate(tcp_server_task, "tcp_server", 4096, NULL, 5, NULL); } diff --git a/examples/protocols/sockets/udp_client/CMakeLists.txt b/examples/protocols/sockets/udp_client/CMakeLists.txt index ee1d3f53..2049b819 100644 --- a/examples/protocols/sockets/udp_client/CMakeLists.txt +++ b/examples/protocols/sockets/udp_client/CMakeLists.txt @@ -2,5 +2,9 @@ # CMakeLists in this exact order for cmake to work correctly cmake_minimum_required(VERSION 3.5) +# (Not part of the boilerplate) +# This example uses an extra component for common functions such as Wi-Fi and Ethernet connection. +set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common) + include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(udp_client) diff --git a/examples/protocols/sockets/udp_client/Makefile b/examples/protocols/sockets/udp_client/Makefile index 34849eee..604aa5af 100644 --- a/examples/protocols/sockets/udp_client/Makefile +++ b/examples/protocols/sockets/udp_client/Makefile @@ -5,5 +5,7 @@ PROJECT_NAME := udp_client +EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/protocol_examples_common + include $(IDF_PATH)/make/project.mk diff --git a/examples/protocols/sockets/udp_client/main/Kconfig.projbuild b/examples/protocols/sockets/udp_client/main/Kconfig.projbuild index 7dfe1ddc..240af89a 100644 --- a/examples/protocols/sockets/udp_client/main/Kconfig.projbuild +++ b/examples/protocols/sockets/udp_client/main/Kconfig.projbuild @@ -1,18 +1,5 @@ menu "Example Configuration" -config WIFI_SSID - string "WiFi SSID" - default "myssid" - help - SSID (network name) for the example to connect to. - -config WIFI_PASSWORD - string "WiFi Password" - default "mypassword" - help - WiFi password (WPA or WPA2) for the example to use. - Can be left blank if the network has no security set. - choice EXAMPLE_IP_MODE prompt "IP Version" help @@ -23,7 +10,7 @@ config EXAMPLE_IPV4 config EXAMPLE_IPV6 bool "IPV6" - select LWIP_IPV6 + select EXAMPLE_CONNECT_IPV6 endchoice diff --git a/examples/protocols/sockets/udp_client/main/udp_client.c b/examples/protocols/sockets/udp_client/main/udp_client.c index 79f9aaa5..caae111e 100644 --- a/examples/protocols/sockets/udp_client/main/udp_client.c +++ b/examples/protocols/sockets/udp_client/main/udp_client.c @@ -8,13 +8,15 @@ */ #include #include + #include "freertos/FreeRTOS.h" #include "freertos/task.h" -#include "freertos/event_groups.h" #include "esp_system.h" -#include "esp_wifi.h" -#include "esp_event_loop.h" #include "esp_log.h" +#include "esp_netif.h" +#include "esp_event.h" +#include "protocol_examples_common.h" +#include "nvs.h" #include "nvs_flash.h" #include "lwip/err.h" @@ -22,15 +24,6 @@ #include "lwip/sys.h" #include - -/* The examples use simple WiFi configuration that you can set via - 'make menuconfig'. - If you'd rather not, just change the below entries to strings with - the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid" -*/ -#define EXAMPLE_WIFI_SSID CONFIG_WIFI_SSID -#define EXAMPLE_WIFI_PASS CONFIG_WIFI_PASSWORD - #ifdef CONFIG_EXAMPLE_IPV4 #define HOST_IP_ADDR CONFIG_EXAMPLE_IPV4_ADDR #else @@ -39,96 +32,9 @@ #define PORT CONFIG_EXAMPLE_PORT -/* FreeRTOS event group to signal when we are connected & ready to make a request */ -static EventGroupHandle_t wifi_event_group; - -const int IPV4_GOTIP_BIT = BIT0; -#ifdef CONFIG_EXAMPLE_IPV6 -const int IPV6_GOTIP_BIT = BIT1; -#endif - static const char *TAG = "example"; static const char *payload = "Message from ESP32 "; -static esp_err_t event_handler(void *ctx, system_event_t *event) -{ - /* For accessing reason codes in case of disconnection */ - system_event_info_t *info = &event->event_info; - - switch (event->event_id) { - case SYSTEM_EVENT_STA_START: - esp_wifi_connect(); - ESP_LOGI(TAG, "SYSTEM_EVENT_STA_START"); - break; - case SYSTEM_EVENT_STA_CONNECTED: -#ifdef CONFIG_EXAMPLE_IPV6 - /* enable ipv6 */ - tcpip_adapter_create_ip6_linklocal(TCPIP_ADAPTER_IF_STA); -#endif - break; - case SYSTEM_EVENT_STA_GOT_IP: - xEventGroupSetBits(wifi_event_group, IPV4_GOTIP_BIT); - ESP_LOGI(TAG, "SYSTEM_EVENT_STA_GOT_IP"); - break; - case SYSTEM_EVENT_STA_DISCONNECTED: - ESP_LOGE(TAG, "Disconnect reason : %d", info->disconnected.reason); - if (info->disconnected.reason == WIFI_REASON_BASIC_RATE_NOT_SUPPORT) { - /*Switch to 802.11 bgn mode */ - esp_wifi_set_protocol(ESP_IF_WIFI_STA, WIFI_PROTOCAL_11B | WIFI_PROTOCAL_11G | WIFI_PROTOCAL_11N); - } - esp_wifi_connect(); - xEventGroupClearBits(wifi_event_group, IPV4_GOTIP_BIT); -#ifdef CONFIG_EXAMPLE_IPV6 - xEventGroupClearBits(wifi_event_group, IPV6_GOTIP_BIT); -#endif - break; - case SYSTEM_EVENT_AP_STA_GOT_IP6: -#ifdef CONFIG_EXAMPLE_IPV6 - xEventGroupSetBits(wifi_event_group, IPV6_GOTIP_BIT); - ESP_LOGI(TAG, "SYSTEM_EVENT_STA_GOT_IP6"); - - char *ip6 = ip6addr_ntoa(&event->event_info.got_ip6.ip6_info.ip); - ESP_LOGI(TAG, "IPv6: %s", ip6); -#endif - 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_storage(WIFI_STORAGE_RAM) ); - wifi_config_t wifi_config = { - .sta = { - .ssid = EXAMPLE_WIFI_SSID, - .password = EXAMPLE_WIFI_PASS, - }, - }; - ESP_LOGI(TAG, "Setting WiFi configuration SSID %s...", wifi_config.sta.ssid); - ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) ); - ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) ); - ESP_ERROR_CHECK( esp_wifi_start() ); -} - -static void wait_for_ip() -{ -#ifdef CONFIG_EXAMPLE_IPV6 - uint32_t bits = IPV4_GOTIP_BIT | IPV6_GOTIP_BIT; -#else - uint32_t bits = IPV4_GOTIP_BIT; -#endif - - ESP_LOGI(TAG, "Waiting for AP connection..."); - xEventGroupWaitBits(wifi_event_group, bits, false, true, portMAX_DELAY); - ESP_LOGI(TAG, "Connected to AP"); -} - static void udp_client_task(void *pvParameters) { char rx_buffer[128]; @@ -202,9 +108,11 @@ static void udp_client_task(void *pvParameters) void app_main() { - ESP_ERROR_CHECK( nvs_flash_init() ); - initialise_wifi(); - wait_for_ip(); + ESP_ERROR_CHECK(nvs_flash_init()); + ESP_ERROR_CHECK(esp_netif_init()); + ESP_ERROR_CHECK(esp_event_loop_create_default()); + + ESP_ERROR_CHECK(example_connect()); xTaskCreate(udp_client_task, "udp_client", 4096, NULL, 5, NULL); } diff --git a/examples/protocols/sockets/udp_multicast/CMakeLists.txt b/examples/protocols/sockets/udp_multicast/CMakeLists.txt index fe4b8fe8..f89b3347 100644 --- a/examples/protocols/sockets/udp_multicast/CMakeLists.txt +++ b/examples/protocols/sockets/udp_multicast/CMakeLists.txt @@ -2,5 +2,9 @@ # in this exact order for cmake to work correctly cmake_minimum_required(VERSION 3.5) +# (Not part of the boilerplate) +# This example uses an extra component for common functions such as Wi-Fi and Ethernet connection. +set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common) + include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(udp-multicast) diff --git a/examples/protocols/sockets/udp_multicast/Makefile b/examples/protocols/sockets/udp_multicast/Makefile index 60f3d59d..47e26b95 100644 --- a/examples/protocols/sockets/udp_multicast/Makefile +++ b/examples/protocols/sockets/udp_multicast/Makefile @@ -5,5 +5,7 @@ PROJECT_NAME := udp-multicast +EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/protocol_examples_common + include $(IDF_PATH)/make/project.mk diff --git a/examples/protocols/sockets/udp_multicast/main/Kconfig.projbuild b/examples/protocols/sockets/udp_multicast/main/Kconfig.projbuild index 4f0d46a3..ea9db39f 100644 --- a/examples/protocols/sockets/udp_multicast/main/Kconfig.projbuild +++ b/examples/protocols/sockets/udp_multicast/main/Kconfig.projbuild @@ -1,19 +1,5 @@ menu "Example Configuration" -config WIFI_SSID - string "WiFi SSID" - default "myssid" - help - SSID (network name) for the example to connect to. - -config WIFI_PASSWORD - string "WiFi Password" - default "mypassword" - help - WiFi password (WPA or WPA2) for the example to use. - - Can be left blank if the network has no security set. - choice EXAMPLE_IP_MODE prompt "Multicast IP type" help @@ -38,7 +24,7 @@ config EXAMPLE_IPV4 bool config EXAMPLE_IPV6 bool - select LWIP_IPV6 + select EXAMPLE_CONNECT_IPV6 config EXAMPLE_MULTICAST_IPV4_ADDR string "Multicast IPV4 Address (send & receive)" diff --git a/examples/protocols/sockets/udp_multicast/main/udp_multicast_example_main.c b/examples/protocols/sockets/udp_multicast/main/udp_multicast_example_main.c index 5dea3f6a..fe5db370 100644 --- a/examples/protocols/sockets/udp_multicast/main/udp_multicast_example_main.c +++ b/examples/protocols/sockets/udp_multicast/main/udp_multicast_example_main.c @@ -8,13 +8,15 @@ */ #include #include + #include "freertos/FreeRTOS.h" #include "freertos/task.h" -#include "freertos/event_groups.h" #include "esp_system.h" -#include "esp_wifi.h" -#include "esp_event_loop.h" #include "esp_log.h" +#include "esp_netif.h" +#include "esp_event.h" +#include "protocol_examples_common.h" +#include "nvs.h" #include "nvs_flash.h" #include "lwip/err.h" @@ -22,15 +24,6 @@ #include "lwip/sys.h" #include -/* The examples use simple WiFi configuration that you can set via - 'make menuconfig'. - - If you'd rather not, just change the below entries to strings with - the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid" -*/ -#define EXAMPLE_WIFI_SSID CONFIG_WIFI_SSID -#define EXAMPLE_WIFI_PASS CONFIG_WIFI_PASSWORD - #define UDP_PORT CONFIG_EXAMPLE_PORT #define MULTICAST_LOOPBACK CONFIG_EXAMPLE_LOOPBACK @@ -42,15 +35,6 @@ #define LISTEN_DEFAULT_IF CONFIG_EXAMPLE_LISTEN_DEFAULT_IF -/* 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, - we use two - one for IPv4 "got ip", and - one for IPv6 "got ip". */ -const int IPV4_GOTIP_BIT = BIT0; -const int IPV6_GOTIP_BIT = BIT1; - static const char *TAG = "multicast"; #ifdef CONFIG_EXAMPLE_IPV4 static const char *V4TAG = "mcast-ipv4"; @@ -59,60 +43,6 @@ static const char *V4TAG = "mcast-ipv4"; static const char *V6TAG = "mcast-ipv6"; #endif -static esp_err_t event_handler(void *ctx, system_event_t *event) -{ - /* For accessing reason codes in case of disconnection */ - system_event_info_t *info = &event->event_info; - - switch(event->event_id) { - case SYSTEM_EVENT_STA_START: - esp_wifi_connect(); - break; - case SYSTEM_EVENT_STA_CONNECTED: - /* enable ipv6 */ - tcpip_adapter_create_ip6_linklocal(TCPIP_ADAPTER_IF_STA); - break; - case SYSTEM_EVENT_STA_GOT_IP: - xEventGroupSetBits(wifi_event_group, IPV4_GOTIP_BIT); - break; - case SYSTEM_EVENT_STA_DISCONNECTED: - ESP_LOGE(TAG, "Disconnect reason : %d", info->disconnected.reason); - if (info->disconnected.reason == WIFI_REASON_BASIC_RATE_NOT_SUPPORT) { - /*Switch to 802.11 bgn mode */ - esp_wifi_set_protocol(ESP_IF_WIFI_STA, WIFI_PROTOCAL_11B | WIFI_PROTOCAL_11G | WIFI_PROTOCAL_11N); - } - esp_wifi_connect(); - xEventGroupClearBits(wifi_event_group, IPV4_GOTIP_BIT); - xEventGroupClearBits(wifi_event_group, IPV6_GOTIP_BIT); - break; - case SYSTEM_EVENT_AP_STA_GOT_IP6: - xEventGroupSetBits(wifi_event_group, IPV6_GOTIP_BIT); - 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_storage(WIFI_STORAGE_RAM) ); - wifi_config_t wifi_config = { - .sta = { - .ssid = EXAMPLE_WIFI_SSID, - .password = EXAMPLE_WIFI_PASS, - }, - }; - ESP_LOGI(TAG, "Setting WiFi configuration SSID %s...", wifi_config.sta.ssid); - ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) ); - ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) ); - ESP_ERROR_CHECK( esp_wifi_start() ); -} - #ifdef CONFIG_EXAMPLE_IPV4 /* Add a socket, either IPV4-only or IPV6 dual mode, to the IPV4 multicast group */ @@ -362,19 +292,6 @@ err: static void mcast_example_task(void *pvParameters) { while (1) { - /* Wait for all the IPs we care about to be set - */ - uint32_t bits = 0; -#ifdef CONFIG_EXAMPLE_IPV4 - bits |= IPV4_GOTIP_BIT; -#endif -#ifdef CONFIG_EXAMPLE_IPV6 - bits |= IPV6_GOTIP_BIT; -#endif - ESP_LOGI(TAG, "Waiting for AP connection..."); - xEventGroupWaitBits(wifi_event_group, bits, false, true, portMAX_DELAY); - ESP_LOGI(TAG, "Connected to AP"); - int sock; #ifdef CONFIG_EXAMPLE_IPV4_ONLY @@ -553,7 +470,11 @@ static void mcast_example_task(void *pvParameters) void app_main() { - ESP_ERROR_CHECK( nvs_flash_init() ); - initialise_wifi(); + ESP_ERROR_CHECK(nvs_flash_init()); + ESP_ERROR_CHECK(esp_netif_init()); + ESP_ERROR_CHECK(esp_event_loop_create_default()); + + ESP_ERROR_CHECK(example_connect()); + xTaskCreate(&mcast_example_task, "mcast_task", 4096, NULL, 5, NULL); } diff --git a/examples/protocols/sockets/udp_server/CMakeLists.txt b/examples/protocols/sockets/udp_server/CMakeLists.txt index d1edab0f..86143180 100644 --- a/examples/protocols/sockets/udp_server/CMakeLists.txt +++ b/examples/protocols/sockets/udp_server/CMakeLists.txt @@ -2,5 +2,9 @@ # CMakeLists in this exact order for cmake to work correctly cmake_minimum_required(VERSION 3.5) +# (Not part of the boilerplate) +# This example uses an extra component for common functions such as Wi-Fi and Ethernet connection. +set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common) + include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(udp_server) diff --git a/examples/protocols/sockets/udp_server/Makefile b/examples/protocols/sockets/udp_server/Makefile index 36552c9b..bc11a430 100644 --- a/examples/protocols/sockets/udp_server/Makefile +++ b/examples/protocols/sockets/udp_server/Makefile @@ -5,5 +5,7 @@ PROJECT_NAME := udp_server +EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/protocol_examples_common + include $(IDF_PATH)/make/project.mk diff --git a/examples/protocols/sockets/udp_server/main/Kconfig.projbuild b/examples/protocols/sockets/udp_server/main/Kconfig.projbuild index c1209ac8..8d1d772c 100644 --- a/examples/protocols/sockets/udp_server/main/Kconfig.projbuild +++ b/examples/protocols/sockets/udp_server/main/Kconfig.projbuild @@ -1,18 +1,5 @@ menu "Example Configuration" -config WIFI_SSID - string "WiFi SSID" - default "myssid" - help - SSID (network name) for the example to connect to. - -config WIFI_PASSWORD - string "WiFi Password" - default "mypassword" - help - WiFi password (WPA or WPA2) for the example to use. - Can be left blank if the network has no security set. - choice EXAMPLE_IP_MODE prompt "IP Version" help @@ -23,7 +10,7 @@ config EXAMPLE_IPV4 config EXAMPLE_IPV6 bool "IPV6" - select LWIP_IPV6 + select EXAMPLE_CONNECT_IPV6 endchoice diff --git a/examples/protocols/sockets/udp_server/main/udp_server.c b/examples/protocols/sockets/udp_server/main/udp_server.c index 2e81d0c5..07af193c 100644 --- a/examples/protocols/sockets/udp_server/main/udp_server.c +++ b/examples/protocols/sockets/udp_server/main/udp_server.c @@ -8,13 +8,15 @@ */ #include #include + #include "freertos/FreeRTOS.h" #include "freertos/task.h" -#include "freertos/event_groups.h" #include "esp_system.h" -#include "esp_wifi.h" -#include "esp_event_loop.h" #include "esp_log.h" +#include "esp_netif.h" +#include "esp_event.h" +#include "protocol_examples_common.h" +#include "nvs.h" #include "nvs_flash.h" #include "lwip/err.h" @@ -22,106 +24,10 @@ #include "lwip/sys.h" #include - -/* The examples use simple WiFi configuration that you can set via - 'make menuconfig'. - If you'd rather not, just change the below entries to strings with - the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid" -*/ -#define EXAMPLE_WIFI_SSID CONFIG_WIFI_SSID -#define EXAMPLE_WIFI_PASS CONFIG_WIFI_PASSWORD - #define PORT CONFIG_EXAMPLE_PORT -/* FreeRTOS event group to signal when we are connected & ready to make a request */ -static EventGroupHandle_t wifi_event_group; - -const int IPV4_GOTIP_BIT = BIT0; -#ifdef CONFIG_EXAMPLE_IPV6 -const int IPV6_GOTIP_BIT = BIT1; -#endif - static const char *TAG = "example"; -static esp_err_t event_handler(void *ctx, system_event_t *event) -{ - /* For accessing reason codes in case of disconnection */ - system_event_info_t *info = &event->event_info; - - switch (event->event_id) { - case SYSTEM_EVENT_STA_START: - esp_wifi_connect(); - ESP_LOGI(TAG, "SYSTEM_EVENT_STA_START"); - break; - case SYSTEM_EVENT_STA_CONNECTED: -#ifdef CONFIG_EXAMPLE_IPV6 - /* enable ipv6 */ - tcpip_adapter_create_ip6_linklocal(TCPIP_ADAPTER_IF_STA); -#endif - break; - case SYSTEM_EVENT_STA_GOT_IP: - xEventGroupSetBits(wifi_event_group, IPV4_GOTIP_BIT); - ESP_LOGI(TAG, "SYSTEM_EVENT_STA_GOT_IP"); - break; - case SYSTEM_EVENT_STA_DISCONNECTED: - ESP_LOGE(TAG, "Disconnect reason : %d", info->disconnected.reason); - if (info->disconnected.reason == WIFI_REASON_BASIC_RATE_NOT_SUPPORT) { - /*Switch to 802.11 bgn mode */ - esp_wifi_set_protocol(ESP_IF_WIFI_STA, WIFI_PROTOCAL_11B | WIFI_PROTOCAL_11G | WIFI_PROTOCAL_11N); - } - esp_wifi_connect(); - xEventGroupClearBits(wifi_event_group, IPV4_GOTIP_BIT); -#ifdef CONFIG_EXAMPLE_IPV6 - xEventGroupClearBits(wifi_event_group, IPV6_GOTIP_BIT); -#endif - break; - case SYSTEM_EVENT_AP_STA_GOT_IP6: -#ifdef CONFIG_EXAMPLE_IPV6 - xEventGroupSetBits(wifi_event_group, IPV6_GOTIP_BIT); - ESP_LOGI(TAG, "SYSTEM_EVENT_STA_GOT_IP6"); - - char *ip6 = ip6addr_ntoa(&event->event_info.got_ip6.ip6_info.ip); - ESP_LOGI(TAG, "IPv6: %s", ip6); -#endif - 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_storage(WIFI_STORAGE_RAM) ); - wifi_config_t wifi_config = { - .sta = { - .ssid = EXAMPLE_WIFI_SSID, - .password = EXAMPLE_WIFI_PASS, - }, - }; - ESP_LOGI(TAG, "Setting WiFi configuration SSID %s...", wifi_config.sta.ssid); - ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) ); - ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) ); - ESP_ERROR_CHECK( esp_wifi_start() ); -} - -static void wait_for_ip() -{ -#ifdef CONFIG_EXAMPLE_IPV6 - uint32_t bits = IPV4_GOTIP_BIT | IPV6_GOTIP_BIT; -#else - uint32_t bits = IPV4_GOTIP_BIT; -#endif - - ESP_LOGI(TAG, "Waiting for AP connection..."); - xEventGroupWaitBits(wifi_event_group, bits, false, true, portMAX_DELAY); - ESP_LOGI(TAG, "Connected to AP"); -} - static void udp_server_task(void *pvParameters) { char rx_buffer[128]; @@ -214,9 +120,11 @@ static void udp_server_task(void *pvParameters) void app_main() { - ESP_ERROR_CHECK( nvs_flash_init() ); - initialise_wifi(); - wait_for_ip(); + ESP_ERROR_CHECK(nvs_flash_init()); + ESP_ERROR_CHECK(esp_netif_init()); + ESP_ERROR_CHECK(esp_event_loop_create_default()); + + ESP_ERROR_CHECK(example_connect()); xTaskCreate(udp_server_task, "udp_server", 4096, NULL, 5, NULL); } diff --git a/examples/provisioning/custom_config/CMakeLists.txt b/examples/provisioning/custom_config/CMakeLists.txt index ec9b0ce6..52f2c76f 100644 --- a/examples/provisioning/custom_config/CMakeLists.txt +++ b/examples/provisioning/custom_config/CMakeLists.txt @@ -2,5 +2,9 @@ # in this exact order for cmake to work correctly cmake_minimum_required(VERSION 3.5) +# (Not part of the boilerplate) +# This example uses an extra component for common functions such as Wi-Fi and Ethernet connection. +set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common) + include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(custom_config) diff --git a/examples/provisioning/custom_config/Makefile b/examples/provisioning/custom_config/Makefile index 8ded51d7..2a74b778 100644 --- a/examples/provisioning/custom_config/Makefile +++ b/examples/provisioning/custom_config/Makefile @@ -5,5 +5,7 @@ PROJECT_NAME := custom_config +EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/protocol_examples_common + include $(IDF_PATH)/make/project.mk diff --git a/examples/provisioning/custom_config/main/Kconfig.projbuild b/examples/provisioning/custom_config/main/Kconfig.projbuild index 866227bb..33f635e0 100644 --- a/examples/provisioning/custom_config/main/Kconfig.projbuild +++ b/examples/provisioning/custom_config/main/Kconfig.projbuild @@ -49,4 +49,10 @@ config RESET_PROVISIONED This erases the NVS to reset provisioned status of the device on every reboot. Provisioned status is determined by the WiFi STA configuration, saved on the NVS. +config EXAMPLE_AP_RECONN_ATTEMPTS + int "Maximum AP connection attempts" + default 5 + help + Set the maximum connection attempts to perform when connecting to a Wi-Fi AP. + endmenu diff --git a/examples/provisioning/custom_config/main/app_main.c b/examples/provisioning/custom_config/main/app_main.c index 17bb29ae..63a8cd44 100644 --- a/examples/provisioning/custom_config/main/app_main.c +++ b/examples/provisioning/custom_config/main/app_main.c @@ -8,13 +8,17 @@ */ #include -#include -#include -#include -#include -#include -#include -#include + +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "esp_system.h" +#include "esp_wifi.h" +#include "esp_log.h" +#include "esp_netif.h" +#include "esp_event.h" +#include "protocol_examples_common.h" +#include "nvs.h" +#include "nvs_flash.h" #include #include @@ -23,48 +27,36 @@ static const char *TAG = "app"; -static esp_err_t event_handler(void *ctx, system_event_t *event) -{ - /* Invoke Provisioning event handler first */ - app_prov_event_handler(ctx, event); +#define EXAMPLE_AP_RECONN_ATTEMPTS CONFIG_EXAMPLE_AP_RECONN_ATTEMPTS - switch(event->event_id) { - case SYSTEM_EVENT_AP_START: - ESP_LOGI(TAG, "SoftAP started"); - break; - case SYSTEM_EVENT_AP_STOP: - ESP_LOGI(TAG, "SoftAP stopped"); - break; - case SYSTEM_EVENT_STA_START: +static void event_handler(void* arg, esp_event_base_t event_base, + int event_id, void* event_data) +{ + static int s_retry_num = 0; + + if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) { esp_wifi_connect(); - break; - case SYSTEM_EVENT_STA_GOT_IP: + } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) { + if (s_retry_num < EXAMPLE_AP_RECONN_ATTEMPTS) { + esp_wifi_connect(); + s_retry_num++; + ESP_LOGI(TAG, "retry to connect to the AP"); + } + ESP_LOGI(TAG,"connect to the AP fail"); + } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) { + ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data; ESP_LOGI(TAG, "got ip:%s", - ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip)); - break; - case SYSTEM_EVENT_AP_STACONNECTED: - ESP_LOGI(TAG, "station:"MACSTR" join, AID=%d", - MAC2STR(event->event_info.sta_connected.mac), - event->event_info.sta_connected.aid); - break; - case SYSTEM_EVENT_AP_STADISCONNECTED: - ESP_LOGI(TAG, "station:"MACSTR"leave, AID=%d", - MAC2STR(event->event_info.sta_disconnected.mac), - event->event_info.sta_disconnected.aid); - break; - case SYSTEM_EVENT_STA_DISCONNECTED: - break; - default: - break; + ip4addr_ntoa(&event->ip_info.ip)); + s_retry_num = 0; } - return ESP_OK; } static void wifi_init_sta() { - /* Start wifi in station mode with credentials set during provisioning */ - wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); - ESP_ERROR_CHECK(esp_wifi_init(&cfg)); + /* Set our event handling */ + ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, event_handler, NULL)); + ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, event_handler, NULL)); + ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) ); ESP_ERROR_CHECK(esp_wifi_start() ); } @@ -92,8 +84,12 @@ void app_main() /* Initialize networking stack */ tcpip_adapter_init(); - /* Set our event handling */ - ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL)); + /* Create default event loop needed by the + * main app and the provisioning service */ + ESP_ERROR_CHECK(esp_event_loop_create_default()); + + /* Initialize NVS needed by Wi-Fi */ + ESP_ERROR_CHECK(nvs_flash_init()); /* Check if device is provisioned */ bool provisioned; diff --git a/examples/provisioning/softap_prov/CMakeLists.txt b/examples/provisioning/softap_prov/CMakeLists.txt index fa6ac143..95d0b77b 100644 --- a/examples/provisioning/softap_prov/CMakeLists.txt +++ b/examples/provisioning/softap_prov/CMakeLists.txt @@ -2,5 +2,9 @@ # in this exact order for cmake to work correctly cmake_minimum_required(VERSION 3.5) +# (Not part of the boilerplate) +# This example uses an extra component for common functions such as Wi-Fi and Ethernet connection. +set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common) + include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(softap_prov) diff --git a/examples/provisioning/softap_prov/Makefile b/examples/provisioning/softap_prov/Makefile index ab167ef2..cb6d6fb2 100644 --- a/examples/provisioning/softap_prov/Makefile +++ b/examples/provisioning/softap_prov/Makefile @@ -5,5 +5,7 @@ PROJECT_NAME := softap_prov +EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/protocol_examples_common + include $(IDF_PATH)/make/project.mk diff --git a/examples/provisioning/softap_prov/main/Kconfig.projbuild b/examples/provisioning/softap_prov/main/Kconfig.projbuild index 08cb2f97..4ec635a9 100644 --- a/examples/provisioning/softap_prov/main/Kconfig.projbuild +++ b/examples/provisioning/softap_prov/main/Kconfig.projbuild @@ -50,4 +50,10 @@ config RESET_PROVISIONED This erases the NVS to reset provisioned status of the device on every reboot. Provisioned status is determined by the WiFi STA configuration, saved on the NVS. +config EXAMPLE_AP_RECONN_ATTEMPTS + int "Maximum AP connection attempts" + default 5 + help + Set the maximum connection attempts to perform when connecting to a Wi-Fi AP. + endmenu diff --git a/examples/provisioning/softap_prov/main/app_main.c b/examples/provisioning/softap_prov/main/app_main.c index 65a100e4..c2eec9bd 100644 --- a/examples/provisioning/softap_prov/main/app_main.c +++ b/examples/provisioning/softap_prov/main/app_main.c @@ -23,48 +23,35 @@ static const char *TAG = "app"; -static esp_err_t event_handler(void *ctx, system_event_t *event) -{ - /* Invoke Provisioning event handler first */ - app_prov_event_handler(ctx, event); +#define EXAMPLE_AP_RECONN_ATTEMPTS CONFIG_EXAMPLE_AP_RECONN_ATTEMPTS - switch(event->event_id) { - case SYSTEM_EVENT_AP_START: - ESP_LOGI(TAG, "SoftAP started"); - break; - case SYSTEM_EVENT_AP_STOP: - ESP_LOGI(TAG, "SoftAP stopped"); - break; - case SYSTEM_EVENT_STA_START: +static void event_handler(void* arg, esp_event_base_t event_base, + int event_id, void* event_data) +{ + static int s_retry_num = 0; + + if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) { esp_wifi_connect(); - break; - case SYSTEM_EVENT_STA_GOT_IP: + } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) { + if (s_retry_num < EXAMPLE_AP_RECONN_ATTEMPTS) { + esp_wifi_connect(); + s_retry_num++; + ESP_LOGI(TAG, "retry to connect to the AP"); + } + ESP_LOGI(TAG,"connect to the AP fail"); + } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) { + ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data; ESP_LOGI(TAG, "got ip:%s", - ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip)); - break; - case SYSTEM_EVENT_AP_STACONNECTED: - ESP_LOGI(TAG, "station:"MACSTR" join, AID=%d", - MAC2STR(event->event_info.sta_connected.mac), - event->event_info.sta_connected.aid); - break; - case SYSTEM_EVENT_AP_STADISCONNECTED: - ESP_LOGI(TAG, "station:"MACSTR"leave, AID=%d", - MAC2STR(event->event_info.sta_disconnected.mac), - event->event_info.sta_disconnected.aid); - break; - case SYSTEM_EVENT_STA_DISCONNECTED: - break; - default: - break; + ip4addr_ntoa(&event->ip_info.ip)); + s_retry_num = 0; } - return ESP_OK; } static void wifi_init_sta() { - /* Start wifi in station mode with credentials set during provisioning */ - wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); - ESP_ERROR_CHECK(esp_wifi_init(&cfg)); + ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, event_handler, NULL)); + ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, event_handler, NULL)); + ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) ); ESP_ERROR_CHECK(esp_wifi_start() ); } @@ -92,8 +79,12 @@ void app_main() /* Initialize networking stack */ tcpip_adapter_init(); - /* Set our event handling */ - ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL)); + /* Create default event loop needed by the + * main app and the provisioning service */ + ESP_ERROR_CHECK(esp_event_loop_create_default()); + + /* Initialize NVS needed by Wi-Fi */ + ESP_ERROR_CHECK(nvs_flash_init()); /* Check if device is provisioned */ bool provisioned; diff --git a/examples/system/console/main/console_example_main.c b/examples/system/console/main/console_example_main.c index 73d164ae..41c9239b 100644 --- a/examples/system/console/main/console_example_main.c +++ b/examples/system/console/main/console_example_main.c @@ -47,7 +47,7 @@ static void initialize_console() * correct while APB frequency is changing in light sleep mode. */ uart_config_t uart_config = { - .baud_rate = CONFIG_CONSOLE_UART_BAUDRATE, + .baud_rate = CONFIG_ESP_CONSOLE_UART_BAUDRATE, .data_bits = UART_DATA_8_BITS, .parity = UART_PARITY_DISABLE, .stop_bits = UART_STOP_BITS_1, diff --git a/examples/system/factory-test/main/main.c b/examples/system/factory-test/main/main.c index 2ac582a5..4f822e8c 100644 --- a/examples/system/factory-test/main/main.c +++ b/examples/system/factory-test/main/main.c @@ -22,10 +22,13 @@ #include "driver/uart.h" #include "nano_console.h" #include "esp_libc.h" +#include "esp_task.h" #ifndef ESP_FACTORY_TEST_EXTRA_COMPONENTS +#ifndef CONFIG_ESP_CONSOLE_UART_NUM #define CONFIG_ESP_CONSOLE_UART_NUM 0 +#endif #define TAG "factory-test" @@ -43,7 +46,7 @@ static void initialize_console() * correct while APB frequency is changing in light sleep mode. */ uart_config_t uart_config = { - .baud_rate = CONFIG_CONSOLE_UART_BAUDRATE, + .baud_rate = CONFIG_ESP_CONSOLE_UART_BAUDRATE, .data_bits = UART_DATA_8_BITS, .parity = UART_PARITY_DISABLE, .stop_bits = UART_STOP_BITS_1, diff --git a/examples/system/ota/native_ota/1MB_flash/new_to_new_no_old/CMakeLists.txt b/examples/system/ota/native_ota/1MB_flash/new_to_new_no_old/CMakeLists.txt index f736f959..09abd053 100644 --- a/examples/system/ota/native_ota/1MB_flash/new_to_new_no_old/CMakeLists.txt +++ b/examples/system/ota/native_ota/1MB_flash/new_to_new_no_old/CMakeLists.txt @@ -2,5 +2,9 @@ # in this exact order for cmake to work correctly cmake_minimum_required(VERSION 3.5) +# (Not part of the boilerplate) +# This example uses an extra component for common functions such as Wi-Fi and Ethernet connection. +set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common) + include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(native_ota) \ No newline at end of file diff --git a/examples/system/ota/native_ota/1MB_flash/new_to_new_no_old/Makefile b/examples/system/ota/native_ota/1MB_flash/new_to_new_no_old/Makefile index 7ffb1023..b276959c 100644 --- a/examples/system/ota/native_ota/1MB_flash/new_to_new_no_old/Makefile +++ b/examples/system/ota/native_ota/1MB_flash/new_to_new_no_old/Makefile @@ -5,5 +5,7 @@ PROJECT_NAME := ota +EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/protocol_examples_common + include $(IDF_PATH)/make/project.mk diff --git a/examples/system/ota/native_ota/1MB_flash/new_to_new_no_old/main/Kconfig.projbuild b/examples/system/ota/native_ota/1MB_flash/new_to_new_no_old/main/Kconfig.projbuild index 74a06d93..051f8917 100644 --- a/examples/system/ota/native_ota/1MB_flash/new_to_new_no_old/main/Kconfig.projbuild +++ b/examples/system/ota/native_ota/1MB_flash/new_to_new_no_old/main/Kconfig.projbuild @@ -1,19 +1,5 @@ menu "Example Configuration" -config WIFI_SSID - string "WiFi SSID" - default "myssid" - help - SSID (network name) for the example to connect to. - -config WIFI_PASSWORD - string "WiFi Password" - default "myssid" - help - WiFi password (WPA or WPA2) for the example to use. - - Can be left blank if the network has no security set. - config SERVER_IP string "HTTP Server IP" default "192.168.0.3" diff --git a/examples/system/ota/native_ota/1MB_flash/new_to_new_no_old/main/ota_example_main.c b/examples/system/ota/native_ota/1MB_flash/new_to_new_no_old/main/ota_example_main.c index 4f1838da..fa3fc46e 100644 --- a/examples/system/ota/native_ota/1MB_flash/new_to_new_no_old/main/ota_example_main.c +++ b/examples/system/ota/native_ota/1MB_flash/new_to_new_no_old/main/ota_example_main.c @@ -13,19 +13,15 @@ #include "freertos/FreeRTOS.h" #include "freertos/task.h" -#include "freertos/event_groups.h" - #include "esp_system.h" -#include "esp_wifi.h" -#include "esp_event_loop.h" #include "esp_log.h" -#include "esp_ota_ops.h" - +#include "esp_netif.h" +#include "esp_event.h" +#include "protocol_examples_common.h" #include "nvs.h" #include "nvs_flash.h" +#include "esp_ota_ops.h" -#define EXAMPLE_WIFI_SSID CONFIG_WIFI_SSID -#define EXAMPLE_WIFI_PASS CONFIG_WIFI_PASSWORD #define EXAMPLE_SERVER_IP CONFIG_SERVER_IP #define EXAMPLE_SERVER_PORT CONFIG_SERVER_PORT #define EXAMPLE_FILENAME CONFIG_EXAMPLE_FILENAME @@ -68,61 +64,6 @@ static int binary_file_length = 0; /*socket id*/ static int socket_id = -1; -/* 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? */ -const int CONNECTED_BIT = BIT0; - -static esp_err_t event_handler(void *ctx, system_event_t *event) -{ - /* For accessing reason codes in case of disconnection */ - system_event_info_t *info = &event->event_info; - - switch (event->event_id) { - case SYSTEM_EVENT_STA_START: - esp_wifi_connect(); - break; - case SYSTEM_EVENT_STA_GOT_IP: - xEventGroupSetBits(wifi_event_group, CONNECTED_BIT); - break; - case SYSTEM_EVENT_STA_DISCONNECTED: - ESP_LOGE(TAG, "Disconnect reason : %d", info->disconnected.reason); - if (info->disconnected.reason == WIFI_REASON_BASIC_RATE_NOT_SUPPORT) { - /*Switch to 802.11 bgn mode */ - esp_wifi_set_protocol(ESP_IF_WIFI_STA, WIFI_PROTOCAL_11B | WIFI_PROTOCAL_11G | WIFI_PROTOCAL_11N); - } - 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_storage(WIFI_STORAGE_RAM) ); - wifi_config_t wifi_config = { - .sta = { - .ssid = EXAMPLE_WIFI_SSID, - .password = EXAMPLE_WIFI_PASS, - }, - }; - ESP_LOGI(TAG, "Setting WiFi configuration SSID %s...", wifi_config.sta.ssid); - ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) ); - ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) ); - ESP_ERROR_CHECK( esp_wifi_start() ); -} - /*read buffer by byte still delim ,return read bytes counts*/ static int read_until(const char *buffer, char delim, int len) { @@ -339,13 +280,6 @@ static void ota_example_task(void *pvParameter) ESP_LOGI(TAG, "Running partition type %d subtype %d (offset 0x%08x)", running->type, running->subtype, running->address); - /* Wait for the callback to set the CONNECTED_BIT in the - event group. - */ - xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT, - false, true, portMAX_DELAY); - ESP_LOGI(TAG, "Connect to Wifi ! Start to Connect to Server...."); - /*connect to http server*/ if (connect_to_http_server()) { ESP_LOGI(TAG, "Connected to http server"); @@ -447,17 +381,11 @@ static void ota_example_task(void *pvParameter) void app_main() { - // Initialize NVS. - esp_err_t err = nvs_flash_init(); - if (err == ESP_ERR_NVS_NO_FREE_PAGES) { - // OTA app partition table has a smaller NVS partition size than the non-OTA - // partition table. This size mismatch may cause NVS initialization to fail. - // If this happens, we erase NVS partition and initialize NVS again. - ESP_ERROR_CHECK(nvs_flash_erase()); - err = nvs_flash_init(); - } - ESP_ERROR_CHECK( err ); + ESP_ERROR_CHECK(nvs_flash_init()); + ESP_ERROR_CHECK(esp_netif_init()); + ESP_ERROR_CHECK(esp_event_loop_create_default()); + + ESP_ERROR_CHECK(example_connect()); - initialise_wifi(); xTaskCreate(&ota_example_task, "ota_example_task", 8192, NULL, 5, NULL); } diff --git a/examples/system/ota/native_ota/1MB_flash/new_to_new_no_old_copy/CMakeLists.txt b/examples/system/ota/native_ota/1MB_flash/new_to_new_no_old_copy/CMakeLists.txt index f736f959..09abd053 100644 --- a/examples/system/ota/native_ota/1MB_flash/new_to_new_no_old_copy/CMakeLists.txt +++ b/examples/system/ota/native_ota/1MB_flash/new_to_new_no_old_copy/CMakeLists.txt @@ -2,5 +2,9 @@ # in this exact order for cmake to work correctly cmake_minimum_required(VERSION 3.5) +# (Not part of the boilerplate) +# This example uses an extra component for common functions such as Wi-Fi and Ethernet connection. +set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common) + include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(native_ota) \ No newline at end of file diff --git a/examples/system/ota/native_ota/1MB_flash/new_to_new_no_old_copy/Makefile b/examples/system/ota/native_ota/1MB_flash/new_to_new_no_old_copy/Makefile index 7ffb1023..b276959c 100644 --- a/examples/system/ota/native_ota/1MB_flash/new_to_new_no_old_copy/Makefile +++ b/examples/system/ota/native_ota/1MB_flash/new_to_new_no_old_copy/Makefile @@ -5,5 +5,7 @@ PROJECT_NAME := ota +EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/protocol_examples_common + include $(IDF_PATH)/make/project.mk diff --git a/examples/system/ota/native_ota/1MB_flash/new_to_new_no_old_copy/main/Kconfig.projbuild b/examples/system/ota/native_ota/1MB_flash/new_to_new_no_old_copy/main/Kconfig.projbuild index 74a06d93..051f8917 100644 --- a/examples/system/ota/native_ota/1MB_flash/new_to_new_no_old_copy/main/Kconfig.projbuild +++ b/examples/system/ota/native_ota/1MB_flash/new_to_new_no_old_copy/main/Kconfig.projbuild @@ -1,19 +1,5 @@ menu "Example Configuration" -config WIFI_SSID - string "WiFi SSID" - default "myssid" - help - SSID (network name) for the example to connect to. - -config WIFI_PASSWORD - string "WiFi Password" - default "myssid" - help - WiFi password (WPA or WPA2) for the example to use. - - Can be left blank if the network has no security set. - config SERVER_IP string "HTTP Server IP" default "192.168.0.3" diff --git a/examples/system/ota/native_ota/1MB_flash/new_to_new_no_old_copy/main/ota_example_main.c b/examples/system/ota/native_ota/1MB_flash/new_to_new_no_old_copy/main/ota_example_main.c index 4df5a494..c9054252 100644 --- a/examples/system/ota/native_ota/1MB_flash/new_to_new_no_old_copy/main/ota_example_main.c +++ b/examples/system/ota/native_ota/1MB_flash/new_to_new_no_old_copy/main/ota_example_main.c @@ -13,19 +13,15 @@ #include "freertos/FreeRTOS.h" #include "freertos/task.h" -#include "freertos/event_groups.h" - #include "esp_system.h" -#include "esp_wifi.h" -#include "esp_event_loop.h" #include "esp_log.h" -#include "esp_ota_ops.h" - +#include "esp_netif.h" +#include "esp_event.h" +#include "protocol_examples_common.h" #include "nvs.h" #include "nvs_flash.h" +#include "esp_ota_ops.h" -#define EXAMPLE_WIFI_SSID CONFIG_WIFI_SSID -#define EXAMPLE_WIFI_PASS CONFIG_WIFI_PASSWORD #define EXAMPLE_SERVER_IP CONFIG_SERVER_IP #define EXAMPLE_SERVER_PORT CONFIG_SERVER_PORT #define EXAMPLE_FILENAME CONFIG_EXAMPLE_FILENAME @@ -68,61 +64,6 @@ static int binary_file_length = 0; /*socket id*/ static int socket_id = -1; -/* 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? */ -const int CONNECTED_BIT = BIT0; - -static esp_err_t event_handler(void *ctx, system_event_t *event) -{ - /* For accessing reason codes in case of disconnection */ - system_event_info_t *info = &event->event_info; - - switch (event->event_id) { - case SYSTEM_EVENT_STA_START: - esp_wifi_connect(); - break; - case SYSTEM_EVENT_STA_GOT_IP: - xEventGroupSetBits(wifi_event_group, CONNECTED_BIT); - break; - case SYSTEM_EVENT_STA_DISCONNECTED: - ESP_LOGE(TAG, "Disconnect reason : %d", info->disconnected.reason); - if (info->disconnected.reason == WIFI_REASON_BASIC_RATE_NOT_SUPPORT) { - /*Switch to 802.11 bgn mode */ - esp_wifi_set_protocol(ESP_IF_WIFI_STA, WIFI_PROTOCAL_11B | WIFI_PROTOCAL_11G | WIFI_PROTOCAL_11N); - } - 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_storage(WIFI_STORAGE_RAM) ); - wifi_config_t wifi_config = { - .sta = { - .ssid = EXAMPLE_WIFI_SSID, - .password = EXAMPLE_WIFI_PASS, - }, - }; - ESP_LOGI(TAG, "Setting WiFi configuration SSID %s...", wifi_config.sta.ssid); - ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) ); - ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) ); - ESP_ERROR_CHECK( esp_wifi_start() ); -} - /*read buffer by byte still delim ,return read bytes counts*/ static int read_until(const char *buffer, char delim, int len) { @@ -339,13 +280,6 @@ static void ota_example_task(void *pvParameter) ESP_LOGI(TAG, "Running partition type %d subtype %d (offset 0x%08x)", running->type, running->subtype, running->address); - /* Wait for the callback to set the CONNECTED_BIT in the - event group. - */ - xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT, - false, true, portMAX_DELAY); - ESP_LOGI(TAG, "Connect to Wifi ! Start to Connect to Server...."); - /*connect to http server*/ if (connect_to_http_server()) { ESP_LOGI(TAG, "Connected to http server"); @@ -447,17 +381,11 @@ static void ota_example_task(void *pvParameter) void app_main() { - // Initialize NVS. - esp_err_t err = nvs_flash_init(); - if (err == ESP_ERR_NVS_NO_FREE_PAGES) { - // OTA app partition table has a smaller NVS partition size than the non-OTA - // partition table. This size mismatch may cause NVS initialization to fail. - // If this happens, we erase NVS partition and initialize NVS again. - ESP_ERROR_CHECK(nvs_flash_erase()); - err = nvs_flash_init(); - } - ESP_ERROR_CHECK( err ); + ESP_ERROR_CHECK(nvs_flash_init()); + ESP_ERROR_CHECK(esp_netif_init()); + ESP_ERROR_CHECK(esp_event_loop_create_default()); + + ESP_ERROR_CHECK(example_connect()); - initialise_wifi(); xTaskCreate(&ota_example_task, "ota_example_task", 8192, NULL, 5, NULL); } diff --git a/examples/system/ota/native_ota/1MB_flash/new_to_new_with_old/CMakeLists.txt b/examples/system/ota/native_ota/1MB_flash/new_to_new_with_old/CMakeLists.txt index f736f959..09abd053 100644 --- a/examples/system/ota/native_ota/1MB_flash/new_to_new_with_old/CMakeLists.txt +++ b/examples/system/ota/native_ota/1MB_flash/new_to_new_with_old/CMakeLists.txt @@ -2,5 +2,9 @@ # in this exact order for cmake to work correctly cmake_minimum_required(VERSION 3.5) +# (Not part of the boilerplate) +# This example uses an extra component for common functions such as Wi-Fi and Ethernet connection. +set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common) + include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(native_ota) \ No newline at end of file diff --git a/examples/system/ota/native_ota/1MB_flash/new_to_new_with_old/Makefile b/examples/system/ota/native_ota/1MB_flash/new_to_new_with_old/Makefile index 7ffb1023..b276959c 100644 --- a/examples/system/ota/native_ota/1MB_flash/new_to_new_with_old/Makefile +++ b/examples/system/ota/native_ota/1MB_flash/new_to_new_with_old/Makefile @@ -5,5 +5,7 @@ PROJECT_NAME := ota +EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/protocol_examples_common + include $(IDF_PATH)/make/project.mk diff --git a/examples/system/ota/native_ota/1MB_flash/new_to_new_with_old/main/Kconfig.projbuild b/examples/system/ota/native_ota/1MB_flash/new_to_new_with_old/main/Kconfig.projbuild index b24c7756..3388e54e 100644 --- a/examples/system/ota/native_ota/1MB_flash/new_to_new_with_old/main/Kconfig.projbuild +++ b/examples/system/ota/native_ota/1MB_flash/new_to_new_with_old/main/Kconfig.projbuild @@ -6,22 +6,6 @@ config CONNECT_ORIGINAL_AP help Connect to the original AP of old SDK -config WIFI_SSID - string "WiFi SSID" - default "myssid" - depends on !CONNECT_ORIGINAL_AP - help - SSID (network name) for the example to connect to. - -config WIFI_PASSWORD - string "WiFi Password" - default "myssid" - depends on !CONNECT_ORIGINAL_AP - help - WiFi password (WPA or WPA2) for the example to use. - - Can be left blank if the network has no security set. - config SERVER_IP string "HTTP Server IP" default "192.168.0.3" diff --git a/examples/system/ota/native_ota/1MB_flash/new_to_new_with_old/main/ota_example_main.c b/examples/system/ota/native_ota/1MB_flash/new_to_new_with_old/main/ota_example_main.c index 53bb569c..29fd3f63 100644 --- a/examples/system/ota/native_ota/1MB_flash/new_to_new_with_old/main/ota_example_main.c +++ b/examples/system/ota/native_ota/1MB_flash/new_to_new_with_old/main/ota_example_main.c @@ -11,24 +11,21 @@ #include #include -#include "freertos/FreeRTOS.h" -#include "freertos/task.h" -#include "freertos/event_groups.h" - +#include "sdkconfig.h" #ifdef CONFIG_CONNECT_ORIGINAL_AP #include "internal/esp_system_internal.h" #endif +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" #include "esp_system.h" -#include "esp_wifi.h" -#include "esp_event_loop.h" #include "esp_log.h" -#include "esp_ota_ops.h" - +#include "esp_netif.h" +#include "esp_event.h" +#include "protocol_examples_common.h" #include "nvs.h" #include "nvs_flash.h" +#include "esp_ota_ops.h" -#define EXAMPLE_WIFI_SSID CONFIG_WIFI_SSID -#define EXAMPLE_WIFI_PASS CONFIG_WIFI_PASSWORD #define EXAMPLE_SERVER_IP CONFIG_SERVER_IP #define EXAMPLE_SERVER_PORT CONFIG_SERVER_PORT #define EXAMPLE_FILENAME CONFIG_EXAMPLE_FILENAME @@ -71,48 +68,14 @@ static int binary_file_length = 0; /*socket id*/ static int socket_id = -1; -/* 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? */ -const int CONNECTED_BIT = BIT0; - -static esp_err_t event_handler(void *ctx, system_event_t *event) -{ - /* For accessing reason codes in case of disconnection */ - system_event_info_t *info = &event->event_info; - - switch (event->event_id) { - case SYSTEM_EVENT_STA_START: - esp_wifi_connect(); - break; - case SYSTEM_EVENT_STA_GOT_IP: - xEventGroupSetBits(wifi_event_group, CONNECTED_BIT); - break; - case SYSTEM_EVENT_STA_DISCONNECTED: - ESP_LOGE(TAG, "Disconnect reason : %d", info->disconnected.reason); - if (info->disconnected.reason == WIFI_REASON_BASIC_RATE_NOT_SUPPORT) { - /*Switch to 802.11 bgn mode */ - esp_wifi_set_protocol(ESP_IF_WIFI_STA, WIFI_PROTOCAL_11B | WIFI_PROTOCAL_11G | WIFI_PROTOCAL_11N); - } - esp_wifi_connect(); - xEventGroupClearBits(wifi_event_group, CONNECTED_BIT); - break; - default: - break; - } - return ESP_OK; -} - static void initialise_wifi(void) { #ifdef CONFIG_CONNECT_ORIGINAL_AP int ret; uint32_t addr; struct old_sysconf *sysconf; - wifi_config_t wifi_config = { 0 }; + char ssid[32]; + char passwd[32]; if (!(addr = esp_get_old_sysconf_addr())) { ESP_LOGE(TAG, "Current firmware does not update from old SDK firmware"); @@ -130,30 +93,15 @@ static void initialise_wifi(void) return ; } - memcpy(&wifi_config.sta.ssid, sysconf->ap_ssid[sysconf->ap_index].ssid, sysconf->ap_ssid[sysconf->ap_index].len); - memcpy(&wifi_config.sta.password, sysconf->ap_ssid[sysconf->ap_index].passwd, 32); + memcpy(ssid, sysconf->ap_ssid[sysconf->ap_index].ssid, sysconf->ap_ssid[sysconf->ap_index].len); + memcpy(passwd, sysconf->ap_ssid[sysconf->ap_index].passwd, 32); + + example_set_connection_info(ssid, passwd); free(sysconf); -#else - wifi_config_t wifi_config = { - .sta = { - .ssid = EXAMPLE_WIFI_SSID, - .password = EXAMPLE_WIFI_PASS, - }, - }; #endif - 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_storage(WIFI_STORAGE_RAM) ); - - ESP_LOGI(TAG, "Setting WiFi configuration SSID %s...", wifi_config.sta.ssid); - ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) ); - ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) ); - ESP_ERROR_CHECK( esp_wifi_start() ); + ESP_ERROR_CHECK(example_connect()); } /*read buffer by byte still delim ,return read bytes counts*/ @@ -381,13 +329,6 @@ static void ota_example_task(void *pvParameter) ESP_LOGI(TAG, "Running partition type %d subtype %d (offset 0x%08x)", running->type, running->subtype, running->address); - /* Wait for the callback to set the CONNECTED_BIT in the - event group. - */ - xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT, - false, true, portMAX_DELAY); - ESP_LOGI(TAG, "Connect to Wifi ! Start to Connect to Server...."); - /*connect to http server*/ if (connect_to_http_server()) { ESP_LOGI(TAG, "Connected to http server"); @@ -489,17 +430,11 @@ static void ota_example_task(void *pvParameter) void app_main() { - // Initialize NVS. - esp_err_t err = nvs_flash_init(); - if (err == ESP_ERR_NVS_NO_FREE_PAGES) { - // OTA app partition table has a smaller NVS partition size than the non-OTA - // partition table. This size mismatch may cause NVS initialization to fail. - // If this happens, we erase NVS partition and initialize NVS again. - ESP_ERROR_CHECK(nvs_flash_erase()); - err = nvs_flash_init(); - } - ESP_ERROR_CHECK( err ); + ESP_ERROR_CHECK(nvs_flash_init()); + ESP_ERROR_CHECK(esp_netif_init()); + ESP_ERROR_CHECK(esp_event_loop_create_default()); initialise_wifi(); + xTaskCreate(&ota_example_task, "ota_example_task", 8192, NULL, 5, NULL); } diff --git a/examples/system/ota/native_ota/2+MB_flash/new_to_new_no_old/CMakeLists.txt b/examples/system/ota/native_ota/2+MB_flash/new_to_new_no_old/CMakeLists.txt index f736f959..09abd053 100644 --- a/examples/system/ota/native_ota/2+MB_flash/new_to_new_no_old/CMakeLists.txt +++ b/examples/system/ota/native_ota/2+MB_flash/new_to_new_no_old/CMakeLists.txt @@ -2,5 +2,9 @@ # in this exact order for cmake to work correctly cmake_minimum_required(VERSION 3.5) +# (Not part of the boilerplate) +# This example uses an extra component for common functions such as Wi-Fi and Ethernet connection. +set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common) + include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(native_ota) \ No newline at end of file diff --git a/examples/system/ota/native_ota/2+MB_flash/new_to_new_no_old/Makefile b/examples/system/ota/native_ota/2+MB_flash/new_to_new_no_old/Makefile index 7ffb1023..b276959c 100644 --- a/examples/system/ota/native_ota/2+MB_flash/new_to_new_no_old/Makefile +++ b/examples/system/ota/native_ota/2+MB_flash/new_to_new_no_old/Makefile @@ -5,5 +5,7 @@ PROJECT_NAME := ota +EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/protocol_examples_common + include $(IDF_PATH)/make/project.mk diff --git a/examples/system/ota/native_ota/2+MB_flash/new_to_new_no_old/main/Kconfig.projbuild b/examples/system/ota/native_ota/2+MB_flash/new_to_new_no_old/main/Kconfig.projbuild index 74a06d93..051f8917 100644 --- a/examples/system/ota/native_ota/2+MB_flash/new_to_new_no_old/main/Kconfig.projbuild +++ b/examples/system/ota/native_ota/2+MB_flash/new_to_new_no_old/main/Kconfig.projbuild @@ -1,19 +1,5 @@ menu "Example Configuration" -config WIFI_SSID - string "WiFi SSID" - default "myssid" - help - SSID (network name) for the example to connect to. - -config WIFI_PASSWORD - string "WiFi Password" - default "myssid" - help - WiFi password (WPA or WPA2) for the example to use. - - Can be left blank if the network has no security set. - config SERVER_IP string "HTTP Server IP" default "192.168.0.3" diff --git a/examples/system/ota/native_ota/2+MB_flash/new_to_new_no_old/main/ota_example_main.c b/examples/system/ota/native_ota/2+MB_flash/new_to_new_no_old/main/ota_example_main.c index 4df5a494..c9054252 100644 --- a/examples/system/ota/native_ota/2+MB_flash/new_to_new_no_old/main/ota_example_main.c +++ b/examples/system/ota/native_ota/2+MB_flash/new_to_new_no_old/main/ota_example_main.c @@ -13,19 +13,15 @@ #include "freertos/FreeRTOS.h" #include "freertos/task.h" -#include "freertos/event_groups.h" - #include "esp_system.h" -#include "esp_wifi.h" -#include "esp_event_loop.h" #include "esp_log.h" -#include "esp_ota_ops.h" - +#include "esp_netif.h" +#include "esp_event.h" +#include "protocol_examples_common.h" #include "nvs.h" #include "nvs_flash.h" +#include "esp_ota_ops.h" -#define EXAMPLE_WIFI_SSID CONFIG_WIFI_SSID -#define EXAMPLE_WIFI_PASS CONFIG_WIFI_PASSWORD #define EXAMPLE_SERVER_IP CONFIG_SERVER_IP #define EXAMPLE_SERVER_PORT CONFIG_SERVER_PORT #define EXAMPLE_FILENAME CONFIG_EXAMPLE_FILENAME @@ -68,61 +64,6 @@ static int binary_file_length = 0; /*socket id*/ static int socket_id = -1; -/* 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? */ -const int CONNECTED_BIT = BIT0; - -static esp_err_t event_handler(void *ctx, system_event_t *event) -{ - /* For accessing reason codes in case of disconnection */ - system_event_info_t *info = &event->event_info; - - switch (event->event_id) { - case SYSTEM_EVENT_STA_START: - esp_wifi_connect(); - break; - case SYSTEM_EVENT_STA_GOT_IP: - xEventGroupSetBits(wifi_event_group, CONNECTED_BIT); - break; - case SYSTEM_EVENT_STA_DISCONNECTED: - ESP_LOGE(TAG, "Disconnect reason : %d", info->disconnected.reason); - if (info->disconnected.reason == WIFI_REASON_BASIC_RATE_NOT_SUPPORT) { - /*Switch to 802.11 bgn mode */ - esp_wifi_set_protocol(ESP_IF_WIFI_STA, WIFI_PROTOCAL_11B | WIFI_PROTOCAL_11G | WIFI_PROTOCAL_11N); - } - 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_storage(WIFI_STORAGE_RAM) ); - wifi_config_t wifi_config = { - .sta = { - .ssid = EXAMPLE_WIFI_SSID, - .password = EXAMPLE_WIFI_PASS, - }, - }; - ESP_LOGI(TAG, "Setting WiFi configuration SSID %s...", wifi_config.sta.ssid); - ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) ); - ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) ); - ESP_ERROR_CHECK( esp_wifi_start() ); -} - /*read buffer by byte still delim ,return read bytes counts*/ static int read_until(const char *buffer, char delim, int len) { @@ -339,13 +280,6 @@ static void ota_example_task(void *pvParameter) ESP_LOGI(TAG, "Running partition type %d subtype %d (offset 0x%08x)", running->type, running->subtype, running->address); - /* Wait for the callback to set the CONNECTED_BIT in the - event group. - */ - xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT, - false, true, portMAX_DELAY); - ESP_LOGI(TAG, "Connect to Wifi ! Start to Connect to Server...."); - /*connect to http server*/ if (connect_to_http_server()) { ESP_LOGI(TAG, "Connected to http server"); @@ -447,17 +381,11 @@ static void ota_example_task(void *pvParameter) void app_main() { - // Initialize NVS. - esp_err_t err = nvs_flash_init(); - if (err == ESP_ERR_NVS_NO_FREE_PAGES) { - // OTA app partition table has a smaller NVS partition size than the non-OTA - // partition table. This size mismatch may cause NVS initialization to fail. - // If this happens, we erase NVS partition and initialize NVS again. - ESP_ERROR_CHECK(nvs_flash_erase()); - err = nvs_flash_init(); - } - ESP_ERROR_CHECK( err ); + ESP_ERROR_CHECK(nvs_flash_init()); + ESP_ERROR_CHECK(esp_netif_init()); + ESP_ERROR_CHECK(esp_event_loop_create_default()); + + ESP_ERROR_CHECK(example_connect()); - initialise_wifi(); xTaskCreate(&ota_example_task, "ota_example_task", 8192, NULL, 5, NULL); } diff --git a/examples/system/ota/native_ota/2+MB_flash/new_to_new_with_old/CMakeLists.txt b/examples/system/ota/native_ota/2+MB_flash/new_to_new_with_old/CMakeLists.txt index f736f959..09abd053 100644 --- a/examples/system/ota/native_ota/2+MB_flash/new_to_new_with_old/CMakeLists.txt +++ b/examples/system/ota/native_ota/2+MB_flash/new_to_new_with_old/CMakeLists.txt @@ -2,5 +2,9 @@ # in this exact order for cmake to work correctly cmake_minimum_required(VERSION 3.5) +# (Not part of the boilerplate) +# This example uses an extra component for common functions such as Wi-Fi and Ethernet connection. +set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common) + include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(native_ota) \ No newline at end of file diff --git a/examples/system/ota/native_ota/2+MB_flash/new_to_new_with_old/Makefile b/examples/system/ota/native_ota/2+MB_flash/new_to_new_with_old/Makefile index 7ffb1023..b276959c 100644 --- a/examples/system/ota/native_ota/2+MB_flash/new_to_new_with_old/Makefile +++ b/examples/system/ota/native_ota/2+MB_flash/new_to_new_with_old/Makefile @@ -5,5 +5,7 @@ PROJECT_NAME := ota +EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/protocol_examples_common + include $(IDF_PATH)/make/project.mk diff --git a/examples/system/ota/native_ota/2+MB_flash/new_to_new_with_old/main/Kconfig.projbuild b/examples/system/ota/native_ota/2+MB_flash/new_to_new_with_old/main/Kconfig.projbuild index b24c7756..3388e54e 100644 --- a/examples/system/ota/native_ota/2+MB_flash/new_to_new_with_old/main/Kconfig.projbuild +++ b/examples/system/ota/native_ota/2+MB_flash/new_to_new_with_old/main/Kconfig.projbuild @@ -6,22 +6,6 @@ config CONNECT_ORIGINAL_AP help Connect to the original AP of old SDK -config WIFI_SSID - string "WiFi SSID" - default "myssid" - depends on !CONNECT_ORIGINAL_AP - help - SSID (network name) for the example to connect to. - -config WIFI_PASSWORD - string "WiFi Password" - default "myssid" - depends on !CONNECT_ORIGINAL_AP - help - WiFi password (WPA or WPA2) for the example to use. - - Can be left blank if the network has no security set. - config SERVER_IP string "HTTP Server IP" default "192.168.0.3" diff --git a/examples/system/ota/native_ota/2+MB_flash/new_to_new_with_old/main/ota_example_main.c b/examples/system/ota/native_ota/2+MB_flash/new_to_new_with_old/main/ota_example_main.c index 53bb569c..29fd3f63 100644 --- a/examples/system/ota/native_ota/2+MB_flash/new_to_new_with_old/main/ota_example_main.c +++ b/examples/system/ota/native_ota/2+MB_flash/new_to_new_with_old/main/ota_example_main.c @@ -11,24 +11,21 @@ #include #include -#include "freertos/FreeRTOS.h" -#include "freertos/task.h" -#include "freertos/event_groups.h" - +#include "sdkconfig.h" #ifdef CONFIG_CONNECT_ORIGINAL_AP #include "internal/esp_system_internal.h" #endif +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" #include "esp_system.h" -#include "esp_wifi.h" -#include "esp_event_loop.h" #include "esp_log.h" -#include "esp_ota_ops.h" - +#include "esp_netif.h" +#include "esp_event.h" +#include "protocol_examples_common.h" #include "nvs.h" #include "nvs_flash.h" +#include "esp_ota_ops.h" -#define EXAMPLE_WIFI_SSID CONFIG_WIFI_SSID -#define EXAMPLE_WIFI_PASS CONFIG_WIFI_PASSWORD #define EXAMPLE_SERVER_IP CONFIG_SERVER_IP #define EXAMPLE_SERVER_PORT CONFIG_SERVER_PORT #define EXAMPLE_FILENAME CONFIG_EXAMPLE_FILENAME @@ -71,48 +68,14 @@ static int binary_file_length = 0; /*socket id*/ static int socket_id = -1; -/* 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? */ -const int CONNECTED_BIT = BIT0; - -static esp_err_t event_handler(void *ctx, system_event_t *event) -{ - /* For accessing reason codes in case of disconnection */ - system_event_info_t *info = &event->event_info; - - switch (event->event_id) { - case SYSTEM_EVENT_STA_START: - esp_wifi_connect(); - break; - case SYSTEM_EVENT_STA_GOT_IP: - xEventGroupSetBits(wifi_event_group, CONNECTED_BIT); - break; - case SYSTEM_EVENT_STA_DISCONNECTED: - ESP_LOGE(TAG, "Disconnect reason : %d", info->disconnected.reason); - if (info->disconnected.reason == WIFI_REASON_BASIC_RATE_NOT_SUPPORT) { - /*Switch to 802.11 bgn mode */ - esp_wifi_set_protocol(ESP_IF_WIFI_STA, WIFI_PROTOCAL_11B | WIFI_PROTOCAL_11G | WIFI_PROTOCAL_11N); - } - esp_wifi_connect(); - xEventGroupClearBits(wifi_event_group, CONNECTED_BIT); - break; - default: - break; - } - return ESP_OK; -} - static void initialise_wifi(void) { #ifdef CONFIG_CONNECT_ORIGINAL_AP int ret; uint32_t addr; struct old_sysconf *sysconf; - wifi_config_t wifi_config = { 0 }; + char ssid[32]; + char passwd[32]; if (!(addr = esp_get_old_sysconf_addr())) { ESP_LOGE(TAG, "Current firmware does not update from old SDK firmware"); @@ -130,30 +93,15 @@ static void initialise_wifi(void) return ; } - memcpy(&wifi_config.sta.ssid, sysconf->ap_ssid[sysconf->ap_index].ssid, sysconf->ap_ssid[sysconf->ap_index].len); - memcpy(&wifi_config.sta.password, sysconf->ap_ssid[sysconf->ap_index].passwd, 32); + memcpy(ssid, sysconf->ap_ssid[sysconf->ap_index].ssid, sysconf->ap_ssid[sysconf->ap_index].len); + memcpy(passwd, sysconf->ap_ssid[sysconf->ap_index].passwd, 32); + + example_set_connection_info(ssid, passwd); free(sysconf); -#else - wifi_config_t wifi_config = { - .sta = { - .ssid = EXAMPLE_WIFI_SSID, - .password = EXAMPLE_WIFI_PASS, - }, - }; #endif - 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_storage(WIFI_STORAGE_RAM) ); - - ESP_LOGI(TAG, "Setting WiFi configuration SSID %s...", wifi_config.sta.ssid); - ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) ); - ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) ); - ESP_ERROR_CHECK( esp_wifi_start() ); + ESP_ERROR_CHECK(example_connect()); } /*read buffer by byte still delim ,return read bytes counts*/ @@ -381,13 +329,6 @@ static void ota_example_task(void *pvParameter) ESP_LOGI(TAG, "Running partition type %d subtype %d (offset 0x%08x)", running->type, running->subtype, running->address); - /* Wait for the callback to set the CONNECTED_BIT in the - event group. - */ - xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT, - false, true, portMAX_DELAY); - ESP_LOGI(TAG, "Connect to Wifi ! Start to Connect to Server...."); - /*connect to http server*/ if (connect_to_http_server()) { ESP_LOGI(TAG, "Connected to http server"); @@ -489,17 +430,11 @@ static void ota_example_task(void *pvParameter) void app_main() { - // Initialize NVS. - esp_err_t err = nvs_flash_init(); - if (err == ESP_ERR_NVS_NO_FREE_PAGES) { - // OTA app partition table has a smaller NVS partition size than the non-OTA - // partition table. This size mismatch may cause NVS initialization to fail. - // If this happens, we erase NVS partition and initialize NVS again. - ESP_ERROR_CHECK(nvs_flash_erase()); - err = nvs_flash_init(); - } - ESP_ERROR_CHECK( err ); + ESP_ERROR_CHECK(nvs_flash_init()); + ESP_ERROR_CHECK(esp_netif_init()); + ESP_ERROR_CHECK(esp_event_loop_create_default()); initialise_wifi(); + xTaskCreate(&ota_example_task, "ota_example_task", 8192, NULL, 5, NULL); } diff --git a/examples/system/ota/simple_ota_example/CMakeLists.txt b/examples/system/ota/simple_ota_example/CMakeLists.txt index ae979292..8753cace 100644 --- a/examples/system/ota/simple_ota_example/CMakeLists.txt +++ b/examples/system/ota/simple_ota_example/CMakeLists.txt @@ -2,5 +2,9 @@ # in this exact order for cmake to work correctly cmake_minimum_required(VERSION 3.5) +# (Not part of the boilerplate) +# This example uses an extra component for common functions such as Wi-Fi and Ethernet connection. +set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common) + include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(simple_ota) \ No newline at end of file diff --git a/examples/system/ota/simple_ota_example/Makefile b/examples/system/ota/simple_ota_example/Makefile index 63bca1ab..63c0d6be 100644 --- a/examples/system/ota/simple_ota_example/Makefile +++ b/examples/system/ota/simple_ota_example/Makefile @@ -5,5 +5,7 @@ PROJECT_NAME := simple_ota +EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/protocol_examples_common + include $(IDF_PATH)/make/project.mk diff --git a/examples/system/ota/simple_ota_example/main/Kconfig.projbuild b/examples/system/ota/simple_ota_example/main/Kconfig.projbuild index 38bf5cb4..8e51daa0 100644 --- a/examples/system/ota/simple_ota_example/main/Kconfig.projbuild +++ b/examples/system/ota/simple_ota_example/main/Kconfig.projbuild @@ -1,17 +1,5 @@ menu "Example Configuration" -config WIFI_SSID - string "WiFi SSID" - default "myssid" - help - SSID (network name) for the example to connect to. - -config WIFI_PASSWORD - string "WiFi Password" - default "mypassword" - help - WiFi password (WPA or WPA2) for the example to use. - config FIRMWARE_UPGRADE_URL string "firmware upgrade url endpoint" default "https://192.168.0.3:8070/hello-world.bin" diff --git a/examples/system/ota/simple_ota_example/main/simple_ota_example.c b/examples/system/ota/simple_ota_example/main/simple_ota_example.c index c2796429..f4c809b5 100644 --- a/examples/system/ota/simple_ota_example/main/simple_ota_example.c +++ b/examples/system/ota/simple_ota_example/main/simple_ota_example.c @@ -10,29 +10,23 @@ #include "freertos/task.h" #include "freertos/event_groups.h" +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" #include "esp_system.h" -#include "esp_wifi.h" -#include "esp_event_loop.h" #include "esp_log.h" +#include "esp_netif.h" +#include "esp_event.h" +#include "protocol_examples_common.h" +#include "nvs.h" +#include "nvs_flash.h" #include "esp_ota_ops.h" #include "esp_http_client.h" #include "esp_https_ota.h" -#include "nvs.h" -#include "nvs_flash.h" - static const char *TAG = "simple_ota_example"; extern const uint8_t server_cert_pem_start[] asm("_binary_ca_cert_pem_start"); extern const uint8_t server_cert_pem_end[] asm("_binary_ca_cert_pem_end"); -/* 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? */ -const int CONNECTED_BIT = BIT0; - esp_err_t _http_event_handler(esp_http_client_event_t *evt) { switch(evt->event_id) { @@ -61,64 +55,10 @@ esp_err_t _http_event_handler(esp_http_client_event_t *evt) return ESP_OK; } -static esp_err_t event_handler(void *ctx, system_event_t *event) -{ - /* For accessing reason codes in case of disconnection */ - system_event_info_t *info = &event->event_info; - - switch (event->event_id) { - case SYSTEM_EVENT_STA_START: - esp_wifi_connect(); - break; - case SYSTEM_EVENT_STA_GOT_IP: - xEventGroupSetBits(wifi_event_group, CONNECTED_BIT); - break; - case SYSTEM_EVENT_STA_DISCONNECTED: - ESP_LOGE(TAG, "Disconnect reason : %d", info->disconnected.reason); - if (info->disconnected.reason == WIFI_REASON_BASIC_RATE_NOT_SUPPORT) { - /*Switch to 802.11 bgn mode */ - esp_wifi_set_protocol(ESP_IF_WIFI_STA, WIFI_PROTOCAL_11B | WIFI_PROTOCAL_11G | WIFI_PROTOCAL_11N); - } - 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_storage(WIFI_STORAGE_RAM) ); - wifi_config_t wifi_config = { - .sta = { - .ssid = CONFIG_WIFI_SSID, - .password = CONFIG_WIFI_PASSWORD, - }, - }; - ESP_LOGI(TAG, "Setting WiFi configuration SSID %s...", wifi_config.sta.ssid); - ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) ); - ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) ); - ESP_ERROR_CHECK( esp_wifi_start() ); -} - void simple_ota_example_task(void * pvParameter) { ESP_LOGI(TAG, "Starting OTA example..."); - - /* Wait for the callback to set the CONNECTED_BIT in the - event group. - */ - xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT, - false, true, portMAX_DELAY); - ESP_LOGI(TAG, "Connect to Wifi ! Start to Connect to Server...."); - + esp_http_client_config_t config = { .url = CONFIG_FIRMWARE_UPGRADE_URL, .cert_pem = (char *)server_cert_pem_start, @@ -137,17 +77,11 @@ void simple_ota_example_task(void * pvParameter) void app_main() { - // Initialize NVS. - esp_err_t err = nvs_flash_init(); - if (err == ESP_ERR_NVS_NO_FREE_PAGES) { - // OTA app partition table has a smaller NVS partition size than the non-OTA - // partition table. This size mismatch may cause NVS initialization to fail. - // If this happens, we erase NVS partition and initialize NVS again. - ESP_ERROR_CHECK(nvs_flash_erase()); - err = nvs_flash_init(); - } - ESP_ERROR_CHECK( err ); + ESP_ERROR_CHECK(nvs_flash_init()); + ESP_ERROR_CHECK(esp_netif_init()); + ESP_ERROR_CHECK(esp_event_loop_create_default()); + + ESP_ERROR_CHECK(example_connect()); - initialise_wifi(); xTaskCreate(&simple_ota_example_task, "ota_example_task", 8192, NULL, 5, NULL); } diff --git a/examples/wifi/espnow/main/espnow_example_main.c b/examples/wifi/espnow/main/espnow_example_main.c index 8bd5a004..37407ca2 100644 --- a/examples/wifi/espnow/main/espnow_example_main.c +++ b/examples/wifi/espnow/main/espnow_example_main.c @@ -39,23 +39,13 @@ static uint16_t s_example_espnow_seq[EXAMPLE_ESPNOW_DATA_MAX] = { 0, 0 }; static void example_espnow_deinit(example_espnow_send_param_t *send_param); -static esp_err_t example_event_handler(void *ctx, system_event_t *event) -{ - switch(event->event_id) { - case SYSTEM_EVENT_STA_START: - ESP_LOGI(TAG, "WiFi started"); - break; - default: - break; - } - return ESP_OK; -} - /* WiFi should start before using ESPNOW */ static void example_wifi_init(void) { tcpip_adapter_init(); - ESP_ERROR_CHECK( esp_event_loop_init(example_event_handler, NULL) ); + + ESP_ERROR_CHECK(esp_event_loop_create_default()); + wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK( esp_wifi_init(&cfg) ); ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) ); diff --git a/examples/wifi/getting_started/softAP/CMakeLists.txt b/examples/wifi/getting_started/softAP/CMakeLists.txt new file mode 100644 index 00000000..e05fb18d --- /dev/null +++ b/examples/wifi/getting_started/softAP/CMakeLists.txt @@ -0,0 +1,6 @@ +# The following five lines of boilerplate have to be in your project's +# CMakeLists in this exact order for cmake to work correctly +cmake_minimum_required(VERSION 3.5) + +include($ENV{IDF_PATH}/tools/cmake/project.cmake) +project(wifi_softAP) diff --git a/examples/wifi/simple_wifi/Makefile b/examples/wifi/getting_started/softAP/Makefile similarity index 84% rename from examples/wifi/simple_wifi/Makefile rename to examples/wifi/getting_started/softAP/Makefile index b9ced39c..507c5e06 100644 --- a/examples/wifi/simple_wifi/Makefile +++ b/examples/wifi/getting_started/softAP/Makefile @@ -3,7 +3,7 @@ # project subdirectory. # -PROJECT_NAME := simple_wifi +PROJECT_NAME := wifi_softAP include $(IDF_PATH)/make/project.mk diff --git a/examples/wifi/getting_started/softAP/README.md b/examples/wifi/getting_started/softAP/README.md new file mode 100644 index 00000000..4b030e01 --- /dev/null +++ b/examples/wifi/getting_started/softAP/README.md @@ -0,0 +1,42 @@ +# WiFi softAP example + +(See the README.md file in the upper level 'examples' directory for more information about examples.) + + +## How to use example + +### Configure the project + +``` +idf.py menuconfig +``` + +* Set serial port under Serial Flasher Options. + +* Set WiFi SSID and WiFi Password and Maximal STA connections under Example Configuration Options. + +### Build and Flash + +Build the project and flash it to the board, then run monitor tool to view serial output: + +``` +idf.py -p PORT flash monitor +``` + +(To exit the serial monitor, type ``Ctrl-]``.) + +See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects. + +## Example Output + +There is the console output for this example: + +``` +I (917) phy: phy_version: 3960, 5211945, Jul 18 2018, 10:40:07, 0, 0 +I (917) wifi: mode : softAP (30:ae:a4:80:45:69) +I (917) wifi softAP: wifi_init_softap finished.SSID:myssid password:mypassword +I (26457) wifi: n:1 0, o:1 0, ap:1 1, sta:255 255, prof:1 +I (26457) wifi: station: 70:ef:00:43:96:67 join, AID=1, bg, 20 +I (26467) wifi softAP: station:70:ef:00:43:96:67 join, AID=1 +I (27657) tcpip_adapter: softAP assign IP to station,IP is: 192.168.4.2 +``` diff --git a/examples/wifi/getting_started/softAP/main/CMakeLists.txt b/examples/wifi/getting_started/softAP/main/CMakeLists.txt new file mode 100644 index 00000000..a8938829 --- /dev/null +++ b/examples/wifi/getting_started/softAP/main/CMakeLists.txt @@ -0,0 +1,2 @@ +idf_component_register(SRCS "softap_example_main.c" + INCLUDE_DIRS ".") \ No newline at end of file diff --git a/examples/wifi/getting_started/softAP/main/Kconfig.projbuild b/examples/wifi/getting_started/softAP/main/Kconfig.projbuild new file mode 100644 index 00000000..37829050 --- /dev/null +++ b/examples/wifi/getting_started/softAP/main/Kconfig.projbuild @@ -0,0 +1,20 @@ +menu "Example Configuration" + + config ESP_WIFI_SSID + string "WiFi SSID" + default "myssid" + help + SSID (network name) for the example to connect to. + + config ESP_WIFI_PASSWORD + string "WiFi Password" + default "mypassword" + help + WiFi password (WPA or WPA2) for the example to use. + + config ESP_MAX_STA_CONN + int "Maximal STA connections" + default 4 + help + Max number of the STA connects to AP. +endmenu diff --git a/examples/wifi/getting_started/softAP/main/component.mk b/examples/wifi/getting_started/softAP/main/component.mk new file mode 100644 index 00000000..61f8990c --- /dev/null +++ b/examples/wifi/getting_started/softAP/main/component.mk @@ -0,0 +1,8 @@ +# +# Main component makefile. +# +# This Makefile can be left empty. By default, it will take the sources in the +# src/ directory, compile them and link them into lib(subdirectory_name).a +# in the build directory. This behaviour is entirely configurable, +# please read the ESP-IDF documents if you need to do this. +# diff --git a/examples/wifi/getting_started/softAP/main/softap_example_main.c b/examples/wifi/getting_started/softAP/main/softap_example_main.c new file mode 100644 index 00000000..25c9ee54 --- /dev/null +++ b/examples/wifi/getting_started/softAP/main/softap_example_main.c @@ -0,0 +1,83 @@ +/* WiFi softAP 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 +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "esp_system.h" +#include "esp_wifi.h" +#include "esp_event.h" +#include "esp_log.h" +#include "nvs_flash.h" + +#include "lwip/err.h" +#include "lwip/sys.h" + +/* The examples use WiFi configuration that you can set via project configuration menu. + + If you'd rather not, just change the below entries to strings with + the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid" +*/ +#define EXAMPLE_ESP_WIFI_SSID CONFIG_ESP_WIFI_SSID +#define EXAMPLE_ESP_WIFI_PASS CONFIG_ESP_WIFI_PASSWORD +#define EXAMPLE_MAX_STA_CONN CONFIG_ESP_MAX_STA_CONN + +static const char *TAG = "wifi softAP"; + +static void wifi_event_handler(void* arg, esp_event_base_t event_base, + int32_t event_id, void* event_data) +{ + if (event_id == WIFI_EVENT_AP_STACONNECTED) { + wifi_event_ap_staconnected_t* event = (wifi_event_ap_staconnected_t*) event_data; + ESP_LOGI(TAG, "station "MACSTR" join, AID=%d", + MAC2STR(event->mac), event->aid); + } else if (event_id == WIFI_EVENT_AP_STADISCONNECTED) { + wifi_event_ap_stadisconnected_t* event = (wifi_event_ap_stadisconnected_t*) event_data; + ESP_LOGI(TAG, "station "MACSTR" leave, AID=%d", + MAC2STR(event->mac), event->aid); + } +} + +void wifi_init_softap() +{ + tcpip_adapter_init(); + ESP_ERROR_CHECK(esp_event_loop_create_default()); + + wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); + ESP_ERROR_CHECK(esp_wifi_init(&cfg)); + + ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL)); + + wifi_config_t wifi_config = { + .ap = { + .ssid = EXAMPLE_ESP_WIFI_SSID, + .ssid_len = strlen(EXAMPLE_ESP_WIFI_SSID), + .password = EXAMPLE_ESP_WIFI_PASS, + .max_connection = EXAMPLE_MAX_STA_CONN, + .authmode = WIFI_AUTH_WPA_WPA2_PSK + }, + }; + if (strlen(EXAMPLE_ESP_WIFI_PASS) == 0) { + wifi_config.ap.authmode = WIFI_AUTH_OPEN; + } + + ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP)); + ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_AP, &wifi_config)); + ESP_ERROR_CHECK(esp_wifi_start()); + + ESP_LOGI(TAG, "wifi_init_softap finished. SSID:%s password:%s", + EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS); +} + +void app_main() +{ + ESP_ERROR_CHECK(nvs_flash_init()); + + ESP_LOGI(TAG, "ESP_WIFI_MODE_AP"); + wifi_init_softap(); +} diff --git a/examples/wifi/getting_started/station/CMakeLists.txt b/examples/wifi/getting_started/station/CMakeLists.txt new file mode 100644 index 00000000..05a40120 --- /dev/null +++ b/examples/wifi/getting_started/station/CMakeLists.txt @@ -0,0 +1,6 @@ +# The following five lines of boilerplate have to be in your project's +# CMakeLists in this exact order for cmake to work correctly +cmake_minimum_required(VERSION 3.5) + +include($ENV{IDF_PATH}/tools/cmake/project.cmake) +project(wifi_station) diff --git a/examples/wifi/getting_started/station/Makefile b/examples/wifi/getting_started/station/Makefile new file mode 100644 index 00000000..ed014837 --- /dev/null +++ b/examples/wifi/getting_started/station/Makefile @@ -0,0 +1,9 @@ +# +# This is a project Makefile. It is assumed the directory this Makefile resides in is a +# project subdirectory. +# + +PROJECT_NAME := wifi_station + +include $(IDF_PATH)/make/project.mk + diff --git a/examples/wifi/getting_started/station/README.md b/examples/wifi/getting_started/station/README.md new file mode 100644 index 00000000..f6e4f702 --- /dev/null +++ b/examples/wifi/getting_started/station/README.md @@ -0,0 +1,109 @@ +# WiFi station example + +(See the README.md file in the upper level 'examples' directory for more information about examples.) + + +## How to use example + +### Configure the project + +``` +idf.py menuconfig +``` + +* Set serial port under Serial Flasher Options. + +* Set WiFi SSID and WiFi Password and Maximum retry under Example Configuration Options. + +### Build and Flash + +Build the project and flash it to the board, then run monitor tool to view serial output: + +``` +idf.py -p PORT flash monitor +``` + +(To exit the serial monitor, type ``Ctrl-]``.) + +See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects. + +## Example Output +Note that the output, in particular the order of the output, may vary depending on the environment. + +Console output if station connects to AP successfully: +``` +I (589) wifi station: ESP_WIFI_MODE_STA +I (599) wifi: wifi driver task: 3ffc08b4, prio:23, stack:3584, core=0 +I (599) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE +I (599) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE +I (629) wifi: wifi firmware version: 2d94f02 +I (629) wifi: config NVS flash: enabled +I (629) wifi: config nano formating: disabled +I (629) wifi: Init dynamic tx buffer num: 32 +I (629) wifi: Init data frame dynamic rx buffer num: 32 +I (639) wifi: Init management frame dynamic rx buffer num: 32 +I (639) wifi: Init management short buffer num: 32 +I (649) wifi: Init static rx buffer size: 1600 +I (649) wifi: Init static rx buffer num: 10 +I (659) wifi: Init dynamic rx buffer num: 32 +I (759) phy: phy_version: 4180, cb3948e, Sep 12 2019, 16:39:13, 0, 0 +I (769) wifi: mode : sta (30:ae:a4:d9:bc:c4) +I (769) wifi station: wifi_init_sta finished. +I (889) wifi: new:<6,0>, old:<1,0>, ap:<255,255>, sta:<6,0>, prof:1 +I (889) wifi: state: init -> auth (b0) +I (899) wifi: state: auth -> assoc (0) +I (909) wifi: state: assoc -> run (10) +I (939) wifi: connected with #!/bin/test, aid = 1, channel 6, BW20, bssid = ac:9e:17:7e:31:40 +I (939) wifi: security type: 3, phy: bgn, rssi: -68 +I (949) wifi: pm start, type: 1 + +I (1029) wifi: AP's beacon interval = 102400 us, DTIM period = 3 +I (2089) esp_netif_handlers: sta ip: 192.168.77.89, mask: 255.255.255.0, gw: 192.168.77.1 +I (2089) wifi station: got ip:192.168.77.89 +I (2089) wifi station: connected to ap SSID:myssid password:mypassword +``` + +Console output if the station failed to connect to AP: +``` +I (589) wifi station: ESP_WIFI_MODE_STA +I (599) wifi: wifi driver task: 3ffc08b4, prio:23, stack:3584, core=0 +I (599) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE +I (599) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE +I (629) wifi: wifi firmware version: 2d94f02 +I (629) wifi: config NVS flash: enabled +I (629) wifi: config nano formating: disabled +I (629) wifi: Init dynamic tx buffer num: 32 +I (629) wifi: Init data frame dynamic rx buffer num: 32 +I (639) wifi: Init management frame dynamic rx buffer num: 32 +I (639) wifi: Init management short buffer num: 32 +I (649) wifi: Init static rx buffer size: 1600 +I (649) wifi: Init static rx buffer num: 10 +I (659) wifi: Init dynamic rx buffer num: 32 +I (759) phy: phy_version: 4180, cb3948e, Sep 12 2019, 16:39:13, 0, 0 +I (759) wifi: mode : sta (30:ae:a4:d9:bc:c4) +I (769) wifi station: wifi_init_sta finished. +I (889) wifi: new:<6,0>, old:<1,0>, ap:<255,255>, sta:<6,0>, prof:1 +I (889) wifi: state: init -> auth (b0) +I (1889) wifi: state: auth -> init (200) +I (1889) wifi: new:<6,0>, old:<6,0>, ap:<255,255>, sta:<6,0>, prof:1 +I (1889) wifi station: retry to connect to the AP +I (1899) wifi station: connect to the AP fail +I (3949) wifi station: retry to connect to the AP +I (3949) wifi station: connect to the AP fail +I (4069) wifi: new:<6,0>, old:<6,0>, ap:<255,255>, sta:<6,0>, prof:1 +I (4069) wifi: state: init -> auth (b0) +I (5069) wifi: state: auth -> init (200) +I (5069) wifi: new:<6,0>, old:<6,0>, ap:<255,255>, sta:<6,0>, prof:1 +I (5069) wifi station: retry to connect to the AP +I (5069) wifi station: connect to the AP fail +I (7129) wifi station: retry to connect to the AP +I (7129) wifi station: connect to the AP fail +I (7249) wifi: new:<6,0>, old:<6,0>, ap:<255,255>, sta:<6,0>, prof:1 +I (7249) wifi: state: init -> auth (b0) +I (8249) wifi: state: auth -> init (200) +I (8249) wifi: new:<6,0>, old:<6,0>, ap:<255,255>, sta:<6,0>, prof:1 +I (8249) wifi station: retry to connect to the AP +I (8249) wifi station: connect to the AP fail +I (10299) wifi station: connect to the AP fail +I (10299) wifi station: Failed to connect to SSID:myssid, password:mypassword +``` diff --git a/examples/wifi/getting_started/station/main/CMakeLists.txt b/examples/wifi/getting_started/station/main/CMakeLists.txt new file mode 100644 index 00000000..444b0f59 --- /dev/null +++ b/examples/wifi/getting_started/station/main/CMakeLists.txt @@ -0,0 +1,2 @@ +idf_component_register(SRCS "station_example_main.c" + INCLUDE_DIRS ".") \ No newline at end of file diff --git a/examples/wifi/getting_started/station/main/Kconfig.projbuild b/examples/wifi/getting_started/station/main/Kconfig.projbuild new file mode 100644 index 00000000..7403f8f8 --- /dev/null +++ b/examples/wifi/getting_started/station/main/Kconfig.projbuild @@ -0,0 +1,20 @@ +menu "Example Configuration" + + config ESP_WIFI_SSID + string "WiFi SSID" + default "myssid" + help + SSID (network name) for the example to connect to. + + config ESP_WIFI_PASSWORD + string "WiFi Password" + default "mypassword" + help + WiFi password (WPA or WPA2) for the example to use. + + config ESP_MAXIMUM_RETRY + int "Maximum retry" + default 5 + help + Set the Maximum retry to avoid station reconnecting to the AP unlimited when the AP is really inexistent. +endmenu diff --git a/examples/wifi/getting_started/station/main/component.mk b/examples/wifi/getting_started/station/main/component.mk new file mode 100644 index 00000000..61f8990c --- /dev/null +++ b/examples/wifi/getting_started/station/main/component.mk @@ -0,0 +1,8 @@ +# +# Main component makefile. +# +# This Makefile can be left empty. By default, it will take the sources in the +# src/ directory, compile them and link them into lib(subdirectory_name).a +# in the build directory. This behaviour is entirely configurable, +# please read the ESP-IDF documents if you need to do this. +# diff --git a/examples/wifi/getting_started/station/main/station_example_main.c b/examples/wifi/getting_started/station/main/station_example_main.c new file mode 100644 index 00000000..fdd27303 --- /dev/null +++ b/examples/wifi/getting_started/station/main/station_example_main.c @@ -0,0 +1,126 @@ +/* WiFi station 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 +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "freertos/event_groups.h" +#include "esp_system.h" +#include "esp_log.h" +#include "esp_netif.h" +#include "esp_event.h" +#include "esp_wifi.h" +#include "nvs.h" +#include "nvs_flash.h" + +#include "lwip/err.h" +#include "lwip/sys.h" + +/* The examples use WiFi configuration that you can set via project configuration menu + + If you'd rather not, just change the below entries to strings with + the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid" +*/ +#define EXAMPLE_ESP_WIFI_SSID CONFIG_ESP_WIFI_SSID +#define EXAMPLE_ESP_WIFI_PASS CONFIG_ESP_WIFI_PASSWORD +#define EXAMPLE_ESP_MAXIMUM_RETRY CONFIG_ESP_MAXIMUM_RETRY + +/* FreeRTOS event group to signal when we are connected*/ +static EventGroupHandle_t s_wifi_event_group; + +/* The event group allows multiple bits for each event, but we only care about two events: + * - we are connected to the AP with an IP + * - we failed to connect after the maximum amount of retries */ +#define WIFI_CONNECTED_BIT BIT0 +#define WIFI_FAIL_BIT BIT1 + +static const char *TAG = "wifi station"; + +static int s_retry_num = 0; + +static void event_handler(void* arg, esp_event_base_t event_base, + int32_t event_id, void* event_data) +{ + if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) { + esp_wifi_connect(); + } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) { + if (s_retry_num < EXAMPLE_ESP_MAXIMUM_RETRY) { + esp_wifi_connect(); + s_retry_num++; + ESP_LOGI(TAG, "retry to connect to the AP"); + } else { + xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT); + } + ESP_LOGI(TAG,"connect to the AP fail"); + } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) { + ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data; + ESP_LOGI(TAG, "got ip:%s", + ip4addr_ntoa(&event->ip_info.ip)); + s_retry_num = 0; + xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT); + } +} + +void wifi_init_sta(void) +{ + s_wifi_event_group = xEventGroupCreate(); + + tcpip_adapter_init(); + + ESP_ERROR_CHECK(esp_event_loop_create_default()); + + wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); + ESP_ERROR_CHECK(esp_wifi_init(&cfg)); + + ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL)); + ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL)); + + wifi_config_t wifi_config = { + .sta = { + .ssid = EXAMPLE_ESP_WIFI_SSID, + .password = EXAMPLE_ESP_WIFI_PASS + }, + }; + ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) ); + ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) ); + ESP_ERROR_CHECK(esp_wifi_start() ); + + ESP_LOGI(TAG, "wifi_init_sta finished."); + + /* Waiting until either the connection is established (WIFI_CONNECTED_BIT) or connection failed for the maximum + * number of re-tries (WIFI_FAIL_BIT). The bits are set by event_handler() (see above) */ + EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group, + WIFI_CONNECTED_BIT | WIFI_FAIL_BIT, + pdFALSE, + pdFALSE, + portMAX_DELAY); + + /* xEventGroupWaitBits() returns the bits before the call returned, hence we can test which event actually + * happened. */ + if (bits & WIFI_CONNECTED_BIT) { + ESP_LOGI(TAG, "connected to ap SSID:%s password:%s", + EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS); + } else if (bits & WIFI_FAIL_BIT) { + ESP_LOGI(TAG, "Failed to connect to SSID:%s, password:%s", + EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS); + } else { + ESP_LOGE(TAG, "UNEXPECTED EVENT"); + } + + ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler)); + ESP_ERROR_CHECK(esp_event_handler_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler)); + vEventGroupDelete(s_wifi_event_group); +} + +void app_main() +{ + ESP_ERROR_CHECK(nvs_flash_init()); + + ESP_LOGI(TAG, "ESP_WIFI_MODE_STA"); + wifi_init_sta(); +} diff --git a/examples/wifi/power_save/CMakeLists.txt b/examples/wifi/power_save/CMakeLists.txt index a9a182fe..07a1535d 100644 --- a/examples/wifi/power_save/CMakeLists.txt +++ b/examples/wifi/power_save/CMakeLists.txt @@ -2,5 +2,9 @@ # in this exact order for cmake to work correctly cmake_minimum_required(VERSION 3.5) +# (Not part of the boilerplate) +# This example uses an extra component for common functions such as Wi-Fi and Ethernet connection. +set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common) + include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(power_save) diff --git a/examples/wifi/power_save/Makefile b/examples/wifi/power_save/Makefile index 6f2f5543..32ca2cfd 100644 --- a/examples/wifi/power_save/Makefile +++ b/examples/wifi/power_save/Makefile @@ -5,5 +5,7 @@ PROJECT_NAME := power_save +EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/protocol_examples_common + include $(IDF_PATH)/make/project.mk diff --git a/examples/wifi/power_save/main/Kconfig.projbuild b/examples/wifi/power_save/main/Kconfig.projbuild index 55b3a477..2a334543 100644 --- a/examples/wifi/power_save/main/Kconfig.projbuild +++ b/examples/wifi/power_save/main/Kconfig.projbuild @@ -1,17 +1,5 @@ menu "Example Configuration" - config ESP_WIFI_SSID - string "WiFi SSID" - default "myssid" - help - SSID (network name) for the example to connect to. - - config ESP_WIFI_PASSWORD - string "WiFi Password" - default "mypassword" - help - WiFi password (WPA or WPA2) for the example to use. - choice ESP_POWER_SAVE_MODE prompt "power save mode" default EXAMPLE_POWER_SAVE_MIN_MODEM diff --git a/examples/wifi/power_save/main/power_save.c b/examples/wifi/power_save/main/power_save.c index 83872e75..df1673c7 100644 --- a/examples/wifi/power_save/main/power_save.c +++ b/examples/wifi/power_save/main/power_save.c @@ -13,19 +13,18 @@ start esp32 and when it connected to AP it will enter power save mode */ #include + #include "freertos/FreeRTOS.h" #include "freertos/task.h" -#include "freertos/event_groups.h" -#include "rom/ets_sys.h" -#include "esp_wifi.h" -#include "esp_event_loop.h" +#include "esp_system.h" #include "esp_log.h" +#include "esp_netif.h" +#include "esp_event.h" +#include "esp_wifi.h" +#include "protocol_examples_common.h" +#include "nvs.h" #include "nvs_flash.h" -/*set the ssid and password via "idf.py menuconfig"*/ -#define EXAMPLE_ESP_WIFI_SSID CONFIG_ESP_WIFI_SSID -#define EXAMPLE_ESP_WIFI_PASS CONFIG_ESP_WIFI_PASSWORD - #if CONFIG_EXAMPLE_POWER_SAVE_MIN_MODEM #define DEFAULT_PS_MODE WIFI_PS_MIN_MODEM #elif CONFIG_EXAMPLE_POWER_SAVE_MAX_MODEM @@ -36,85 +35,13 @@ #define DEFAULT_PS_MODE WIFI_PS_NONE #endif /*CONFIG_POWER_SAVE_MODEM*/ -/* FreeRTOS event group to signal when we are connected*/ -static EventGroupHandle_t wifi_event_group; - -static const char *TAG = "power_save"; - -static esp_err_t event_handler(void *ctx, system_event_t *event) -{ - /* For accessing reason codes in case of disconnection */ - system_event_info_t *info = &event->event_info; - - switch(event->event_id) { - case SYSTEM_EVENT_STA_START: - esp_wifi_connect(); - break; - case SYSTEM_EVENT_STA_GOT_IP: - ESP_LOGI(TAG, "got ip:%s", - ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip)); - break; - case SYSTEM_EVENT_AP_STACONNECTED: - ESP_LOGI(TAG, "station:"MACSTR" join, AID=%d", - MAC2STR(event->event_info.sta_connected.mac), - event->event_info.sta_connected.aid); - break; - case SYSTEM_EVENT_AP_STADISCONNECTED: - ESP_LOGI(TAG, "station:"MACSTR"leave, AID=%d", - MAC2STR(event->event_info.sta_disconnected.mac), - event->event_info.sta_disconnected.aid); - break; - case SYSTEM_EVENT_STA_DISCONNECTED: - ESP_LOGE(TAG, "Disconnect reason : %d", info->disconnected.reason); - if (info->disconnected.reason == WIFI_REASON_BASIC_RATE_NOT_SUPPORT) { - /*Switch to 802.11 bgn mode */ - esp_wifi_set_protocol(ESP_IF_WIFI_STA, WIFI_PROTOCAL_11B | WIFI_PROTOCAL_11G | WIFI_PROTOCAL_11N); - } - esp_wifi_connect(); - break; - default: - break; - } - return ESP_OK; -} - -/*init wifi as sta and set power save mode*/ -static void wifi_power_save(void) -{ - wifi_event_group = xEventGroupCreate(); - - tcpip_adapter_init(); - 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)); - wifi_config_t wifi_config = { - .sta = { - .ssid = EXAMPLE_ESP_WIFI_SSID, - .password = EXAMPLE_ESP_WIFI_PASS - }, - }; - - ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) ); - ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) ); - ESP_ERROR_CHECK(esp_wifi_start() ); - - ESP_LOGI(TAG, "wifi_init_sta finished."); - ESP_LOGI(TAG, "connect to ap SSID:%s password:%s", - EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS); - ESP_LOGI(TAG, "esp_wifi_set_ps()."); - esp_wifi_set_ps(DEFAULT_PS_MODE); -} - void app_main(void) { - //Initialize NVS - esp_err_t ret = nvs_flash_init(); - if (ret == ESP_ERR_NVS_NO_FREE_PAGES) { - ESP_ERROR_CHECK(nvs_flash_erase()); - ret = nvs_flash_init(); - } - ESP_ERROR_CHECK(ret); + ESP_ERROR_CHECK(nvs_flash_init()); + ESP_ERROR_CHECK(esp_netif_init()); + ESP_ERROR_CHECK(esp_event_loop_create_default()); + + ESP_ERROR_CHECK(example_connect()); - wifi_power_save(); + esp_wifi_set_ps(DEFAULT_PS_MODE); } diff --git a/examples/wifi/simple_wifi/CMakeLists.txt b/examples/wifi/simple_wifi/CMakeLists.txt deleted file mode 100644 index 1ef4ddb5..00000000 --- a/examples/wifi/simple_wifi/CMakeLists.txt +++ /dev/null @@ -1,6 +0,0 @@ -# The following four lines of boilerplate have to be in your project's CMakeLists -# in this exact order for cmake to work correctly -cmake_minimum_required(VERSION 3.5) - -include($ENV{IDF_PATH}/tools/cmake/project.cmake) -project(simple_wifi) diff --git a/examples/wifi/simple_wifi/main/CMakeLists.txt b/examples/wifi/simple_wifi/main/CMakeLists.txt deleted file mode 100644 index db9e8e94..00000000 --- a/examples/wifi/simple_wifi/main/CMakeLists.txt +++ /dev/null @@ -1,3 +0,0 @@ -set(COMPONENT_SRCS "simple_wifi.c") - -register_component() diff --git a/examples/wifi/simple_wifi/main/Kconfig.projbuild b/examples/wifi/simple_wifi/main/Kconfig.projbuild deleted file mode 100644 index 67bb8677..00000000 --- a/examples/wifi/simple_wifi/main/Kconfig.projbuild +++ /dev/null @@ -1,37 +0,0 @@ -menu "Example Configuration" - -choice ESP_WIFI_MODE - prompt "AP or STA" - default ESP_WIFI_IS_STATION - help - Whether the esp32 is softAP or station. - -config ESP_WIFI_IS_SOFTAP - bool "SoftAP" -config ESP_WIFI_IS_STATION - bool "Station" -endchoice - -config ESP_WIFI_MODE_AP - bool - default y if ESP_WIFI_IS_SOFTAP - default n if ESP_WIFI_IS_STATION - -config ESP_WIFI_SSID - string "WiFi SSID" - default "myssid" - help - SSID (network name) for the example to connect to. - -config ESP_WIFI_PASSWORD - string "WiFi Password" - default "mypassword" - help - WiFi password (WPA or WPA2) for the example to use. - -config MAX_STA_CONN - int "Max STA conn" - default 4 - help - Max number of the STA connects to AP. -endmenu diff --git a/examples/wifi/simple_wifi/main/component.mk b/examples/wifi/simple_wifi/main/component.mk deleted file mode 100644 index 3277c9f9..00000000 --- a/examples/wifi/simple_wifi/main/component.mk +++ /dev/null @@ -1,4 +0,0 @@ -# -# "main" pseudo-component makefile. -# -# (Uses default behaviour of compiling all source files in directory, adding 'include' to include path.) \ No newline at end of file diff --git a/examples/wifi/simple_wifi/main/simple_wifi.c b/examples/wifi/simple_wifi/main/simple_wifi.c deleted file mode 100644 index 975d393e..00000000 --- a/examples/wifi/simple_wifi/main/simple_wifi.c +++ /dev/null @@ -1,152 +0,0 @@ -/* Simple WiFi 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 -#include "freertos/FreeRTOS.h" -#include "freertos/task.h" -#include "freertos/event_groups.h" -#include "rom/ets_sys.h" -#include "esp_wifi.h" -#include "esp_event_loop.h" -#include "esp_log.h" -#include "nvs_flash.h" - -/* The examples use simple WiFi configuration that you can set via - 'make menuconfig'. - - If you'd rather not, just change the below entries to strings with - the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid" -*/ -#define EXAMPLE_ESP_WIFI_MODE_AP CONFIG_ESP_WIFI_MODE_AP //TRUE:AP FALSE:STA -#define EXAMPLE_ESP_WIFI_SSID CONFIG_ESP_WIFI_SSID -#define EXAMPLE_ESP_WIFI_PASS CONFIG_ESP_WIFI_PASSWORD -#define EXAMPLE_MAX_STA_CONN CONFIG_MAX_STA_CONN - -/* FreeRTOS event group to signal when we are connected*/ -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? */ -const int WIFI_CONNECTED_BIT = BIT0; - -static const char *TAG = "simple wifi"; - -static esp_err_t event_handler(void *ctx, system_event_t *event) -{ - /* For accessing reason codes in case of disconnection */ - system_event_info_t *info = &event->event_info; - - switch(event->event_id) { - case SYSTEM_EVENT_STA_START: - esp_wifi_connect(); - break; - case SYSTEM_EVENT_STA_GOT_IP: - ESP_LOGI(TAG, "got ip:%s", - ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip)); - xEventGroupSetBits(wifi_event_group, WIFI_CONNECTED_BIT); - break; - case SYSTEM_EVENT_AP_STACONNECTED: - ESP_LOGI(TAG, "station:"MACSTR" join, AID=%d", - MAC2STR(event->event_info.sta_connected.mac), - event->event_info.sta_connected.aid); - break; - case SYSTEM_EVENT_AP_STADISCONNECTED: - ESP_LOGI(TAG, "station:"MACSTR"leave, AID=%d", - MAC2STR(event->event_info.sta_disconnected.mac), - event->event_info.sta_disconnected.aid); - break; - case SYSTEM_EVENT_STA_DISCONNECTED: - ESP_LOGE(TAG, "Disconnect reason : %d", info->disconnected.reason); - if (info->disconnected.reason == WIFI_REASON_BASIC_RATE_NOT_SUPPORT) { - /*Switch to 802.11 bgn mode */ - esp_wifi_set_protocol(ESP_IF_WIFI_STA, WIFI_PROTOCAL_11B | WIFI_PROTOCAL_11G | WIFI_PROTOCAL_11N); - } - esp_wifi_connect(); - xEventGroupClearBits(wifi_event_group, WIFI_CONNECTED_BIT); - break; - default: - break; - } - return ESP_OK; -} - -void wifi_init_softap() -{ - wifi_event_group = xEventGroupCreate(); - - tcpip_adapter_init(); - 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)); - wifi_config_t wifi_config = { - .ap = { - .ssid = EXAMPLE_ESP_WIFI_SSID, - .ssid_len = strlen(EXAMPLE_ESP_WIFI_SSID), - .password = EXAMPLE_ESP_WIFI_PASS, - .max_connection = EXAMPLE_MAX_STA_CONN, - .authmode = WIFI_AUTH_WPA_WPA2_PSK - }, - }; - if (strlen(EXAMPLE_ESP_WIFI_PASS) == 0) { - wifi_config.ap.authmode = WIFI_AUTH_OPEN; - } - - ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP)); - ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_AP, &wifi_config)); - ESP_ERROR_CHECK(esp_wifi_start()); - - ESP_LOGI(TAG, "wifi_init_softap finished.SSID:%s password:%s", - EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS); -} - -void wifi_init_sta() -{ - wifi_event_group = xEventGroupCreate(); - - tcpip_adapter_init(); - 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)); - wifi_config_t wifi_config = { - .sta = { - .ssid = EXAMPLE_ESP_WIFI_SSID, - .password = EXAMPLE_ESP_WIFI_PASS - }, - }; - - ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) ); - ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) ); - ESP_ERROR_CHECK(esp_wifi_start() ); - - ESP_LOGI(TAG, "wifi_init_sta finished."); - ESP_LOGI(TAG, "connect to ap SSID:%s password:%s", - EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS); -} - -void app_main() -{ - //Initialize NVS - esp_err_t ret = nvs_flash_init(); - if (ret == ESP_ERR_NVS_NO_FREE_PAGES) { - ESP_ERROR_CHECK(nvs_flash_erase()); - ret = nvs_flash_init(); - } - ESP_ERROR_CHECK(ret); - -#if EXAMPLE_ESP_WIFI_MODE_AP - ESP_LOGI(TAG, "ESP_WIFI_MODE_AP"); - wifi_init_softap(); -#else - ESP_LOGI(TAG, "ESP_WIFI_MODE_STA"); - wifi_init_sta(); -#endif /*EXAMPLE_ESP_WIFI_MODE_AP*/ - -} diff --git a/examples/wifi/smart_config/main/smartconfig_main.c b/examples/wifi/smart_config/main/smartconfig_main.c index 5bca3459..52c2f22b 100644 --- a/examples/wifi/smart_config/main/smartconfig_main.c +++ b/examples/wifi/smart_config/main/smartconfig_main.c @@ -13,7 +13,7 @@ #include "freertos/task.h" #include "freertos/event_groups.h" #include "esp_wifi.h" -#include "esp_event_loop.h" +#include "esp_event.h" #include "esp_log.h" #include "esp_system.h" #include "nvs_flash.h" @@ -33,44 +33,42 @@ static const char *TAG = "sc"; void smartconfig_example_task(void * parm); -static esp_err_t event_handler(void *ctx, system_event_t *event) +static void on_wifi_disconnect(void *arg, esp_event_base_t event_base, + int32_t event_id, void *event_data) { - /* For accessing reason codes in case of disconnection */ - system_event_info_t *info = &event->event_info; + system_event_sta_disconnected_t *event = (system_event_sta_disconnected_t *)event_data; - 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_LOGE(TAG, "Disconnect reason : %d", info->disconnected.reason); - if (info->disconnected.reason == WIFI_REASON_BASIC_RATE_NOT_SUPPORT) { - /*Switch to 802.11 bgn mode */ - esp_wifi_set_protocol(ESP_IF_WIFI_STA, WIFI_PROTOCAL_11B | WIFI_PROTOCAL_11G | WIFI_PROTOCAL_11N); - } - esp_wifi_connect(); - xEventGroupClearBits(wifi_event_group, CONNECTED_BIT); - break; - default: - break; + ESP_LOGI(TAG, "Wi-Fi disconnected, trying to reconnect..."); + if (event->reason == WIFI_REASON_BASIC_RATE_NOT_SUPPORT) { + /*Switch to 802.11 bgn mode */ + esp_wifi_set_protocol(ESP_IF_WIFI_STA, WIFI_PROTOCAL_11B | WIFI_PROTOCAL_11G | WIFI_PROTOCAL_11N); } - return ESP_OK; + ESP_ERROR_CHECK(esp_wifi_connect()); +} + +static void on_got_ip(void *arg, esp_event_base_t event_base, + int32_t event_id, void *event_data) +{ + xEventGroupSetBits(wifi_event_group, CONNECTED_BIT); } static void initialise_wifi(void) { tcpip_adapter_init(); wifi_event_group = xEventGroupCreate(); - ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) ); + + ESP_ERROR_CHECK(esp_event_loop_create_default()); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK( esp_wifi_init(&cfg) ); + + ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &on_wifi_disconnect, NULL)); + ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &on_got_ip, NULL)); + ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) ); ESP_ERROR_CHECK( esp_wifi_start() ); + ESP_ERROR_CHECK( esp_wifi_connect() ); } static void sc_callback(smartconfig_status_t status, void *pdata) diff --git a/examples/wifi/sniffer/main/sniffer_main.c b/examples/wifi/sniffer/main/sniffer_main.c index da2d2e2b..379f0684 100644 --- a/examples/wifi/sniffer/main/sniffer_main.c +++ b/examples/wifi/sniffer/main/sniffer_main.c @@ -12,7 +12,7 @@ #include "esp_system.h" #include "esp_log.h" #include "esp_timer.h" -#include "esp_event_loop.h" +#include "esp_event.h" #include "nvs_flash.h" #include "freertos/FreeRTOS.h" @@ -155,27 +155,27 @@ static void sniffer_task(void* pvParameters) vTaskDelete(NULL); } -static esp_err_t event_handler(void* ctx, system_event_t* event) +static void wifi_event_handler(void* arg, esp_event_base_t event_base, + int32_t event_id, void* event_data) { - switch (event->event_id) { - case SYSTEM_EVENT_STA_START: - xEventGroupSetBits(wifi_event_group, START_BIT); - break; - - default: - break; + if (event_id == WIFI_EVENT_STA_START) { + xEventGroupSetBits(wifi_event_group, START_BIT); } - - return ESP_OK; } static void initialise_wifi(void) { tcpip_adapter_init(); + + ESP_ERROR_CHECK(esp_event_loop_create_default()); + 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_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL)); + ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); ESP_ERROR_CHECK(esp_wifi_start()); } diff --git a/examples/wifi/wpa2_enterprise/main/wpa2_enterprise_main.c b/examples/wifi/wpa2_enterprise/main/wpa2_enterprise_main.c index c24466cd..1c9eb3db 100644 --- a/examples/wifi/wpa2_enterprise/main/wpa2_enterprise_main.c +++ b/examples/wifi/wpa2_enterprise/main/wpa2_enterprise_main.c @@ -77,51 +77,23 @@ extern uint8_t client_crt_end[] asm("_binary_wpa2_client_crt_end"); extern uint8_t client_key_start[] asm("_binary_wpa2_client_key_start"); extern uint8_t client_key_end[] asm("_binary_wpa2_client_key_end"); -static esp_err_t event_handler(void* ctx, system_event_t* event) +static void event_handler(void* arg, esp_event_base_t event_base, + int32_t event_id, void* event_data) { - /* For accessing reason codes in case of disconnection */ - system_event_info_t* info = &event->event_info; + if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) { + esp_wifi_connect(); + } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) { + system_event_sta_disconnected_t *event = (system_event_sta_disconnected_t *)event_data; - switch (event->event_id) { - case SYSTEM_EVENT_STA_START: - esp_wifi_connect(); - break; - - case SYSTEM_EVENT_STA_GOT_IP: - ESP_LOGI(TAG, "got ip:%s", - ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip)); - xEventGroupSetBits(wifi_event_group, WIFI_CONNECTED_BIT); - break; - - case SYSTEM_EVENT_AP_STACONNECTED: - ESP_LOGI(TAG, "station:"MACSTR" join, AID=%d", - MAC2STR(event->event_info.sta_connected.mac), - event->event_info.sta_connected.aid); - break; - - case SYSTEM_EVENT_AP_STADISCONNECTED: - ESP_LOGI(TAG, "station:"MACSTR"leave, AID=%d", - MAC2STR(event->event_info.sta_disconnected.mac), - event->event_info.sta_disconnected.aid); - break; - - case SYSTEM_EVENT_STA_DISCONNECTED: - ESP_LOGE(TAG, "Disconnect reason : %d", info->disconnected.reason); - - if (info->disconnected.reason == WIFI_REASON_BASIC_RATE_NOT_SUPPORT) { - /*Switch to 802.11 bgn mode */ - esp_wifi_set_protocol(ESP_IF_WIFI_STA, WIFI_PROTOCAL_11B | WIFI_PROTOCAL_11G | WIFI_PROTOCAL_11N); - } - - esp_wifi_connect(); - xEventGroupClearBits(wifi_event_group, WIFI_CONNECTED_BIT); - break; - - default: - break; + if (event->reason == WIFI_REASON_BASIC_RATE_NOT_SUPPORT) { + /*Switch to 802.11 bgn mode */ + esp_wifi_set_protocol(ESP_IF_WIFI_STA, WIFI_PROTOCAL_11B | WIFI_PROTOCAL_11G | WIFI_PROTOCAL_11N); + } + esp_wifi_connect(); + xEventGroupClearBits(wifi_event_group, WIFI_CONNECTED_BIT); + } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) { + xEventGroupSetBits(wifi_event_group, WIFI_CONNECTED_BIT); } - - return ESP_OK; } static void initialise_wifi(void) @@ -133,9 +105,11 @@ static void initialise_wifi(void) tcpip_adapter_init(); wifi_event_group = xEventGroupCreate(); - ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL)); + ESP_ERROR_CHECK(esp_event_loop_create_default()); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK(esp_wifi_init(&cfg)); + ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL)); + ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL)); ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM)); wifi_config_t wifi_config = { .sta = { diff --git a/examples/wifi/wps/main/wps.c b/examples/wifi/wps/main/wps.c index bef9ebb8..6ab5a813 100644 --- a/examples/wifi/wps/main/wps.c +++ b/examples/wifi/wps/main/wps.c @@ -39,78 +39,73 @@ static const char* TAG = "example_wps"; static esp_wps_config_t config = WPS_CONFIG_INIT_DEFAULT(WPS_TEST_MODE); -static esp_err_t event_handler(void* ctx, system_event_t* event) +static void wifi_event_handler(void* arg, esp_event_base_t event_base, + int32_t event_id, void* event_data) { - /* For accessing reason codes in case of disconnection */ - system_event_info_t *info = &event->event_info; - - switch (event->event_id) { - case SYSTEM_EVENT_STA_START: - ESP_LOGI(TAG, "SYSTEM_EVENT_STA_START"); + switch (event_id) { + case WIFI_EVENT_STA_START: + ESP_LOGI(TAG, "WIFI_EVENT_STA_START"); break; + case WIFI_EVENT_STA_DISCONNECTED: { + system_event_sta_disconnected_t *event = (system_event_sta_disconnected_t *)event_data; - case SYSTEM_EVENT_STA_GOT_IP: - ESP_LOGI(TAG, "SYSTEM_EVENT_STA_GOT_IP"); - ESP_LOGI(TAG, "got ip:%s\n", - ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip)); - break; - - case SYSTEM_EVENT_STA_DISCONNECTED: - ESP_LOGI(TAG, "SYSTEM_EVENT_STA_DISCONNECTED"); - ESP_LOGE(TAG, "Disconnect reason : %d", info->disconnected.reason); - if (info->disconnected.reason == WIFI_REASON_BASIC_RATE_NOT_SUPPORT) { - /*Switch to 802.11 bgn mode */ - esp_wifi_set_protocol(ESP_IF_WIFI_STA, WIFI_PROTOCAL_11B | WIFI_PROTOCAL_11G | WIFI_PROTOCAL_11N); + ESP_LOGI(TAG, "WIFI_EVENT_STA_DISCONNECTED"); + if (event->reason == WIFI_REASON_BASIC_RATE_NOT_SUPPORT) { + /*Switch to 802.11 bgn mode */ + esp_wifi_set_protocol(ESP_IF_WIFI_STA, WIFI_PROTOCAL_11B | WIFI_PROTOCAL_11G | WIFI_PROTOCAL_11N); + } + ESP_ERROR_CHECK(esp_wifi_connect()); } - ESP_ERROR_CHECK(esp_wifi_connect()); break; - - case SYSTEM_EVENT_STA_WPS_ER_SUCCESS: - /*point: the function esp_wifi_wps_start() only get ssid & password - * so call the function esp_wifi_connect() here - * */ - ESP_LOGI(TAG, "SYSTEM_EVENT_STA_WPS_ER_SUCCESS"); + case WIFI_EVENT_STA_WPS_ER_SUCCESS: + ESP_LOGI(TAG, "WIFI_EVENT_STA_WPS_ER_SUCCESS"); + /* esp_wifi_wps_start() only gets ssid & password, so call esp_wifi_connect() here. */ ESP_ERROR_CHECK(esp_wifi_wps_disable()); ESP_ERROR_CHECK(esp_wifi_connect()); break; - - case SYSTEM_EVENT_STA_WPS_ER_FAILED: - ESP_LOGI(TAG, "SYSTEM_EVENT_STA_WPS_ER_FAILED"); + case WIFI_EVENT_STA_WPS_ER_FAILED: + ESP_LOGI(TAG, "WIFI_EVENT_STA_WPS_ER_FAILED"); ESP_ERROR_CHECK(esp_wifi_wps_disable()); ESP_ERROR_CHECK(esp_wifi_wps_enable(&config)); ESP_ERROR_CHECK(esp_wifi_wps_start(0)); break; - - case SYSTEM_EVENT_STA_WPS_ER_TIMEOUT: - ESP_LOGI(TAG, "SYSTEM_EVENT_STA_WPS_ER_TIMEOUT"); + case WIFI_EVENT_STA_WPS_ER_TIMEOUT: + ESP_LOGI(TAG, "WIFI_EVENT_STA_WPS_ER_TIMEOUT"); ESP_ERROR_CHECK(esp_wifi_wps_disable()); ESP_ERROR_CHECK(esp_wifi_wps_enable(&config)); ESP_ERROR_CHECK(esp_wifi_wps_start(0)); break; - - case SYSTEM_EVENT_STA_WPS_ER_PIN: - ESP_LOGI(TAG, "SYSTEM_EVENT_STA_WPS_ER_PIN"); - /*show the PIN code here*/ - ESP_LOGI(TAG, "WPS_PIN = "PINSTR, PIN2STR(event->event_info.sta_er_pin.pin_code)); + case WIFI_EVENT_STA_WPS_ER_PIN: + ESP_LOGI(TAG, "WIFI_EVENT_STA_WPS_ER_PIN"); + /* display the PIN code */ + wifi_event_sta_wps_er_pin_t* event = (wifi_event_sta_wps_er_pin_t*) event_data; + ESP_LOGI(TAG, "WPS_PIN = " PINSTR, PIN2STR(event->pin_code)); break; - default: break; } +} - return ESP_OK; +static void got_ip_event_handler(void* arg, esp_event_base_t event_base, + int32_t event_id, void* event_data) +{ + ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data; + ESP_LOGI(TAG, "got ip: %s", ip4addr_ntoa(&event->ip_info.ip)); } /*init wifi as sta and start wps*/ static void start_wps(void) { tcpip_adapter_init(); - ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL)); + ESP_ERROR_CHECK(esp_event_loop_create_default()); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK(esp_wifi_init(&cfg)); + ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL)); + ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &got_ip_event_handler, NULL)); + ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); ESP_ERROR_CHECK(esp_wifi_start());