Skip to content

Commit f72e924

Browse files
committed
feat(wifi): add currentRSSI() for live connection signal strength
rssi(int) only reports RSSI from the last scanNetworks() results, so there was no way to read the signal strength of the AP currently connected to without rescanning. currentRSSI() queries AT+CWJAP? directly and parses the live RSSI out of the response, returning INT16_MIN if not connected or the command fails. Verified on hardware: firmware's +CWJAP: response matches stock ESP-AT format, RSSI trends weaker with distance, and disconnected state returns a bare OK with no +CWJAP: line (clean failure path).
1 parent 2ca0528 commit f72e924

2 files changed

Lines changed: 30 additions & 0 deletions

File tree

‎src/system/wifi/esp32SpiAt.cpp‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,6 +1018,35 @@ int WiFiClass::rssi(int _ssidNumber)
10181018
return _lastUsedSsidData.rssi;
10191019
}
10201020

1021+
/**
1022+
* @brief Method gets the live RSSI of the AP the ESP32 is currently connected to, without needing
1023+
* a prior scanNetworks() call. Confirmed on hardware: firmware's "+CWJAP:" response matches
1024+
* stock ESP-AT format ("+CWJAP:<ssid>,<bssid>,<channel>,<rssi>,...").
1025+
*
1026+
* @return int
1027+
* RSSI value in dBm of the current AP connection.
1028+
* INT16_MIN - Not connected to an AP, or the command failed.
1029+
*/
1030+
int WiFiClass::currentRSSI()
1031+
{
1032+
// Send AT command and get the response. Check if the response is expected. Otherwise, return sentinel.
1033+
// Confirmed on hardware: while disconnected, this command returns a bare "OK" with no "+CWJAP:" line,
1034+
// so this call fails cleanly instead of hanging or returning stale data.
1035+
if (!sendAtCommandWithResponse((char *)"AT+CWJAP?\r\n", 200ULL, 4ULL, (char *)"+CWJAP:",
1036+
INKPLATE_ESP32_AT_EXPECTED_RESPONSE_START, true, NULL, 0, 0, NULL))
1037+
return INT16_MIN;
1038+
1039+
// Parse the channel and RSSI fields out of the response. SSID and BSSID are quoted strings that may
1040+
// themselves contain commas (see begin()'s note on escaping), so skip them by quote matching instead
1041+
// of splitting on commas.
1042+
int _channel = 0;
1043+
int _rssi = INT16_MIN;
1044+
if (sscanf(_dataBuffer, "+CWJAP:\"%*[^\"]\",\"%*[^\"]\",%d,%d", &_channel, &_rssi) != 2)
1045+
return INT16_MIN;
1046+
1047+
return _rssi;
1048+
}
1049+
10211050
/**
10221051
* @brief Obtain a local IP.
10231052
*

‎src/system/wifi/esp32SpiAt.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ class WiFiClass
9898
char *ssid(int _ssidNumber);
9999
bool auth(int _ssidNumber);
100100
int rssi(int _ssidNumber);
101+
int currentRSSI();
101102
IPAddress localIP();
102103
IPAddress gatewayIP();
103104
IPAddress subnetMask();

0 commit comments

Comments
 (0)