# 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)
#
PROJECT_NAME := mqtt_publish
+EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/protocol_examples_common
+
include $(IDF_PATH)/make/project.mk
### 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
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"
#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"
static const char *TAG = "PUBLISH_TEST";
-static EventGroupHandle_t wifi_event_group;
static EventGroupHandle_t mqtt_event_group;
const static int CONNECTED_BIT = BIT0;
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-----";
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) {
# 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)
#
PROJECT_NAME := mqtt_ssl
+EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/protocol_examples_common
+
include $(IDF_PATH)/make/project.mk
### 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:
```
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"
+/* 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 <stdio.h>
#include <stdint.h>
#include <stddef.h>
#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"
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-----";
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();
}
# 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)
#
PROJECT_NAME := mqtt_ssl_mutual_auth
+EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/protocol_examples_common
+
include $(IDF_PATH)/make/project.mk
### 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
+++ /dev/null
-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
+/* 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 <stdio.h>
#include <stdint.h>
#include <stddef.h>
#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"
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");
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();
}
# 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
#
PROJECT_NAME := mqtt_tcp
+EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/protocol_examples_common
+
include $(IDF_PATH)/make/project.mk
### 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
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"
+/* 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 <stdio.h>
#include <stdint.h>
#include <stddef.h>
#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"
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)
{
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 = {
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();
}
# 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)
#
PROJECT_NAME := mqtt_websocket
+EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/protocol_examples_common
+
include $(IDF_PATH)/make/project.mk
### 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
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"
+/* 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 <stdio.h>
#include <stdint.h>
#include <stddef.h>
#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"
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)
{
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_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();
}
# 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)
#
PROJECT_NAME := mqtt_websocket_secure
+EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/protocol_examples_common
+
include $(IDF_PATH)/make/project.mk
### 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:
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"
+/* 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 <stdio.h>
#include <stdint.h>
#include <stddef.h>
#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"
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-----";
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();
}