recvfrom returns -1 with error-code 11

Wamor45
Posts: 9
Joined: Fri Feb 19, 2016 3:50 pm

recvfrom returns -1 with error-code 11

Postby Wamor45 » Wed Apr 19, 2017 8:58 pm

Hello,

I have the below program.
Function recvfrom(...) in receive_UDP(...) returns -1 with error-code 11.
I believe this is caused by a time-out but I should receive the "Hello world!"-text instead ?
Is there someone who could help me finding what is wrong ?

Best regards,

Wamor

Code: Select all

/* HTTP GET Example using plain POSIX sockets

   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 "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 "lwip/ip4_addr.h"
#include "nvs_flash.h"

#include "lwip/err.h"
#include "lwip/sockets.h"
#include "lwip/sys.h"
#include "lwip/netdb.h"
#include "lwip/dns.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

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

/* The event group allows multiple bits for each event,
   but we only care about one event - are we connected
   to the AP with an IP? */
const int CONNECTED_BIT = BIT0;

#define PORT_NUMBER 34952

static const char *TAG = "ESP32";
static const char *TAG_RECEIVE = "ESP32 Receive";
static const char *TAG_SEND = "ESP32 Send";

int sock = 0;
struct sockaddr_in source, destination;

static void connect_UDP(void)
{
    tcpip_adapter_ip_info_t ip_info;

    struct timeval tv;
    tv.tv_sec = 3;  /* 3 Seconds Time-out */
    tv.tv_usec = 0;

	if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
        ESP_LOGE(TAG, "... Failed to allocate socket.");

    /*set the socket options*/
    setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (char *) &tv, sizeof(struct timeval));

    /*Initialize source to zero*/
    memset(&source, 0, sizeof(source));       //source is an instance of sockaddr_in. Initialization to zero
    /*Initialize destination to zero*/
    memset(&destination, 0, sizeof(destination));

    ESP_ERROR_CHECK( tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &ip_info) );

    ESP_LOGE(TAG, "Local IP-address=%s", inet_ntoa(ip_info.ip));
    //source.sin_len = sizeof(source);
	source.sin_family = AF_INET;
	source.sin_port = htons(7005);
	source.sin_addr.s_addr = INADDR_ANY; //ip_info.ip.addr;
	memset(source.sin_zero, '\0', sizeof source.sin_zero);

	if (bind(sock, (struct sockaddr *) &source, sizeof(source)) < 0)
        ESP_LOGE(TAG, "... Failed to bind socket.");

    ESP_LOGE(TAG, "Gateway IP-address=%s", inet_ntoa(ip_info.gw));
    //destination.sin_len = sizeof(destination);
	destination.sin_family = AF_INET;
	destination.sin_port = htons(7005);
	destination.sin_addr.s_addr = inet_addr("127.0.0.1"); //ip_info.gw.addr;
	memset(destination.sin_zero, '\0', sizeof destination.sin_zero);
}

static void disconnect_UDP(void)
{
    close(sock);
}

static esp_err_t event_handler(void *ctx, system_event_t *event)
{
    switch(event->event_id) {
    case SYSTEM_EVENT_WIFI_READY:		        /**< ESP32 WiFi ready */
		ESP_LOGI(TAG, "SYSTEM_EVENT_WIFI_READY");
		break;
    case SYSTEM_EVENT_SCAN_DONE:                /**< ESP32 finish scanning AP */
		ESP_LOGI(TAG, "SYSTEM_EVENT_SCAN_DONE");
		break;
    case SYSTEM_EVENT_STA_START:                /**< ESP32 station start */
		ESP_LOGI(TAG, "SYSTEM_EVENT_STA_START");
		ESP_ERROR_CHECK( esp_wifi_connect() );
		break;
    case SYSTEM_EVENT_STA_STOP:                 /**< ESP32 station stop */
		ESP_LOGI(TAG, "SYSTEM_EVENT_STA_STOP");
		break;
    case SYSTEM_EVENT_STA_CONNECTED:            /**< ESP32 station connected to AP */
		ESP_LOGI(TAG, "SYSTEM_EVENT_STA_CONNECTED");
		break;
    case SYSTEM_EVENT_STA_DISCONNECTED:         /**< ESP32 station disconnected from AP */
		ESP_LOGI(TAG, "SYSTEM_EVENT_STA_DISCONNECTED");
		/* This is a workaround as ESP32 WiFi libs don't currently
		   auto-reassociate. */
		xEventGroupClearBits(wifi_event_group, CONNECTED_BIT);
		ESP_ERROR_CHECK( esp_wifi_connect() );
		disconnect_UDP();
		break;
    case SYSTEM_EVENT_STA_AUTHMODE_CHANGE:      /**< the auth mode of AP connected by ESP32 station changed */
		ESP_LOGI(TAG, "SYSTEM_EVENT_STA_AUTHMODE_CHANGE");
		break;
    case SYSTEM_EVENT_STA_GOT_IP:               /**< ESP32 station got IP from connected AP */
		ESP_LOGI(TAG, "SYSTEM_EVENT_STA_GOT_IP");
		connect_UDP();
		xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
		break;
    case SYSTEM_EVENT_STA_WPS_ER_SUCCESS:       /**< ESP32 station wps succeeds in enrollee mode */
		ESP_LOGI(TAG, "SYSTEM_EVENT_STA_WPS_ER_SUCCESS");
		break;
    case SYSTEM_EVENT_STA_WPS_ER_FAILED:        /**< ESP32 station wps fails in enrollee mode */
		ESP_LOGI(TAG, "SYSTEM_EVENT_STA_WPS_ER_FAILED");
		break;
    case SYSTEM_EVENT_STA_WPS_ER_TIMEOUT:       /**< ESP32 station wps timeout in enrollee mode */
		ESP_LOGI(TAG, "SYSTEM_EVENT_STA_WPS_ER_TIMEOUT");
		break;
    case SYSTEM_EVENT_STA_WPS_ER_PIN:           /**< ESP32 station wps pin code in enrollee mode */
		ESP_LOGI(TAG, "SYSTEM_EVENT_STA_WPS_ER_PIN");
		break;
    case SYSTEM_EVENT_AP_START:                 /**< ESP32 soft-AP start */
		ESP_LOGI(TAG, "SYSTEM_EVENT_AP_START");
		break;
    case SYSTEM_EVENT_AP_STOP:                  /**< ESP32 soft-AP stop */
		ESP_LOGI(TAG, "SYSTEM_EVENT_AP_STOP");
		break;
    case SYSTEM_EVENT_AP_STACONNECTED:          /**< a station connected to ESP32 soft-AP */
		ESP_LOGI(TAG, "SYSTEM_EVENT_AP_STACONNECTED");
		break;
    case SYSTEM_EVENT_AP_STADISCONNECTED:       /**< a station disconnected from ESP32 soft-AP */
		ESP_LOGI(TAG, "SYSTEM_EVENT_AP_STADISCONNECTED");
		break;
    case SYSTEM_EVENT_AP_PROBEREQRECVED:        /**< Receive probe request packet in soft-AP interface */
		ESP_LOGI(TAG, "SYSTEM_EVENT_AP_PROBEREQRECVED");
		break;
    case SYSTEM_EVENT_AP_STA_GOT_IP6:           /**< ESP32 station or ap interface v6IP addr is preferred */
		ESP_LOGI(TAG, "SYSTEM_EVENT_AP_STA_GOT_IP6");
		break;
    case SYSTEM_EVENT_ETH_START:                /**< ESP32 ethernet start */
		ESP_LOGI(TAG, "SYSTEM_EVENT_ETH_START");
		break;
    case SYSTEM_EVENT_ETH_STOP:                 /**< ESP32 ethernet stop */
		ESP_LOGI(TAG, "SYSTEM_EVENT_ETH_STOP");
		break;
    case SYSTEM_EVENT_ETH_CONNECTED:            /**< ESP32 ethernet phy link up */
		ESP_LOGI(TAG, "SYSTEM_EVENT_ETH_CONNECTED");
		break;
    case SYSTEM_EVENT_ETH_DISCONNECTED:         /**< ESP32 ethernet phy link down */
		ESP_LOGI(TAG, "SYSTEM_EVENT_ETH_DISCONNECTED");
		break;
    case SYSTEM_EVENT_ETH_GOT_IP:               /**< ESP32 ethernet got IP from connected AP */
		ESP_LOGI(TAG, "SYSTEM_EVENT_ETH_GOT_IP");
		break;
    default:
        break;
    }
    return ESP_OK;
}

static void initialise_wifi(void)
{
    tcpip_adapter_init();
    wifi_event_group = xEventGroupCreate();
    ESP_ERROR_CHECK( esp_event_loop_init(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) );
    wifi_config_t wifi_config = {
        .sta = {
        		.ssid = "Console",
				.password = "password",
				.bssid_set = false
        },
    };
    ESP_LOGI(TAG, "Setting WiFi configuration SSID %s...", wifi_config.sta.ssid);
    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() );
}

static void send_UDP(void *pvParameters)
{
	int r;

	while (1) {
        xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT,
                            false, true, portMAX_DELAY);

        ESP_LOGI(TAG_SEND, "Connected to AP");
        ESP_LOGE(TAG_SEND, "socketnumber=%d", sock);

        ESP_LOGE(TAG_SEND, "Local IP-address=%s", inet_ntoa(source.sin_addr.s_addr));

        ESP_LOGE(TAG_SEND, "Len=%d", destination.sin_len);
        ESP_LOGE(TAG_SEND, "Family=%d", destination.sin_family);
        ESP_LOGE(TAG_SEND, "Port number=%d", destination.sin_port);
        ESP_LOGE(TAG_SEND, "Decimal Destination IP-address=%x", destination.sin_addr.s_addr);
        ESP_LOGE(TAG_SEND, "Destination IP-address=%s", inet_ntoa(destination.sin_addr.s_addr));

        /* Send UDP data*/
    	char *data = "Hello world!";
        r = sendto(sock, data, strlen(data), 0, (struct sockaddr *) &destination, sizeof(destination));

        ESP_LOGI(TAG_SEND, "... done sending to socket. Last read return=%d errno=%d", r, errno);
        vTaskDelay(1000 / portTICK_PERIOD_MS);
        ESP_LOGI(TAG_SEND, "Starting again!");
	}
}

static void receive_UDP(void *pvParameters)
{
	int r;
	struct sockaddr_storage fromAddr;   // same as the previous entity struct sockaddr_storage serverStorage;
    unsigned int addrlen;
    uint8_t *recv_buffer;

    addrlen = sizeof(fromAddr);
    recv_buffer = (uint8_t *) malloc(250);

    while (1) {
        xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT,
                            false, true, portMAX_DELAY);

        ESP_LOGI(TAG_RECEIVE, "Connected to AP");
        ESP_LOGE(TAG_RECEIVE, "socketnumber=%d", sock);

        /* Read UDP data */
        do {
        	memset((void *) recv_buffer, 0, 250);
            r = recvfrom(sock, recv_buffer, 98, 0, (struct sockaddr *) &destination, &addrlen);
            for (int i = 0; i < r; i++) {
                putchar(recv_buffer[i]);
            }
        } while (r > 0);

        ESP_LOGI(TAG_RECEIVE, "... done reading from socket. Last read return=%d errno=%d", r, errno);
        ESP_LOGI(TAG_RECEIVE, "Starting again!");
    }
}

void app_main()
{
    nvs_flash_init();
    initialise_wifi();
    xTaskCreate(&receive_UDP, "receive_UDP", 2048, NULL, 5, NULL);
    xTaskCreate(&send_UDP, "send_UDP", 2048, NULL, 5, NULL);
}

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: recvfrom returns -1 with error-code 11

Postby WiFive » Wed Apr 19, 2017 10:48 pm


Wamor45
Posts: 9
Joined: Fri Feb 19, 2016 3:50 pm

Re: recvfrom returns -1 with error-code 11

Postby Wamor45 » Fri Apr 21, 2017 8:32 pm

Thank you WiFive for your response.
I looked at the link you mentioned and decided not to use loop-back but send information to my gateway.
My gateway is returning the data back again then.
Please find below my program. I don't receive anything at my gateway and nothing is returned.
Any clues what could be wrong here ?

Regards,

Wamor

Code: Select all

/* HTTP GET Example using plain POSIX sockets

   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 "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 "lwip/ip4_addr.h"
#include "nvs_flash.h"

#include "lwip/err.h"
#include "lwip/sockets.h"
#include "lwip/sys.h"
#include "lwip/netdb.h"
#include "lwip/dns.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

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

/* The event group allows multiple bits for each event,
   but we only care about one event - are we connected
   to the AP with an IP? */
const int CONNECTED_BIT = BIT0;

#define PORT_NUMBER 34952

static const char *TAG = "ESP32";
static const char *TAG_RECEIVE = "ESP32 Receive";
static const char *TAG_SEND = "ESP32 Send";

int sock_server = 0;
struct sockaddr_in sa_server;
int sock_client = 0;
struct sockaddr_in sa_client;

static void connect_UDP(void)
{
    tcpip_adapter_ip_info_t ip_info;

    ESP_ERROR_CHECK( tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &ip_info) );

	memset(&sa_server, 0, sizeof sa_server);
	sa_server.sin_family = AF_INET;
	sa_server.sin_addr.s_addr = ip_info.ip.addr;
	sa_server.sin_port = htons(PORT_NUMBER);

	sock_server = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
	if (bind(sock_server, (struct sockaddr *) &sa_server, sizeof sa_server) < 0) {
		ESP_LOGE(TAG, "error bind failed");
		close(sock_server);
		exit(EXIT_FAILURE);
	}

	/* create an Internet, datagram, socket using UDP */
	sock_client = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
	if (sock_client < 0) {
		/* if socket failed to initialize, exit */
		ESP_LOGE(TAG, "Error Creating Socket");
		exit(EXIT_FAILURE);
	}

	memset(&sa_client, 0, sizeof sa_client);
	sa_client.sin_family = AF_INET;
	sa_client.sin_addr.s_addr = ip_info.gw.addr;
	sa_client.sin_port = htons(PORT_NUMBER);
}

static void disconnect_UDP(void)
{
    close(sock_server);
    close(sock_client);
}

static esp_err_t event_handler(void *ctx, system_event_t *event)
{
    switch(event->event_id) {
    case SYSTEM_EVENT_WIFI_READY:		        /**< ESP32 WiFi ready */
		ESP_LOGI(TAG, "SYSTEM_EVENT_WIFI_READY");
		break;
    case SYSTEM_EVENT_SCAN_DONE:                /**< ESP32 finish scanning AP */
		ESP_LOGI(TAG, "SYSTEM_EVENT_SCAN_DONE");
		break;
    case SYSTEM_EVENT_STA_START:                /**< ESP32 station start */
		ESP_LOGI(TAG, "SYSTEM_EVENT_STA_START");
		ESP_ERROR_CHECK( esp_wifi_connect() );
		break;
    case SYSTEM_EVENT_STA_STOP:                 /**< ESP32 station stop */
		ESP_LOGI(TAG, "SYSTEM_EVENT_STA_STOP");
		break;
    case SYSTEM_EVENT_STA_CONNECTED:            /**< ESP32 station connected to AP */
		ESP_LOGI(TAG, "SYSTEM_EVENT_STA_CONNECTED");
		break;
    case SYSTEM_EVENT_STA_DISCONNECTED:         /**< ESP32 station disconnected from AP */
		ESP_LOGI(TAG, "SYSTEM_EVENT_STA_DISCONNECTED");
		/* This is a workaround as ESP32 WiFi libs don't currently
		   auto-reassociate. */
		xEventGroupClearBits(wifi_event_group, CONNECTED_BIT);
		ESP_ERROR_CHECK( esp_wifi_connect() );
		disconnect_UDP();
		break;
    case SYSTEM_EVENT_STA_AUTHMODE_CHANGE:      /**< the auth mode of AP connected by ESP32 station changed */
		ESP_LOGI(TAG, "SYSTEM_EVENT_STA_AUTHMODE_CHANGE");
		break;
    case SYSTEM_EVENT_STA_GOT_IP:               /**< ESP32 station got IP from connected AP */
		ESP_LOGI(TAG, "SYSTEM_EVENT_STA_GOT_IP");
		connect_UDP();
		xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
		break;
    case SYSTEM_EVENT_STA_WPS_ER_SUCCESS:       /**< ESP32 station wps succeeds in enrollee mode */
		ESP_LOGI(TAG, "SYSTEM_EVENT_STA_WPS_ER_SUCCESS");
		break;
    case SYSTEM_EVENT_STA_WPS_ER_FAILED:        /**< ESP32 station wps fails in enrollee mode */
		ESP_LOGI(TAG, "SYSTEM_EVENT_STA_WPS_ER_FAILED");
		break;
    case SYSTEM_EVENT_STA_WPS_ER_TIMEOUT:       /**< ESP32 station wps timeout in enrollee mode */
		ESP_LOGI(TAG, "SYSTEM_EVENT_STA_WPS_ER_TIMEOUT");
		break;
    case SYSTEM_EVENT_STA_WPS_ER_PIN:           /**< ESP32 station wps pin code in enrollee mode */
		ESP_LOGI(TAG, "SYSTEM_EVENT_STA_WPS_ER_PIN");
		break;
    case SYSTEM_EVENT_AP_START:                 /**< ESP32 soft-AP start */
		ESP_LOGI(TAG, "SYSTEM_EVENT_AP_START");
		break;
    case SYSTEM_EVENT_AP_STOP:                  /**< ESP32 soft-AP stop */
		ESP_LOGI(TAG, "SYSTEM_EVENT_AP_STOP");
		break;
    case SYSTEM_EVENT_AP_STACONNECTED:          /**< a station connected to ESP32 soft-AP */
		ESP_LOGI(TAG, "SYSTEM_EVENT_AP_STACONNECTED");
		break;
    case SYSTEM_EVENT_AP_STADISCONNECTED:       /**< a station disconnected from ESP32 soft-AP */
		ESP_LOGI(TAG, "SYSTEM_EVENT_AP_STADISCONNECTED");
		break;
    case SYSTEM_EVENT_AP_PROBEREQRECVED:        /**< Receive probe request packet in soft-AP interface */
		ESP_LOGI(TAG, "SYSTEM_EVENT_AP_PROBEREQRECVED");
		break;
    case SYSTEM_EVENT_AP_STA_GOT_IP6:           /**< ESP32 station or ap interface v6IP addr is preferred */
		ESP_LOGI(TAG, "SYSTEM_EVENT_AP_STA_GOT_IP6");
		break;
    case SYSTEM_EVENT_ETH_START:                /**< ESP32 ethernet start */
		ESP_LOGI(TAG, "SYSTEM_EVENT_ETH_START");
		break;
    case SYSTEM_EVENT_ETH_STOP:                 /**< ESP32 ethernet stop */
		ESP_LOGI(TAG, "SYSTEM_EVENT_ETH_STOP");
		break;
    case SYSTEM_EVENT_ETH_CONNECTED:            /**< ESP32 ethernet phy link up */
		ESP_LOGI(TAG, "SYSTEM_EVENT_ETH_CONNECTED");
		break;
    case SYSTEM_EVENT_ETH_DISCONNECTED:         /**< ESP32 ethernet phy link down */
		ESP_LOGI(TAG, "SYSTEM_EVENT_ETH_DISCONNECTED");
		break;
    case SYSTEM_EVENT_ETH_GOT_IP:               /**< ESP32 ethernet got IP from connected AP */
		ESP_LOGI(TAG, "SYSTEM_EVENT_ETH_GOT_IP");
		break;
    default:
        break;
    }
    return ESP_OK;
}

static void initialise_wifi(void)
{
    tcpip_adapter_init();
    wifi_event_group = xEventGroupCreate();
    ESP_ERROR_CHECK( esp_event_loop_init(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) );
    wifi_config_t wifi_config = {
        .sta = {
        		.ssid = "Console",
				.password = "password",
				.bssid_set = false
        },
    };
    ESP_LOGI(TAG, "Setting WiFi configuration SSID %s...", wifi_config.sta.ssid);
    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() );
}

static void send_UDP(void *pvParameters)
{
	int r;

	while (1) {
        xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT,
                            false, true, portMAX_DELAY);

        ESP_LOGI(TAG_SEND, "Connected to AP");
        ESP_LOGE(TAG_SEND, "socket_client number=%d", sock_client);

        ESP_LOGE(TAG_SEND, "Local IP-address=%s", inet_ntoa(sa_server.sin_addr.s_addr));

        ESP_LOGE(TAG_SEND, "Len=%d", sa_client.sin_len);
        ESP_LOGE(TAG_SEND, "Family=%d", sa_client.sin_family);
        ESP_LOGE(TAG_SEND, "Port number=%d", sa_client.sin_port);
        ESP_LOGE(TAG_SEND, "Destination IP-address=%s", inet_ntoa(sa_client.sin_addr.s_addr));

        /* Send UDP data*/
    	char *data = "Hello world!";
        r = sendto(sock_client, data, strlen(data), 0, (struct sockaddr *) &sa_client, sizeof(sa_client));

        ESP_LOGI(TAG_SEND, "... done sending to socket. Last read return=%d errno=%d", r, errno);
        vTaskDelay(1000 / portTICK_PERIOD_MS);
        ESP_LOGI(TAG_SEND, "Starting again!");
	}
}

static void receive_UDP(void *pvParameters)
{
	int r;
    unsigned int addrlen;
    uint8_t *recv_buffer;

    addrlen = sizeof(sa_server);
    recv_buffer = (uint8_t *) malloc(250);

    while (1) {
        xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT,
                            false, true, portMAX_DELAY);

        ESP_LOGI(TAG_RECEIVE, "Connected to AP");
        ESP_LOGE(TAG_RECEIVE, "socket_server number=%d", sock_server);

        /* Read UDP data */
        do {
        	memset((void *) recv_buffer, 0, 250);
            r = recvfrom(sock_server, recv_buffer, sizeof(recv_buffer) - 1, 0, (struct sockaddr *) &sa_server, &addrlen);
            for (int i = 0; i < r; i++) {
                putchar(recv_buffer[i]);
            }
        } while (r > 0);

        ESP_LOGI(TAG_RECEIVE, "... done reading from socket. Last read return=%d errno=%d", r, errno);
        ESP_LOGI(TAG_RECEIVE, "Starting again!");
    }
}

void app_main()
{
    nvs_flash_init();
    initialise_wifi();
    xTaskCreate(&receive_UDP, "receive_UDP", 2048, NULL, 5, NULL);
    xTaskCreate(&send_UDP, "send_UDP", 2048, NULL, 5, NULL);
}

Who is online

Users browsing this forum: No registered users and 138 guests