Hi all,
today I’m facing a strange UDP send/receive strange behaviour and I’d like to know the causes.
Setting:
Electron in Paris with a 3rd party SIM card (ThingsMobile)
SYSTEM_THREAD(ENABLED);
SYSTEM_MODE(MANUAL);
STARTUP(System.enableFeature(FEATURE_RETAINED_MEMORY));
const size_t UDP_BUFFER_SIZE = 513;
const int REMOTE_UDP_PORT = 60004;
const IPAddress REMOTE_UDP_ADDR = IPAddress( *HIDDEN* );
const int LOCAL_UDP_PORT = 60004;
const unsigned long SEND_INTERVAL_MS = 15000;
My connection loop is:
Cellular.connect(); //Connect
if (!waitFor(Cellular.ready, 60000*2)){
Serial.println("Connection attempt timeout.");
connFailed = true;
}
else{
connFailed = false;
}
if(!connFailed){ //Connecting
Serial.print("Connected in ");
Serial.print((millis () - initialTime) / 1000);
Serial.println(" seconds");
cConnecting = 0;
while(Cellular.connecting()) {
cConnecting++;
if(cConnecting == 60){
cConnecting = 0;
Serial.println("Connectioning attempt timeout.");
connFailed = true;
break;
}
delay(1000);
}
}
if(!connFailed){
//Sampling and sending to the server
int sendAttemps = 0;
while(!sentToServer(sentC)){
if((sendAttemps++) == 4){
Serial.println("Server timeout");
break;
}
}
connFailed = false;
sentToServer(char* sent):
udp.begin(LOCAL_UDP_PORT);
Serial.println(Cellular.localIP());
int sentByte = udp.sendPacket(sent, strlen(sent), REMOTE_UDP_ADDR, REMOTE_UDP_PORT);
if (sentByte < 0){ //CONNECTION ERROR
Serial.println("SendPacket error");
udp.stop();
return false;
}
else{
char udpBuffer[UDP_BUFFER_SIZE];
while (udp.available() <= 0) {
int count = udp.receivePacket((uint8_t * ) udpBuffer, UDP_BUFFER_SIZE - 1);
if (count > 0) { //OK parse the server ACK
Serial.print("Byte received: ");
Serial.println(count);
Serial.printlnf(">>> %s:%d %s", udp.remoteIP().toString().c_str(), udp.remotePort(), udpBuffer);
parseResponse(udpBuffer, count);
break;
}
else if (count == 0) { //WAITING FOR ACK
attemps++;
Serial.printlnf("** No packets %d", count);
if (attemps == 20){ //NO ACK
attemps = 0;
udp.stop();
return false;
}
delay(500);
}
else { //CONNECTION ERROR
Serial.printlnf("** receivePacket error %d", count);
udp.stop();
return false;
}
}
}
udp.stop();
delay(500);
return true;
It usually runs every minute without any problems, but last night it sent 10/15 values out of 60 per hour.
In the picture below you, every dot is a loop with connection success to the server.
To help you checking the log, let’s analyze the sample at 11:10 (1569402600 epoch time).
The log:
Connection...
Connected in 1 seconds
Char array to send: {1,41260563,7,1569402600,1:45.188529,2:5.724524,3:0.000000,7:6,8:6,9:0,4:26.23,5:47.85,6:1001.64,10:4.04}
Sending
4932.085 AT send 4 "AT\r\n"
4932.947 AT read OK 6 "\r\nOK\r\n"
4932.947 AT send 15 "AT+USOCTL=0,0\r\n"
4932.954 AT read + 19 "\r\n+USOCTL: 0,0,17\r\n"
4932.955 AT read OK 6 "\r\nOK\r\n"
Socket handle 0 (UDP) open, closing...
4932.955 AT send 12 "AT+USOCL=0\r\n"
4933.222 AT read OK 6 "\r\nOK\r\n"
Socket handle 0 was closed.
4933.222 AT send 15 "AT+USOCTL=1,0\r\n"
4933.231 AT read ERR 37 "\r\n+CME ERROR: operation not allowed\r\n"
4933.232 AT send 15 "AT+USOCTL=2,0\r\n"
4933.240 AT read ERR 37 "\r\n+CME ERROR: operation not allowed\r\n"
4933.241 AT send 15 "AT+USOCTL=3,0\r\n"
4933.250 AT read ERR 37 "\r\n+CME ERROR: operation not allowed\r\n"
4933.251 AT send 15 "AT+USOCTL=4,0\r\n"
4933.259 AT read ERR 37 "\r\n+CME ERROR: operation not allowed\r\n"
4933.260 AT send 15 "AT+USOCTL=5,0\r\n"
4933.269 AT read ERR 37 "\r\n+CME ERROR: operation not allowed\r\n"
4933.270 AT send 15 "AT+USOCTL=6,0\r\n"
4933.278 AT read ERR 37 "\r\n+CME ERROR: operation not allowed\r\n"
socketSocket: closed stale socket handle(s)
socketSocket(UDP)
4933.279 AT send 19 "AT+USOCR=17,60004\r\n"
4933.287 AT read + 13 "\r\n+USOCR: 0\r\n"
4933.288 AT read OK 6 "\r\nOK\r\n"
Socket 0: handle 0 was created
//First attempt
Connected: 10.162.188.11
socketSendTo(0,**MY_SERVER_IP**,60004,,105)
4934.289 AT send 38 "AT+USOST=0,\"**MY_SERVER_IP**\",60004,105\r\n"
4934.297 AT read > 3 "\r\n@"
4934.297 AT send 105 "{1,41260563,7,1569402600,1:45.188529,2:5.724524,3:0.000000,7:6,8:6,9:0,4:26.23,5:47.85,6:1001.64,10:4.04}"
4934.439 AT read + 17 "\r\n+USOST: 0,105\r\n"
4934.440 AT read OK 6 "\r\nOK\r\n"
4934.440 AT send 4 "AT\r\n"
4934.443 AT read OK 6 "\r\nOK\r\n"
4934.443 AT send 14 "AT+USORF=0,0\r\n"
4934.449 AT read + 15 "\r\n+USORF: 0,0\r\n"
Socket 0: handle 0 has 0 bytes pending
4934.450 AT read OK 6 "\r\nOK\r\n"
Byte sent: 105
** No packets 0
** No packets 0
** No packets 0
** No packets 0
** No packets 0
** No packets 0
** No packets 0
** No packets 0
** No packets 0
** No packets 0
** No packets 0
** No packets 0
** No packets 0
** No packets 0
** No packets 0
** No packets 0
** No packets 0
** No packets 0
** No packets 0
** No packets 0
4949.170 AT send 15 "AT+USOCTL=0,0\r\n"
socketClose(0)(UNK)
4950.172 AT send 12 "AT+USOCL=0\r\n"
4950.700 AT read + 19 "\r\n+USOCTL: 0,0,17\r\n"
4950.701 AT read OK 6 "\r\nOK\r\n"
socketFree(0)
4951.201 AT send 4 "AT\r\n"
4951.205 AT read OK 6 "\r\nOK\r\n"
4951.205 AT send 15 "AT+USOCTL=0,0\r\n"
4951.212 AT read + 19 "\r\n+USOCTL: 0,0,17\r\n"
4951.213 AT read OK 6 "\r\nOK\r\n"
Socket handle 0 (UDP) open, closing...
4951.213 AT send 12 "AT+USOCL=0\r\n"
4951.479 AT read OK 6 "\r\nOK\r\n"
Socket handle 0 was closed.
4951.479 AT send 15 "AT+USOCTL=1,0\r\n"
4951.487 AT read ERR 37 "\r\n+CME ERROR: operation not allowed\r\n"
4951.488 AT send 15 "AT+USOCTL=2,0\r\n"
4951.496 AT read ERR 37 "\r\n+CME ERROR: operation not allowed\r\n"
4951.497 AT send 15 "AT+USOCTL=3,0\r\n"
4951.505 AT read ERR 37 "\r\n+CME ERROR: operation not allowed\r\n"
4951.506 AT send 15 "AT+USOCTL=4,0\r\n"
4951.514 AT read ERR 37 "\r\n+CME ERROR: operation not allowed\r\n"
4951.515 AT send 15 "AT+USOCTL=5,0\r\n"
4951.523 AT read ERR 37 "\r\n+CME ERROR: operation not allowed\r\n"
4951.524 AT send 15 "AT+USOCTL=6,0\r\n"
4951.532 AT read ERR 37 "\r\n+CME ERROR: operation not allowed\r\n"
socketSocket: closed stale socket handle(s)
socketSocket(UDP)
4951.533 AT send 19 "AT+USOCR=17,60004\r\n"
4951.541 AT read + 13 "\r\n+USOCR: 0\r\n"
4951.542 AT read OK 6 "\r\nOK\r\n"
Socket 0: handle 0 was created
//Second attempt
Connected: 10.162.188.11
socketSendTo(0,**MY_SERVER_IP**,60004,,105)
4952.543 AT send 38 "AT+USOST=0,\"**MY_SERVER_IP**\",60004,105\r\n"
4952.551 AT read > 3 "\r\n@"
4952.551 AT send 105 "{1,41260563,7,1569402600,1:45.188529,2:5.724524,3:0.000000,7:6,8:6,9:0,4:26.23,5:47.85,6:1001.64,10:4.04}"
4952.694 AT read + 17 "\r\n+USOST: 0,105\r\n"
4952.695 AT read OK 6 "\r\nOK\r\n"
4952.695 AT send 4 "AT\r\n"
4952.698 AT read OK 6 "\r\nOK\r\n"
4952.698 AT send 14 "AT+USORF=0,0\r\n"
4952.704 AT read + 15 "\r\n+USORF: 0,0\r\n"
Socket 0: handle 0 has 0 bytes pending
4952.705 AT read OK 6 "\r\nOK\r\n"
Byte sent: 105
** No packets 0
** No packets 0
** No packets 0
** No packets 0
** No packets 0
** No packets 0
** No packets 0
** No packets 0
** No packets 0
** No packets 0
** No packets 0
** No packets 0
** No packets 0
** No packets 0
** No packets 0
** No packets 0
** No packets 0
** No packets 0
** No packets 0
** No packets 0
4967.426 AT send 15 "AT+USOCTL=0,0\r\n"
socketClose(0)(UNK)
4968.428 AT send 12 "AT+USOCL=0\r\n"
4968.827 AT read + 19 "\r\n+USOCTL: 0,0,17\r\n"
4968.828 AT read OK 6 "\r\nOK\r\n"
socketFree(0)
4969.328 AT send 4 "AT\r\n"
4969.332 AT read OK 6 "\r\nOK\r\n"
4969.332 AT send 15 "AT+USOCTL=0,0\r\n"
4969.339 AT read + 19 "\r\n+USOCTL: 0,0,17\r\n"
4969.340 AT read OK 6 "\r\nOK\r\n"
Socket handle 0 (UDP) open, closing...
4969.340 AT send 12 "AT+USOCL=0\r\n"
4969.606 AT read OK 6 "\r\nOK\r\n"
Socket handle 0 was closed.
4969.606 AT send 15 "AT+USOCTL=1,0\r\n"
4969.614 AT read ERR 37 "\r\n+CME ERROR: operation not allowed\r\n"
4969.615 AT send 15 "AT+USOCTL=2,0\r\n"
4969.623 AT read ERR 37 "\r\n+CME ERROR: operation not allowed\r\n"
4969.624 AT send 15 "AT+USOCTL=3,0\r\n"
4969.632 AT read ERR 37 "\r\n+CME ERROR: operation not allowed\r\n"
4969.633 AT send 15 "AT+USOCTL=4,0\r\n"
4969.641 AT read ERR 37 "\r\n+CME ERROR: operation not allowed\r\n"
4969.642 AT send 15 "AT+USOCTL=5,0\r\n"
4969.650 AT read ERR 37 "\r\n+CME ERROR: operation not allowed\r\n"
4969.651 AT send 15 "AT+USOCTL=6,0\r\n"
4969.659 AT read ERR 37 "\r\n+CME ERROR: operation not allowed\r\n"
socketSocket: closed stale socket handle(s)
socketSocket(UDP)
4969.660 AT send 19 "AT+USOCR=17,60004\r\n"
4969.668 AT read + 13 "\r\n+USOCR: 0\r\n"
4969.669 AT read OK 6 "\r\nOK\r\n"
Socket 0: handle 0 was created
//Third attempt
Connected: 10.162.188.11
socketSendTo(0,**MY_SERVER_IP**,60004,,105)
4970.670 AT send 38 "AT+USOST=0,\"**MY_SERVER_IP**\",60004,105\r\n"
4970.679 AT read > 3 "\r\n@"
4970.679 AT send 105 "{1,41260563,7,1569402600,1:45.188529,2:5.724524,3:0.000000,7:6,8:6,9:0,4:26.23,5:47.85,6:1001.64,10:4.04}"
4970.821 AT read + 17 "\r\n+USOST: 0,105\r\n"
4970.822 AT read OK 6 "\r\nOK\r\n"
4970.822 AT send 4 "AT\r\n"
4970.825 AT read OK 6 "\r\nOK\r\n"
4970.825 AT send 14 "AT+USORF=0,0\r\n"
4970.832 AT read + 15 "\r\n+USORF: 0,0\r\n"
Socket 0: handle 0 has 0 bytes pending
4970.833 AT read OK 6 "\r\nOK\r\n"
Byte sent: 105
** No packets 0
** No packets 0
** No packets 0
** No packets 0
** No packets 0
** No packets 0
** No packets 0
** No packets 0
** No packets 0
** No packets 0
** No packets 0
** No packets 0
** No packets 0
** No packets 0
** No packets 0
** No packets 0
** No packets 0
** No packets 0
** No packets 0
** No packets 0
4985.556 AT send 15 "AT+USOCTL=0,0\r\n"
socketClose(0)(UNK)
4986.557 AT send 12 "AT+USOCL=0\r\n"
4986.907 AT read + 19 "\r\n+USOCTL: 0,0,17\r\n"
4986.908 AT read OK 6 "\r\nOK\r\n"
socketFree(0)
4987.408 AT send 4 "AT\r\n"
4987.412 AT read OK 6 "\r\nOK\r\n"
4987.412 AT send 15 "AT+USOCTL=0,0\r\n"
4987.419 AT read + 19 "\r\n+USOCTL: 0,0,17\r\n"
4987.420 AT read OK 6 "\r\nOK\r\n"
Socket handle 0 (UDP) open, closing...
4987.420 AT send 12 "AT+USOCL=0\r\n"
4987.686 AT read OK 6 "\r\nOK\r\n"
Socket handle 0 was closed.
4987.686 AT send 15 "AT+USOCTL=1,0\r\n"
4987.695 AT read ERR 37 "\r\n+CME ERROR: operation not allowed\r\n"
4987.696 AT send 15 "AT+USOCTL=2,0\r\n"
4987.704 AT read ERR 37 "\r\n+CME ERROR: operation not allowed\r\n"
4987.705 AT send 15 "AT+USOCTL=3,0\r\n"
4987.713 AT read ERR 37 "\r\n+CME ERROR: operation not allowed\r\n"
4987.714 AT send 15 "AT+USOCTL=4,0\r\n"
4987.723 AT read ERR 37 "\r\n+CME ERROR: operation not allowed\r\n"
4987.724 AT send 15 "AT+USOCTL=5,0\r\n"
4987.733 AT read ERR 37 "\r\n+CME ERROR: operation not allowed\r\n"
4987.734 AT send 15 "AT+USOCTL=6,0\r\n"
4987.742 AT read ERR 37 "\r\n+CME ERROR: operation not allowed\r\n"
socketSocket: closed stale socket handle(s)
socketSocket(UDP)
4987.743 AT send 19 "AT+USOCR=17,60004\r\n"
4987.751 AT read + 13 "\r\n+USOCR: 0\r\n"
4987.752 AT read OK 6 "\r\nOK\r\n"
Socket 0: handle 0 was created
Connected: 10.162.188.11
socketSendTo(0,**MY_SERVER_IP**,60004,,105)
4988.753 AT send 38 "AT+USOST=0,\"**MY_SERVER_IP**\",60004,105\r\n"
4988.762 AT read > 3 "\r\n@"
4988.762 AT send 105 "{1,41260563,7,1569402600,1:45.188529,2:5.724524,3:0.000000,7:6,8:6,9:0,4:26.23,5:47.85,6:1001.64,10:4.04}"
4988.905 AT read + 17 "\r\n+USOST: 0,105\r\n"
4988.906 AT read OK 6 "\r\nOK\r\n"
4988.906 AT send 4 "AT\r\n"
4988.909 AT read OK 6 "\r\nOK\r\n"
4988.909 AT send 14 "AT+USORF=0,0\r\n"
4988.915 AT read + 15 "\r\n+USORF: 0,0\r\n"
Socket 0: handle 0 has 0 bytes pending
4988.916 AT read OK 6 "\r\nOK\r\n"
Byte sent: 105
** No packets 0
** No packets 0
** No packets 0
** No packets 0
** No packets 0
** No packets 0
** No packets 0
** No packets 0
** No packets 0
** No packets 0
** No packets 0
** No packets 0
** No packets 0
** No packets 0
** No packets 0
** No packets 0
** No packets 0
** No packets 0
** No packets 0
** No packets 0
5003.640 AT send 15 "AT+USOCTL=0,0\r\n"
socketClose(0)(UNK)
5004.641 AT send 12 "AT+USOCL=0\r\n"
5004.889 AT read + 19 "\r\n+USOCTL: 0,0,17\r\n"
5004.890 AT read OK 6 "\r\nOK\r\n"
socketFree(0)
5005.390 AT send 4 "AT\r\n"
5005.394 AT read OK 6 "\r\nOK\r\n"
5005.394 AT send 15 "AT+USOCTL=0,0\r\n"
5005.401 AT read + 19 "\r\n+USOCTL: 0,0,17\r\n"
5005.402 AT read OK 6 "\r\nOK\r\n"
Socket handle 0 (UDP) open, closing...
5005.402 AT send 12 "AT+USOCL=0\r\n"
5005.668 AT read OK 6 "\r\nOK\r\n"
Socket handle 0 was closed.
5005.668 AT send 15 "AT+USOCTL=1,0\r\n"
5005.677 AT read ERR 37 "\r\n+CME ERROR: operation not allowed\r\n"
5005.678 AT send 15 "AT+USOCTL=2,0\r\n"
5005.686 AT read ERR 37 "\r\n+CME ERROR: operation not allowed\r\n"
5005.687 AT send 15 "AT+USOCTL=3,0\r\n"
5005.695 AT read ERR 37 "\r\n+CME ERROR: operation not allowed\r\n"
5005.696 AT send 15 "AT+USOCTL=4,0\r\n"
5005.704 AT read ERR 37 "\r\n+CME ERROR: operation not allowed\r\n"
5005.705 AT send 15 "AT+USOCTL=5,0\r\n"
5005.713 AT read ERR 37 "\r\n+CME ERROR: operation not allowed\r\n"
5005.714 AT send 15 "AT+USOCTL=6,0\r\n"
5005.722 AT read ERR 37 "\r\n+CME ERROR: operation not allowed\r\n"
socketSocket: closed stale socket handle(s)
socketSocket(UDP)
5005.723 AT send 19 "AT+USOCR=17,60004\r\n"
5005.732 AT read + 13 "\r\n+USOCR: 0\r\n"
5005.733 AT read OK 6 "\r\nOK\r\n"
Socket 0: handle 0 was created
//Fourth attempt
Connected: 10.162.188.11
socketSendTo(0,**MY_SERVER_IP**,60004,,105)
5006.734 AT send 38 "AT+USOST=0,\"**MY_SERVER_IP**\",60004,105\r\n"
5006.743 AT read > 3 "\r\n@"
5006.743 AT send 105 "{1,41260563,7,1569402600,1:45.188529,2:5.724524,3:0.000000,7:6,8:6,9:0,4:26.23,5:47.85,6:1001.64,10:4.04}"
5006.886 AT read + 17 "\r\n+USOST: 0,105\r\n"
5006.887 AT read OK 6 "\r\nOK\r\n"
5006.887 AT send 4 "AT\r\n"
5006.890 AT read OK 6 "\r\nOK\r\n"
5006.890 AT send 14 "AT+USORF=0,0\r\n"
5006.896 AT read + 15 "\r\n+USORF: 0,0\r\n"
Socket 0: handle 0 has 0 bytes pending
5006.897 AT read OK 6 "\r\nOK\r\n"
Byte sent: 105
** No packets 0
** No packets 0
** No packets 0
** No packets 0
** No packets 0
** No packets 0
** No packets 0
** No packets 0
** No packets 0
** No packets 0
** No packets 0
** No packets 0
** No packets 0
** No packets 0
** No packets 0
** No packets 0
** No packets 0
** No packets 0
** No packets 0
** No packets 0
5021.622 AT send 15 "AT+USOCTL=0,0\r\n"
socketClose(0)(UNK)
5022.623 AT send 12 "AT+USOCL=0\r\n"
5023.088 AT read + 19 "\r\n+USOCTL: 0,0,17\r\n"
5023.089 AT read OK 6 "\r\nOK\r\n"
socketFree(0)
Server timeout
In this case I actually got the UDP packet to the server, in fact server side I had:
1569402613.77 Received 105 bytes from ('5.35.166.171', 4292)
< {1,41260563,7,1569402600,1:45.188529,2:5.724524,3:0.000000,7:6,8:6,9:0,4:26.23,5:47.85,6:1001.64,10:4.04}
41260563 > INSERT INTO ....
41260563 10 records inserted
1569402613.78 Sending 25 bytes to ('5.35.166.171', 4292) //ACK
> {1,41260563,7,1569402613}
Then the server stopped receiving samples for 12minutes
Any idea why the connection electron/server is unreliable? Am I missing something?
Thank you for the help