how can I get wifi rssi at faster rate
-
- Posts: 3
- Joined: Sun Jun 10, 2018 10:11 am
how can I get wifi rssi at faster rate
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);
}
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);
}
Re: how can I get wifi rssi at faster rate
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
(Note that spending less time on each channel may also reduce the total number of results you see per scan.)
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);
Re: how can I get wifi rssi at faster rate
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)?
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)?
Re: how can I get wifi rssi at faster rate
Hi Red_A3,
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.)
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.Red_A3 wrote: ↑Wed Nov 13, 2019 10:38 pmMy 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)?
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.)
Re: how can I get wifi rssi at faster rate
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:
2. Locate WiFiScan.cpp and open it using notepad.exe. Then modify the line that says:
And replace it by:
Then scroll down around 12 lines or so, you'll find:
Replace it by:
3. Locate WiFiScan.h and open it using notepad.exe. Then modify the line that says:
To be:
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
Then use the modified function directly in your code, example:
And then inside the loop, put something like:
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 . Just plain old "How do I pass parameters by value" example in C programming.
Thanks again @ESP_Angus
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
Code: Select all
int16_t WiFiScanClass::scanNetworks(bool async, bool show_hidden, bool passive, uint32_t max_ms_per_chan)
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)
Code: Select all
wifi_scan_config_t config;
config.ssid = 0;
config.bssid = 0;
config.channel = 0;
config.show_hidden = show_hidden;
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;
Code: Select all
int16_t scanNetworks(bool async = false, bool show_hidden = false, bool passive = false, uint32_t max_ms_per_chan = 300);
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);
Then in your sketch (i.e.: My_WiFi_Scanner.ino) include the modified library using
Code: Select all
#include "WiFiScan.h"
Code: Select all
unsigned char TSSID[] = "My Hotspot"; unsigned char* Target_SSID = TSSID;
void setup() {Serial.begin(115200);}
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);
Simple . Just plain old "How do I pass parameters by value" example in C programming.
Thanks again @ESP_Angus
-
- Posts: 4
- Joined: Thu Sep 16, 2021 2:56 pm
Re: how can I get wifi rssi at faster rate
A simpler and easier solution is to go to the WifiScan.h file and change the value of [/b] which defaults to 300, change it to 80 which is an acceptable value.
Code: Select all
uint32_t max_ms_per_chan
-
- Posts: 1
- Joined: Sun Feb 20, 2022 1:08 am
Re: how can I get wifi rssi at faster rate
a big thank you
Who is online
Users browsing this forum: No registered users and 43 guests