Getting the MKR NB 1500 with the Sara-R410M to connect to an MQTT server

Hello NB- and Arduino-IoT fellows,

I would like to collect environmental sensor data and send it via NB-IoT and the vodafone network in Germany to an MQTT server. I have purchased the Arduino board 'MKR NB 1500':

That is equipped with a Sara-R410M module:

So far, I have accomplished following progress:
The device successfully connects to the internet and is able to send UDP packets. However, it fails to connect via HTTP or MQTT!

I am using the standard code example from Arduino:

>     /*
>    SerialSARAPassthrough sketch

>    This sketch allows you to send AT commands from the USB CDC serial port
>    of the MKR NB 1500 board to the onboard ublox SARA-R410 celluar module.

>    For a list of supported AT commands see:
>    https://www.u-blox.com/sites/default/files/u-blox-CEL_ATCommands_%28UBX-13002752%29.pdf

>    Circuit:
>    - MKR NB 1500 board
>    - Antenna
>    - 1500 mAh or higher lipo battery connected
>    - SIM card

>    Make sure the Serial Monitor's line ending is set to "Both NL and CR"

>    create 11 December 2017
>    Sandeep Mistry
> */

> // baud rate used for both Serial ports
> unsigned long baud = 115200;

> void setup() {
>   // enable the POW_ON pin
>   pinMode(SARA_PWR_ON, OUTPUT);
>   digitalWrite(SARA_PWR_ON, HIGH);

>   // reset the ublox module
>   pinMode(SARA_RESETN, OUTPUT);
>   digitalWrite(SARA_RESETN, HIGH);
>   delay(100);
>   digitalWrite(SARA_RESETN, LOW);

>   Serial.begin(baud);
>   SerialSARA.begin(baud);
> }

> void loop() {
>   if (Serial.available()) {
>     SerialSARA.write(Serial.read());
>   }

>   if (SerialSARA.available()) {
>     Serial.write(SerialSARA.read());
>   }
> }

along with the AT commands provided in the datasheet:

>     // connecting to Vodafone
>     AT

>     17:02:31.643 -> OK
>     AT+URAT=8

>     17:02:50.328 -> OK
>     AT+CGDCONT=1,"IP","nb.inetd.gdsp"

>     17:04:18.603 -> OK
>     AT+COPS=1,2,"26202"

>     17:04:41.152 -> OK

>     //MQTT
>     AT+UMQTT=0,"352753090071689"


>     17:12:42.941 -> +UMQTT: 0,1

>     17:12:42.941 -> 
>     17:12:42.941 -> OK
>     AT+UMQTT=1,1883


>     17:13:05.804 -> +UMQTT: 1,1

>     17:13:05.804 -> 
>     17:13:05.804 -> OK
>     AT+UMQTT=2, "www.farmer.cloudmqtt.com",16806


>     17:14:00.439 -> +UMQTT: 2,1

>     17:14:00.439 -> 
>     17:14:00.439 -> OK
>     AT+UMQTT=4,"KEY","KEY"


>     17:14:58.856 -> +UMQTT: 4,1

>     17:14:58.856 -> 
>     17:14:58.856 -> OK
>     AT+UMQTTC=1

As MQTT cloud service, I am using farmers cloud for testing. Currently I'm stuck with seeing no data arrive at the cloud. The sim cards are connected, according to the vodafone m2m portal.

Thank you for your help!

Does that code compile for you?

pylon:
Does that code compile for you?

Jup, compiles for me!

Jup, compiles for me!

I doubt that! Please show me the definition of the SerialSARA variable! Post complete code and don't let us remove the '>' from the beginning of your lines!

pylon:
I doubt that! Please show me the definition of the SerialSARA variable! Post complete code and don't let us remove the '>' from the beginning of your lines!

Hi pylon, sorry, my bad! I would be happy if you can help me. Here is the complete code:

// NB-IoT to Cloud v0.0
// 11.09.2019
// Uses some commands found at https://create.arduino.cc/projecthub/voske65/arduino-nb-iot-with-sim7020-and-t-mobile-027f8f

#include <SoftwareSerial.h>
#define TIMEOUT 200        // AT-command timeout in 100ms  -> 200=20sec
#define COMMANDMAX 1024
const int SleepPin = 6;
const int PowerPin = 7;
char SimError[] = "SIM **Error ";
char SimTimeout[] = "SIM **Timeout ";
char MyIp[16];
char MyCicc[24];
int TimeOut = TIMEOUT;
SoftwareSerial mySerial(2,3);

char simresult[COMMANDMAX];

const char* NBstart[] = {
 "ATZ",
 "AT+CFUN=0",
 "AT+CREG=2",
 "AT*MCGDEFCONT=\"IP\",\"nb.inetd.gdsp\"",
 "AT+CFUN=1",
 
 "AT+COPS=1,2,\"26202\"",  // sign up to Vodafone DE
 "AT+CGCONTRDP",
 "AT+CSQ",
 "\0"  // end script with 0x00
};

//const char* NBopensocket[] = {
//  "AT+CSOC=1,2,1",
//  "AT+CSOCON=0,15683,\"172.27.131.100\"", // T-Mobile Server socket 0
//  "\0"     // end script with 0x00
//};

const char* NBopensocket[] = {
 "AT+CSOC=1,2,1",
 "AT+CSOCON=0,15683,\"172.27.131.100\"", // T-Mobile Server socket 0
//  "AT+CSOCON=0,80,\"https://us-central1-nb-iot-5f5c0.cloudfunctions.net/addMessage\?text=yash\"",
 "\0"     // end script with 0x00
};

const char* NBclosesocket[] = {
 "AT+CSODIS=0",
 "AT+CSOCL=0",
 "AT+CGACT=0,1",
 "\0"     // end script with 0x00
};

const char* NBhelloworld[] = {
 "AT+CSOSEND=0,0,\"Hello World!\"",
 "\0"     // end script with 0x00
};


int runscript(const char** scrpt) {
 int t = 0;
 int s = 1;
 //Serial.println("\n** SimScript:");
 while ( strlen( scrpt[t]) > 1) {
   s = writecommand(scrpt[t]);
   delay(1000);
   if ( s == 1 ) ++t;
   else break;
 }
 return (s);
}

int writecommand(const char* cmd) {
 char c = 0;
 int t = 0, n = 0;
 simresult[0] = 0;
 mySerial.flush();
 mySerial.print(cmd); mySerial.print('\r'); mySerial.print('\n');
 while ( n <= TIMEOUT ) {
   if (mySerial.available()) {
     c = mySerial.read();
     simresult[t] = c; simresult[++t] = 0;
     Serial.write(c);
   }
   else {
     delay(100);
     n++;
   };
   if ( (t > 3) &&  (  ( (simresult[t - 4] == 'O') && (simresult[t - 3] == 'K') && (simresult[t - 2] == '\r') && (simresult[t - 1] == '\n') )  ||  (  (simresult[t - 4] == 'O') && (simresult[t - 3] == 'R') && (simresult[t - 2] == '\r') && (simresult[t - 1] == '\n')  )  ) )  break;
 }
 if (n >= TIMEOUT) {
   Serial.println(SimTimeout);
   return (-1);
 }
 else {
   if ( (simresult[t - 4] == 'O') && (simresult[t - 3] == 'R') ) {
     Serial.println(SimError);
     return (0);
   }
   else return (1);
 }
}

void setup()
{
 mySerial.begin(19200);
 Serial.begin(19200);
 delay(1000);
 runscript(NBstart);
 delay(200);
 runscript(NBopensocket);
}

void loop()
{
 if (mySerial.available())
   Serial.write(mySerial.read());
 if (Serial.available())
   mySerial.write(Serial.read());

 //runscript(NBopensocket);
 //delay(200);
 runscript(NBhelloworld);
 delay(200);
 //runscript(NBclosesocket);
 delay(500);
}

172.27.131.100 is a private range IP, I doubt that you can connect to that private IP of T-Mobile (at least the comment says that) through Vodafone.

BTW, there another thread active that has also problems connecting through the Vodafone network. Maybe they use some network tricks to construct their network that aren't compatible with the GPRS modem used in the MKR 1500.