mirror of
https://github.com/espressif/esp-idf.git
synced 2024-10-05 20:47:46 -04:00
Examples: Use event groups for waiting until WiFi is associated & ESP has IP
This commit is contained in:
parent
db183e653c
commit
8016e862e0
@ -9,6 +9,7 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "freertos/FreeRTOS.h"
|
#include "freertos/FreeRTOS.h"
|
||||||
#include "freertos/task.h"
|
#include "freertos/task.h"
|
||||||
|
#include "freertos/event_groups.h"
|
||||||
#include "esp_wifi.h"
|
#include "esp_wifi.h"
|
||||||
#include "esp_event.h"
|
#include "esp_event.h"
|
||||||
#include "esp_log.h"
|
#include "esp_log.h"
|
||||||
@ -28,8 +29,13 @@
|
|||||||
#define EXAMPLE_WIFI_SSID CONFIG_WIFI_SSID
|
#define EXAMPLE_WIFI_SSID CONFIG_WIFI_SSID
|
||||||
#define EXAMPLE_WIFI_PASS CONFIG_WIFI_PASSWORD
|
#define EXAMPLE_WIFI_PASS CONFIG_WIFI_PASSWORD
|
||||||
|
|
||||||
/* Flag for when we are connected & ready to make a request */
|
/* FreeRTOS event group to signal when we are connected & ready to make a request */
|
||||||
static volatile bool ready;
|
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;
|
||||||
|
|
||||||
/* Constants that aren't configurable in menuconfig */
|
/* Constants that aren't configurable in menuconfig */
|
||||||
#define WEB_SERVER "example.com"
|
#define WEB_SERVER "example.com"
|
||||||
@ -46,11 +52,17 @@ static const char *REQUEST = "GET " WEB_URL " HTTP/1.1\n"
|
|||||||
static esp_err_t wifi_event_cb(void *ctx, system_event_t *event)
|
static esp_err_t wifi_event_cb(void *ctx, system_event_t *event)
|
||||||
{
|
{
|
||||||
switch(event->event_id) {
|
switch(event->event_id) {
|
||||||
|
case SYSTEM_EVENT_STA_START:
|
||||||
|
esp_wifi_connect();
|
||||||
|
break;
|
||||||
case SYSTEM_EVENT_STA_GOT_IP:
|
case SYSTEM_EVENT_STA_GOT_IP:
|
||||||
ready = true;
|
xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
|
||||||
break;
|
break;
|
||||||
case SYSTEM_EVENT_STA_DISCONNECTED:
|
case SYSTEM_EVENT_STA_DISCONNECTED:
|
||||||
ready = false;
|
/* This is a workaround as ESP32 WiFi libs don't currently
|
||||||
|
auto-reassociate. */
|
||||||
|
esp_wifi_connect();
|
||||||
|
xEventGroupClearBits(wifi_event_group, CONNECTED_BIT);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
@ -83,11 +95,11 @@ static void http_get_task(void *pvParameters)
|
|||||||
char recv_buf[64];
|
char recv_buf[64];
|
||||||
|
|
||||||
while(1) {
|
while(1) {
|
||||||
esp_wifi_connect();
|
/* Wait for the callback to set the CONNECTED_BIT in the
|
||||||
/* Wait for the event callback to tell us we are connected */
|
event group.
|
||||||
while (!ready) {
|
*/
|
||||||
vTaskDelay(1);
|
xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT,
|
||||||
}
|
false, true, portMAX_DELAY);
|
||||||
ESP_LOGI(TAG, "Connected to AP");
|
ESP_LOGI(TAG, "Connected to AP");
|
||||||
|
|
||||||
int err = getaddrinfo(WEB_SERVER, "80", &hints, &res);
|
int err = getaddrinfo(WEB_SERVER, "80", &hints, &res);
|
||||||
@ -153,6 +165,7 @@ static void http_get_task(void *pvParameters)
|
|||||||
|
|
||||||
void app_main()
|
void app_main()
|
||||||
{
|
{
|
||||||
|
wifi_event_group = xEventGroupCreate();
|
||||||
esp_event_set_cb(wifi_event_cb, NULL);
|
esp_event_set_cb(wifi_event_cb, NULL);
|
||||||
set_wifi_configuration();
|
set_wifi_configuration();
|
||||||
xTaskCreate(&http_get_task, "http_get_task", 2048, NULL, 5, NULL);
|
xTaskCreate(&http_get_task, "http_get_task", 2048, NULL, 5, NULL);
|
||||||
|
@ -24,6 +24,7 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "freertos/FreeRTOS.h"
|
#include "freertos/FreeRTOS.h"
|
||||||
#include "freertos/task.h"
|
#include "freertos/task.h"
|
||||||
|
#include "freertos/event_groups.h"
|
||||||
#include "esp_wifi.h"
|
#include "esp_wifi.h"
|
||||||
#include "esp_event.h"
|
#include "esp_event.h"
|
||||||
#include "esp_log.h"
|
#include "esp_log.h"
|
||||||
@ -52,8 +53,13 @@
|
|||||||
#define EXAMPLE_WIFI_SSID CONFIG_WIFI_SSID
|
#define EXAMPLE_WIFI_SSID CONFIG_WIFI_SSID
|
||||||
#define EXAMPLE_WIFI_PASS CONFIG_WIFI_PASSWORD
|
#define EXAMPLE_WIFI_PASS CONFIG_WIFI_PASSWORD
|
||||||
|
|
||||||
/* Flag for when we are connected & ready to make a request */
|
/* FreeRTOS event group to signal when we are connected & ready to make a request */
|
||||||
static volatile bool ready;
|
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;
|
||||||
|
|
||||||
/* Constants that aren't configurable in menuconfig */
|
/* Constants that aren't configurable in menuconfig */
|
||||||
#define WEB_SERVER "www.howsmyssl.com"
|
#define WEB_SERVER "www.howsmyssl.com"
|
||||||
@ -78,7 +84,7 @@ extern const char *server_root_cert;
|
|||||||
to ESP_LOGx debug output.
|
to ESP_LOGx debug output.
|
||||||
|
|
||||||
MBEDTLS_DEBUG_LEVEL 4 means all mbedTLS debug output gets sent here,
|
MBEDTLS_DEBUG_LEVEL 4 means all mbedTLS debug output gets sent here,
|
||||||
and then filtered to the
|
and then filtered to the ESP logging mechanism.
|
||||||
*/
|
*/
|
||||||
static void mbedtls_debug(void *ctx, int level,
|
static void mbedtls_debug(void *ctx, int level,
|
||||||
const char *file, int line,
|
const char *file, int line,
|
||||||
@ -117,11 +123,17 @@ static void mbedtls_debug(void *ctx, int level,
|
|||||||
static esp_err_t wifi_event_cb(void *ctx, system_event_t *event)
|
static esp_err_t wifi_event_cb(void *ctx, system_event_t *event)
|
||||||
{
|
{
|
||||||
switch(event->event_id) {
|
switch(event->event_id) {
|
||||||
|
case SYSTEM_EVENT_STA_START:
|
||||||
|
esp_wifi_connect();
|
||||||
|
break;
|
||||||
case SYSTEM_EVENT_STA_GOT_IP:
|
case SYSTEM_EVENT_STA_GOT_IP:
|
||||||
ready = true;
|
xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
|
||||||
break;
|
break;
|
||||||
case SYSTEM_EVENT_STA_DISCONNECTED:
|
case SYSTEM_EVENT_STA_DISCONNECTED:
|
||||||
ready = false;
|
/* This is a workaround as ESP32 WiFi libs don't currently
|
||||||
|
auto-reassociate. */
|
||||||
|
esp_wifi_connect();
|
||||||
|
xEventGroupClearBits(wifi_event_group, CONNECTED_BIT);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
@ -154,8 +166,6 @@ static void https_get_task(void *pvParameters)
|
|||||||
mbedtls_ssl_config conf;
|
mbedtls_ssl_config conf;
|
||||||
mbedtls_net_context server_fd;
|
mbedtls_net_context server_fd;
|
||||||
|
|
||||||
esp_wifi_connect();
|
|
||||||
|
|
||||||
mbedtls_ssl_init(&ssl);
|
mbedtls_ssl_init(&ssl);
|
||||||
mbedtls_x509_crt_init(&cacert);
|
mbedtls_x509_crt_init(&cacert);
|
||||||
mbedtls_ctr_drbg_init(&ctr_drbg);
|
mbedtls_ctr_drbg_init(&ctr_drbg);
|
||||||
@ -213,29 +223,24 @@ static void https_get_task(void *pvParameters)
|
|||||||
mbedtls_ssl_conf_dbg(&conf, mbedtls_debug, NULL);
|
mbedtls_ssl_conf_dbg(&conf, mbedtls_debug, NULL);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
ESP_LOGI(TAG, "%d free...", system_get_free_heap_size());
|
|
||||||
|
|
||||||
char *x = malloc(8192);
|
|
||||||
memset(x, 'a', 8192);
|
|
||||||
|
|
||||||
ESP_LOGI(TAG, "%d free now...", system_get_free_heap_size());
|
|
||||||
|
|
||||||
if ((ret = mbedtls_ssl_setup(&ssl, &conf)) != 0)
|
if ((ret = mbedtls_ssl_setup(&ssl, &conf)) != 0)
|
||||||
{
|
{
|
||||||
ESP_LOGE(TAG, "mbedtls_ssl_setup returned -0x%x\n\n", -ret);
|
ESP_LOGE(TAG, "mbedtls_ssl_setup returned -0x%x\n\n", -ret);
|
||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
ESP_LOGI(TAG, "Waiting for WiFi online...");
|
|
||||||
while (!ready) {
|
|
||||||
vTaskDelay(1);
|
|
||||||
}
|
|
||||||
ESP_LOGI(TAG, "WiFi is online");
|
|
||||||
|
|
||||||
while(1) {
|
while(1) {
|
||||||
ESP_LOGI(TAG, "Connecting to %s:%s...", WEB_SERVER, WEB_PORT);
|
/* 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");
|
||||||
|
|
||||||
mbedtls_net_init(&server_fd);
|
mbedtls_net_init(&server_fd);
|
||||||
|
|
||||||
|
ESP_LOGI(TAG, "Connecting to %s:%s...", WEB_SERVER, WEB_PORT);
|
||||||
|
|
||||||
if ((ret = mbedtls_net_connect(&server_fd, WEB_SERVER,
|
if ((ret = mbedtls_net_connect(&server_fd, WEB_SERVER,
|
||||||
WEB_PORT, MBEDTLS_NET_PROTO_TCP)) != 0)
|
WEB_PORT, MBEDTLS_NET_PROTO_TCP)) != 0)
|
||||||
{
|
{
|
||||||
@ -343,6 +348,7 @@ static void https_get_task(void *pvParameters)
|
|||||||
|
|
||||||
void app_main()
|
void app_main()
|
||||||
{
|
{
|
||||||
|
wifi_event_group = xEventGroupCreate();
|
||||||
esp_event_set_cb(wifi_event_cb, NULL);
|
esp_event_set_cb(wifi_event_cb, NULL);
|
||||||
set_wifi_configuration();
|
set_wifi_configuration();
|
||||||
xTaskCreate(&https_get_task, "https_get_task", 8192, NULL, 5, NULL);
|
xTaskCreate(&https_get_task, "https_get_task", 8192, NULL, 5, NULL);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user