ESP32 TCP sock长连接 断线重连

wifi配网代码如下:

#include "freertos/FreeRTOS.h"
#include "esp_wifi.h"
#include "esp_system.h"
#include "esp_event.h"
#include "esp_event_loop.h"
#include "nvs_flash.h"
#include "driver/gpio.h"
#include "wifi_tcp.h"

#define CONFIG_ESP_WIFI_SSID "jack"
#define CONFIG_ESP_WIFI_PASSWORD         "jack.12306"

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_GOT_IP:
		tcp_ini_client();
		break;
	case SYSTEM_EVENT_STA_DISCONNECTED:
		esp_wifi_connect();
		break;
	default:
		break;
	}
	return ESP_OK;
}

void app_main(void) {
	nvs_flash_init();
	tcpip_adapter_init();
	ESP_ERROR_CHECK(esp_event_loop_init(wifi_event_handler, NULL));
	wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT()
	;
	ESP_ERROR_CHECK(esp_wifi_init(&cfg));
	ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
	ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
	wifi_config_t sta_config = { .sta = { .ssid = CONFIG_ESP_WIFI_SSID,
			.password = CONFIG_ESP_WIFI_PASSWORD, .bssid_set = false } };
	ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &sta_config));
	ESP_ERROR_CHECK(esp_wifi_start());
	ESP_ERROR_CHECK(esp_wifi_connect());

}

tcp连接代码如下:
wifi_tcp.h

/*
 * test.h
 *
 *  Created on: 2019年10月19日
 *      Author: Administrator
 */

#ifndef _WIFI_TCP_H_
#define _WIFI_TCP_H_

#include 
#include 
#include 
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_system.h"
#include "esp_wifi.h"
#include "esp_event.h"
#include "esp_log.h"
#include "nvs_flash.h"
#include "tcpip_adapter.h"
#include "lwip/err.h"
#include "lwip/sockets.h"
#include "lwip/sys.h"
#include 

void tcp_ini_client(void);

#define OPENSSL_DEMO_THREAD_STACK_WORDS 10240
#define OPENSSL_DEMO_THREAD_PRORIOTY    8


#endif /* MAIN_TEST_H_ */

wifi_tcp.c

#include "wifi_tcp.h"
#define HOST_IP_ADDR "192.168.2.188"
#define PORT         3355
int tcpsock = 0;
struct sockaddr_in dest_addr;
TaskHandle_t xtcpRecvTask = NULL;
void tcp_recv_data(void *pvParameters);
xTaskHandle xHandle;
void init_socket() {
	tcpsock = socket(AF_INET, SOCK_STREAM, 0);
	printf("sock:%d\n", tcpsock);
	if (tcpsock < 0) {
		printf("Unable to create socket: errno %d", errno);
		return;
	}
	printf("Socket created, sending to %s:%d", HOST_IP_ADDR, PORT);
	dest_addr.sin_addr.s_addr = inet_addr(HOST_IP_ADDR);
	dest_addr.sin_family = AF_INET;
	dest_addr.sin_port = htons(PORT);
}

void connect_socket(int reConflag) {
	init_socket();
	int err = connect(tcpsock, (struct sockaddr * ) &dest_addr,
			sizeof(dest_addr));
	if (err != 0) {
		printf("Socket unable to connect: errno %d", errno);
		close(tcpsock);
		tcpsock = -1;
		vTaskDelay(3000 / portTICK_PERIOD_MS);
		connect_socket(reConflag);
	} else {
		printf("Successfully connected\n");
		if (!reConflag) {
			xTaskCreate(tcp_recv_data, "tcp_recv_data",
					OPENSSL_DEMO_THREAD_STACK_WORDS, NULL,
					OPENSSL_DEMO_THREAD_PRORIOTY, &xHandle);
		}

	}

}

//周期定时器的回调函数
void tcp_reconnect(int reConflag) {
	printf("zhaoming-------tcp_reconnect-----");
	connect_socket(reConflag);

}

void tcp_recv_data(void *pvParameters) {
	uint8_t rx_buffer[1024] = { 0 };
	while (1) {
		int len = recv(tcpsock, rx_buffer, sizeof(rx_buffer) - 1, 0);
		if (len > 0) {
			rx_buffer[len] = 0; // Null-terminate whatever we received and treat like a string
			printf("Received %d bytes from %s:\n", len, rx_buffer);
//			int err = send(tcpsock, rx_buffer, len, 0);
//			if (err < 0) {
//				printf("Error occurred during sending: errno %d", errno);
//				return;
//			}
//			printf("Message sent");
		} else {
			close(tcpsock);
			tcpsock = -1;
			tcp_reconnect(1);

		}
	}

}

void tcp_ini_client(void) {
	connect_socket(0);
}


你可能感兴趣的:(WIFI模块)