-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTCP Socket Server.c
165 lines (139 loc) · 5.03 KB
/
TCP Socket Server.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// TCP Server with WiFi connection communication via Socket
#include <stdio.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/timers.h"
#include "freertos/event_groups.h"
#include "freertos/queue.h"
#include "esp_wifi.h"
#include "esp_log.h"
#include "esp_check.h"
#include "esp_mac.h"
#include "esp_eth.h"
#include "esp_netif.h"
#include "esp_http_server.h"
#include "esp_http_client.h"
#include "esp_event.h"
#include "esp_system.h"
#include "lwip/inet.h"
#include "lwip/netdb.h"
#include "lwip/sockets.h"
#include "lwip/ip_addr.h"
#include "nvs_flash.h"
#include "ping/ping_sock.h"
#include "driver/gpio.h"
#include "my_data.h"
#define PORT 3333
static const char *TAG = "TCP_SOCKET";
static void tcp_server_task(void *pvParameters)
{
char addr_str[128];
char rx_buffer[128];
char string_data[128];
char data_to_send[128];
int data_len, string_data_len;
int keepAlive = 1;
int keepIdle = 5;
int keepInterval = 5;
int keepCount = 3;
struct sockaddr_storage dest_addr;
int counter = 0;
struct sockaddr_in *dest_addr_ip4 = (struct sockaddr_in *)&dest_addr;
dest_addr_ip4->sin_addr.s_addr = htonl(INADDR_ANY);
dest_addr_ip4->sin_family = AF_INET;
dest_addr_ip4->sin_port = htons(PORT);
// Open socket
int listen_sock = socket(AF_INET, SOCK_STREAM, 0); // 0 for TCP Protocol
int opt = 1;
setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
ESP_LOGI(TAG, "Socket created");
bind(listen_sock, (struct sockaddr *)&dest_addr, sizeof(dest_addr));
ESP_LOGI(TAG, "Socket bound, port %d", PORT);
// Listen to the socket
listen(listen_sock, 1);
while (1)
{
ESP_LOGI(TAG, "Socket listening");
struct sockaddr_storage source_addr; // Large enough for both IPv4 or IPv6
socklen_t addr_len = sizeof(source_addr);
// Accept socket
int sock = accept(listen_sock, (struct sockaddr *)&source_addr, &addr_len);
if (sock < 0)
{
ESP_LOGE(TAG, "Unable to accept connection: errno %d", errno);
break;
}
// Set tcp keepalive option
setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &keepAlive, sizeof(int));
setsockopt(sock, IPPROTO_TCP, TCP_KEEPIDLE, &keepIdle, sizeof(int));
setsockopt(sock, IPPROTO_TCP, TCP_KEEPINTVL, &keepInterval, sizeof(int));
setsockopt(sock, IPPROTO_TCP, TCP_KEEPCNT, &keepCount, sizeof(int));
// Convert ip address to string
if (source_addr.ss_family == PF_INET)
{
inet_ntoa_r(((struct sockaddr_in *)&source_addr)->sin_addr, addr_str, sizeof(addr_str) - 1);
}
ESP_LOGI(TAG, "Socket accepted ip address: %s", addr_str);
recv(sock, rx_buffer, sizeof(rx_buffer) - 1, 0);
strcpy(string_data,"Response from ESP32 Server via Socket connection");
string_data_len = strlen(string_data);
sprintf(data_to_send, "HTTP/1.1 200 OK\r\nContent-Length: %d\r\n\r\n", string_data_len);
strcat(data_to_send, string_data);
data_len = strlen(data_to_send);
// Send data via socket
send(sock, data_to_send, data_len, 0);
counter++;
printf("send_reply function number %d was activated\n", counter);
vTaskDelay(2000 / portTICK_PERIOD_MS);
shutdown(sock, 0);
close(sock);
}
close(listen_sock);
vTaskDelete(NULL);
}
static void wifi_event_handler(void *event_handler_arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
{
switch (event_id)
{
case WIFI_EVENT_STA_START:
printf("WiFi connecting WIFI_EVENT_STA_START ... \n");
break;
case WIFI_EVENT_STA_CONNECTED:
printf("WiFi connected WIFI_EVENT_STA_CONNECTED ... \n");
break;
case WIFI_EVENT_STA_DISCONNECTED:
printf("WiFi lost connection WIFI_EVENT_STA_DISCONNECTED ... \n");
break;
case IP_EVENT_STA_GOT_IP:
printf("WiFi got IP ... \n\n");
break;
default:
break;
}
}
void wifi_connection()
{
nvs_flash_init();
esp_netif_init();
esp_event_loop_create_default();
esp_netif_create_default_wifi_sta();
wifi_init_config_t wifi_initiation = WIFI_INIT_CONFIG_DEFAULT();
esp_wifi_init(&wifi_initiation);
esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, wifi_event_handler, NULL);
esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, wifi_event_handler, NULL);
wifi_config_t wifi_configuration = {
.sta = {
.ssid = SSID,
.password = PASS}};
esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_configuration);
esp_wifi_set_mode(WIFI_MODE_STA);
esp_wifi_start();
esp_wifi_connect();
}
void app_main(void)
{
wifi_connection();
vTaskDelay(5000 / portTICK_PERIOD_MS);
xTaskCreate(tcp_server_task, "tcp_server", 4096, (void *)AF_INET, 5, NULL);
}