API to Get RSSI Cellular Strength Took long time ~2+s

I have an application that need to display the signal strength update periodically, say every 5 mins.
The code to get the signal :grinning:

auto sigStregth = Cellular.RSSI().getStrength();

But once a while, that one line of code could take 2+s and it was a blocking call. This is a concern to me to have blocking call with this long duration.

Any idea why this API can take so long and if there is any better recommendation?

@parhusip, which device are you using?

It is Electron

Getting the RSSI requires communicating with the cellular modem. Since only one operation can be done on the modem at a time, it’s possible that it will block for an indefinite amount of time. Usually it’s milliseconds but in reality it could be up to 10 minutes in certain rare conditions.

If you need your code to remain responsive, it’s best to run Cellular.RSSI() from a separate thread so if it blocks, the main loop will not block. Same goes for Particle.publish, Cellular.command, and anything else that uses the modem.

Thank you @rickkas7, would you mind to brief me how to create a new thread? Does Device OS platform provide this feature?

Hi, here is a Particle threads tutorial link

and here is part of My Boron code with threadFunction()


#include "Particle.h"


extern void startupFunction();
Thread *thread;
os_mutex_t mutex;

void startupFunction(){
  os_mutex_create(&mutex);
}

SYSTEM_THREAD(ENABLED);
STARTUP(startupFunction());


char bat_sig_data[256] = "{\"charg\":%f,\"bat\":%f,\"rat\":%d,\"strengthVal\":%f,\"qualityVal\":%f,\"qualityPercentage\":%f,\"strengthPercentage\":%.02f}";
unsigned long interval = 0;


void setup() {
  
  SYSTEM_MODE(AUTOMATIC);
  
  Serial.begin(); 
  
  
 
  os_mutex_lock(mutex);
  thread = new Thread("threadFunction", threadFunction);

  
  
}

void loop() {

     sone other stuff running in loop
     .................................
     .................................
     
     os_mutex_unlock(mutex);
     
   
    
   
 }


        



void threadFunction() { 
	while(true) {
	    os_mutex_lock(mutex);
            if (millis() - interval > 300000) {
              interval = millis();
             
              FuelGauge fuel;
              CellularSignal sig = Cellular.RSSI();
    
   

              int rat = sig.getAccessTechnology();
              float strengthVal = floorf(sig.getStrengthValue()*100)/100;
              float strengthPercentage = floorf(sig.getStrength()*100)/100;
              float qualityVal = floorf(sig.getQualityValue()*100)/100;
              float qualityPercentage = floorf(sig.getQuality()*100)/100;
              float bat = floorf(fuel.getVCell()*100)/100;
              float charg = floorf(fuel.getSoC()*100)/100;

              memset(bat_sig_data, 0, sizeof(bat_sig_data));    
              JSONBufferWriter writer(bat_sig_data, sizeof(bat_sig_data) - 1);
              writer.beginObject();
              writer.name("charg").value(charg);
              writer.name("bat").value(bat);
              writer.name("rat").value(rat);
              writer.name("strengthVal").value(strengthVal);
              writer.name("qualityVal").value(qualityVal);
              writer.name("qualityPercentage").value(qualityPercentage);
              writer.name("strengthPercentage").value(strengthPercentage);
              writer.endObject();
        
              Particle.publish("All_sign_data", bat_sig_data, 60, PRIVATE);


             }  
	    
	    
      }
 }
    

I’m not sure if is gonna work for Electron also I didn’t tested yet with my Boron, but will give you some idea how to deal with Threads.

Should Publish data from threadFunction() every 5 min