Correct use of Cellular functions - UDP connection

Hi there!
I’m doing some tests with my Electron and a THIRD-PARTY sim (CTM - Macau).
The sw is basically a weather station that samples, send a String to the server and receives an ACK through UDP.

These are the important rows:

//#include "cellular_hal.h"
STARTUP(cellular_credentials_set("ctm-mobile", "", "", NULL));
SYSTEM_MODE(SEMI_AUTOMATIC);

bool wasConnected = false;

...

setup{ ..only sensors stuff... }

void loop(){

....

if(!Cellular.ready()){
    Cellular.connect();
    Serial.println("Connect");
}

delay(1000);

while(!Cellular.connecting()){
   Serial.println("Connecting...");
   delay(1000);
   if( Cellular.ready() ){
    if(!wasConnected){
        Serial.println("Wasn't connected");
        udp.begin(LOCAL_UDP_PORT); //60004 that is also server's port
        wasConnected = true;   
     }
     else{
             Serial.println("wasConnected");
             break;
         }
  }
  else
    wasConnected=false;
}

Serial.print("Connected: ");

int sentByte = udp.sendPacket((uint8_t *)udpBuffer, strlen(udpBuffer), REMOTE_UDP_ADDR, REMOTE_UDP_PORT); //60004
if(sentByte<0){
  Serial.println("SendPacket error");
  return;
}

int count = udp.receivePacket((uint8_t *)udpBuffer, UDP_BUFFER_SIZE - 1);
if (count > 0) {
      Serial.println("OK");
}
else if (count == 0) {
      Serial.printlnf("** No ACK %d", count); 
}
else{
      Serial.printlnf("** receivePacket error %d", count);
}

So, this sw above WORKS. In fact I get:

1 cycle:
BREATHING WHITE
BLINKING GREEN (Seconds)
BREATH GREEN
Connect
Connecting…
Wasn’t connected
Connected: 10.119.151.208
OK

2 cycle:
BREATH GREEN
Connecting…
wasConnected
Connected: 10.119.151.208
OK


TEST 2:
In this second test I decided to “simulate” the disconnection/reconnection to the Cellular cell, using Cellular.connect() and .disconnect().

I put

//if(!Cellular.ready()) //Always connect at the beginning
Cellular.connect();

... (no edits from test 1)

if(sentByte<0){
      Serial.println("SendPacket error");
      Cellular.disconnect(); //Disconnect if send failed
      wasConnected=false;
      return;
}

....

Cellular.disconnect(); //Disconnect and restart loop
wasConnected=false;

Actually, this test has the weirdest behaviour:

1:
BREATHING WHITE
BL GREEN (Seconds)
Connect
GREEN BREATH
Connecting…
Wasn’t connected
Connected: 10.119.100.26
OK

2:
BL GREEN (on Connect()!! Blocking for 5.15min)
Connect
Blue BREATH
WHITE BREATH
Connecting…
GREEN BLINKING
Wasn’t connected
GREEN BREATH
Connected: 10.119.100.26
OK
BLUE BREATHING


TEST 3 (and similars):
Now I would like to put into System.sleep(SLEEP_MODE_DEEP, 60, SLEEP_NETWORK_STANDBY), so, as the doc says, Keep cellular running. That means I shouldn’t need to connect() and udp.begin(PORT) again.

  • If I do that, the led is always breathing white and I can’t break from while(Cellular.connecting()).

  • If I connect() every restart, without udp.begin(PORT) the 2nd+ times, send fails.

  • If I connect() and udp.begin(PORT) everytime, I CAN SEND but I don’t receive the ACK.

1 CYCLE:
Connect
Connecting…
UDP begin…
Connected: 10.119.49.145
OK!

2 CYCLE:
Connect
Connecting…
UDP begin…
Connected: 10.119.49.145
** No packets 0 (NO ACK!!!)


So, what’s the correct way to call Cellular functions?
Note that:

  • I don’t have SYSTEM_THREAD(ENABLED). Actually the doc says to call udp.begin(LOCAL_PORT) after Cellular.ready() == true ONLY if system_thread is enabled. What about if is not enabled?
    Actually If I also tried putting udp.begin(LOCAL_PORT) in setup(), I got IP from connect()/ready() but I can’t send any packets.

"If you are listening on a specific port (my case), you need to call begin(port) again every time the network is disconnected and reconnects, as well."
What about with system sleep? Is a disconnection/reconnection? Actually we saw as udp.beginning every sleeps prevent receiving acks.

  • APN is correct. It doesn’t need credentials, so I didn’t include cellular_hal.h

Sorry for the long post.