Calculating RSSI value while using ESP_NOW protocol

cipher
Posts: 1
Joined: Sat Aug 24, 2024 6:24 pm

Calculating RSSI value while using ESP_NOW protocol

Postby cipher » Sat Aug 24, 2024 6:38 pm

Hi, I have been using ESP_NOW protocol to create a many to one network with my ESP32 wroom, I want to check the RSSI value of to calculate the distance between 2 ESP32.

Below is the Reciever code, but it Outputs a constant value of -93.

Code: Select all

#include <stdio.h>
#include <string.h>
#include <esp_log.h>
#include <esp_err.h>
#include <esp_wifi.h>
#include <esp_now.h>
#include <nvs_flash.h>

static const char *TAG = "esp_now_receiver";

// Structure example to receive data
typedef struct struct_message {
    char a[32];
    int b;
    float c;
    bool d;
} struct_message;

// Create a struct_message called myData
struct_message myData;

// Callback function that will be executed when data is received
void OnDataRecv(const uint8_t *mac_addr, const uint8_t *incomingData, int len) {
    memcpy(&myData, incomingData, sizeof(myData));

    // Get the RSSI value
    wifi_pkt_rx_ctrl_t *pkt_ctrl = (wifi_pkt_rx_ctrl_t *)(incomingData - sizeof(wifi_pkt_rx_ctrl_t));
    int8_t rssi = pkt_ctrl->rssi;

    ESP_LOGI(TAG, "Bytes received: %d", len);
    ESP_LOGI(TAG, "Char: %s", myData.a);
    ESP_LOGI(TAG, "Int: %d", myData.b);
    ESP_LOGI(TAG, "Float: %f", myData.c);
    ESP_LOGI(TAG, "Bool: %s", myData.d ? "true" : "false");
    ESP_LOGI(TAG, "RSSI: %d dBm", rssi);
}

void app_main(void) {
    esp_err_t ret;

    // Initialize NVS
    ret = nvs_flash_init();
    if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
        ESP_ERROR_CHECK(nvs_flash_erase());
        ret = nvs_flash_init();
    }
    ESP_ERROR_CHECK(ret);

    // Initialize Wi-Fi
    wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
    ESP_ERROR_CHECK(esp_wifi_init(&cfg));
    ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
    ESP_ERROR_CHECK(esp_wifi_start());

    // Initialize ESP-NOW
    ESP_ERROR_CHECK(esp_now_init());

    // Register receive callback function
    ESP_ERROR_CHECK(esp_now_register_recv_cb(OnDataRecv));

    // Enter an infinite loop
    while (1) {
        vTaskDelay(pdMS_TO_TICKS(1000)); // Delay to allow background tasks to run
    }
}
I would be grateful if anyone can help me with this problem.

Who is online

Users browsing this forum: No registered users and 12 guests