Arduino MKR1500 stops working after a while without apparent reason. GSM issue

111 Views Asked by At

I am developing tracking systems sending sensor data via MQTT to a broker to create dashboards and home automation. To prototype I am using Arduino's MKR1400 amd MKR1500 with T-mobile GSM IoT SIM cards. In lab condition everything works perfectly for days but in real life condition I have many issues. The arduino's are installed in electric tour boats under deck and in a steel box so the connection is really bad and I am wondering if this might be the issue. So my questions is is there something in my code that could explain why the arduino's sometimes stop working after a couple of hours and sometimes keep going for a few days? Or does the community thinks that very poor GPRS connection could be the reason for all issues. I will of course move them to a better location but in the meantime I am curious.

In my code I have added GSM reconnect functions (running every loop) and MQTT reconnect function (running every 60 seconds) as well as a watchdog triggering every 2 minutes to increase reliability but it has not prevented issues


#include <MKRNB.h>
#include <Arduino_MKRGPS.h>
#include <ArduinoJson.h>
#include "Secrets.h"
#include <PubSubClient.h>
#include <WDTZero.h>




WDTZero MyWatchDoggy; // Define WDT  



// messages for serial monitor response
String oktext = "OK";
String errortext = "ERROR";

//set-up a delay using millis()
unsigned long previousMillis = 0;   // Stores last time loop ran
const long interval = 15000;        // Interval at which to publish sensor readings in milliseconds
unsigned long lastReconnectTime =0; // for checking the MQTT connection
unsigned long lastGPSCheck =0; // for checking the GPS connection



//create the JSON Document
StaticJsonDocument<300> doc;

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

//  MQTT Credentials
const char* mqtt_server = MQTT_Server;

PubSubClient mqttClient(mkrClient);

void setup() {

  // initialize serial communications and wait for port to open:
  Serial.begin(9600);


  if (!GPS.begin(GPS_MODE_SHIELD)) {
    Serial.println("Failed to initialize GPS!");
    while (1);
  }

  //initialize MQTT server
  mqttClient.setServer(mqtt_server, 1883);  

  //Enable the watchdog 
  MyWatchDoggy.setup(WDT_SOFTCYCLE2M);  // initialize WDT-softcounter refesh cycle on 32sec interval                                
}

void loop() {

  //check GSM connection
  if (nbAccess.status() != NB_READY || gprsAccess.status() != GPRS_READY){
   connectGSM();
  }

  //mqttClient.loop();

  // check MQTT connection every 60 seconds
  unsigned long currentReconnectTime = millis();
  if (currentReconnectTime - lastReconnectTime >= 60000) {
    lastReconnectTime = currentReconnectTime;
    
    if (!mqttClient.connected()) {
      reconnect();
    }
  }

 //HERE I READ ALL MY SENSORS (GPS, BATTERY VOLTAGE, CURRENT ETC...)

  //prepare and send data periodically

  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;  

    MyWatchDoggy.clear();  //clear the watchdog timer

//HERE IS THE CODE PREPARING THE DATA FROM THE SENSOR, THIS PART WORKS FINE

   //publish to broker
    char jsonString[300];
    serializeJson(doc, jsonString, sizeof(jsonString));
    Serial.println(jsonString);
    mqttClient.publish(MQTT_TOPIC1 , jsonString);
  }
}

void reconnect() {
  // Loop until we're reconnected  
  while (!mqttClient.connected()) {
    Serial.print("Attempting MQTT connection...");
    if (mqttClient.connect(CLIENT_NAME,CLIENT_LOGIN,CLIENT_PASS)) { 
      Serial.println("connected");
    } else {
      Serial.print("failed, rc=");
      Serial.print(mqttClient.state());
      Serial.println(" retrying");
      // Wait 5 seconds before retrying
     delay(5000);
    }
  }
}


void connectGSM() {

  // start module
  // if your SIM has PIN, pass it as a parameter of begin() in quotes
  Serial.println("Connecting NB IoT / LTE Cat M1 network...");
  while (nbAccess.begin(SECRET_PINNUMBER) != NB_READY) {
    Serial.println(errortext);
  }
  Serial.println(oktext);

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

    Serial.println(oktext);
  }
}




1

There are 1 best solutions below

0
nksky On

Definitely poor signal will impact your battery usage and also connectivity. Have you checked your RSSI while out in the field as opposed to the lab. The location of the units can also be hampered by their position in relation their position above or below the waterline, this is caused by how your transmission frequencies travel/bounce off water, rough seas add aanother element to it.

I'd be interested to hear more, as I am working on a remote application using the same boards, except for a different industry, and have faced a lot of these issues too.