ESP8266学习二---SOCKET-TCP_CLIENT

    上次学习了wifi热点信息扫描的东西。本次测试学习下tcp client的内容。

    全部源码如下:

/* 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"
#include "esp_event_loop.h"
#include "esp_log.h"
#include "nvs_flash.h"

#include "lwip/err.h"
#include "lwip/sockets.h"
#include "lwip/sys.h"
#include <lwip/netdb.h>


/* The examples use simple WiFi configuration that you can set via
   'make menuconfig'.
   If you'd rather not, just change the below entries to strings with
   the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid"
*/

#define EXAMPLE_WIFI_SSID CONFIG_WIFI_SSID
#define EXAMPLE_WIFI_PASS CONFIG_WIFI_PASSWORD

#ifdef CONFIG_EXAMPLE_IPV4
#define HOST_IP_ADDR CONFIG_EXAMPLE_IPV4_ADDR
#else
#define HOST_IP_ADDR CONFIG_EXAMPLE_IPV6_ADDR
#endif

#define PORT CONFIG_EXAMPLE_PORT

/* FreeRTOS event group to signal when we are connected & ready to make a request */
static EventGroupHandle_t wifi_event_group;

const int 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以在ESP32-C3上使用以下代码来设置TCP客户端连接WiFi超时重连: ```c #include "esp_wifi.h" #include "esp_log.h" #include "lwip/err.h" #include "lwip/sys.h" #include "lwip/netdb.h" #include "lwip/dns.h" #include "lwip/sockets.h" #define WIFI_TIMEOUT_MS 10000 // 设置连接WiFi的超时时间为10s #define HOST_IP_ADDR "192.168.1.100" // 设置TCP服务器IP地址 #define HOST_PORT 8080 // 设置TCP服务器端口号 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(); // WiFi STA模式启动时连接WiFi } else if (event_id == WIFI_EVENT_STA_DISCONNECTED) { esp_wifi_connect(); // WiFi STA模式连接断开时重新连接WiFi } } void wifi_init_sta() { wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); esp_wifi_init(&cfg); esp_wifi_set_mode(WIFI_MODE_STA); wifi_config_t wifi_config = { .sta = { .ssid = "your_wifi_ssid", .password = "your_wifi_password", }, }; esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config); esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL); esp_wifi_start(); TickType_t start_tick = xTaskGetTickCount(); // 记录连接WiFi的开始时间 while (1) { wifi_ap_record_t ap_info; if (esp_wifi_sta_get_ap_info(&ap_info) == ESP_OK) { // 获取连接到的WiFi的信息 printf("Connected to %s\n", ap_info.ssid); break; // 连接成功,退出循环 } TickType_t now_tick = xTaskGetTickCount(); if ((now_tick - start_tick) * portTICK_PERIOD_MS > WIFI_TIMEOUT_MS) { // 超时未连接成功,则重新连接WiFi printf("WiFi connection timeout, retrying...\n"); esp_wifi_disconnect(); esp_wifi_connect(); start_tick = now_tick; } vTaskDelay(pdMS_TO_TICKS(1000)); // 等待1s后再次尝试连接WiFi } } void tcp_client_task(void *pvParameters) { int ret; int sock = -1; struct sockaddr_in dest_addr; while (1) { sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (sock < 0) { ESP_LOGE("TCP CLIENT", "Failed to create socket"); continue; } dest_addr.sin_family = AF_INET; dest_addr.sin_port = htons(HOST_PORT); dest_addr.sin_addr.s_addr = inet_addr(HOST_IP_ADDR); ret = connect(sock, (struct sockaddr*)&dest_addr, sizeof(dest_addr)); if (ret != 0) { ESP_LOGE("TCP CLIENT", "Failed to connect to server"); close(sock); continue; } ESP_LOGI("TCP CLIENT", "Connected to server"); while (1) { // TODO: 在此处添加TCP客户端的业务逻辑 vTaskDelay(pdMS_TO_TICKS(1000)); } close(sock); } } void app_main() { esp_err_t ret = nvs_flash_init(); if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { ESP_ERROR_CHECK(nvs_flash_erase()); ret = nvs_flash_init(); } ESP_ERROR_CHECK(ret); tcpip_adapter_init(); wifi_init_sta(); xTaskCreate(tcp_client_task, "tcp_client_task", 4096, NULL, 5, NULL); } ``` 在该代码中,我们使用了TCP客户端的示例代码,并在其中加入了WiFi连接超时重连的逻辑。在程序初始化时,我们启动了WiFi STA模式并设置连接的WiFi SSID和密码。在连接WiFi时,我们通过`esp_wifi_sta_get_ap_info()`函数获取连接到的WiFi信息,如果10s内未连接成功,则会重新连接WiFi。在TCP连接失败时,我们会关闭套接字并重新尝试连接。当TCP连接成功时,我们可以在`tcp_client_task()`函数中添加TCP客户端的业务逻辑。 注意:在使用该代码时,需要将`your_wifi_ssid`和`your_wifi_password`替换为实际的WiFi SSID和密码,并将`HOST_IP_ADDR`和`HOST_PORT`替换为实际的TCP服务器IP地址和端口号。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值