ESP32-C3, esp-idf 4.3.5,连入指定无线网后做为tcp client循环向指定ip收发数据,要有重连机制,提供个例程,
时间: 2023-12-08 15:06:45 浏览: 112
以下是一个简单的 ESP32-C3 ESP-IDF 4.3.5 示例程序,它将作为 TCP 客户端连接到指定的 IP 地址,并循环收发数据。如果连接断开,它将尝试重新连接。
```c
#include <stdio.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_wifi.h"
#include "esp_system.h"
#include "esp_event.h"
#include "esp_log.h"
#include "esp_netif.h"
#include "esp_tls.h"
#include "lwip/sockets.h"
#include "lwip/dns.h"
#include "lwip/netdb.h"
#define WIFI_SSID "your_wifi_ssid"
#define WIFI_PASSWORD "your_wifi_password"
#define HOST_IP_ADDR "10.0.0.2"
#define HOST_PORT 1234
#define TAG "TCP_CLIENT"
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)
{
switch (event_id) {
case WIFI_EVENT_STA_START:
esp_wifi_connect();
break;
case WIFI_EVENT_STA_DISCONNECTED:
if (s_retry_num < 3) {
esp_wifi_connect();
s_retry_num++;
ESP_LOGI(TAG, "retry to connect to the AP");
}
break;
default:
break;
}
}
static void tcp_client_task(void* arg)
{
char rx_buffer[128];
char tx_buffer[128];
int sock = -1;
struct sockaddr_in server_addr;
while (1) {
// Wait for Wi-Fi connection
xEventGroupWaitBits(wifi_event_group, WIFI_CONNECTED_BIT, false, true, portMAX_DELAY);
// Create socket
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
if (sock < 0) {
ESP_LOGE(TAG, "Failed to create socket");
break;
}
// Set server address
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = inet_addr(HOST_IP_ADDR);
server_addr.sin_port = htons(HOST_PORT);
// Connect to server
if (connect(sock, (struct sockaddr*)&server_addr, sizeof(server_addr)) != 0) {
ESP_LOGE(TAG, "Failed to connect to server");
close(sock);
continue;
}
ESP_LOGI(TAG, "Connected to server");
while (1) {
// Receive data from server
int rx_len = recv(sock, rx_buffer, sizeof(rx_buffer) - 1, 0);
if (rx_len < 0) {
ESP_LOGE(TAG, "Failed to receive data");
break;
} else if (rx_len == 0) {
ESP_LOGI(TAG, "Server disconnected");
break;
}
rx_buffer[rx_len] = '\0';
ESP_LOGI(TAG, "Received: %s", rx_buffer);
// Send data to server
sprintf(tx_buffer, "Hello, server!");
int tx_len = send(sock, tx_buffer, strlen(tx_buffer), 0);
if (tx_len < 0) {
ESP_LOGE(TAG, "Failed to send data");
break;
}
}
close(sock);
}
vTaskDelete(NULL);
}
void app_main(void)
{
// Initialize Wi-Fi
tcpip_adapter_init();
wifi_event_group = xEventGroupCreate();
esp_event_loop_create_default();
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
esp_wifi_init(&cfg);
esp_wifi_set_mode(WIFI_MODE_STA);
esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config);
esp_wifi_start();
// Register Wi-Fi event handler
esp_event_handler_instance_t instance_any_id;
esp_event_handler_instance_t instance_got_ip;
esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL, &instance_any_id);
esp_event_handler_instance_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &wifi_event_handler, NULL, &instance_got_ip);
// Wait for Wi-Fi connection
xEventGroupWaitBits(wifi_event_group, WIFI_CONNECTED_BIT, false, true, portMAX_DELAY);
// Start TCP client task
xTaskCreate(tcp_client_task, "tcp_client_task", 4096, NULL, 5, NULL);
}
```
请注意,此示例程序假定您已经将 Wi-Fi 配置为连接到指定的 SSID,并已经设置了 Wi-Fi 密码。如果您还没有这样做,请将 `WIFI_SSID` 和 `WIFI_PASSWORD` 宏定义替换为您自己的 SSID 和密码。
阅读全文