Thanks for your response @Jan, that removed some confusion already.
I am trying to send some sensor values through UDP to AllThingsTalk through my Vodafone NB IoT network using R410 AFF. Following is the code. I have commented out some of the project related info.
const char* provider = "NL VODAFONE NB-IoT";
const char* apn = "nb.inetd.gdsp";
const char* forceOperator = "20404"; // optional - depends on SIM / network
const char* urat = "8";
#define CURRENT_APN "nb.inetd.gdsp"
#define CURRENT_OPERATOR AUTOMATIC_OPERATOR
#define CURRENT_URAT SODAQ_R4X_NBIOT_URAT
#define CURRENT_MNO_PROFILE MNOProfiles::VODAFONE
#define NBIOT_BANDMASK "524420"
/* SODAQ SARA AFF*/
#define DEBUG_STREAM SerialUSB
#define MODEM_STREAM Serial1
#define powerPin SARA_ENABLE
#define enablePin SARA_TX_ENABLE
#warning "ARDUINO_SODAQ_SARA selected"
static Sodaq_SARA_R4XX_OnOff saraR4xxOnOff;
void configureSODAQPins() {
#ifdef powerPin
// Put voltage on the nb-iot module
pinMode(powerPin, OUTPUT);
digitalWrite(powerPin, HIGH);
#endif
#ifdef R4XX
// Switch module voltage
pinMode(voltagePin, OUTPUT);
digitalWrite(voltagePin, LOW);
#endif
#ifdef enablePin
// Set state to active
pinMode(enablePin, OUTPUT);
digitalWrite(enablePin, HIGH);
#endif // enablePin
}
void sendMessageThroughUDP()
{
sodaq_wdt_safe_delay(1000);
int localPort = 16666;
int socketID = r4x.socketCreate(localPort);
if (socketID >= 7 || socketID < 0) {
DEBUG_STREAM.println("Failed to create socket");
DEBUG_STREAM.print("Socket ID"); DEBUG_STREAM.println(socketID);
}
else{
String deviceId = <<deviceID-Allthingstalk>>;
String token = <<deviceToken-Allthingstalk>>;
// create JSON values
String value = "{\"asset_1\":{\"value\":" + <<VALUE>> + "}}";
String reading = deviceId + '\n' + token + '\n' + value;
uint8_t rsize = reading.length();
int lengthSent = r4x.socketSend(socketID, ALLTHINGSTALK_IP, ALLTHINGSTALK_PORT, (uint8_t*)reading.c_str(), rsize);
socketClosed = r4x.socketClose(socketID);
}
return;
}
void setup() {
configureSODAQPins();
DEBUG_STREAM.begin(115200);
// InitSodaq();
MODEM_STREAM.begin(r4x.getDefaultBaudrate()); // set to 115200 in r4x.h
if (SODAQ_DEBUG) r4x.setDiag(DEBUG_STREAM);
r4x.init(&saraR4xxOnOff, MODEM_STREAM);
// Other init functions for sensors
...
...
...
startMillis = millis(); // Saves the initial millis value
}
void loop() {
sodaq_wdt_safe_delay(200);
unsigned long ElapseCnt;
ElapseCnt = millis() - startMillis;
....
// read all sensor values
.....
// send every hour
if (ElapseCnt >= SENDINTERVAL) { //
// if (!r4x.connect(CURRENT_APN, CURRENT_URAT, CURRENT_MNO_PROFILE, CURRENT_OPERATOR, BAND_MASK_UNCHANGED, NBIOT_BANDMASK)){
if (!r4x.connect(apn, urat, NBIOT_BANDMASK)) {
// if (!r4x.connect(apn, urat)){
DEBUG_STREAM.println("########FAILED TO CONNECT TO MODEM########");
return;
}
else {
DEBUG_STREAM.println("########MODEM CONNECTED SUCCESSFULLY########");
}
sendMessageThroughUDP();
ElapseCnt = 0;
if (!r4x.disconnect()) {
DEBUG_STREAM.println("########FAILED TO DISCONNECT FROM MODEM########");
}
else {
DEBUG_STREAM.println("########MODEM DISCONNECTED SUCCESSFULLY########");
}
}
// wait for some time (10 minutes) for next measurement
sodaq_wdt_safe_delay(MEASUREINTERVAL);
}
I edited the code to remove some sensitive information, but kept all the ones with the pinModes and connections and message sending. This edit might have caused some formatiing errors.
As you can see, I do regular measurements (maybe around every 10 minutes) and the data is sent every hour. The current approach is where I connect to the service and disconnects every time I send a message. Also, I have different options to connect to the service using r4x.connect(), I have multiple examples that work currently.
The problem I have is the unstability of the network. After sending messages for a few hours( 4 or 5), the modem does not respond to any AT commands. I even wrote a procedure to reset the modem when that happens with the following.
//1
digitalWrite(powerPin, LOW);
digitalWrite(powerPin, HIGH);
//2
sodaq_wdt_reset();
They did not restore the connection, however a hard reset with the physical button reconnects my modem.
Perhaps something that I missed caught your eye? I have seen many discussions in the forum and tried multiple solutions. The whole code is based on multiple examples I found in the forum, but this connectivity is something I cannot solve yet.
I am also quite new to SODAQ and also NB IoT services, so any help is appreciated.!