how can I get wifi rssi at faster rate

funnygiraffe
Posts: 3
Joined: Sun Jun 10, 2018 10:11 am

how can I get wifi rssi at faster rate

Postby funnygiraffe » Sun Jun 10, 2018 10:15 am

I'm doing a project on lcoalization using wifi RSSI value.
I found some code only it's too slow for me since the rssi value can be interfere and vary very easy , so I need faster rssi value to do a moving avg before use it in my applicaion

code:
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <cmath>

const char* SSID = "wifi";

// Return RSSI or 0 if target SSID not found
double getRSSI(const char* target_ssid) {
byte available_networks = WiFi.scanNetworks();

for (int network = 0; network < available_networks; network++) {
if (strcmp(WiFi.SSID(network).c_str(), SSID) == 0) {
return WiFi.RSSI(network);
}
}
return 0;
}

void setup() {
Serial.begin(115200);
}

double getDistance(double rssi, double txPower) {
/*
* RSSI = TxPower - 10 * n * lg(d)
* n = 2 (in free space)
*
* d = 10 ^ ((TxPower - RSSI) / (10 * n))
*/

return std::pow(10, ((double) txPower - rssi) / (10 * 2));
}
//66.5 , 67.8484848485 ,

void loop() {
unsigned long before = millis();
double rssi = getRSSI(SSID);
unsigned long after = millis();
Serial.print("Signal strength: ");
Serial.print(rssi);
Serial.println("dBm");
//////
Serial.print("Distance: ");
Serial.print(getDistance(rssi,-53));
Serial.println("m");
//////
Serial.print("Took ");
Serial.print(after - before);
Serial.println("ms");

delay(100);
}

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

Re: how can I get wifi rssi at faster rate

Postby ESP_Angus » Mon Jun 11, 2018 12:40 am

Hi funnygiraffe,

I've moved this topic the Arduino forum as you're using Arduino. If using Arduino isn't a requirement then I can move it back.

The last parameter of WiFi.scanNetworks() function is "uint32_t max_ms_per_chan = 300", which is the amount of time to spend listening on each channel as part of the scan. You can turn this down to get results faster, ie

Code: Select all

WiFi.scanNetworks(false, false, false, 100);
(Note that spending less time on each channel may also reduce the total number of results you see per scan.)

Red_A3
Posts: 2
Joined: Wed Nov 13, 2019 10:25 pm

Re: how can I get wifi rssi at faster rate

Postby Red_A3 » Wed Nov 13, 2019 10:38 pm

Hello @ESP_Angus, I was trying to achieve the same target (i.e.: Higher "than default" refresh rate for RSSI measurements). I was looking on the Arduino implementation at: ( https://github.com/espressif/arduino-es ... an.cpp#L57 ).
I realized that the function esp_wifi_scan_start() can actually accept more inputs [ according to: https://docs.espressif.com/projects/esp ... l-channels ], although only 3 boolean parameters and a milliseconds parameters.
My question is: How can I access (pass my own values) to the other config parameters "using Arduino IDE"? Namely, I'm targeting the bssid and channel parameters.
Is that possible to be done without recompiling (rebuilding) the esp-idf arduino library (after passing those parameters in the WiFiScan.cpp and the WiFiScan.h files to generate a custom library files *.a)?

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

Re: how can I get wifi rssi at faster rate

Postby ESP_Angus » Thu Nov 14, 2019 2:35 am

Hi Red_A3,
Red_A3 wrote:
Wed Nov 13, 2019 10:38 pm
My question is: How can I access (pass my own values) to the other config parameters "using Arduino IDE"? Namely, I'm targeting the bssid and channel parameters.
Is that possible to be done without recompiling (rebuilding) the esp-idf arduino library (after passing those parameters in the WiFiScan.cpp and the WiFiScan.h files to generate a custom library files *.a)?
I'm not quite sure as Arduino isn't my thing, but this should work - you can also follow the instructions to include Arduino as an ESP-IDF component, and then change it.

It is probably also possible to include the ESP-IDF header file and call the IDF API directly from your sketch, but you might need to add some other support code into the sketch as well (basically, copy the implementation from WifiScan.cpp into your sketch.)

Red_A3
Posts: 2
Joined: Wed Nov 13, 2019 10:25 pm

Re: how can I get wifi rssi at faster rate

Postby Red_A3 » Thu Nov 14, 2019 8:37 pm

Many thanks @ESP_Angus, I solved my question without re-compiling (re-building) the library. The issue was simply adding some parameters to be passed by the function call to the WiFiScan.cpp implementation. I achieved that by appending 3 new parameters Target_SSID, Target_MAC and Target_Channel to the function definition of scanNetworks() and then later assigned the passed values to their respective fields in the config structure.

For those who are wondering (and for sake of sharing), what I did to solve my question is just the following:

1. Navigate to this path:

Code: Select all

C:\Users\[b][i]USER[/i][/b]\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.4\libraries\WiFi\src
2. Locate WiFiScan.cpp and open it using notepad.exe. Then modify the line that says:

Code: Select all

int16_t WiFiScanClass::scanNetworks(bool async, bool show_hidden, bool passive, uint32_t max_ms_per_chan)
And replace it by:

Code: Select all

int16_t WiFiScanClass::scanNetworks(bool async, bool show_hidden, bool passive, uint32_t max_ms_per_chan, uint8_t * Target_SSID, uint8_t * Target_MAC, uint8_t Target_Channel)
Then scroll down around 12 lines or so, you'll find:

Code: Select all

    wifi_scan_config_t config;
    config.ssid = 0;
    config.bssid = 0;
    config.channel = 0;
    config.show_hidden = show_hidden;
Replace it by:

Code: Select all

    wifi_scan_config_t config;
    config.ssid = Target_SSID;
    config.bssid = Target_MAC;
    config.channel = Target_Channel;
    config.show_hidden = show_hidden;
3. Locate WiFiScan.h and open it using notepad.exe. Then modify the line that says:

Code: Select all

int16_t scanNetworks(bool async = false, bool show_hidden = false, bool passive = false, uint32_t max_ms_per_chan = 300);
To be:

Code: Select all

int16_t scanNetworks(bool async = false, bool show_hidden = false, bool passive = false, uint32_t max_ms_per_chan = 300, uint8_t * Target_SSID = 0, uint8_t * Target_MAC = 0, uint8_t Target_Channel = 0);
4. Save and close (overwrite) both files. (PS: don't leave a backup copy of those two files in that same directory)


Then in your sketch (i.e.: My_WiFi_Scanner.ino) include the modified library using

Code: Select all

#include "WiFiScan.h"
Then use the modified function directly in your code, example:

Code: Select all

unsigned char TSSID[] = "My Hotspot"; unsigned char* Target_SSID = TSSID;
void setup() {Serial.begin(115200);}
And then inside the loop, put something like:

Code: Select all

int N = WiFi.scanNetworks(false,false,false,100,Target_SSID,0,1);
int RSSI_Value = WiFi.RSSI(0);
Serial.println(RSSI_Value);
Just to mention: Here I know beforehand that My Hotspot is broadcasting its beacon frame in channel 1 (this will reduce the scan time considerably, as it limits the listening to only 1 channel instead of the 11 WiFi channels). Also I just targeted a specific SSID name (not a specific MAC address).

Simple :lol: . Just plain old "How do I pass parameters by value" example in C programming.

Thanks again @ESP_Angus :D

JeanPinhal
Posts: 4
Joined: Thu Sep 16, 2021 2:56 pm

Re: how can I get wifi rssi at faster rate

Postby JeanPinhal » Thu Sep 16, 2021 3:01 pm

A simpler and easier solution is to go to the WifiScan.h file and change the value of

Code: Select all

uint32_t max_ms_per_chan
[/b] which defaults to 300, change it to 80 which is an acceptable value. :D

LeMagicien
Posts: 1
Joined: Sun Feb 20, 2022 1:08 am

Re: how can I get wifi rssi at faster rate

Postby LeMagicien » Sun Feb 20, 2022 1:09 am

a big thank you :D

Who is online

Users browsing this forum: No registered users and 44 guests