ESP32-C3 esp-idf 4.3.5 连入指定无线网后向指定ip发送数据
时间: 2023-08-13 14:09:04 浏览: 265
esp32用到的esp-idf工具
在 ESP32-C3 的 STA 模式下,可以通过 ESP-IDF 中提供的 Wi-Fi 库连接到指定的无线网络,然后向指定 IP 发送数据。具体步骤如下:
1. 配置 Wi-Fi 连接:
```c
#include "esp_wifi.h"
#include "esp_event.h"
#include "esp_log.h"
#define WIFI_SSID "无线网络名称"
#define WIFI_PASS "无线网络密码"
static esp_err_t wifi_event_handler(void *ctx, system_event_t *event)
{
switch (event->event_id) {
case SYSTEM_EVENT_STA_START:
esp_wifi_connect();
break;
case SYSTEM_EVENT_STA_CONNECTED:
ESP_LOGI(TAG, "Connected to AP");
break;
case SYSTEM_EVENT_STA_GOT_IP:
ESP_LOGI(TAG, "Got IP address");
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
ESP_LOGI(TAG, "Disconnected from AP");
esp_wifi_connect();
break;
default:
break;
}
return ESP_OK;
}
void wifi_init_sta(void)
{
tcpip_adapter_init();
ESP_ERROR_CHECK(esp_event_loop_create_default());
wifi_init_config_t wifi_init_config = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&wifi_init_config));
ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL));
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &wifi_event_handler, NULL));
wifi_config_t wifi_config = {
.sta = {
.ssid = WIFI_SSID,
.password = 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());
}
```
2. 创建一个 TCP 连接并连接到指定 IP:
```c
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
void send_data_to_ip(void)
{
// 创建目标地址
struct sockaddr_in dest_addr;
dest_addr.sin_addr.s_addr = inet_addr("目标IP地址");
dest_addr.sin_family = AF_INET;
dest_addr.sin_port = htons(目标端口);
// 创建 socket
int socket_fd = socket(AF_INET, SOCK_STREAM, 0);
if (socket_fd < 0) {
printf("Failed to create socket\n");
return;
}
// 连接到目标地址
if (connect(socket_fd, (struct sockaddr *)&dest_addr, sizeof(dest_addr)) != 0) {
printf("Failed to connect to server\n");
close(socket_fd);
return;
}
// 发送数据
const char *data = "要发送的数据";
if (send(socket_fd, data, strlen(data), 0) < 0) {
printf("Failed to send data\n");
close(socket_fd);
return;
}
// 关闭连接
close(socket_fd);
}
```
3. 完整的代码示例:
```c
#include "esp_wifi.h"
#include "esp_event.h"
#include "esp_log.h"
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define WIFI_SSID "无线网络名称"
#define WIFI_PASS "无线网络密码"
static esp_err_t wifi_event_handler(void *ctx, system_event_t *event)
{
switch (event->event_id) {
case SYSTEM_EVENT_STA_START:
esp_wifi_connect();
break;
case SYSTEM_EVENT_STA_CONNECTED:
ESP_LOGI(TAG, "Connected to AP");
break;
case SYSTEM_EVENT_STA_GOT_IP:
ESP_LOGI(TAG, "Got IP address");
send_data_to_ip();
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
ESP_LOGI(TAG, "Disconnected from AP");
esp_wifi_connect();
break;
default:
break;
}
return ESP_OK;
}
void wifi_init_sta(void)
{
tcpip_adapter_init();
ESP_ERROR_CHECK(esp_event_loop_create_default());
wifi_init_config_t wifi_init_config = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&wifi_init_config));
ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL));
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &wifi_event_handler, NULL));
wifi_config_t wifi_config = {
.sta = {
.ssid = WIFI_SSID,
.password = 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());
}
void send_data_to_ip(void)
{
// 创建目标地址
struct sockaddr_in dest_addr;
dest_addr.sin_addr.s_addr = inet_addr("目标IP地址");
dest_addr.sin_family = AF_INET;
dest_addr.sin_port = htons(目标端口);
// 创建 socket
int socket_fd = socket(AF_INET, SOCK_STREAM, 0);
if (socket_fd < 0) {
printf("Failed to create socket\n");
return;
}
// 连接到目标地址
if (connect(socket_fd, (struct sockaddr *)&dest_addr, sizeof(dest_addr)) != 0) {
printf("Failed to connect to server\n");
close(socket_fd);
return;
}
// 发送数据
const char *data = "要发送的数据";
if (send(socket_fd, data, strlen(data), 0) < 0) {
printf("Failed to send data\n");
close(socket_fd);
return;
}
// 关闭连接
close(socket_fd);
}
```
需要注意的是,以上代码仅提供了一个示例,实际应用中需要根据具体需求进行修改和完善。
阅读全文