I am at work in the position of a software engineer and previously we produced devices without wireless data transmission capabilities. Therefore, we used AVR and STM microcontrollers.
Now I want to control devices with a wireless interface and for this I took esp32-wroom-32D to solve the problem. I have been studying esp for 2 weeks already and for gradual growth I set myself the following tasks:
1.receive data from UART atmega64
2.initialize esp STA && esp AP
3.initialize tcp_server (on AP) && tcp_client (on STA)
4. send "HELLO WORLD": STA-> AP-> STA
5.Transmit UART data with CRC check over the line:
atmega88P [UART] -> esp32_STA [UART] -> esp32_STA [socket] ->
esp32_AP [socket] -> esp32_AP [UART] -> atmega64 [UART]
and back.
Now I could not realize only 4 points. I have some peculiarities. this is the static IP of the server. Judging by what IP address is assigned to the client by the server, the static IP assignment for the server is successful. But the client socket cannot connect to the server. In debug, AP displays STA connection and connection. Then it displays the message that it is listening.
The STA debugger displays a successful connection to the AP and getting an IP from the server, but the connect function returns a message from the topic header with error 113.
For several days I have been trying to find the reason for constantly changing the code, but alas, neither examples nor Google search gives a result. Please help with the solution.
AP func:
Code: Select all
static void tcp_server_task(void *pvParameters)
{
char addr_str[128];
int addr_family = (int)pvParameters;
struct sockaddr_in servaddr, cliaddr;
int err;
char *TAG = "tcp_server_task";
int listenfd;
if ((listenfd = socket(addr_family, SOCK_STREAM, IPPROTO_TCP)) < 0) {
ESP_LOGE(TAG, "Unable to create socket: errno %d", errno);
vTaskDelete(NULL);
return;
}
ESP_LOGI(TAG, "Socket created");
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(SERV_PORT);
if ((err = bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr))) < 0) {
ESP_LOGE(TAG, "bind: %d %s", err, strerror(errno));
goto CLEAN_UP;
}
ESP_LOGI(TAG, "Socket bound, port %d", SERV_PORT);
if ((err = listen(listenfd, 5)) != 0) {
ESP_LOGE(TAG, "Error occurred during listen: errno %d", errno);
goto CLEAN_UP;
}
while (1) {
ESP_LOGI(TAG, "Socket listening");
socklen_t clilen = sizeof(cliaddr);
int sock = accept(listenfd, (struct sockaddr *)&cliaddr, &clilen);
if (sock < 0) {
ESP_LOGE(TAG, "accept: %d %s", sock, strerror(errno));
goto CLEAN_UP;
}
if (cliaddr.sin_family == PF_INET)
inet_ntoa_r(((struct sockaddr_in *)&cliaddr)->sin_addr.s_addr, addr_str, sizeof(addr_str) - 1);
ESP_LOGI(TAG, "Socket accepted ip address: %s", addr_str);
do_retransmit(sock);
shutdown(sock, 0);
close(sock);
}
CLEAN_UP:
close(listenfd);
vTaskDelete(NULL);
}
Code: Select all
void wifi_init_ap(void)
{
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
esp_netif_create_default_wifi_ap();
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
tcpip_adapter_ip_info_t info;
IP4_ADDR(&info.ip, 192, 168, 123, 1);
IP4_ADDR(&info.gw, 192, 168, 123, 1);
IP4_ADDR(&info.netmask, 255, 255, 255, 0);
ESP_ERROR_CHECK(tcpip_adapter_dhcps_stop(TCPIP_ADAPTER_IF_AP));
ESP_ERROR_CHECK(tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_AP, &info));
ESP_ERROR_CHECK(tcpip_adapter_dhcps_start(TCPIP_ADAPTER_IF_AP));
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
ESP_EVENT_ANY_ID,
&wifi_event_handler,
NULL,
NULL));
wifi_config_t wifi_config = {
.ap = {
.ssid = WIFI_SSID,
.ssid_len = strlen(WIFI_SSID),
.channel = WIFI_CHANNEL,
.password = WIFI_PASS,
.max_connection = MAX_STA_CONN,
.authmode = WIFI_AUTH_WPA_WPA2_PSK
},
};
if (strlen(WIFI_PASS) == 0) {
wifi_config.ap.authmode = WIFI_AUTH_OPEN;
}
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP));
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_AP, &wifi_config));
ESP_ERROR_CHECK(esp_wifi_start());
ESP_LOGI("WIFI_INI_AP", "finished");
}
Code: Select all
void wifi_init_sta(void)
{
s_wifi_event_group = xEventGroupCreate();
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
esp_netif_create_default_wifi_sta();
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
esp_event_handler_instance_t instance_any_id;
esp_event_handler_instance_t instance_got_ip;
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
ESP_EVENT_ANY_ID,
&event_handler,
NULL,
&instance_any_id));
ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT,
IP_EVENT_STA_GOT_IP,
&event_handler,
NULL,
&instance_got_ip));
wifi_config_t wifi_config = {
.sta = {
.ssid = WIFI_SSID,
.password = WIFI_PASS,
.threshold.authmode = WIFI_AUTH_WPA_WPA2_PSK,
.pmf_cfg = {
.capable = true,
.required = false
},
},
};
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());
ESP_LOGI(WIFI_STA_LBL, "wifi_init_sta finished.");
EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group,
WIFI_CONNECTED_BIT | WIFI_FAIL_BIT, pdFALSE, pdFALSE, portMAX_DELAY);
if (bits & WIFI_CONNECTED_BIT)
ESP_LOGI(WIFI_STA_LBL, "connected to ap SSID:%s password:%s", WIFI_SSID, WIFI_PASS);
else if (bits & WIFI_FAIL_BIT)
ESP_LOGI(WIFI_STA_LBL, "Failed to connect to SSID:%s, password:%s", WIFI_SSID, WIFI_PASS);
else
ESP_LOGE(WIFI_STA_LBL, "UNEXPECTED EVENT");
ESP_ERROR_CHECK(esp_event_handler_instance_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, instance_got_ip));
ESP_ERROR_CHECK(esp_event_handler_instance_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, instance_any_id));
vEventGroupDelete(s_wifi_event_group);
}
Code: Select all
void tcp_client_task(void *pvParameters)
{
uint8_t rx_buffer[128];
char host_ip[] = HOST_IP_ADDR;
int sockfd, n;
while (1) {
struct sockaddr_in servaddr;
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(SERV_PORT);
if ((sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_IP)) < 0) {
ESP_LOGE(WIFI_STA_LBL, "Unable to create socket: errno %d", errno);
break;
}
ESP_LOGI(WIFI_STA_LBL, "Socket: OK. Connecting to %s:%d...", host_ip, SERV_PORT);
if ((n = connect(sockfd, (struct sockaddr *)&servaddr, sizeof(struct sockaddr_in))) != 0) {
ESP_LOGE(WIFI_STA_LBL, "Socket unable to connect: errno %d", errno);
break;
}
ESP_LOGI(WIFI_STA_LBL, "Successfully connected");
while (1) {
//if (uart_buffer) {
//if (CRC_check(uart_buffer, 0)) {
if ((n = send(sockfd, /*uart_buffer*/"hello world", 11, 0)) < 0) {
ESP_LOGE(WIFI_STA_LBL, "Error occurred during sending: errno %d", errno);
break;
}
printf("SEND:\t");
print_buffer(uart_buffer, n, 0);
// } else {
// ESP_LOGW(CRC_LBL, "error checked CRC");
// break;
// }
// free(uart_buffer);
// uart_buffer = NULL;
if ((n = recv(sockfd, rx_buffer, sizeof(rx_buffer) - 1, 0)) < 0) {
ESP_LOGE(WIFI_STA_LBL, "recv failed: errno %d", errno);
break;
} else {
rx_buffer[n] = 0;
//if (CRC_check(rx_buffer, 0)) {
ESP_LOGI(WIFI_STA_LBL, "Received %d bytes from %s:", n, host_ip);
printf("RESIV:\t");
printf("%s\n", rx_buffer);
//print_buffer(rx_buffer, n, 0);
// } else {
// ESP_LOGW(CRC_LBL, "error checked CRC");
// break;
// }
}
//}
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
if (sockfd != -1) {
ESP_LOGE(WIFI_STA_LBL, "Shutting down socket and restarting...");
shutdown(sockfd, 0);
close(sockfd);
}
}
vTaskDelete(NULL);
}[/Codebox]
if you need a complete project please write. I will attach a link to the GIT repository. I would be very grateful for your help and for pointing out my mistakes.