Failed to create socket & disconnect issues

Hi!

I am using a Sara SFF R410 and use it to sample some sensors which are then sent via NB-IoT to a backend. However, after running for a while, the createSocket method fails and keeps failing every time it is called. The only way to work around it is to manually restart the board which is not something I want to do. The following is the code I’m using to send messages.

bool sendMessage(const char* payload) {
    if (nbiot.isConnected()) {
        socketID = nbiot.createSocket(localPort);
        if (socketID >= 7 || socketID < 0) {  
            DEBUG.println("Failed to create socket");
            return false;
        }
        if (!nbiot.socketSend(socketID, cdp, port, payload)) {  
            DEBUG.println("Could not send message!");
            nbiot.closeSocket(socketID);
            return false;
        }
        else {         
            DEBUG.println("Message successufully sent!");
            nbiot.closeSocket(socketID);
            return true;
        }
    }
    else {
        nbiot.disconnect();     // is this needed?
        tryConnect();           // try new connection to nb-iot network 
        return false;
    }
}

Is it a good practice to open and close a socket (with the same id) each time a message is to be sent or should the socket be open at all time?

Also, sometimes my board gets disconnected from the network and is unable to connect again although it is stationary. Once again the only thing that works is manually resetting it. Is there any way to reset the whole board by code?

Any help is greatly appreciated!

hi @mmolder,

It looks like you use the old Sodaq_nbIOT library.
We have switched to the N2X and R4X libraries. Can you update your sketch to use one of these libraries?

Best regards,
Jan

Oh I see, didn’t know that. I will try to rewrite my project using the new library.

Is there any new board files needed for this? When trying to compile the http_test with a SODAQ SFF as my board, it gives the following error:

...\Arduino\libraries\Sodaq_R4X\src\Sodaq_R4X.cpp: In constructor 'Sodaq_SARA_R4XX_OnOff::Sodaq_SARA_R4XX_OnOff()':

...\Arduino\libraries\Sodaq_R4X\src\Sodaq_R4X.cpp:2527:18: error: 'SARA_TX_ENABLE' was not declared in this scope

     digitalWrite(SARA_TX_ENABLE, LOW);

I have the SFF R410M.

Hi @mmolder,

Do you have the following line in your code:
static Sodaq_SARA_R4XX_OnOff saraR4xxOnOff;

The latest version of the boardsfiles has the SARA_TX_ENABLE pin.
Can you check if you have 1.6.20 installed?
https://support.sodaq.com/getting_started/

Best regards,
Jan

Thank you! It compiled fine after updating from version 1.6.18 to the current one.

1 Like

I’ll continue with this thread as I still have some issues with my connection to the NB-IoT network. Initially the connection works fine and keeps like this for a while until suddenly connection is lost for some reason. When I then try to do a connect again using the specified apn and urat (same as the first time) it never succeeds and keeps returning false.

Is there anything more needed than a connect when connection is lost? Do you need to do a new init or maybe a disconnect before trying again?

Hi @mmolder,

I see that v2.0.0 isn’t available in the Arduino Library Manager yet.
Can you manually update the files from GitHub and test again?

Best regards,
Jan

I will try and see if it helps. The biggest problem is that it takes time to test since the error not immediate. Just to be sure that what I’m testing is not incorrectly implemented with the new R4X library, this is now my code for sending messages given that there is an active connection to the NB-IoT network.

#include <Sodaq_R4X.h>
#define DEBUG SerialUSB    
#define UBLOX Serial1    
  
static Sodaq_R4X r4x;
static Sodaq_SARA_R4XX_OnOff saraR4xxOnOff;

uint8_t  socketID;
uint16_t localPort = 16666;

const char* apn = "...";
const char* cdp = "...";
const char* urat = "8";
uint16_t port = ...;
void setup() {  
    while (!DEBUG);  
    DEBUG.begin(baud);
    UBLOX.begin(r4x.getDefaultBaudrate());
    r4x.setDiag(DEBUG);
    r4x.init(&saraR4xxOnOff, UBLOX);
    nbiotConnect(); // try connection to nb-iot network
}
//
void loop() {
    /* 
    ....
    do other things
    ....
    */
    sendMessage(data);
}
//
bool nbiotConnect() {
    // try to connect to nbiot endpoint and udp-server  
    DEBUG.println("Trying to connect..");
    isConnected = r4x.connect(apn, urat);
    DEBUG.println(isConnected ? "Connected successfully!" : "Network connection failed");
    return isConnected;
}
//
bool sendMessage(const char* payload) {
    if (r4x.isConnected()) {
        // there isn't a socket already open
        if (socketID >= 7 || socketID < 0 || !socketID) {
            socketID = r4x.socketCreate(localPort, UDP);
            if (socketID >= 7 || socketID < 0) {  
                DEBUG.println("Failed to create socket");
                return false;
            }
        }
        if (!r4x.socketConnect(socketID, cdp, port)) {
            DEBUG.println("Could not connect to remote host");
            return false;
        }
        else {
            r4x.socketWrite(socketID, (const uint8_t *)payload, strlen(payload));      
            DEBUG.println("Message successufully sent!");
            return true;
        }
    }
    else {
        DEBUG.println("Not connected to NB-IoT network");
        nbiotConnect(); // try new connection to the network
        return false;
    }
}

Hi @mmolder,

Thanks for sharing the code you use.
I see the issue. The new firmware requires you to set a MNO profile.

r4x.connect(CURRENT_APN, CURRENT_URAT, CURRENT_MNO_PROFILE, CURRENT_OPERATOR, BAND_MASK_UNCHANGED, BAND_MASK_UNCHANGED);

See also the updated example code inside the library

Also inside the Sodaq_R4X.cpp you can uncomment //#define DEBUG together with r4x.setDiag(DEBUG);you will get mode information about what is send and received from the ublox module.

Best regards,
Jan

Ah, I used this version of connect since, with the old version, it would not compile when using the one with 6 arguments. I was using the library available from the arduino ide while reading the source code from the updated git which caused a lot of confusion as to why it was not working although I was following the examples provided.

Now it compiled fine when using the correct version. Hopefully this fixes my problem with being unable to reconnect after losing connection to the network.

Thank you for the help and for taking the time!

Hmm, I still have problems with managing the sockets for sending my data. After running for a while with the code I posted earlier, the socketConnect method fails, and keeps failing indefinitly. Am I doing something in the wrong order or simply incorrectly in my sendMessage method?

Also, I turned on debug in the R4X.cpp file and will add the error code which is returned as soon as I can replicate it while debug is active.

I “finally” managed to reproduce the error after running my board for some time. The exchange of AT-commands happening at the time of the error is as follows. From the last successful connection to the first failed.

>> AT+CGATT?
<< +CGATT: 1
<< OK
>> AT+CSQ
<< +CSQ: 2,99
<< OK
>> AT+CGDCONT?
<< +CGDCONT: 1,"IP","APN","IPADDR",0,0,0,0
<< OK
>> AT+USOCO=1,"CDP",PORT
<< OK
>> AT+UDCONF=1,1
<< OK
>> AT+USOWR=1,77
<< @
>> <data>
<< +USOWR: 1,77
<< OK
>> AT+CGATT?
<< +CGATT: 1
<< OK
>> AT+CSQ
<< +CSQ: 2,99
<< OK
>> AT+CGDCONT?
<< +CGDCONT: 1,"IP","APN","IPADDR",0,0,0,0
<< OK
>> AT+CGATT?
<< +CGATT: 1
<< OK
>> AT+CSQ
<< +CSQ: 4,99
<< OK
>> AT+CGDCONT?
<< +CGDCONT: 1,"IP","APN","IPADDR",0,0,0,0
<< OK
>> AT+USOCO=1,"CDP",PORT
<< +CME ERROR: Operation not allowed

After this, the CME ERROR is given each time I try to connect to the socket.

It looks like you are still connected to socket 1, and are not allowed to make a 2nd connection to socket 1.

1 Like

Hello , iam using u blox kit for finding signal strength also using a software mcentre.can you please tell me the AT command to know to which base station we are connected to?

Hi @Akshatha,

You can find all R410M commands here:

With the command AT+CEREG=2 you can get the cell info.

Best regards,
Jan