Sunday 27 October 2019

Connecting Arduino MKR NB 1500 to Vodafone UK NB-IoT trial service

I struggled to find good documentation on how to connect the Arduino MKR NB 1500 to an NB-IoT service.  There's scant information on the Arduino website which is disappointing, I've been using Arduino boards for years and usually find them simple and well documented and the quality of libraries is good.  In this case though, I found little info on how to setup the uBlox SARA R410M chip on the MKR NB 1500.  The Arduino MKRNB library doesn't seem to include enough functionality to configure things like the APN and band leaving you having to figure it out from the UBlox documentation and using AT commands.  This is something I've done before with other embedded systems but do not expect it to be this hard with Arduino!

Anyway, enough complaining, I get this is new technology!  I've got it working with the Vodafone UK NB-IoT service now and this is how.

You'll need to be able to send AT commands to the uBlox chip and see the returned results, here's some code that'll allow you to do this interactively using the serial connection to the Arduino:

#include <Arduino.h>
#include <MKRNB.h>
NBModem modem;

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

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

  Serial.begin(baud);

  while (!Serial) {
    ;
  }
  Serial.println("CPU connected to PC");
  if (modem.begin()) {
    Serial.println("Modem connected to CPU");
  } else {
    Serial.println("ERROR, no modem answer.");
  }

  SerialSARA.begin(baud);
}

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

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

I'm using Atom IDE with Platform IO plugin, if you're using the Arduino IDE then remove the first include line from the code. You'll need to have installed the  MKRNB library from the Platform IO libraries window.

You'll also need something to send messages to from the MKR1500 board, I used a simple Python script that listens for UDP connections and prints out any text received.  I'll let you figure that out on your own, as an even mode basic test you could just run tcpdump or Wireshark on the receiving computer, it needs to be publicly accessible though as the UDP packet will come form Vodafone's M2M network.

Before we start, a few notes on the Vodafone UK trial NB-IoT service:


  • it's a trial service, it's not yet meant for use in anger, I suppose it could go offline at any time for any reason with no guarantees of when it'll come back
  • SIMs are not publicly available yet, I got a trial as it's related to my job but I cannot help you get SIMs so please don't ask.  I am hoping that we'll be able to offer SIMs from my work once Vodafone move from trial to general release of their service
  • It's in the T&Cs that you only use UDP, I think this is to keep network traffic to a minimum for the trial
  • coverage is limited and there's no published map.  They say the west side of the UK is mostly covered, I'm near Manchester and it works.  10 miles away on the other side of the Pennines where my office is, it doesn't work yet.
Once you can send AT commands to the uBlox chip, here's a list if commands in the order I used them to get the connection working and send a "hello world" message over UDP.  Since I figured this out by reading the uBlox documentation and using trial & error, some steps may not be necessary.  Once I've got this written down I plan on factory resetting the uBlox chip and re-doing it, if anything changes I'll edit this post.

Oh, also, here is a link to the uBlox documentation listing all the AT command set:


AT commands (run in this order):

CommandExpected ResponseDescription
ATOKno operation, tests execution of AT commands
AT+URAT=8OKSet radio access technology. 8 means NB-IoT only
AT+CGDCONT=1,"IP","ep.inetd.gdsp"OKSet connection type and APN
AT+CGACT=1,1OKActivate the connection config (not sure this is needed)
AT+UBANDMASK=1,524288OKChange band. This sets the band for NB-IoT to 20 which Voda UK uses.
AT+CREG=2OKEnable network registration
AT+CFUN=15OKResets/reboots the modem, takes a couple of seconds
AT+CGATT?+CGATT: 1Queries the attachment status, 1 means attached! Wait and retry if it's 0
AT+CSQ+csq: 15,99Query signal quality, 1st number returned, if it's 99 then no signal at all
AT+USOCR=17+USOCR: 0Create a socket, 17 is UDP. The returned number is the socket ID used later on
AT+USOCO=0,"0.0.0.0",0000OKConnect over socket, 0.0.0.0 is the IP to connect to, 0000 is the port. The first number (0) is the socket ID from above.
AT+USOWR=0,12,"Hello world!"+USOWR: 0,12Send some data! First number is the socket ID, 2nd number is the data length
AT+COPS?+cops: 0,0,"Vodafone@ DATA ONLY",9Query network connected info
AT+CGDCONT?+CGDCONT: 1,"IP","ep.inetd.gdsp","123.123.123.123",0,0,0,0Query APN & IP info. 123.123.123.123 is the IP assigned by the network.
AT+USOCL=0OKClose the socket once finished.
AT+CGATT=0OKDetach from the network.
That should have got you connected and some data sent over UDP. As far as I'm aware, the data such as APN, bands etc... should be stored in the uBlox chip so after this, I think using the Arduino MKRNB library should work OK. That's what I'll try next.

I also think that after this you can just do "AT+CGATT=1" to re-attached to the network.