0
votes

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");
  }

}
1
You are not calling client.loop() in your loop function so the MQTT client is not getting any time to process things.hardillb
Also you should edit the question to include the serial trace from the app so we can have half a chance of guessing where it's crashing.hardillb
First off, thanks for the quick response. I would like to post the serial trace, but there is none. The code uploading finishes and then I get a Windows notification stating that my device is corrupted. I have to upload a clean Arduino script to fix it again. It works without problems with any other script.dvbeelen
Looking again, you've not called your connect() function or initialised the PubSubClient so you will never get to your first Serial.println() callhardillb
Yup, that did it. Thanks for all the help.dvbeelen

1 Answers

1
votes

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.