Hello Arduino Community,
since a few days I do not get on with my project.
I try to connect with an Arduino MKR NB 1500 as MQTT client to a MQTT broker. Therefore I use the MKRNB.h and the PubSubClient.h libaries.
My hardware setup also includes a suitable antenna and an IoT SIM card. I am confident that the problem is not with my hardware setup because I was already able to output the HTML of a website from the serial interface using the example code "NBWebClient.ino" of the MKRNB.h library.
I already tried to connect to different brokers from this list as well as shiftr .io and tingg .io.
Now I am going to let the code speak.
// libraries
#include <MKRNB.h>
#include <PubSubClient.h>
//The MKRNB lib needs this even if its blank; My sim card does not require a pin
const char PINNUMBER[] = "";
//MQTT info
const char* mqtt_server = "broker.hivemq.com";
const int port = 1883;
const char* clientID = "MKRNB1500";
const char* topic = "test";
const char* payload = "Hallo MQTT Welt";
// const char* username = "username";
// const char* password = "password";
// initialize the library instance
NBClient LTEclient;
GPRS gprs;
NB nbAccess;
//connect the pubsub client
PubSubClient client(LTEclient);
//connection and reconnection function
void reconnect() {
while (!client.connected()) {
// Attemp to connect
Serial.println("connection attemp to broker");
if (client.connect(clientID)) { //client.connect(clientID,username,password)
Serial.println("Connected");
client.publish(topic,"hello mqtt");
//client.subscribe(subtopic);
} else {
Serial.print("Failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 2 seconds");
// Wait 2 seconds before retrying
delay(2000);
}
}
}
void setup() {
// initialize serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.println("starting up");
// connection state
bool connected = false;
// After starting the modem with NB.begin()
// attach to the GPRS network with the APN, login and password
while (!connected) {
if ((nbAccess.begin(PINNUMBER) == NB_READY) &&
(gprs.attachGPRS() == GPRS_READY)) {
connected = true;
} else {
Serial.println("Not connected");
delay(1000);
}
}
Serial.println("modem started && attached to GPRS network");
client.setServer(mqtt_server, port);
Serial.println("server details set");
}
void loop(){
//if the connection drops then reconnect it
if (!client.connected()) {
reconnect();
}
//This should be called regularly to allow the client to process incoming messages
//and maintain its connection to the server.
client.loop();
client.publish(topic,payload);
Serial.println("Message Published");
}
My output is as follows:
12:34:17.355 -> starting up
12:34:21.875 -> modem started && attached to GPRS network
12:34:21.875 -> server details set
12:34:21.875 -> connection attemp to broker
12:36:47.813 -> Failed, rc=-2 try again in 2 seconds
12:36:49.835 -> connection attemp to broker
12:40:11.125 -> Failed, rc=-2 try again in 2 seconds
I would be very happy if someone could help me out.
Thank you in advance.