How to Send UDP packet successfully then immediately set_channel.

bank23232525
Posts: 2
Joined: Sun Feb 28, 2021 11:56 am

How to Send UDP packet successfully then immediately set_channel.

Postby bank23232525 » Sat Mar 13, 2021 8:21 am

I am trying to create a pair of AP and STA that can change their channel continuously (from 1-13) together while always keeping connection.

If you have a suggestion on this ,please share.

My idea is
- AP tell STA via UDP socket when the channel need to change.
- STA receive UDP socket from AP then change the channel accordingly.

I wrote a code for communication successfully.

AP :

Code: Select all

   int newChannel = next_channel();
   while(1){
        printf("tell cli to change ch to %d.\n",newChannel);  
        if (sendto(sockfd, &newChannel, sizeof(newChannel), 0, &cliaddr, len) != sizeof(newChannel)) {
            printf("send failed.\n");  
            continue;
        }else{
            curChannel=newChannel;
            printf("send success. \n"); 
       	    // change_channel(newChannel);
            break;   
        }
    }
STA :

Code: Select all

    int newChannel;
    int n, len;
    n = recvfrom(sockfd, &newChannel, sizeof(newChannel), 0, (struct sockaddr *)&servaddr, (socklen_t *)&len);
    if (n > 0)
    {
        outprintf("Told by server to change ch to %d \n", newChannel);
        // change_channel(newChannel);
    }
change_channel:

Code: Select all

void change_channel(int newChannel)
{
    while (1)
    {
        outprintf("change ch to %d\n", newChannel);
        if (esp_wifi_set_channel(newChannel, WIFI_SECOND_CHAN_NONE) == ESP_OK)
        {
            uint8_t primaryChan;
            wifi_second_chan_t secondChan;
            esp_wifi_get_channel(&primaryChan, &secondChan);
            printf("check ch  %d %d.\n", primaryChan, secondChan);
            printf("esp_wifi_set_channel success");
            if (primaryChan == newChannel)
            {
                printf("ful\n");
                break;
            }
            else
            {
                printf("less\n");
                continue;
            }
        }
        else
        {
            printf("esp_wifi_set_channel fail\n");
            continue;
        }
    }
}
When I commented the // change_channel(newChannel); in AP and STA , they communicate successfully.

but when I place change_channel(newChannel); (or esp_restart()) immediately after sending UDP packet. the UDP packet seems unsent. Even after it print printf("send success. \n"); .

I research for a while and realize in ESP32 the sendto(..........) doesn't send data right a way. But it wait for some kernel interval to really send to.

So, I would like to know if there is anyway to call some function after the sendto(..........) really finished. Where I can do like esp_wifi_set_channel or esp_restart

Who is online

Users browsing this forum: No registered users and 46 guests