dvbeelen
dvbeelen

Reputation: 117

Arduino MKRGSM 1400 crashes when connecting to MQTT

I have a question concerning the Arduino MKRGSM 1400 and MQTT.

I use the code below to connect my MKRGSM to the internet via a SIM-card, then connect it to a HiveMQ-broker I have installed on Docker. Even though the code is compiled without any errors, once I upload it to my board, it crashes. Once it has crashed, I have to reset my board entirely. I've tried this code with the Arduino IDE and Platform.io on VS Code, both give the same result.

Before I put in the MQTT-connection, the board connected successfully to the internet and the DHT11-sensor was able to read humidity and temperature values without problems.

I'm not great with Arduino and this is the first time I'm trying to use MQTT myself.

Does anyone know why the code not only doesn't work, but also makes my board crash? Thanks in advance!

//Includes
#include <PubSubClient.h>
#include <MKRGSM.h>
#include "DHT.h"
#include <Adafruit_Sensor.h>

//Var declaration
//SIM-internet connection
GSMClient net;
GPRS gprs;
GSM gsmAccess;

const char pin[]      = "my pin";
const char apn[]      = "my apn";
const char login[]    = "my login";
const char password[] = "my password";

//MQTT connection
PubSubClient client;
const String serialNumber = "1";
const String mqtt_server = "server_ip";
const String topic = "/prototype/" + serialNumber;

//DHT sensor PIN declaration
#define DHTPIN 2  //DHT is pinned on 2
#define DHTTYPE DHT11 
DHT dht(DHTPIN, DHTTYPE);

void connect() {
//SIM not connected
    bool connected = false;
    Serial.print("Connecting to cellular network.");

//SIM connecting
    while (!connected) {
    if ((gsmAccess.begin(pin) == GSM_READY) &&
        (gprs.attachGPRS(apn, login, password) == GPRS_READY)) {
        //SIM connected
        connected = true;
        Serial.print("Connected to cellular network.");
    }
    else {
        //If SIM doesn't connect
        Serial.print(".");
        delay(1000);
        }
    }
}

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

    //Connect to Docker MQTT
    client.setServer(mqtt_server.c_str(), 8086);
    client.connect(serialNumber.c_str());
    Serial.print("MQTT connection state: ");
    Serial.println(client.state());

    //Start DHT 11
    dht.begin();
}

void loop() {
    delay(10000);

    //Get DHT values
    float humidty = dht.readHumidity();
    float temperature = dht.readTemperature();

    //Create JSON out of values and send it.
    const String json = "{\"temperature\": " + String(temperature, 2) + ", \"humidity\": " + String(humidty) + " }";
    Serial.println(json);
    client.publish(topic.c_str(), json.c_str());

    //Check if MQTT connection is holding.
    Serial.print("MQTT connection state: ");
    Serial.println(client.state());

    //Reconnect if MQTT connection is lost.
    if (!client.connected()) {
    Serial.println("MQTT disconnected! Trying reconnect.");
    client.connect("whatever");
  }

}

Upvotes: 0

Views: 484

Answers (1)

hardillb
hardillb

Reputation: 59751

As hashed out in the comments

You never called the connect() function so the GSM network was never setup.

You then probably need to use the GSMClient to initialise the PubSubClient so it knows how to access the network.

Upvotes: 1

Related Questions