How to get wifi router distance from device?

I want to calculate distance from wifi router in c/c++ code.Now I am trying a method which use RSSI and FSPL to calculate distance.

RSSI = Ptx – 10 * n * log10(d)

d = 10 ^ ((Ptx – RSSI) / (10 * n))

Ptx is the transmitter power in dBm.
n is the path-loss exponent.There are ranges of n for every type of area

2 for free space,

2.7 to 3.5 for urban areas,

3.0 to 5.0 in suburban areas,

1.6 to 1.8 for indoors when there is line of sight to the router.

But I want to find more universal formula thats works everywhere.So I have found that I can use signal-to-noicse ratio for more accurately estimate.
How is it works or is there any formula that I can use?
I have found similar links but there are not helpfull –
Link 1
Link 2

How to get wifi router distance from device?