From 6548afcf49edaf5e74f7d6ce8c462c67d1e93060 Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Wed, 21 Nov 2018 00:42:03 +0800 Subject: [PATCH] examples/protocols/mqtt: use common network component --- .../mqtt/publish_test/CMakeLists.txt | 4 + examples/protocols/mqtt/publish_test/Makefile | 2 + .../protocols/mqtt/publish_test/README.md | 12 +-- .../mqtt/publish_test/main/Kconfig.projbuild | 12 --- .../mqtt/publish_test/main/publish_test.c | 58 ++++----------- examples/protocols/mqtt/ssl/CMakeLists.txt | 5 +- examples/protocols/mqtt/ssl/Makefile | 2 + examples/protocols/mqtt/ssl/README.md | 10 +-- .../protocols/mqtt/ssl/main/Kconfig.projbuild | 12 --- examples/protocols/mqtt/ssl/main/app_main.c | 73 ++++++------------- .../mqtt/ssl_mutual_auth/CMakeLists.txt | 5 +- .../protocols/mqtt/ssl_mutual_auth/Makefile | 2 + .../protocols/mqtt/ssl_mutual_auth/README.md | 10 +-- .../ssl_mutual_auth/main/Kconfig.projbuild | 15 ---- .../mqtt/ssl_mutual_auth/main/app_main.c | 73 ++++++------------- examples/protocols/mqtt/tcp/CMakeLists.txt | 5 +- examples/protocols/mqtt/tcp/Makefile | 2 + examples/protocols/mqtt/tcp/README.md | 10 +-- .../protocols/mqtt/tcp/main/Kconfig.projbuild | 12 --- examples/protocols/mqtt/tcp/main/app_main.c | 71 ++++++------------ examples/protocols/mqtt/ws/CMakeLists.txt | 7 +- examples/protocols/mqtt/ws/Makefile | 2 + examples/protocols/mqtt/ws/README.md | 10 +-- .../protocols/mqtt/ws/main/Kconfig.projbuild | 12 --- examples/protocols/mqtt/ws/main/app_main.c | 69 ++++++------------ examples/protocols/mqtt/wss/CMakeLists.txt | 5 +- examples/protocols/mqtt/wss/Makefile | 2 + examples/protocols/mqtt/wss/README.md | 10 +-- .../protocols/mqtt/wss/main/Kconfig.projbuild | 12 --- examples/protocols/mqtt/wss/main/app_main.c | 71 ++++++------------ 30 files changed, 175 insertions(+), 420 deletions(-) delete mode 100644 examples/protocols/mqtt/ssl_mutual_auth/main/Kconfig.projbuild diff --git a/examples/protocols/mqtt/publish_test/CMakeLists.txt b/examples/protocols/mqtt/publish_test/CMakeLists.txt index 75f161908..2203d063f 100644 --- a/examples/protocols/mqtt/publish_test/CMakeLists.txt +++ b/examples/protocols/mqtt/publish_test/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_publish) diff --git a/examples/protocols/mqtt/publish_test/Makefile b/examples/protocols/mqtt/publish_test/Makefile index d1341ae90..7d552c3be 100644 --- a/examples/protocols/mqtt/publish_test/Makefile +++ b/examples/protocols/mqtt/publish_test/Makefile @@ -4,4 +4,6 @@ # PROJECT_NAME := mqtt_publish +EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/protocol_examples_common + include $(IDF_PATH)/make/project.mk diff --git a/examples/protocols/mqtt/publish_test/README.md b/examples/protocols/mqtt/publish_test/README.md index bc4e02cca..bdc2bc879 100644 --- a/examples/protocols/mqtt/publish_test/README.md +++ b/examples/protocols/mqtt/publish_test/README.md @@ -22,16 +22,10 @@ This example can be executed on any ESP32 board, the only required interface is ### Configure the project -``` -make menuconfig -``` - -* Set serial port under Serial Flasher Options. - -* Set ssid and password for the board to connect to AP. - +* Run `make menuconfig` (or `idf.py menuconfig` if using CMake build system) +* Configure Wi-Fi or Ethernet under "Example Connection Configuration" menu. See "Establishing Wi-Fi or Ethernet Connection" section in [examples/protocols/README.md](../../README.md) for more details. +* When using Make build system, set `Default serial port` under `Serial flasher config`. * Set brokers for all 4 transports (TCP, SSL, WS, WSS), also set certificate if needed - * Set topics for publishing from and to ESP32 ### Build and Flash diff --git a/examples/protocols/mqtt/publish_test/main/Kconfig.projbuild b/examples/protocols/mqtt/publish_test/main/Kconfig.projbuild index 3d534a477..66d3cb09e 100644 --- a/examples/protocols/mqtt/publish_test/main/Kconfig.projbuild +++ b/examples/protocols/mqtt/publish_test/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_SSL_URI string "Broker SSL URL" default "mqtts://iot.eclipse.org:8883" diff --git a/examples/protocols/mqtt/publish_test/main/publish_test.c b/examples/protocols/mqtt/publish_test/main/publish_test.c index 65073187f..99ef19d74 100644 --- a/examples/protocols/mqtt/publish_test/main/publish_test.c +++ b/examples/protocols/mqtt/publish_test/main/publish_test.c @@ -13,7 +13,9 @@ #include "esp_wifi.h" #include "esp_system.h" #include "nvs_flash.h" -#include "esp_event_loop.h" +#include "esp_event.h" +#include "tcpip_adapter.h" +#include "protocol_examples_common.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" @@ -30,7 +32,6 @@ static const char *TAG = "PUBLISH_TEST"; -static EventGroupHandle_t wifi_event_group; static EventGroupHandle_t mqtt_event_group; const static int CONNECTED_BIT = BIT0; @@ -43,47 +44,6 @@ static size_t expected_published = 0; static size_t actual_published = 0; static int qos_test = 0; -static esp_err_t wifi_event_handler(void *ctx, system_event_t *event) -{ - 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_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-----"; @@ -208,8 +168,16 @@ void app_main() esp_log_level_set("TRANSPORT", ESP_LOG_VERBOSE); esp_log_level_set("OUTBOX", ESP_LOG_VERBOSE); - nvs_flash_init(); - wifi_init(); + ESP_ERROR_CHECK(nvs_flash_init()); + tcpip_adapter_init(); + ESP_ERROR_CHECK(esp_event_loop_create_default()); + + /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig. + * Read "Establishing Wi-Fi or Ethernet Connection" section in + * examples/protocols/README.md for more information about this function. + */ + ESP_ERROR_CHECK(example_connect()); + mqtt_app_start(); while (1) { diff --git a/examples/protocols/mqtt/ssl/CMakeLists.txt b/examples/protocols/mqtt/ssl/CMakeLists.txt index 13bdd8c21..a7b2c4420 100644 --- a/examples/protocols/mqtt/ssl/CMakeLists.txt +++ b/examples/protocols/mqtt/ssl/CMakeLists.txt @@ -2,8 +2,11 @@ # in this exact order for cmake to work correctly cmake_minimum_required(VERSION 3.5) -include($ENV{IDF_PATH}/tools/cmake/project.cmake) +# (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) target_add_binary_data(mqtt_ssl.elf "main/iot_eclipse_org.pem" TEXT) diff --git a/examples/protocols/mqtt/ssl/Makefile b/examples/protocols/mqtt/ssl/Makefile index bae0d73ba..efe7f7d57 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/README.md b/examples/protocols/mqtt/ssl/README.md index 3d369bdf9..929257d40 100644 --- a/examples/protocols/mqtt/ssl/README.md +++ b/examples/protocols/mqtt/ssl/README.md @@ -14,13 +14,9 @@ This example can be executed on any ESP32 board, the only required interface is ### Configure the project -``` -make menuconfig -``` - -* Set serial port under Serial Flasher Options. - -* Set ssid and password for the board to connect to AP. +* Run `make menuconfig` (or `idf.py menuconfig` if using CMake build system) +* Configure Wi-Fi or Ethernet under "Example Connection Configuration" menu. See "Establishing Wi-Fi or Ethernet Connection" section in [examples/protocols/README.md](../../README.md) for more details. +* When using Make build system, set `Default serial port` under `Serial flasher config`. Note how to create a PEM certificate for iot.eclipse.org: ``` diff --git a/examples/protocols/mqtt/ssl/main/Kconfig.projbuild b/examples/protocols/mqtt/ssl/main/Kconfig.projbuild index 7bd1fcb98..e31873366 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 86697a03c..88e1a0c38 100644 --- a/examples/protocols/mqtt/ssl/main/app_main.c +++ b/examples/protocols/mqtt/ssl/main/app_main.c @@ -1,3 +1,12 @@ +/* MQTT over SSL 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 #include @@ -5,13 +14,14 @@ #include "esp_wifi.h" #include "esp_system.h" #include "nvs_flash.h" -#include "esp_event_loop.h" +#include "esp_event.h" +#include "tcpip_adapter.h" +#include "protocol_examples_common.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "freertos/semphr.h" #include "freertos/queue.h" -#include "freertos/event_groups.h" #include "lwip/sockets.h" #include "lwip/dns.h" @@ -22,52 +32,6 @@ 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) -{ - 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_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-----"; @@ -150,8 +114,15 @@ 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(); + ESP_ERROR_CHECK(nvs_flash_init()); + tcpip_adapter_init(); + ESP_ERROR_CHECK(esp_event_loop_create_default()); + /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig. + * Read "Establishing Wi-Fi or Ethernet Connection" section in + * examples/protocols/README.md for more information about this function. + */ + ESP_ERROR_CHECK(example_connect()); + + mqtt_app_start(); } diff --git a/examples/protocols/mqtt/ssl_mutual_auth/CMakeLists.txt b/examples/protocols/mqtt/ssl_mutual_auth/CMakeLists.txt index 84bf37525..472a3ca3e 100644 --- a/examples/protocols/mqtt/ssl_mutual_auth/CMakeLists.txt +++ b/examples/protocols/mqtt/ssl_mutual_auth/CMakeLists.txt @@ -2,8 +2,11 @@ # in this exact order for cmake to work correctly cmake_minimum_required(VERSION 3.5) -include($ENV{IDF_PATH}/tools/cmake/project.cmake) +# (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) target_add_binary_data(${CMAKE_PROJECT_NAME}.elf "main/client.crt" TEXT) diff --git a/examples/protocols/mqtt/ssl_mutual_auth/Makefile b/examples/protocols/mqtt/ssl_mutual_auth/Makefile index cfc04f81b..1394f99a6 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/README.md b/examples/protocols/mqtt/ssl_mutual_auth/README.md index 7ffe53704..763e2671a 100644 --- a/examples/protocols/mqtt/ssl_mutual_auth/README.md +++ b/examples/protocols/mqtt/ssl_mutual_auth/README.md @@ -14,13 +14,9 @@ This example can be executed on any ESP32 board, the only required interface is ### Configure the project -``` -make menuconfig -``` - -* Set serial port under Serial Flasher Options. - -* Set ssid and password for the board to connect to AP. +* Run `make menuconfig` (or `idf.py menuconfig` if using CMake build system) +* Configure Wi-Fi or Ethernet under "Example Connection Configuration" menu. See "Establishing Wi-Fi or Ethernet Connection" section in [examples/protocols/README.md](../../README.md) for more details. +* When using Make build system, set `Default serial port` under `Serial flasher config`. * Generate your client keys and certificate 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 ed4e69168..000000000 --- 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 62b970056..21c76ff44 100644 --- a/examples/protocols/mqtt/ssl_mutual_auth/main/app_main.c +++ b/examples/protocols/mqtt/ssl_mutual_auth/main/app_main.c @@ -1,3 +1,11 @@ +/* MQTT Mutual Authentication 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 #include @@ -5,13 +13,14 @@ #include "esp_wifi.h" #include "esp_system.h" #include "nvs_flash.h" -#include "esp_event_loop.h" +#include "esp_event.h" +#include "tcpip_adapter.h" +#include "protocol_examples_common.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "freertos/semphr.h" #include "freertos/queue.h" -#include "freertos/event_groups.h" #include "lwip/sockets.h" #include "lwip/dns.h" @@ -22,53 +31,6 @@ 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) -{ - 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_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"); @@ -148,8 +110,15 @@ 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(); + ESP_ERROR_CHECK(nvs_flash_init()); + tcpip_adapter_init(); + ESP_ERROR_CHECK(esp_event_loop_create_default()); + /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig. + * Read "Establishing Wi-Fi or Ethernet Connection" section in + * examples/protocols/README.md for more information about this function. + */ + ESP_ERROR_CHECK(example_connect()); + + mqtt_app_start(); } diff --git a/examples/protocols/mqtt/tcp/CMakeLists.txt b/examples/protocols/mqtt/tcp/CMakeLists.txt index 678d787af..5a70e7aa0 100644 --- a/examples/protocols/mqtt/tcp/CMakeLists.txt +++ b/examples/protocols/mqtt/tcp/CMakeLists.txt @@ -2,6 +2,9 @@ # in this exact order for cmake to work correctly cmake_minimum_required(VERSION 3.5) -include($ENV{IDF_PATH}/tools/cmake/project.cmake) +# (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 cd53fdbf5..530276501 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/README.md b/examples/protocols/mqtt/tcp/README.md index 2fda24070..aea663094 100644 --- a/examples/protocols/mqtt/tcp/README.md +++ b/examples/protocols/mqtt/tcp/README.md @@ -14,13 +14,9 @@ This example can be executed on any ESP32 board, the only required interface is ### Configure the project -``` -make menuconfig -``` - -* Set serial port under Serial Flasher Options. - -* Set ssid and password for the board to connect to AP. +* Run `make menuconfig` (or `idf.py menuconfig` if using CMake build system) +* Configure Wi-Fi or Ethernet under "Example Connection Configuration" menu. See "Establishing Wi-Fi or Ethernet Connection" section in [examples/protocols/README.md](../../README.md) for more details. +* When using Make build system, set `Default serial port` under `Serial flasher config`. ### Build and Flash diff --git a/examples/protocols/mqtt/tcp/main/Kconfig.projbuild b/examples/protocols/mqtt/tcp/main/Kconfig.projbuild index f5c519baa..fbd48103b 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 ce1640adb..1863a013a 100644 --- a/examples/protocols/mqtt/tcp/main/app_main.c +++ b/examples/protocols/mqtt/tcp/main/app_main.c @@ -1,3 +1,12 @@ +/* MQTT (over TCP) 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 #include @@ -5,13 +14,14 @@ #include "esp_wifi.h" #include "esp_system.h" #include "nvs_flash.h" -#include "esp_event_loop.h" +#include "esp_event.h" +#include "tcpip_adapter.h" +#include "protocol_examples_common.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "freertos/semphr.h" #include "freertos/queue.h" -#include "freertos/event_groups.h" #include "lwip/sockets.h" #include "lwip/dns.h" @@ -22,9 +32,6 @@ 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) { @@ -76,48 +83,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) -{ - 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_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 = { @@ -168,7 +133,15 @@ void app_main() esp_log_level_set("TRANSPORT", ESP_LOG_VERBOSE); esp_log_level_set("OUTBOX", ESP_LOG_VERBOSE); - nvs_flash_init(); - wifi_init(); + ESP_ERROR_CHECK(nvs_flash_init()); + tcpip_adapter_init(); + ESP_ERROR_CHECK(esp_event_loop_create_default()); + + /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig. + * Read "Establishing Wi-Fi or Ethernet Connection" section in + * examples/protocols/README.md for more information about this function. + */ + ESP_ERROR_CHECK(example_connect()); + mqtt_app_start(); } diff --git a/examples/protocols/mqtt/ws/CMakeLists.txt b/examples/protocols/mqtt/ws/CMakeLists.txt index 58a2135da..f0490061c 100644 --- a/examples/protocols/mqtt/ws/CMakeLists.txt +++ b/examples/protocols/mqtt/ws/CMakeLists.txt @@ -2,6 +2,9 @@ # in this exact order for cmake to work correctly cmake_minimum_required(VERSION 3.5) -include($ENV{IDF_PATH}/tools/cmake/project.cmake) +# (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) -project(mqtt_websocket) \ No newline at end of file +include($ENV{IDF_PATH}/tools/cmake/project.cmake) +project(mqtt_websocket) diff --git a/examples/protocols/mqtt/ws/Makefile b/examples/protocols/mqtt/ws/Makefile index 668719bf1..2b97fa44c 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/README.md b/examples/protocols/mqtt/ws/README.md index 619519d92..efe2efcb3 100644 --- a/examples/protocols/mqtt/ws/README.md +++ b/examples/protocols/mqtt/ws/README.md @@ -14,13 +14,9 @@ This example can be executed on any ESP32 board, the only required interface is ### Configure the project -``` -make menuconfig -``` - -* Set serial port under Serial Flasher Options. - -* Set ssid and password for the board to connect to AP. +* Run `make menuconfig` (or `idf.py menuconfig` if using CMake build system) +* Configure Wi-Fi or Ethernet under "Example Connection Configuration" menu. See "Establishing Wi-Fi or Ethernet Connection" section in [examples/protocols/README.md](../../README.md) for more details. +* When using Make build system, set `Default serial port` under `Serial flasher config`. ### Build and Flash diff --git a/examples/protocols/mqtt/ws/main/Kconfig.projbuild b/examples/protocols/mqtt/ws/main/Kconfig.projbuild index 9e47709c2..46f3ad565 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 024be48bd..0387e967c 100644 --- a/examples/protocols/mqtt/ws/main/app_main.c +++ b/examples/protocols/mqtt/ws/main/app_main.c @@ -1,3 +1,11 @@ +/* MQTT over Websockets 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 #include @@ -5,13 +13,14 @@ #include "esp_wifi.h" #include "esp_system.h" #include "nvs_flash.h" -#include "esp_event_loop.h" +#include "esp_event.h" +#include "tcpip_adapter.h" +#include "protocol_examples_common.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "freertos/semphr.h" #include "freertos/queue.h" -#include "freertos/event_groups.h" #include "lwip/sockets.h" #include "lwip/dns.h" @@ -22,9 +31,6 @@ 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) { @@ -73,47 +79,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) -{ - 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_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) { @@ -141,7 +106,15 @@ void app_main() esp_log_level_set("TRANSPORT", ESP_LOG_VERBOSE); esp_log_level_set("OUTBOX", ESP_LOG_VERBOSE); - nvs_flash_init(); - wifi_init(); + ESP_ERROR_CHECK(nvs_flash_init()); + tcpip_adapter_init(); + ESP_ERROR_CHECK(esp_event_loop_create_default()); + + /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig. + * Read "Establishing Wi-Fi or Ethernet Connection" section in + * examples/protocols/README.md for more information about this function. + */ + ESP_ERROR_CHECK(example_connect()); + mqtt_app_start(); } diff --git a/examples/protocols/mqtt/wss/CMakeLists.txt b/examples/protocols/mqtt/wss/CMakeLists.txt index 7ba5e6295..3e2d5fc23 100644 --- a/examples/protocols/mqtt/wss/CMakeLists.txt +++ b/examples/protocols/mqtt/wss/CMakeLists.txt @@ -2,8 +2,11 @@ # in this exact order for cmake to work correctly cmake_minimum_required(VERSION 3.5) -include($ENV{IDF_PATH}/tools/cmake/project.cmake) +# (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) target_add_binary_data(mqtt_websocket_secure.elf "main/iot_eclipse_org.pem" TEXT) diff --git a/examples/protocols/mqtt/wss/Makefile b/examples/protocols/mqtt/wss/Makefile index 27047d049..50ed13dd3 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/README.md b/examples/protocols/mqtt/wss/README.md index 43d829ccb..9433986a5 100644 --- a/examples/protocols/mqtt/wss/README.md +++ b/examples/protocols/mqtt/wss/README.md @@ -13,13 +13,9 @@ This example can be executed on any ESP32 board, the only required interface is ### Configure the project -``` -make menuconfig -``` - -* Set serial port under Serial Flasher Options. - -* Set ssid and password for the board to connect to AP. +* Run `make menuconfig` (or `idf.py menuconfig` if using CMake build system) +* Configure Wi-Fi or Ethernet under "Example Connection Configuration" menu. See "Establishing Wi-Fi or Ethernet Connection" section in [examples/protocols/README.md](../../README.md) for more details. +* When using Make build system, set `Default serial port` under `Serial flasher config`. Note how to create a PEM certificate for iot.eclipse.org: diff --git a/examples/protocols/mqtt/wss/main/Kconfig.projbuild b/examples/protocols/mqtt/wss/main/Kconfig.projbuild index a16174012..d3f7bf51c 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 50595495a..cbf8631e2 100644 --- a/examples/protocols/mqtt/wss/main/app_main.c +++ b/examples/protocols/mqtt/wss/main/app_main.c @@ -1,3 +1,11 @@ +/* MQTT over Secure Websockets 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 #include @@ -5,13 +13,14 @@ #include "esp_wifi.h" #include "esp_system.h" #include "nvs_flash.h" -#include "esp_event_loop.h" +#include "esp_event.h" +#include "tcpip_adapter.h" +#include "protocol_examples_common.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "freertos/semphr.h" #include "freertos/queue.h" -#include "freertos/event_groups.h" #include "lwip/sockets.h" #include "lwip/dns.h" @@ -22,52 +31,6 @@ 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) -{ - 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_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-----"; @@ -149,7 +112,15 @@ void app_main() esp_log_level_set("TRANSPORT", ESP_LOG_VERBOSE); esp_log_level_set("OUTBOX", ESP_LOG_VERBOSE); - nvs_flash_init(); - wifi_init(); + ESP_ERROR_CHECK(nvs_flash_init()); + tcpip_adapter_init(); + ESP_ERROR_CHECK(esp_event_loop_create_default()); + + /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig. + * Read "Establishing Wi-Fi or Ethernet Connection" section in + * examples/protocols/README.md for more information about this function. + */ + ESP_ERROR_CHECK(example_connect()); + mqtt_app_start(); }