Arduino MKR NB 1500 Connection to Losan

Hi,

I am trying to use Arduino MKR NB 1500 to connect to Losant platform via MQTT. However, I can only find examples of MQTT connection using WIFI. Would you be able to help me with that on how to use MKR NB 1500 connecting to LOSANT MQTT using GPRS?

Thank you very much.
Regards
Eric

Hey @Eric_Wang,

Welcome to the Losant forums! We’re happy to have you.

I took a look at the Arduino MKR NB 1500, and it looks like it’s a narrowband LTE-connected device. Once your device is connected to an LTE/Cellular network, the connection to the Losant MQTT broker should not be any different than with a WiFi connection.

While I don’t have a Losant specific example for you, Arduino provides a great Getting Started Page, and they also provide an example sketch for selecting radio access technology for that board.

If you run into any issues with connecting to the Losant MQTT Broker, please don’t hesitate to reach out.

Thank you,
Heath

Hi, Heath

Thanks a lot for the reply. Yes. I could connect the board to other MQTT brokers such as HiveMQ easily

However, when I looked the provided example for Arduino on your turtorial, I could not find any code specifying the broker and port, such as

In addition, how do I use this function

device.connectSecure(wifiClient, LOSANT_ACCESS_KEY, LOSANT_ACCESS_SECRET);

without a wificlient but an NBClient instead. Should I use the general Arduino MQTT library?

Any help would be appreciated.

Regards

Eric

@Eric_Wang,

I’m with you now!

I’m not entirely sure if our library will take NBClient instead of wifiClient in device.connectSecure(wifiClient, LOSANT_ACCESS_KEY, LOSANT_ACCESS_SECRET);, this is something for me to test. :slight_smile:

Have you tried to use NBClient instead? Did you run into an error?

If you’re looking for broker URL and port information for the Losant MQTT broker, you can find that in our documentation.

Let me know if this helps.

Thank you,
Heath

I use the MKR1500 with that library.

Works great.

Stew

Hi, Stewart

Which library were you referring to? Thanks a lot.

Cheers,
Eric

Sorry , I used the MKRNB library and the Losant MQTT library

I mashed code from the MKR1010 & MKRNB Webclient examples and basically replaced the Wifi connection code with the NB code.

The below code is just snippets and not compiled or tested

#include <MKRNB.h>
#include <Losant.h>

const char* LOSANT_DEVICE_ID = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
const char* LOSANT_ACCESS_KEY = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
const char* LOSANT_ACCESS_SECRET = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

LosantDevice device(LOSANT_DEVICE_ID);
NBClient client;
GPRS gprs;
NB nbAccess;

void setup() {
  Serial.begin(115200);

while (!connected) {
    if ((nbAccess.begin("","your APN here") == NB_READY) &&
        (gprs.attachGPRS() == GPRS_READY)) {
connected = true;         
 } 
else {
      Serial.println("Not connected");
      delay(1000);
    }

  Serial.println("connecting...");

device.connect(client, LOSANT_ACCESS_KEY, LOSANT_ACCESS_SECRET);

while (!device.connected()) {
   Serial.println("Working...");
  }
  Serial.println("Connected to Losant!!");

StaticJsonDocument<200> jsonBuffer;
  JsonObject root = jsonBuffer.to<JsonObject>();
  root["update_reason"] = global_activity;
  root["door_A_status"] = DOOR_A_STATE;
  root["door_B_status"] = DOOR_B_STATE;
  root["tamper"] = TAMPER_STATE;
  root["mcu_batt"] = batt;
  root["last_User_0"] = uid[0];
  root["last_User_1"] = uid[1];
  root["last_User_2"] = uid[2];
  root["last_User_3"] = uid[3];
  device.sendState(root);
  }

Hi, Health

Thanks for the reply. There is some progress now, and I can see the log from the Losant application. However, the log kept having these message, and in the Arduino monitor, the connection kept hanging.
Application Log

Clear

Device Test disconnected from Losant

Connection Closed

Sat May 22, 2021 07:38:59.506 GMT+10:00

Device Test connected to Losant

Connection succeeded over an insecure channel

Sat May 22, 2021 07:38:58.788 GMT+10:00

Device Test authenticated with Losant

Authentication succeeded

Sat May 22, 2021 07:38:58.786 GMT+10:00

Device Test disconnected from Losant

Connection Closed

Sat May 22, 2021 07:33:42.067 GMT+10:00

Device Test authenticated with Losant

Authentication succeeded

Sat May 22, 2021 07:33:41.355 GMT+10:00

Device Test connected to Losant

Connection succeeded over an insecure channel

Sat May 22, 2021 07:33:41.355 GMT+10:00

Device Test disconnected from Losant

Connection Closed

Sat May 22, 2021 07:33:21.608 GMT+10:00

Device Test connected to Losant

Connection succeeded over an insecure channel

Sat May 22, 2021 07:33:20.867 GMT+10:00

Device Test authenticated with Losant

Authentication succeeded

Sat May 22, 2021 07:33:20.863 GMT+10:00

Device Test disconnected from Losant

Connection Closed

Sat May 22, 2021 07:32:55.988 GMT+10:00

Device Test connected to Losant

Connection succeeded over an insecure channel

Sat May 22, 2021 07:32:55.271 GMT+10:00

Device Test authenticated with Losant

Authentication succeeded

Sat May 22, 2021 07:32:55.270 GMT+10:00

@Eric_Wang,

Can you please share a screenshot of your application log? when your device is connecting/disconnecting rapidly like this?

Would you be able to share your updated code now that you have been able to connect to the platform?

Thank you,
Heath

Hi, Health

Here are the codes:

#include <MKRNB.h>
#include <Losant.h>

const char PINNUMBER[] = "";

// initialize the library instance
NB nbAccess;        // NB access: include a 'true' parameter for debug enabled
GPRS gprsAccess;  // GPRS access
NBClient client;  // Client service for TCP connection

// Losant credentials.
const char* LOSANT_DEVICE_ID = "";
const char* LOSANT_ACCESS_KEY = "";
const char* LOSANT_ACCESS_SECRET = "";

LosantDevice device(LOSANT_DEVICE_ID);

void setup() {
  // initialize serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  

  connect();
}

void connect() 
{
  Serial.print("Connecting NB IoT / LTE Cat M1 network...");
  if (nbAccess.begin(PINNUMBER) != NB_READY) {
    Serial.println("error");
    while (true);
  }
  Serial.println("ready");

  // attach GPRS
  Serial.println("Attaching to GPRS...");
  if (gprsAccess.attachGPRS() != GPRS_READY) {
    Serial.println("error");
  } else {

  Serial.println("connected");}
  // Connect to Losant.
  Serial.println();
  Serial.print("Connecting to Losant...");

  device.connect(client,LOSANT_ACCESS_KEY, LOSANT_ACCESS_SECRET);

  while(!device.connected()) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("Connected!");
}

void loop() {
  bool toReconnect = false;


  if (gprsAccess.attachGPRS() != GPRS_READY) {
      Serial.println("Disconnected from GPRS");
      toReconnect = true;
    }

  if(!device.connected()) {
    Serial.println("Disconnected from Losant");
    toReconnect = true;
  }

  if(toReconnect) {
    connect();
  }

  device.loop();

  StaticJsonDocument<200> jsonBuffer;
  JsonObject root = jsonBuffer.to<JsonObject>();
  root["Temp2"] = random(10);

  // Send the state to Losant.
  device.sendState(root);

  delay(100);

}

Thank you very much,

Hi @Eric_Wang,

I am unfamiliar with the GPRS library that you are using, but what does gprsAccess.attachGPRS() return? Reading through the example that Arduino provides, it looks like this function establishing a connection.

To check to see if the your GPRS access is still available, I would recommend that you use something like the available() method that this library provides. The example that I linked in the above paragraph has an example on how this is used, as well as a reference here.

Lots of things can cause this looping to occur, though. Here is a great post on some of the debugging steps that you can take if the above code changes do not help.

Let me know if this doesn’t help and you need more guidance.

Thank you,
Heath

Hi, Health

Thank you very much for the help. I have tried the example code and it connected fine to the web service. However, I am still having the same problem. Any chance it was the problem of my losant end?

Here are my losant credentials and my account is eric.wgk@hotmail.com.

const char* LOSANT_DEVICE_ID = “60a4e8ca6bd5010006b562ef”;
const char* LOSANT_ACCESS_KEY = “b09adc31-d0ec-463a-adf7-66f0b7b4ed4c”;
const char* LOSANT_ACCESS_SECRET = “2edabb64fdabf05e5513b2d4fd503cee80215ec31728854380034f02d906030e”;

I have tried these credentials using Python, and it worked fine. Could you please check that for me?

Regards,
Eric

Hi, Stewart

Thanks for providing the example. I am actually using the same example, however, I am getting this type of errors again and again on my losant application

Device Test disconnected from Losant

Connection Closed

Sat May 22, 2021 07:38:59.506 GMT+10:00

Device Test connected to Losant

Connection succeeded over an insecure channel

Sat May 22, 2021 07:38:58.788 GMT+10:00

Device Test authenticated with Losant

Authentication succeeded

Have you encountered this issue before?

Thank you very much.
Eric

Are you posting any MQTT data? If that works your 99% there.
The NB-IoT connection stays connected to your cell network/APN but the MQTT connection only stays active for about 3 seconds. Just long enough to post data.

You need to connect to the client(Losant) again if it disconnects before you send more data
Stewart

@Eric_Wang,

We advise that you do not post personal information in the forums as this is a public space.

Any chance it was the problem of my losant end?

This is an unlikely case. As you mentioned, the credentials you listed work fine with a python script.

Are you able to test a different device? Are you able to test a different network?

I have tried the example code and it connected fine to the web service.

The example that I linked from Arduino was specifically dealing with GPRS connection. The reason I mentioned that example is because they use attachGPRS() only once, and in checking for connection, the example uses available(). To me, this seems like in your code where you are using attachGPRS() in the loop() function, your device is disconnecting and reconnecting every time the code loops through.

Thank you,
Heath

device.connect(client,LOSANT_ACCESS_KEY, LOSANT_ACCESS_SECRET);

while(!device.connected()) {
delay(500);
Serial.print(".");
}

Serial.println(“Connected!”);
}

void loop() {
// if there are incoming bytes available
// from the server, read them and print them:
device.loop();

StaticJsonDocument<200> jsonBuffer;
JsonObject root = jsonBuffer.to();
root[“temp2”] = random(10);

// Send the state to Losant.
device.sendState(root);

delay(100);
}

Hi Stewart

The program stuck at the while loop that checks device connection as i saw from the serial monitor, so it did not even get to the post message part.

Any other possibilities?

Thanks.
Eric

Hi, Heath

device.connect(client,LOSANT_ACCESS_KEY, LOSANT_ACCESS_SECRET);

while(!device.connected()) {
delay(500);
Serial.print(".");
}

Serial.println(“Connected!”);
}

void loop() {
// if there are incoming bytes available
// from the server, read them and print them:
device.loop();

StaticJsonDocument<200> jsonBuffer;
JsonObject root = jsonBuffer.to();
root[“temp2”] = random(10);

// Send the state to Losant.
device.sendState(root);

delay(100);
}

The program stuck at the while loop that checks device connection as i saw from the serial monitor, so it did not even get to the post message part.

Any help would be great
Thanks.
Eric

Hi, Heath

I have finally solved the problem from someone in Optus.We have to use the MQTT connection by AT command instead of this HTTP library. Thanks a lot for the help anyway.

On another question, I am running a IoT design project subject at James Cook University. I have completed your Losant university course, and should be able to deliver them to students. I am wondering if you have other resources that I could use for the class? e.g., tutorial, workshop, slides. Thanks for your help.

Regards
Eric

@Eric_Wang,

Glad you were able to get things working.

I am wondering if you have other resources that I could use for the class? e.g., tutorial, workshop, slides.

In our documentation, we have the Workflow Lab that helps users get better at using Losant’s visual workflow engine. We also have the Losant Walkthrough which allows users to interact with Losant features without a physical device.

Feel free to ask more questions here in the forums, or have your students ask questions here as well!

Thank you,
Heath