esp idf wifi tcp数据发送例程
时间: 2023-12-22 10:03:42 浏览: 227
ESP32 IDF开发-ESP32 MQTT_TCP例程
5星 · 资源好评率100%
以下是 ESP-IDF 中使用 WiFi TCP 发送数据的示例代码:
```c
#include <stdio.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_wifi.h"
#include "esp_event_loop.h"
#include "esp_log.h"
#include "tcpip_adapter.h"
#define EXAMPLE_ESP_WIFI_SSID "your_wifi_ssid"
#define EXAMPLE_ESP_WIFI_PASS "your_wifi_password"
#define EXAMPLE_ESP_MAXIMUM_RETRY 3
static const char *TAG = "example";
static EventGroupHandle_t s_wifi_event_group;
const int WIFI_CONNECTED_BIT = BIT0;
static int s_retry_num = 0;
static void wifi_event_handler(void* arg, esp_event_base_t event_base,
int32_t event_id, void* event_data)
{
if (event_id == WIFI_EVENT_STA_START) {
esp_wifi_connect();
} else if (event_id == WIFI_EVENT_STA_DISCONNECTED) {
if (s_retry_num < EXAMPLE_ESP_MAXIMUM_RETRY) {
esp_wifi_connect();
s_retry_num++;
ESP_LOGI(TAG, "retry to connect to the AP");
}
ESP_LOGI(TAG,"connect to the AP fail");
} else if (event_id == IP_EVENT_STA_GOT_IP) {
ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip));
s_retry_num = 0;
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
}
}
void wifi_init_sta(void)
{
s_wifi_event_group = xEventGroupCreate();
tcpip_adapter_init();
ESP_ERROR_CHECK(esp_event_loop_create_default());
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
wifi_config_t wifi_config = {
.sta = {
.ssid = EXAMPLE_ESP_WIFI_SSID,
.password = EXAMPLE_ESP_WIFI_PASS,
},
};
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());
ESP_LOGI(TAG, "wifi_init_sta finished.");
xEventGroupWaitBits(s_wifi_event_group, WIFI_CONNECTED_BIT, false, true, portMAX_DELAY);
}
void tcp_client_task(void *pvParameters)
{
char buffer[128];
int sock = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
if (sock < 0) {
ESP_LOGE(TAG, "Unable to create socket: errno %d", errno);
vTaskDelete(NULL);
return;
}
struct sockaddr_in server_address;
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = inet_addr("192.168.1.100");
server_address.sin_port = htons(8080);
int err = connect(sock, (struct sockaddr *)&server_address, sizeof(server_address));
if (err != 0) {
ESP_LOGE(TAG, "Socket unable to connect: errno %d", errno);
close(sock);
vTaskDelete(NULL);
return;
}
while (1) {
sprintf(buffer, "Hello from ESP32!");
int err = send(sock, buffer, strlen(buffer), 0);
if (err < 0) {
ESP_LOGE(TAG, "Error occurred during sending: errno %d", errno);
break;
}
vTaskDelay(2000 / portTICK_PERIOD_MS);
}
shutdown(sock, 0);
close(sock);
vTaskDelete(NULL);
}
void app_main()
{
ESP_LOGI(TAG, "ESP_WIFI_MODE_STA");
wifi_init_sta();
xTaskCreate(tcp_client_task, "tcp_client", 4096, NULL, 5, NULL);
}
```
以上代码实现了一个 TCP 客户端任务,与 IP 地址为 192.168.1.100、端口号为 8080 的服务器建立连接,并每隔 2 秒发送一条字符串数据。在使用时需要将 EXAMPLE_ESP_WIFI_SSID 和 EXAMPLE_ESP_WIFI_PASS 替换为实际的 WiFi SSID 和密码,并且根据实际情况修改服务器的 IP 地址和端口号。
阅读全文