[SOLVED] AT+UPING command not working

Hi,

I have been trying to use the AT+UPING command from @rickkas7’s electronsample tutorial to ping the Google DNS server. The code is below: it basically pings the server and prints out whether it worked or not with the specific Cellular.command response code.

Currently every ping is returning a RESP_ERROR with response code = -3. When I ping the Google DNS server from my command line, it works perfectly. Why is the Electron not able to get a response?

    #pragma Particle_NO_PREPROCESSOR;
    #include <Particle.h>

    SYSTEM_THREAD(ENABLED);
    SYSTEM_MODE(AUTOMATIC);

    const unsigned long PING_TIMEOUT = 10000;

    void setup() {
        Serial.begin(9600);
    }

    void loop() {
      delay(PING_TIMEOUT);
      int ret = Cellular.command(PING_TIMEOUT, "AT+UPING=8.8.8.8\r\n");
      if (ret == RESP_OK) {
          Serial.println("Ping OK");
      } else {
          Serial.println("Ping NOT OK");
      }
      Serial.printlnf("Ping response: %d", ret);
    }

Is your Electron past blinking green and at least to blinking cyan? With SYSTEM_THREAD(ENABLED) the loop code will run before you’ve connected to the cellular network and ping will fail until you have.

Hi rickkas,

Yes, the electron was past the blinking green phase.

It was breathing cyan while it was checking 8.8.8.8 and returning the -3 response code.

Are you using a standard SIM card? I have seem some other service providers that do not allow ping.

Hi bko,

Yes we are using the standard Particle Sim card

Actually,

The purpose of this ping check was to check if the cellular modem needs to be reset.

Is there another way you might suggest to do this if AT+UPING is having trouble?

I believe the parameter to AT+UPING must be quoted, as in:

int ret = Cellular.command(PING_TIMEOUT, "AT+UPING=\"8.8.8.8\"\r\n");
1 Like

That works! Thank you very much for recommending the quotes needed to be added around the DNS address.

from: https://github.com/rickkas7/electronsample/blob/master/electronsample.cpp

bool cloudConnectDebug() {
	int res = Cellular.command(PING_TIMEOUT, "AT+UPING=8.8.8.8\r\n");
	connectionEventAdd(CONNECTION_EVENT_PING_DNS, res);

Oops. I had the quotes in the CellularHelper but not the earlier electronsample. I fixed the electronsample code.

2 Likes

Hello. Apologies for reviving this topic. I want to do some pings in my application to test cloud connectivity before attempting resets or something else, so decided to start learning this command. I set up the following code to learn using the ping in several scenarios:

STARTUP(System.enableFeature(FEATURE_RETAINED_MEMORY));
SYSTEM_THREAD(ENABLED);
SYSTEM_MODE(MANUAL);

const unsigned long espera_process = 3*1000;
unsigned long last_process;
unsigned long pingTimeout = 10000; // milliseconds
int res;

void setup() {
    
 if(!Cellular.ready()){
    Cellular.connect();
    for(uint32_t ms = millis(); millis() - ms < 500; Particle.process());
   }

}

void loop() {

 if (millis() - last_process >= espera_process){
      last_process = millis();
      Particle.process();
   }
    
    if (Serial.available()){
       char c = Serial.read();
       switch (c) {
         case 'c':
         
            Particle.connect();
            waitUntil(Particle.connected);
            Serial.println("Nube ON");
            delay(500);
            
            break;
            
        case 'C':
            Cellular.connect();
            delay(1000);
            Particle.connect();
            waitUntil(Particle.connected);
            Serial.println("Nube ON");
            delay(500);
            break;    
            
         case 'd':
            
            Particle.disconnect();
            Serial.println("Particle Disconnect");
            
            break;
            
        case 'D':
            Particle.disconnect();
            waitUntil(Particle.disconnected);
            Cellular.disconnect();
            delay(2000);
            Serial.println("Particle Disconnect + Cellular Disconnect");
            break; 
            
        case 'o':
            Particle.disconnect();
            delay(2000);
            Cellular.off();
            delay(2000);
            Serial.println("Particle Disconnect + Cellular OFF");
            break;      
            
         case 'p':
         
           res = Cellular.command(pingTimeout, "AT+UPING=\"api.particle.io\"\r\n");
           delay(1000);
           Serial.println(res);
           
           break;
           
         case 'q':  
           res = Cellular.command(pingTimeout, "AT+UPING=\"8.8.8.8\"\r\n");
           delay(1000);
           Serial.println(res);
           break;
       }
    }   
}

When I make the Particle to connect (perfect nice blue cyan breathing), I was expecting to get “-2” responses from the ping command all the time but what I get is 50% -2 (RESP_OK) and the other half “-3” (RESP_ERROR)

Conversely, when I disconnect the particle (breathing green), I was expecting to get only -3 responses but 30% of the responses are actually good ones -2.

If I do the last option (cellular.off - breathing white), then I finally get what I expect with -1 (TIME_OUT) responses.

What I am doing wrong please? Tried with 2 different electrons, tried adding delays after and before the disconnects/connects, but nothing works. Double checked and copied from this thread the right quoted commands (Hope my eyes are not tricking me).

Thanks in advance