mirror of
https://github.com/espressif/esp-idf.git
synced 2025-08-09 12:35:28 +00:00
examples/protocols/sockets: use common network component
This commit is contained in:
@@ -13,9 +13,11 @@
|
||||
#include "freertos/event_groups.h"
|
||||
#include "esp_system.h"
|
||||
#include "esp_wifi.h"
|
||||
#include "esp_event_loop.h"
|
||||
#include "esp_event.h"
|
||||
#include "esp_log.h"
|
||||
#include "nvs_flash.h"
|
||||
#include "tcpip_adapter.h"
|
||||
#include "protocol_examples_common.h"
|
||||
|
||||
#include "lwip/err.h"
|
||||
#include "lwip/sockets.h"
|
||||
@@ -23,14 +25,6 @@
|
||||
#include <lwip/netdb.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
|
||||
|
||||
#ifdef CONFIG_EXAMPLE_IPV4
|
||||
#define HOST_IP_ADDR CONFIG_EXAMPLE_IPV4_ADDR
|
||||
#else
|
||||
@@ -39,77 +33,9 @@
|
||||
|
||||
#define PORT CONFIG_EXAMPLE_PORT
|
||||
|
||||
/* FreeRTOS event group to signal when we are connected & ready to make a request */
|
||||
static EventGroupHandle_t wifi_event_group;
|
||||
|
||||
const int IPV4_GOTIP_BIT = BIT0;
|
||||
const int IPV6_GOTIP_BIT = BIT1;
|
||||
|
||||
static const char *TAG = "example";
|
||||
static const char *payload = "Message from ESP32 ";
|
||||
|
||||
static esp_err_t event_handler(void *ctx, system_event_t *event)
|
||||
{
|
||||
switch (event->event_id) {
|
||||
case SYSTEM_EVENT_STA_START:
|
||||
esp_wifi_connect();
|
||||
ESP_LOGI(TAG, "SYSTEM_EVENT_STA_START");
|
||||
break;
|
||||
case SYSTEM_EVENT_STA_CONNECTED:
|
||||
/* enable ipv6 */
|
||||
tcpip_adapter_create_ip6_linklocal(TCPIP_ADAPTER_IF_STA);
|
||||
break;
|
||||
case SYSTEM_EVENT_STA_GOT_IP:
|
||||
xEventGroupSetBits(wifi_event_group, IPV4_GOTIP_BIT);
|
||||
ESP_LOGI(TAG, "SYSTEM_EVENT_STA_GOT_IP");
|
||||
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, IPV4_GOTIP_BIT);
|
||||
xEventGroupClearBits(wifi_event_group, IPV6_GOTIP_BIT);
|
||||
break;
|
||||
case SYSTEM_EVENT_AP_STA_GOT_IP6:
|
||||
xEventGroupSetBits(wifi_event_group, IPV6_GOTIP_BIT);
|
||||
ESP_LOGI(TAG, "SYSTEM_EVENT_STA_GOT_IP6");
|
||||
|
||||
char *ip6 = ip6addr_ntoa(&event->event_info.got_ip6.ip6_info.ip);
|
||||
ESP_LOGI(TAG, "IPv6: %s", ip6);
|
||||
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 wait_for_ip()
|
||||
{
|
||||
uint32_t bits = IPV4_GOTIP_BIT | IPV6_GOTIP_BIT ;
|
||||
|
||||
ESP_LOGI(TAG, "Waiting for AP connection...");
|
||||
xEventGroupWaitBits(wifi_event_group, bits, false, true, portMAX_DELAY);
|
||||
ESP_LOGI(TAG, "Connected to AP");
|
||||
}
|
||||
|
||||
static void tcp_client_task(void *pvParameters)
|
||||
{
|
||||
char rx_buffer[128];
|
||||
@@ -120,21 +46,21 @@ static void tcp_client_task(void *pvParameters)
|
||||
while (1) {
|
||||
|
||||
#ifdef CONFIG_EXAMPLE_IPV4
|
||||
struct sockaddr_in destAddr;
|
||||
destAddr.sin_addr.s_addr = inet_addr(HOST_IP_ADDR);
|
||||
destAddr.sin_family = AF_INET;
|
||||
destAddr.sin_port = htons(PORT);
|
||||
struct sockaddr_in dest_addr;
|
||||
dest_addr.sin_addr.s_addr = inet_addr(HOST_IP_ADDR);
|
||||
dest_addr.sin_family = AF_INET;
|
||||
dest_addr.sin_port = htons(PORT);
|
||||
addr_family = AF_INET;
|
||||
ip_protocol = IPPROTO_IP;
|
||||
inet_ntoa_r(destAddr.sin_addr, addr_str, sizeof(addr_str) - 1);
|
||||
inet_ntoa_r(dest_addr.sin_addr, addr_str, sizeof(addr_str) - 1);
|
||||
#else // IPV6
|
||||
struct sockaddr_in6 destAddr;
|
||||
inet6_aton(HOST_IP_ADDR, &destAddr.sin6_addr);
|
||||
destAddr.sin6_family = AF_INET6;
|
||||
destAddr.sin6_port = htons(PORT);
|
||||
struct sockaddr_in6 dest_addr;
|
||||
inet6_aton(HOST_IP_ADDR, &dest_addr.sin6_addr);
|
||||
dest_addr.sin6_family = AF_INET6;
|
||||
dest_addr.sin6_port = htons(PORT);
|
||||
addr_family = AF_INET6;
|
||||
ip_protocol = IPPROTO_IPV6;
|
||||
inet6_ntoa_r(destAddr.sin6_addr, addr_str, sizeof(addr_str) - 1);
|
||||
inet6_ntoa_r(dest_addr.sin6_addr, addr_str, sizeof(addr_str) - 1);
|
||||
#endif
|
||||
|
||||
int sock = socket(addr_family, SOCK_STREAM, ip_protocol);
|
||||
@@ -142,9 +68,9 @@ static void tcp_client_task(void *pvParameters)
|
||||
ESP_LOGE(TAG, "Unable to create socket: errno %d", errno);
|
||||
break;
|
||||
}
|
||||
ESP_LOGI(TAG, "Socket created");
|
||||
ESP_LOGI(TAG, "Socket created, connecting to %s:%d", HOST_IP_ADDR, PORT);
|
||||
|
||||
int err = connect(sock, (struct sockaddr *)&destAddr, sizeof(destAddr));
|
||||
int err = connect(sock, (struct sockaddr *)&dest_addr, sizeof(dest_addr));
|
||||
if (err != 0) {
|
||||
ESP_LOGE(TAG, "Socket unable to connect: errno %d", errno);
|
||||
break;
|
||||
@@ -154,12 +80,12 @@ static void tcp_client_task(void *pvParameters)
|
||||
while (1) {
|
||||
int err = send(sock, payload, strlen(payload), 0);
|
||||
if (err < 0) {
|
||||
ESP_LOGE(TAG, "Error occured during sending: errno %d", errno);
|
||||
ESP_LOGE(TAG, "Error occurred during sending: errno %d", errno);
|
||||
break;
|
||||
}
|
||||
|
||||
int len = recv(sock, rx_buffer, sizeof(rx_buffer) - 1, 0);
|
||||
// Error occured during receiving
|
||||
// Error occurred during receiving
|
||||
if (len < 0) {
|
||||
ESP_LOGE(TAG, "recv failed: errno %d", errno);
|
||||
break;
|
||||
@@ -185,9 +111,15 @@ static void tcp_client_task(void *pvParameters)
|
||||
|
||||
void app_main()
|
||||
{
|
||||
ESP_ERROR_CHECK( nvs_flash_init() );
|
||||
initialise_wifi();
|
||||
wait_for_ip();
|
||||
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());
|
||||
|
||||
xTaskCreate(tcp_client_task, "tcp_client", 4096, NULL, 5, NULL);
|
||||
}
|
||||
|
Reference in New Issue
Block a user