How to convert the AT+CSQ RSSI reading into dB(mW)

I want to convert the return from getSignalStrength() into an the signal power given in dB(mW).
One problem is, that the return is of the datatype string. In order to calculate the signal strengeth in dB(mW), I tried to convert it to an integer. I found that function:

int rssiInt = atoi(rssi.c_str());

The sketch compiles fine, however the serial monitor doesnt show the expected result. The conversion result is not shown/incorrect.

Opened the serial port - COM3
RSSI to dB(mW) converter
AT

OK
Get current carrier: AT+COPS?

+COPS: 1,0,"262 01 telekom.net",9

OK
262 01 telekom.net
Get current signal strength: AT+CSQ

+CSQ: 10,99

OK
10
AT+CSQ

+CSQ: 10,99

OK
10
�FpG���F����FpGRSSI to dB(mW) converter

According to that link, every inkrement in the getSignalStrength() return value starting at 0 equals an improvement of 2 dB(mW) over -113 db(mW), topping out 31 and -51 dB(mW).
https://m2msupport.net/m2msupport/atcsq-signal-quality/

Question: Is my conversion wrong? What alternatives are there for typecasting from string to interger?

Below is the full code:

#include <MKRNB.h>

NBScanner scanner;
NB nb(1);

void setup()
{
	Serial.begin(9600);
    while(!Serial);
    
    Serial.println(F("RSSI to dB(mW) converter"));

    scanner.begin();
    Serial.print("Get current carrier: ");
    Serial.println(scanner.getCurrentCarrier());

    Serial.print("Get current signal strength: ");
    Serial.println(scanner.getSignalStrength());
    //convert RSSI to dB(mW)
    String rssi = scanner.getSignalStrength();
    Serial.println(rssi);
    int rssiInt = atoi(rssi.c_str());                           //String -> Int
    int dBm = (-113 + 2*rssiInt);                               //RSSI -> dB(mW) : -113 dBm = 0, -111 dBm = 1, -109 dBm = 2, ... -51 dBm = 31
    Serial.println(dBm +" dB(mW) [-113 < P < -51]dB(mW)");      //     
}

void loop()
{
    delay(1000);
}

This is the function I use, which strips away the the unwanted text so the result is readable, and returns the signal_power as an INT. For the SARA-R4 it will always report the qual as 99. (I can’t remember where I got this, I may have modified it from the library, or from another forum post, I don’t think I wrote it):

// AT+CSQ
// returns "+CSQ: <signal_power>,<qual>"
int getSignalStrength()
{
  String response;
  int rssi = -1;

  MODEM.send("AT+CSQ");
  if (MODEM.waitForResponse(100, &response) == 1) {
    int firstSpaceIndex = response.indexOf(' ');
    int lastCommaIndex = response.lastIndexOf(',');
    // Serial.println(response);

    if (firstSpaceIndex != -1 && lastCommaIndex != -1) {
      String temp = response.substring(firstSpaceIndex + 1, lastCommaIndex);
      rssi = temp.toInt();
    }
  }

  return rssi;
}

Then, you could use the map function to reassign the value to dBm as per your reference to the documentation.
https://www.arduino.cc/reference/en/language/functions/math/map/

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.