Unable to receive TCPIP-packets with wifi callback function

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

Unable to receive TCPIP-packets with wifi callback function

Postby Wamor45 » Thu Jan 05, 2017 7:47 pm

Hello,

I wrote a small program which once connected to wifi should receive TCPIP packages.
My program is listed below:

Code: Select all

nvs_flash_init();
tcpip_adapter_init();
ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) );
ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
ESP_ERROR_CHECK( esp_wifi_get_mac(WIFI_IF_STA, mac) );
printf("MAC address: " MACSTR "\r\n", MAC2STR(mac));
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 = "my_wifi",
        .password = "123456",
        .bssid_set = false
    }
};
ESP_ERROR_CHECK( esp_wifi_set_config(WIFI_IF_STA, &sta_config) );
ESP_ERROR_CHECK( esp_wifi_internal_reg_rxcb(WIFI_IF_STA, &my_wifi_rxcb) );
ESP_ERROR_CHECK( esp_wifi_start() );
ESP_ERROR_CHECK( esp_wifi_connect() );
When I send some TCP or UDP-packages with another computer on the same network I don't receive any packages in the my_wifi_rxcb call back function. Am I missing some additional initialization or what could be wrong here?

Thank you for your responses,

Wamor

ESP_Angus
Posts: 2344
Joined: Sun May 08, 2016 4:11 am

Re: Unable to receive TCPIP-packets with wifi callback function

Postby ESP_Angus » Thu Jan 05, 2017 10:56 pm

Hi Wamor,

esp_wifi_internal_reg_rxcb is an internal function, so it's used to implement the IP stack (LWIP) that runs on top of the WiFi libraries. It may not be what you want in order to exchange TCP/IP packets with another host on the network.

Can you explain a bit more about what you're trying to do? If you take a look at some of the examples in esp-idf that use TCP/IP (like the 'http_request' example, for example) you'll be able to see the recommended way to work with TCP/IP on ESP32.

Angus

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: Unable to receive TCPIP-packets with wifi callback function

Postby kolban » Fri Jan 06, 2017 3:26 am

Howdy @Wamor,
To follow up on what Mr Angus said ... in case it is useful to you ... here is a snippet I use for a TCP/IP TCP socket client:

https://github.com/nkolban/esp32-snippe ... etClient.c

and one for a TCP/IP TCP socket server:

https://github.com/nkolban/esp32-snippe ... t_server.c

The trick (for me) is to invest time studying generic books on the TCP/IP sockets APIs and there are some fine books and resources available on those subjects. ESP32 sockets API seems to be a high fidelity implementation.
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

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

Re: Unable to receive TCPIP-packets with wifi callback function

Postby Wamor45 » Fri Jan 06, 2017 8:54 pm

Hello Angus,

What I basically want to do is receiving UDP-packages from a remote computer and send them out as analog values through the DAC of the ESP32. I hope this make sense to you.

Regards,

Wamor

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

Re: Unable to receive TCPIP-packets with wifi callback function

Postby Wamor45 » Fri Jan 06, 2017 8:59 pm

Hello kolban,

Thank you for your response.
I will get me some good books about TCPIP and sockets.

Regards,

Wamor

zjuliwang
Posts: 1
Joined: Fri Aug 18, 2017 9:51 am

Re: Unable to receive TCPIP-packets with wifi callback function

Postby zjuliwang » Fri Aug 18, 2017 9:59 am

Wamor45 wrote:Hello Angus,

What I basically want to do is receiving UDP-packages from a remote computer and send them out as analog values through the DAC of the ESP32. I hope this make sense to you.

Regards,

Wamor
Hello Angus,nkolban:
How can I get thefeedback (callback function) of tcp's receive or send ?

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: Unable to receive TCPIP-packets with wifi callback function

Postby kolban » Fri Aug 18, 2017 2:34 pm

Howdy,
You will want to study the API called "sockets". This is a standard C/C++ API that is used to TCP/UDP programming against the TCP/IP protocol. If you can google using the phrase "sockets tutorial", I think you will be rewarded by a whole host of articles.

I am also a fan of books on a subject and there are a wealth at Amazon.
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

zjuliwang
Posts: 1
Joined: Fri Aug 18, 2017 9:51 am

Re: Unable to receive TCPIP-packets with wifi callback function

Postby zjuliwang » Wed Aug 23, 2017 7:00 am

kolban wrote:Howdy,
You will want to study the API called "sockets". This is a standard C/C++ API that is used to TCP/UDP programming against the TCP/IP protocol. If you can google using the phrase "sockets tutorial", I think you will be rewarded by a whole host of articles.

I am also a fan of books on a subject and there are a wealth at Amazon.
HI kolban:
I have googled the phrase and found the socket API ,they looks nearly the same as ESP32IDF,I don't see any callback function in ESP32IDF,when I used 8266 ,there were many callback functions like sendback_callback(); I think lwip must have the callback function. if not,how can I send or recieve datas one by one continuely? :lol:

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: Unable to receive TCPIP-packets with wifi callback function

Postby kolban » Thu Aug 24, 2017 10:04 pm

With TCP/IP sockets API there isn't a "callback" event processing system. Instead, most of the sockets API calls are blocking. For example, if you execute recv() to read data, the call will block until data is made available. Now you have choices that involve task processing. You can create a FreeRTOS task for each socket upon which you want to simultaneously listen. An alternative design is to use the TCP/IP select() API call. This will take an array of sockets as input and will block watching them "all" simultaneously. You will unblock when the first of them has data available to be read without blocking.
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

tele_player
Posts: 90
Joined: Sun Jul 02, 2017 3:38 am

Re: Unable to receive TCPIP-packets with wifi callback function

Postby tele_player » Fri Aug 25, 2017 12:57 am

select() has a timeout argument which can be used to set no block, block until timeout, or block forever.

Who is online

Users browsing this forum: No registered users and 131 guests