diff --git a/examples/protocols/mqtt/main/Kconfig.projbuild b/examples/protocols/mqtt/main/Kconfig.projbuild new file mode 100644 index 00000000..92a75195 --- /dev/null +++ b/examples/protocols/mqtt/main/Kconfig.projbuild @@ -0,0 +1,17 @@ +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/mqtt/main/MQTTEcho.c b/examples/protocols/mqtt/main/MQTTEcho.c index be815f6f..41f5cc3b 100644 --- a/examples/protocols/mqtt/main/MQTTEcho.c +++ b/examples/protocols/mqtt/main/MQTTEcho.c @@ -18,22 +18,84 @@ #include #include -#include "esp_sta.h" #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 "MQTTClient.h" -#include "user_config.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 = 1<<0; + +#define MQTT_BROKER "iot.eclipse.org" /* MQTT Broker Address*/ +#define MQTT_PORT 1883 /* MQTT Port*/ #define MQTT_CLIENT_THREAD_NAME "mqtt_client_thread" #define MQTT_CLIENT_THREAD_STACK_WORDS 2048 #define MQTT_CLIENT_THREAD_PRIO 8 -static xTaskHandle mqttc_client_handle; +static const char *TAG = "example"; + +static esp_err_t 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: + /* This is a workaround as ESP32 WiFi libs don't currently + auto-reassociate. */ + 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 messageArrived(MessageData* data) { @@ -50,6 +112,13 @@ static void mqtt_client_thread(void* pvParameters) printf("mqtt client thread starts\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"); + NetworkInit(&network); MQTTClientInit(&client, &network, 30000, sendbuf, sizeof(sendbuf), readbuf, sizeof(readbuf)); @@ -108,21 +177,6 @@ static void mqtt_client_thread(void* pvParameters) return; } -void user_conn_init(void) -{ - int ret; - ret = xTaskCreate(mqtt_client_thread, - MQTT_CLIENT_THREAD_NAME, - MQTT_CLIENT_THREAD_STACK_WORDS, - NULL, - MQTT_CLIENT_THREAD_PRIO, - &mqttc_client_handle); - - if (ret != pdPASS) { - printf("mqtt create client thread %s failed\n", MQTT_CLIENT_THREAD_NAME); - } -} - /****************************************************************************** * FunctionName : user_rf_cal_sector_set * Description : SDK just reversed 4 sectors, used for rf init data and paramters. @@ -175,49 +229,14 @@ uint32_t user_rf_cal_sector_set(void) return rf_cal_sec; } -void wifi_event_handler_cb(System_Event_t* event) +void app_main(void) { - if (event == NULL) { - return; - } - - switch (event->event_id) { - case EVENT_STAMODE_GOT_IP: - printf("sta got ip ,create task and free heap size is %d\n", esp_get_free_heap_size()); - user_conn_init(); - break; - - case EVENT_STAMODE_CONNECTED: - printf("sta connected\n"); - break; - - case EVENT_STAMODE_DISCONNECTED: - wifi_station_connect(); - break; - - default: - break; - } -} - -/****************************************************************************** - * FunctionName : user_init - * Description : entry of user application, init user function here - * Parameters : none - * Returns : none -*******************************************************************************/ -void user_init(void) -{ - printf("SDK version:%s %d\n", esp_get_idf_version(), esp_get_free_heap_size()); - wifi_set_opmode(STATION_MODE); - - { - struct station_config config; - bzero(&config, sizeof(struct station_config)); - sprintf((char*)config.ssid, SSID); - sprintf((char*)config.password, PASSWORD); - wifi_station_set_config(&config); - } - - wifi_set_event_handler_cb(wifi_event_handler_cb); + ESP_ERROR_CHECK( nvs_flash_init() ); + initialise_wifi(); + xTaskCreate(&mqtt_client_thread, + MQTT_CLIENT_THREAD_NAME, + MQTT_CLIENT_THREAD_STACK_WORDS, + NULL, + MQTT_CLIENT_THREAD_PRIO, + NULL); } diff --git a/examples/protocols/mqtt/main/user_config.h b/examples/protocols/mqtt/main/user_config.h deleted file mode 100644 index 5f237566..00000000 --- a/examples/protocols/mqtt/main/user_config.h +++ /dev/null @@ -1,19 +0,0 @@ -/* - 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 __USER_CONFIG_H__ -#define __USER_CONFIG_H__ - -#define SSID "espressif" /* Wi-Fi SSID */ -#define PASSWORD "1234567890" /* Wi-Fi Password */ - -#define MQTT_BROKER "iot.eclipse.org" /* MQTT Broker Address*/ -#define MQTT_PORT 1883 /* MQTT Port*/ - -#endif -