angelo canales
angelo canales

Reputation: 11

how to change the channel in ESP-now when is already asigned to some peer?

I am trying to change some peer channel to a default value, i implemented the following code:

bool changeToDefaultChannel(){
  esp_now_del_peer(slave.peer_addr);
  esp_wifi_set_channel(CHANNEL_DEFAULT, WIFI_SECOND_CHAN_NONE); 
  esp_now_peer_info_t slave_aux;
  memcpy(slave_aux.peer_addr, MAC_slave, 6);
  slave_aux.channel = CHANNEL_DEFAULT; // pick a channel
  slave_aux.encrypt = 0; // no encryption
  if (esp_now_add_peer(&slave_aux) != ESP_OK) {
    Serial.println("Error al actualizar el canal del peer");
    return false;
  }else{
    Serial.println("Se cambió correctamente de canal la ESP32");
    return true;
    } 
  } 

In this code the i wanted to change the slave channel to a channel_default, first i delete the peer and then i add it again with the other channel, i use esp_wifi_set_channel to change the esp channel too. (i do the same code in the slave device to change the master to the default channel). I get this error "E (167155) ESPNOW: Peer interface is invalid", i dont know how to solve this (i asked to chat GPT and it told me nonsenses)

Upvotes: 0

Views: 1649

Answers (1)

hcheung
hcheung

Reputation: 4034

esp_now_peer_info_t is a struct that consists of the ifidx - Wi-Fi interface that peer uses to send/receive ESPNOW data. The message telling you exactly that, you have an invalid peer interface. If you create an instance of esp_now_peer_info_t with esp_now_peer_info_t slave_aux; without initialise it, you may get junk in ifidx value. It is a good practise to always initalise a struct that you created:

esp_now_peer_info_t slave_aux = {};

or

esp_now_peer_info_t slave_aux;
memset(&slave_aux, 0, sizeof(slave_aux));

You now at least have a valid ifidx value of 0, you can then decide to set it to WIFI_IF_STA (0 = default) or WIFI_IF_AP if necessary.

BTW, to ask chatGPT for help is like to ask to be spoon-feed, you might survive for one meal, but it won't get you far. Do research yourself, try to find out what the error message mean and read the document is like learning how to cook, it will benefit you for the rest of your life. There is simply no short cut...

Upvotes: 2

Related Questions