examples/protocols/esp_http_client: use common network component
This commit is contained in:
parent
e1d1f10e8a
commit
fbc407f088
7 changed files with 18 additions and 115 deletions
|
@ -2,5 +2,9 @@
|
||||||
# CMakeLists in this exact order for cmake to work correctly
|
# CMakeLists in this exact order for cmake to work correctly
|
||||||
cmake_minimum_required(VERSION 3.5)
|
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)
|
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
||||||
project(esp-http-client-example)
|
project(esp-http-client-example)
|
||||||
|
|
|
@ -5,5 +5,7 @@
|
||||||
|
|
||||||
PROJECT_NAME := esp-http-client-example
|
PROJECT_NAME := esp-http-client-example
|
||||||
|
|
||||||
|
EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/protocol_examples_common
|
||||||
|
|
||||||
include $(IDF_PATH)/make/project.mk
|
include $(IDF_PATH)/make/project.mk
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
set(COMPONENT_SRCS "app_wifi.c"
|
set(COMPONENT_SRCS "esp_http_client_example.c")
|
||||||
"esp_http_client_example.c")
|
|
||||||
set(COMPONENT_ADD_INCLUDEDIRS ".")
|
set(COMPONENT_ADD_INCLUDEDIRS ".")
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -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
|
|
|
@ -1,75 +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)
|
|
||||||
{
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
|
@ -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
|
|
|
@ -14,7 +14,9 @@
|
||||||
#include "esp_log.h"
|
#include "esp_log.h"
|
||||||
#include "esp_system.h"
|
#include "esp_system.h"
|
||||||
#include "nvs_flash.h"
|
#include "nvs_flash.h"
|
||||||
#include "app_wifi.h"
|
#include "esp_event.h"
|
||||||
|
#include "tcpip_adapter.h"
|
||||||
|
#include "protocol_examples_common.h"
|
||||||
|
|
||||||
#include "esp_http_client.h"
|
#include "esp_http_client.h"
|
||||||
|
|
||||||
|
@ -486,8 +488,6 @@ static void https_async()
|
||||||
|
|
||||||
static void http_test_task(void *pvParameters)
|
static void http_test_task(void *pvParameters)
|
||||||
{
|
{
|
||||||
app_wifi_wait_connected();
|
|
||||||
ESP_LOGI(TAG, "Connected to AP, begin http example");
|
|
||||||
http_rest_with_url();
|
http_rest_with_url();
|
||||||
http_rest_with_hostname_path();
|
http_rest_with_hostname_path();
|
||||||
http_auth_basic();
|
http_auth_basic();
|
||||||
|
@ -514,7 +514,14 @@ void app_main()
|
||||||
ret = nvs_flash_init();
|
ret = nvs_flash_init();
|
||||||
}
|
}
|
||||||
ESP_ERROR_CHECK(ret);
|
ESP_ERROR_CHECK(ret);
|
||||||
app_wifi_initialise();
|
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());
|
||||||
|
|
||||||
xTaskCreate(&http_test_task, "http_test_task", 8192, NULL, 5, NULL);
|
xTaskCreate(&http_test_task, "http_test_task", 8192, NULL, 5, NULL);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue