Update http_request & https_request examples for new startup flow

This commit is contained in:
Angus Gratton
2016-09-27 11:16:40 +10:00
parent 6850751ee8
commit c3f418aa90
2 changed files with 33 additions and 16 deletions

View File

@@ -10,9 +10,11 @@
#include "freertos/FreeRTOS.h" #include "freertos/FreeRTOS.h"
#include "freertos/task.h" #include "freertos/task.h"
#include "freertos/event_groups.h" #include "freertos/event_groups.h"
#include "esp_system.h"
#include "esp_wifi.h" #include "esp_wifi.h"
#include "esp_event.h" #include "esp_event_loop.h"
#include "esp_log.h" #include "esp_log.h"
#include "nvs_flash.h"
#include "lwip/err.h" #include "lwip/err.h"
#include "lwip/sockets.h" #include "lwip/sockets.h"
@@ -49,7 +51,7 @@ static const char *REQUEST = "GET " WEB_URL " HTTP/1.1\n"
"User-Agent: esp-idf/1.0 esp32\n" "User-Agent: esp-idf/1.0 esp32\n"
"\n"; "\n";
static esp_err_t wifi_event_cb(void *ctx, system_event_t *event) static esp_err_t event_handler(void *ctx, system_event_t *event)
{ {
switch(event->event_id) { switch(event->event_id) {
case SYSTEM_EVENT_STA_START: case SYSTEM_EVENT_STA_START:
@@ -70,8 +72,14 @@ static esp_err_t wifi_event_cb(void *ctx, system_event_t *event)
return ESP_OK; return ESP_OK;
} }
static void set_wifi_configuration(void) 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_event_loop_get_queue() );
ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );
wifi_config_t wifi_config = { wifi_config_t wifi_config = {
.sta = { .sta = {
.ssid = EXAMPLE_WIFI_SSID, .ssid = EXAMPLE_WIFI_SSID,
@@ -79,8 +87,9 @@ static void set_wifi_configuration(void)
}, },
}; };
ESP_LOGI(TAG, "Setting WiFi configuration SSID %s...", wifi_config.sta.ssid); ESP_LOGI(TAG, "Setting WiFi configuration SSID %s...", wifi_config.sta.ssid);
esp_wifi_set_mode(WIFI_MODE_STA); ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
esp_wifi_set_config(WIFI_IF_STA, &wifi_config); ESP_ERROR_CHECK( esp_wifi_set_config(WIFI_IF_STA, &wifi_config) );
ESP_ERROR_CHECK( esp_wifi_start() );
} }
static void http_get_task(void *pvParameters) static void http_get_task(void *pvParameters)
@@ -165,8 +174,8 @@ static void http_get_task(void *pvParameters)
void app_main() void app_main()
{ {
wifi_event_group = xEventGroupCreate(); nvs_flash_init(6, 3);
esp_event_set_cb(wifi_event_cb, NULL); system_init();
set_wifi_configuration(); initialise_wifi();
xTaskCreate(&http_get_task, "http_get_task", 2048, NULL, 5, NULL); xTaskCreate(&http_get_task, "http_get_task", 2048, NULL, 5, NULL);
} }

View File

@@ -26,9 +26,10 @@
#include "freertos/task.h" #include "freertos/task.h"
#include "freertos/event_groups.h" #include "freertos/event_groups.h"
#include "esp_wifi.h" #include "esp_wifi.h"
#include "esp_event.h" #include "esp_event_loop.h"
#include "esp_log.h" #include "esp_log.h"
#include "esp_system.h" #include "esp_system.h"
#include "nvs_flash.h"
#include "lwip/err.h" #include "lwip/err.h"
#include "lwip/sockets.h" #include "lwip/sockets.h"
@@ -120,7 +121,7 @@ static void mbedtls_debug(void *ctx, int level,
#endif #endif
static esp_err_t wifi_event_cb(void *ctx, system_event_t *event) static esp_err_t event_handler(void *ctx, system_event_t *event)
{ {
switch(event->event_id) { switch(event->event_id) {
case SYSTEM_EVENT_STA_START: case SYSTEM_EVENT_STA_START:
@@ -141,8 +142,14 @@ static esp_err_t wifi_event_cb(void *ctx, system_event_t *event)
return ESP_OK; return ESP_OK;
} }
static void set_wifi_configuration(void) 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_event_loop_get_queue() );
ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );
wifi_config_t wifi_config = { wifi_config_t wifi_config = {
.sta = { .sta = {
.ssid = EXAMPLE_WIFI_SSID, .ssid = EXAMPLE_WIFI_SSID,
@@ -150,8 +157,9 @@ static void set_wifi_configuration(void)
}, },
}; };
ESP_LOGI(TAG, "Setting WiFi configuration SSID %s...", wifi_config.sta.ssid); ESP_LOGI(TAG, "Setting WiFi configuration SSID %s...", wifi_config.sta.ssid);
esp_wifi_set_mode(WIFI_MODE_STA); ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
esp_wifi_set_config(WIFI_IF_STA, &wifi_config); ESP_ERROR_CHECK( esp_wifi_set_config(WIFI_IF_STA, &wifi_config) );
ESP_ERROR_CHECK( esp_wifi_start() );
} }
static void https_get_task(void *pvParameters) static void https_get_task(void *pvParameters)
@@ -348,8 +356,8 @@ static void https_get_task(void *pvParameters)
void app_main() void app_main()
{ {
wifi_event_group = xEventGroupCreate(); nvs_flash_init(6, 3);
esp_event_set_cb(wifi_event_cb, NULL); system_init();
set_wifi_configuration(); initialise_wifi();
xTaskCreate(&https_get_task, "https_get_task", 8192, NULL, 5, NULL); xTaskCreate(&https_get_task, "https_get_task", 8192, NULL, 5, NULL);
} }