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;
}
}
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);
}
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;
}
}
}
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