UDP client does not start to send in AP mode
Posted: Wed Mar 13, 2019 8:21 pm
Hi to everybody,
I have a problem with a UDP client setup:
I start a DHCP server, then
WIFI in AP mode.
Once this is done I start a UDP client in a task.
This works all without problems, I can connect to it with my computer, receive an address and start sending UDP messages.
The bad thing is: I do not receive any of these messages until I send at message from the UDP server on my computer to the ESP32. This message from my terminal program (YAT) seems to wake up a part of the ESP, as after this, I get the messages.
Here's the code:
Initialisation of the tcpip adapter and ESP32 as softAP:
and here my UDP_client task:
As global defines I have
#define WIFI_IP_ADDR "192.168.1.1"
#define UDP_SENDER_ADDR WIFI_IP_ADDR
#define UDP_RECEIVER_ADDR INADDR_BROADCAST
#define UDP_SENDER_PORT 3001
#define UDP_RECEIVER_PORT 3000
I have other tasks running in parallel, everything on one core.
Does anyone have an idea why I do not receive any UDP messages on my computer before I send a single message to the ESP32?
Am I missing out on starting something, is something not configured or needs to be woken up?
I searched the internet for hours, updated ESP IDP, the terminal program, alas, no success.
I appreciate you taking the time to read this, check the code and hopefully come back with an solution.
The code is based on various examples. As I do not read and messages from the connected station (computer), I do not use the receive section. It is not checked or cleaned in any way.
Kind regards
Greetings from Germany
Georg
I have a problem with a UDP client setup:
I start a DHCP server, then
WIFI in AP mode.
Once this is done I start a UDP client in a task.
This works all without problems, I can connect to it with my computer, receive an address and start sending UDP messages.
The bad thing is: I do not receive any of these messages until I send at message from the UDP server on my computer to the ESP32. This message from my terminal program (YAT) seems to wake up a part of the ESP, as after this, I get the messages.
Here's the code:
Initialisation of the tcpip adapter and ESP32 as softAP:
- EventGroupHandle_t wifi_event_group;
- const int AP_STARTED_BIT = BIT0;
- const int STA_CONNECTED_BIT = BIT1;
- const int STA_DISCONNECTED_BIT = BIT2;
- const int UDP_SOCKED_BINDING = BIT3;
- static esp_err_t event_handler(void *ctx, system_event_t *event)
- {
- switch(event->event_id) {
- case SYSTEM_EVENT_AP_START:
- ESP_LOGI("WIFI_AP","ESP32 is started in AP mode");
- xEventGroupSetBits(wifi_event_group, AP_STARTED_BIT);
- break;
- case SYSTEM_EVENT_AP_STACONNECTED:
- ESP_LOGI("WIFI_AP", "station:"MACSTR" join, AID=%d",
- MAC2STR(event->event_info.sta_connected.mac),
- event->event_info.sta_connected.aid);
- xEventGroupSetBits(wifi_event_group, STA_CONNECTED_BIT);
- esp_wifi_set_ps(WIFI_PS_NONE);
- break;
- case SYSTEM_EVENT_AP_STADISCONNECTED:
- ESP_LOGI("WIFI_AP", "station:"MACSTR"leave, AID=%d",
- MAC2STR(event->event_info.sta_disconnected.mac),
- event->event_info.sta_disconnected.aid);
- xEventGroupSetBits(wifi_event_group, STA_DISCONNECTED_BIT);
- break;
- default:
- break;
- }
- return ESP_OK;
- }
- void DHCP_server_init(){
- // initialize the tcp stack
- tcpip_adapter_init();
- // stop DHCP server
- ESP_ERROR_CHECK(tcpip_adapter_dhcps_stop(TCPIP_ADAPTER_IF_AP));
- // assign a static IP to the network interface
- tcpip_adapter_ip_info_t info;
- memset(&info, 0, sizeof(info));
- info.ip.addr = inet_addr(WIFI_IP_ADDR);
- info.gw.addr = inet_addr(WIFI_IP_ADDR);
- //IP4_ADDR(&info.ip, 192, 168, 1, 1);
- //IP4_ADDR(&info.gw, 192, 168, 1, 1);//ESP acts as router, so gw addr will be its own addr
- IP4_ADDR(&info.netmask, 255, 255, 255, 0);
- ESP_ERROR_CHECK(tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_AP, &info));
- // start the DHCP server
- ESP_ERROR_CHECK(tcpip_adapter_dhcps_start(TCPIP_ADAPTER_IF_AP));
- printf("DHCP server started \n");
- }
- void WIFI_AP_init(void)
- {
- ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) );
- wifi_event_group = xEventGroupCreate();
- DHCP_server_init();
- //xTaskCreate(&tcp_server,"tcp_server",4096,NULL,5,NULL);
- esp_log_level_set("wifi", ESP_LOG_NONE); // disable wifi driver logging
- 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_AP) );
- // configure the wifi connection and start the interface
- wifi_config_t ap_config = {
- .ap = {
- .ssid = AP_SSID,
- .password = AP_PASSPHARSE,
- .ssid_len = strlen(AP_SSID),
- .channel = 0,
- .authmode = AP_AUTHMODE,
- .ssid_hidden = AP_SSID_HIDDEN,
- .max_connection = AP_MAX_CONNECTIONS,
- .beacon_interval = AP_BEACON_INTERVAL,
- },
- };
- if (strlen(WIFI_PSWD)==0)
- ap_config.ap.authmode = WIFI_AUTH_OPEN;
- ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP, &ap_config));
- ESP_ERROR_CHECK( esp_wifi_start() );
- printf("ESP WiFi started in AP mode \n");
- xTaskCreate(&task_UDP_client, "UDP", 4096, NULL, 5, NULL);
- }
- int udp_socket_tx;
- int udp_socket_rx;
- struct sockaddr_in udp_sender;
- struct sockaddr_in udp_receiver;
- void task_UDP_client(void *pvParameters)
- {
- uint16_t cnt = 0;
- char rx_buffer[128];
- char addr_str[128];
- int addr_family;
- int ip_protocol;
- while (1) {
- ESP_LOGI("WIFI","Waiting for Station");
- EventBits_t staBits = xEventGroupWaitBits(wifi_event_group, STA_CONNECTED_BIT, pdTRUE, pdFALSE, portMAX_DELAY);
- if((staBits & STA_CONNECTED_BIT) != 0)
- ESP_LOGI("UDP","Connected, starting UDP_Client\n");
- else {
- ESP_LOGI("UDP", "timeout waiting for connection...");
- break;
- };
- udp_socket_tx = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
- if (udp_socket_tx < 0) {
- ESP_LOGE("UDP", "Unable to create socket: errno %d", errno);
- xEventGroupClearBits(wifi_event_group, UDP_SOCKED_BINDING);
- break;
- }
- else{
- ESP_LOGI("UDP", "Socket created");
- xEventGroupSetBits(wifi_event_group, UDP_SOCKED_BINDING);
- };
- udp_sender.sin_family = AF_INET;
- udp_sender.sin_len = sizeof(udp_sender);
- udp_sender.sin_addr.s_addr = inet_addr(UDP_SENDER_ADDR);
- udp_sender.sin_port = htons(UDP_SENDER_PORT);
- if(bind(udp_socket_tx, (struct sockaddr *)&udp_sender, sizeof(struct sockaddr_in)) == -1) {
- ESP_LOGE("UDP", "bind failed");
- close (udp_socket_tx);
- exit(1);
- }
- else
- ESP_LOGI("UDP", "bind created");
- udp_receiver.sin_family = AF_INET;
- udp_receiver.sin_len = sizeof(udp_receiver);
- udp_receiver.sin_addr.s_addr = htonl(UDP_RECEIVER_ADDR);
- udp_receiver.sin_port = htons(UDP_RECEIVER_PORT);
- while (1) {
- static const char *msg = "test UDP send \n";
- vTaskDelay(2000 / portTICK_PERIOD_MS);
- cnt++;
- char text[128];
- sprintf(&text[0],"UDP message %d", cnt++);
- int err = sendto(udp_socket_tx, text, strlen(text), 0, (struct sockaddr *)&udp_receiver, sizeof(udp_sender));
- //int err = UDP_send("UDP: msg no %d", cnt);
- //int err = sendto(udp_socket_tx, msg, strlen(msg), 0, (struct sockaddr *)&udp_receiver, sizeof(udp_sender));
- if (err < 0) {
- ESP_LOGE("UDP", "Error occured during sending: errno %d", errno);
- break;
- }
- else
- ESP_LOGI("UDP","send to %s ", ip4addr_ntoa(&(udp_sender.sin_addr)));
- ESP_LOGI("UDP", "Message %d sent", cnt);
- #ifdef UDP_WAIT_FOR_REPLY
- /* //struct sockaddr_in sourceAddr; // Large enough for both IPv4 or IPv6
- socklen_t socklen = sizeof(udp_receiver_addr);
- int len = recvfrom(udp_socket_rx, rx_buffer, sizeof(rx_buffer) - 1, 0, (struct sockaddr *)&udp_receiver_addr, &socklen);
- //int len = recv(udp_socket, rx_buffer, sizeof(rx_buffer) - 1, 0);
- ESP_LOGI("UDP", "past recvfrom");
- // Error occured during receiving
- if (len < 0) {
- ESP_LOGE("UDP", "recvfrom failed: errno %d", errno);
- break;
- }
- // Data received
- else {
- rx_buffer[len] = 0; // Null-terminate whatever we received and treat like a string
- ESP_LOGI("UDP", "Received %d bytes from %s:", len, addr_str);
- ESP_LOGI("UDP", "%s", rx_buffer);
- }*/
- #endif
- }
- if (udp_socket_tx != -1) {
- ESP_LOGI("UDP", "Shutting down socket and restarting...");
- shutdown(udp_socket_tx, 0);
- close(udp_socket_tx);
- }
- }
- ESP_LOGE("UDP", "leaving task");
- vTaskDelay(200 / portTICK_PERIOD_MS);
- vTaskDelete(NULL);
- };
#define WIFI_IP_ADDR "192.168.1.1"
#define UDP_SENDER_ADDR WIFI_IP_ADDR
#define UDP_RECEIVER_ADDR INADDR_BROADCAST
#define UDP_SENDER_PORT 3001
#define UDP_RECEIVER_PORT 3000
I have other tasks running in parallel, everything on one core.
Does anyone have an idea why I do not receive any UDP messages on my computer before I send a single message to the ESP32?
Am I missing out on starting something, is something not configured or needs to be woken up?
I searched the internet for hours, updated ESP IDP, the terminal program, alas, no success.
I appreciate you taking the time to read this, check the code and hopefully come back with an solution.
The code is based on various examples. As I do not read and messages from the connected station (computer), I do not use the receive section. It is not checked or cleaned in any way.
Kind regards
Greetings from Germany
Georg