Arduino MKR1500 一段时间后无明显原因停止工作。 GSM问题

问题描述 投票:0回答:1

我正在开发跟踪系统,通过 MQTT 将传感器数据发送给经纪人,以创建仪表板和家庭自动化。为了进行原型设计,我使用 Arduino 的 MKR1400 amd MKR1500 和 T-mobile GSM IoT SIM 卡。 在实验室条件下,一切都可以完美运行几天,但在现实生活条件下,我遇到了很多问题。 arduino安装在电动游船上的甲板下和钢箱中,因此连接非常糟糕,我想知道这是否可能是问题所在。 所以我的问题是我的代码中是否有一些东西可以解释为什么arduino有时会在几个小时后停止工作,有时会继续工作几天?或者社区是否认为 GPRS 连接很差可能是所有问题的原因。 我当然会把它们搬到更好的位置,但同时我很好奇。

在我的代码中,我添加了 GSM 重新连接功能(每个循环运行)和 MQTT 重新连接功能(每 60 秒运行一次)以及每 2 分钟触发一次的看门狗以提高可靠性,但它并没有阻止问题


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




mqtt gsm gprs
1个回答
0
投票

信号不佳肯定会影响您的电池使用和连接。您是否在野外而不是实验室检查过您的 RSSI?设备的位置也可能受到其相对于水线上方或下方位置的影响,这是由您的传输频率如何传播/从水面反射引起的,波涛汹涌的大海为其添加了另一个元素。

我有兴趣听到更多信息,因为我正在使用相同的板开发远程应用程序,除了不同的行业,并且也遇到了很多这些问题。

© www.soinside.com 2019 - 2024. All rights reserved.