esp idf wifi tcp数据发送接收不同数组例程
时间: 2023-07-30 09:04:21 浏览: 169
ESP32 IDF开发-ESP32 MQTT_TCP例程
5星 · 资源好评率100%
下面是ESP-IDF的WiFi TCP数据发送与接收不同数组的例程:
发送方代码:
```c
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_wifi.h"
#include "esp_event_loop.h"
#include "esp_log.h"
#include "nvs_flash.h"
#include "lwip/err.h"
#include "lwip/sys.h"
#include "lwip/sockets.h"
#define WIFI_SSID "your_wifi_ssid"
#define WIFI_PASS "your_wifi_password"
#define HOST_IP_ADDR "192.168.1.100" // 远程主机IP地址
#define HOST_PORT 12345 // 远程主机端口号
static const char *TAG = "tcp_send";
void wifi_init_sta() {
static const wifi_sta_config_t sta_config = {
.ssid = WIFI_SSID,
.password = WIFI_PASS,
};
esp_wifi_set_mode(WIFI_MODE_STA);
esp_wifi_set_config(ESP_IF_WIFI_STA, &sta_config);
esp_wifi_start();
}
void tcp_send_task(void *pvParameters) {
char send_buf[1024] = {0};
while (1) {
int sockfd;
struct sockaddr_in dest_addr;
sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sockfd == -1) {
ESP_LOGE(TAG, "Failed to create socket. errno=%d", errno);
break;
}
memset(&dest_addr, 0, sizeof(dest_addr));
dest_addr.sin_family = AF_INET;
dest_addr.sin_addr.s_addr = inet_addr(HOST_IP_ADDR);
dest_addr.sin_port = htons(HOST_PORT);
if (connect(sockfd, (struct sockaddr *)&dest_addr, sizeof(dest_addr)) != 0) {
ESP_LOGE(TAG, "Failed to connect. errno=%d", errno);
close(sockfd);
break;
}
ESP_LOGI(TAG, "Connected to server");
sprintf(send_buf, "Hello from ESP32!");
if (send(sockfd, send_buf, strlen(send_buf), 0) == -1) {
ESP_LOGE(TAG, "Failed to send. errno=%d", errno);
close(sockfd);
break;
}
ESP_LOGI(TAG, "Sent data");
close(sockfd);
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
vTaskDelete(NULL);
}
void app_main() {
nvs_flash_init();
tcpip_adapter_init();
esp_event_loop_create_default();
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
esp_wifi_init(&cfg);
wifi_init_sta();
xTaskCreate(tcp_send_task, "tcp_send_task", 4096, NULL, 5, NULL);
}
```
接收方代码:
```c
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_wifi.h"
#include "esp_event_loop.h"
#include "esp_log.h"
#include "nvs_flash.h"
#include "lwip/err.h"
#include "lwip/sys.h"
#include "lwip/sockets.h"
#define WIFI_SSID "your_wifi_ssid"
#define WIFI_PASS "your_wifi_password"
#define HOST_PORT 12345 // 监听端口号
static const char *TAG = "tcp_recv";
void wifi_init_sta() {
static const wifi_sta_config_t sta_config = {
.ssid = WIFI_SSID,
.password = WIFI_PASS,
};
esp_wifi_set_mode(WIFI_MODE_STA);
esp_wifi_set_config(ESP_IF_WIFI_STA, &sta_config);
esp_wifi_start();
}
void tcp_recv_task(void *pvParameters) {
char recv_buf[1024] = {0};
while (1) {
int sockfd, new_sockfd;
struct sockaddr_in dest_addr;
socklen_t addr_len = sizeof(dest_addr);
sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sockfd == -1) {
ESP_LOGE(TAG, "Failed to create socket. errno=%d", errno);
break;
}
memset(&dest_addr, 0, sizeof(dest_addr));
dest_addr.sin_family = AF_INET;
dest_addr.sin_addr.s_addr = htonl(INADDR_ANY);
dest_addr.sin_port = htons(HOST_PORT);
if (bind(sockfd, (struct sockaddr *)&dest_addr, sizeof(dest_addr)) != 0) {
ESP_LOGE(TAG, "Failed to bind. errno=%d", errno);
close(sockfd);
break;
}
if (listen(sockfd, 5) != 0) {
ESP_LOGE(TAG, "Failed to listen. errno=%d", errno);
close(sockfd);
break;
}
ESP_LOGI(TAG, "Listening...");
new_sockfd = accept(sockfd, (struct sockaddr *)&dest_addr, &addr_len);
if (new_sockfd < 0) {
ESP_LOGE(TAG, "Failed to accept. errno=%d", errno);
close(sockfd);
break;
}
ESP_LOGI(TAG, "Connected to client");
while (1) {
int len = recv(new_sockfd, recv_buf, sizeof(recv_buf) - 1, 0);
if (len < 0) {
ESP_LOGE(TAG, "Failed to receive. errno=%d", errno);
break;
} else if (len == 0) {
ESP_LOGI(TAG, "Connection closed");
break;
} else {
recv_buf[len] = '\0';
ESP_LOGI(TAG, "Received data: %s", recv_buf);
}
}
close(new_sockfd);
close(sockfd);
}
vTaskDelete(NULL);
}
void app_main() {
nvs_flash_init();
tcpip_adapter_init();
esp_event_loop_create_default();
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
esp_wifi_init(&cfg);
wifi_init_sta();
xTaskCreate(tcp_recv_task, "tcp_recv_task", 4096, NULL, 5, NULL);
}
```
以上代码中,发送方通过WiFi连接到远程主机并向其发送数据,接收方在本地监听指定端口,并接收来自远程主机的数据。发送方和接收方都使用了1024字节的数组来存储数据,但实际上可以根据需要调整数组大小。
阅读全文