2018-10-02 14:33:16 +00:00
|
|
|
/* BSD Socket API 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 <string.h>
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/task.h"
|
|
|
|
#include "freertos/event_groups.h"
|
|
|
|
#include "esp_system.h"
|
|
|
|
#include "esp_wifi.h"
|
2018-11-20 16:41:08 +00:00
|
|
|
#include "esp_event.h"
|
2018-10-02 14:33:16 +00:00
|
|
|
#include "esp_log.h"
|
|
|
|
#include "nvs_flash.h"
|
2019-08-31 14:19:21 +00:00
|
|
|
#include "esp_netif.h"
|
2018-11-20 16:41:08 +00:00
|
|
|
#include "protocol_examples_common.h"
|
2020-03-17 19:30:33 +00:00
|
|
|
#include "addr_from_stdin.h"
|
2018-10-02 14:33:16 +00:00
|
|
|
#include "lwip/err.h"
|
|
|
|
#include "lwip/sockets.h"
|
|
|
|
|
|
|
|
|
2020-03-17 19:30:33 +00:00
|
|
|
#if defined(CONFIG_EXAMPLE_IPV4)
|
2018-10-02 14:33:16 +00:00
|
|
|
#define HOST_IP_ADDR CONFIG_EXAMPLE_IPV4_ADDR
|
2020-03-17 19:30:33 +00:00
|
|
|
#elif defined(CONFIG_EXAMPLE_IPV6)
|
2018-10-02 14:33:16 +00:00
|
|
|
#define HOST_IP_ADDR CONFIG_EXAMPLE_IPV6_ADDR
|
2020-03-17 19:30:33 +00:00
|
|
|
#else
|
|
|
|
#define HOST_IP_ADDR ""
|
2018-10-02 14:33:16 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#define PORT CONFIG_EXAMPLE_PORT
|
|
|
|
|
|
|
|
static const char *TAG = "example";
|
|
|
|
static const char *payload = "Message from ESP32 ";
|
|
|
|
|
|
|
|
static void tcp_client_task(void *pvParameters)
|
|
|
|
{
|
|
|
|
char rx_buffer[128];
|
2020-03-17 19:30:33 +00:00
|
|
|
char host_ip[] = HOST_IP_ADDR;
|
|
|
|
int addr_family = 0;
|
|
|
|
int ip_protocol = 0;
|
2018-10-02 14:33:16 +00:00
|
|
|
|
|
|
|
while (1) {
|
2020-03-17 19:30:33 +00:00
|
|
|
#if defined(CONFIG_EXAMPLE_IPV4)
|
2018-11-20 16:41:08 +00:00
|
|
|
struct sockaddr_in dest_addr;
|
2020-03-17 19:30:33 +00:00
|
|
|
dest_addr.sin_addr.s_addr = inet_addr(host_ip);
|
2018-11-20 16:41:08 +00:00
|
|
|
dest_addr.sin_family = AF_INET;
|
|
|
|
dest_addr.sin_port = htons(PORT);
|
2018-10-02 14:33:16 +00:00
|
|
|
addr_family = AF_INET;
|
|
|
|
ip_protocol = IPPROTO_IP;
|
2020-03-17 19:30:33 +00:00
|
|
|
#elif defined(CONFIG_EXAMPLE_IPV6)
|
2020-02-04 15:38:23 +00:00
|
|
|
struct sockaddr_in6 dest_addr = { 0 };
|
2020-03-17 19:30:33 +00:00
|
|
|
inet6_aton(host_ip, &dest_addr.sin6_addr);
|
2018-11-20 16:41:08 +00:00
|
|
|
dest_addr.sin6_family = AF_INET6;
|
|
|
|
dest_addr.sin6_port = htons(PORT);
|
2020-02-04 15:38:23 +00:00
|
|
|
dest_addr.sin6_scope_id = esp_netif_get_netif_impl_index(EXAMPLE_INTERFACE);
|
2018-10-02 14:33:16 +00:00
|
|
|
addr_family = AF_INET6;
|
|
|
|
ip_protocol = IPPROTO_IPV6;
|
2020-03-17 19:30:33 +00:00
|
|
|
#elif defined(CONFIG_EXAMPLE_SOCKET_IP_INPUT_STDIN)
|
|
|
|
struct sockaddr_in6 dest_addr = { 0 };
|
|
|
|
ESP_ERROR_CHECK(get_addr_from_stdin(PORT, SOCK_STREAM, &ip_protocol, &addr_family, &dest_addr));
|
2018-10-02 14:33:16 +00:00
|
|
|
#endif
|
|
|
|
int sock = socket(addr_family, SOCK_STREAM, ip_protocol);
|
|
|
|
if (sock < 0) {
|
|
|
|
ESP_LOGE(TAG, "Unable to create socket: errno %d", errno);
|
|
|
|
break;
|
|
|
|
}
|
2020-03-17 19:30:33 +00:00
|
|
|
ESP_LOGI(TAG, "Socket created, connecting to %s:%d", host_ip, PORT);
|
2018-10-02 14:33:16 +00:00
|
|
|
|
2020-03-17 19:30:33 +00:00
|
|
|
int err = connect(sock, (struct sockaddr *)&dest_addr, sizeof(struct sockaddr_in6));
|
2018-10-02 14:33:16 +00:00
|
|
|
if (err != 0) {
|
|
|
|
ESP_LOGE(TAG, "Socket unable to connect: errno %d", errno);
|
2019-02-21 08:25:07 +00:00
|
|
|
break;
|
2018-10-02 14:33:16 +00:00
|
|
|
}
|
|
|
|
ESP_LOGI(TAG, "Successfully connected");
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
int err = send(sock, payload, strlen(payload), 0);
|
|
|
|
if (err < 0) {
|
2018-11-20 16:41:08 +00:00
|
|
|
ESP_LOGE(TAG, "Error occurred during sending: errno %d", errno);
|
2018-10-02 14:33:16 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
int len = recv(sock, rx_buffer, sizeof(rx_buffer) - 1, 0);
|
2018-11-20 16:41:08 +00:00
|
|
|
// Error occurred during receiving
|
2018-10-02 14:33:16 +00:00
|
|
|
if (len < 0) {
|
|
|
|
ESP_LOGE(TAG, "recv failed: errno %d", errno);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Data received
|
|
|
|
else {
|
|
|
|
rx_buffer[len] = 0; // Null-terminate whatever we received and treat like a string
|
2020-03-17 19:30:33 +00:00
|
|
|
ESP_LOGI(TAG, "Received %d bytes from %s:", len, host_ip);
|
2018-10-02 14:33:16 +00:00
|
|
|
ESP_LOGI(TAG, "%s", rx_buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
vTaskDelay(2000 / portTICK_PERIOD_MS);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sock != -1) {
|
|
|
|
ESP_LOGE(TAG, "Shutting down socket and restarting...");
|
|
|
|
shutdown(sock, 0);
|
|
|
|
close(sock);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
vTaskDelete(NULL);
|
|
|
|
}
|
|
|
|
|
2019-07-16 09:33:30 +00:00
|
|
|
void app_main(void)
|
2018-10-02 14:33:16 +00:00
|
|
|
{
|
2018-11-20 16:41:08 +00:00
|
|
|
ESP_ERROR_CHECK(nvs_flash_init());
|
2019-11-29 09:54:02 +00:00
|
|
|
ESP_ERROR_CHECK(esp_netif_init());
|
2018-11-20 16:41:08 +00:00
|
|
|
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());
|
2018-10-02 14:33:16 +00:00
|
|
|
|
|
|
|
xTaskCreate(tcp_client_task, "tcp_client", 4096, NULL, 5, NULL);
|
|
|
|
}
|