Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix WiFi TxPower, plus a ton of other refactoring #1615

Merged
merged 40 commits into from
Feb 14, 2024
Merged

Conversation

proddy
Copy link
Contributor

@proddy proddy commented Feb 11, 2024

added harcoded dBm values into Web's Network Settings page along with an extra setting called Auto which picks the lowest Wifi TX power based on the RSSI value. This is calculated and set after the WiFi has connected (STA); as before, it was never called. The idea is to save on power and reduce interference in noisy areas. Not sure if it's needed or if it works, but I stole the idea from Tasmota and ESPEasy.

I also fixed a bug where settings did not persist when a Restart was required.

-- EDIT --

I should really have done this in a new PR, but anyway...I've added a few more changes inspired by @kam821

  • Fixed uninitialized variable usage (NetworkSettingsService::_stopping).
  • Added resource cleaning in MqttSettingsService.
  • Mark SecuritySettingsService as final to prevent potential call of virtual function in constructor (wrapRequest).
  • BSSID format logic moved to separate function
  • Removed std::bind/std::placeholders, use lambda instead
  • Removed c style casts (still more to go)
  • Use auto in variable initialization from cast/new expression.
  • Use braced initialization in return statements.
  • Added missing std::move for non-trivial type
  • Use const& in range for loop instead of value (User)
  • Replace malloc by new expression
  • Single argument constructors marked as explicit
  • Added missing 'override'
  • Removed VLA used in ArduinoJsonJWT::decode
  • Wrapped while loop assignment expression in parenthesis
  • Wrapped WIFI_RECONNECTION_DELAY in parenthesis
  • Changed [&] closure capture to more explicit capture of this parameter.
  • Change find_first_of(".") to find_first_of('.')
  • Replace String == "" by isEmpty().
  • Replace push_back by emplace_back
  • Fixed unused parameters warning
  • Renamed NTPSettingsService connected_ to _connected to match convention, moved initialization to constructor in CPP file
  • Replace chained ternary operators by immediately invoked lambda to improve readability (NTPStatus.cpp)
  • Use std::vector instead of std::list that hasn't already been modified
  • Reworked some places to change mutable references to be able to use const references in parameters.
  • Replace empty destructor by default one
  • NULL -> nullptr
  • time.h -> ctime
  • Replace hand-written array length by sizeof

thanks to @kam821. Changes taken from:

kam821@d265ab8
kam821@ed3ea06
kam821@2225aa4

Copy link
Contributor

@MichaelDvP MichaelDvP left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a lot of code moved within the change commits, difficult to find and review the new code.
The tx-auto seems to give very low results, i get on the router rssi < -70 dbM receive from ems-esp with datarate <10Mbit/s. . log is Recommended set WiFi Tx Power (set_power 4, new power 8, rssi -530, threshold -690
I think we should set the auto to tx-power resulting in ~-65dBm (or higher) in the router to have 150mBit/s connection. (For me this needs ~ 11dBm tx-power ( giving rssi -67dBm, 135 mBit/s).

p = WIFI_POWER_7dBm;
else if (set_power >= 5)
p = WIFI_POWER_5dBm;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This table set a too low value, e.g. if calculated min is 12.5dBm it sets 11, this results in very low connection rates.
It should set 11dBm for calculate of >8.5 <=11, etc.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that would explain the low values

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, partly. I tested now with:

    int threshold  = WIFI_SENSITIVITY_n + 70; // Margin in dBm * 10 on top of threshold

(goal -65 dBm -> threshold 650)
and

wifi_power_t p = WIFI_POWER_2dBm;
    if (min_tx_pwr > 185)
        p = WIFI_POWER_19_5dBm;
    else if (min_tx_pwr > 170)
        p = WIFI_POWER_18_5dBm;
    else if (min_tx_pwr > 150)
        p = WIFI_POWER_17dBm;
    else if (min_tx_pwr > 130)
        p = WIFI_POWER_15dBm;
    else if (min_tx_pwr > 110)
        p = WIFI_POWER_13dBm;
    else if (min_tx_pwr > 85)
        p = WIFI_POWER_11dBm;
    else if (min_tx_pwr > 70)
        p = WIFI_POWER_8_5dBm;
    else if (min_tx_pwr > 50)
        p = WIFI_POWER_7dBm;
    else if (min_tx_pwr > 20)
        p = WIFI_POWER_5dBm;

(using min_tx_power for the limits with decimals)
now i get tx_power 11 dBm and connection rate of 80-130 MBit/s, so i think lower limit of usefll setting.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, I'll make those changes and test locally.

lib/framework/NetworkSettingsService.cpp Outdated Show resolved Hide resolved
lib/framework/NetworkSettingsService.cpp Outdated Show resolved Hide resolved
@proddy proddy changed the title fix WiFi TxPower fix WiFi TxPower, plus a ton of other refactoring Feb 12, 2024
@proddy
Copy link
Contributor Author

proddy commented Feb 12, 2024

I noticed I broke the eventStreaming so will need to look into that

@proddy
Copy link
Contributor Author

proddy commented Feb 12, 2024

I noticed I broke the eventStreaming so will need to look into that

fixed.

@proddy
Copy link
Contributor Author

proddy commented Feb 14, 2024

with the latest changes a few days ago I managed to squeeze out 3-4KB extra heap, now it seems to have gone back. So now I need to trace back in the code what caused it. When that's done I'll merge this PR

@proddy proddy merged commit 8ebc552 into emsesp:dev Feb 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants