Connection problem between MKR NB 1500 and Arduino Cloud

Hello everyone !

I am working on a project of a monitoring station in a remote location. I am working with an Arduino Uno R3 in one room and with an MKR NB 1500 in an other room. So basicaly the Arduino R3 is sending his measured values (humidity and temperature) to the MKR NB1500 with a radio module NRF24 and then the MKR NB 1500 is sending all the values (humidity + temp) to the arduino cloud with a GSM connection. The problem is that after few days the board goes offline and I have to unplug and replug it the set the connection back and since I want to use it in remote area it is not possible.
The power supply is not the problem.
So maybe the lost connection can come from the SIM card, but I doubt that since in France we have good signals almost everywhere.
So I think it is coming either from the board or from the cloud. I have tried to setup some callback and some fonction in my code to restart or reboot the card when the connection is lost but nothing is working. I dont know what to do or how I can fix this connection issue
I hope you can help me maybe by giving me advice with my code.
Thank you all !

// MKRNB - Version: Latest 
#include <MKRNB.h>

// MKRGSM - Version: Latest 
/*#include <MKRGSM.h>*/

// Arduino_ConnectionHandler - Version: Latest 
#include <Arduino_ConnectionHandler.h>
/*#include <Arduino_GSMConnectionHandler.h>*/
#include <Arduino_NBConnectionHandler.h>

// Arduino Low Power - Version: Latest 
#include <ArduinoLowPower.h>

/* 
  Sketch generated by the Arduino IoT Cloud Thing "Untitled"
  https://create.arduino.cc/cloud/things/64cb9af9-e604-46fb-9ada-682a38aff5c2 

  Arduino IoT Cloud Variables description

  The following variables are automatically generated and updated when changes are made to the Thing

  float humidity_GSM;
  float humidity_R3;
  float temperature_GSM;
  float temperature_R3;

  Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
  which are called when their values are changed from the Dashboard.
  These functions are generated with the Thing and added at the end of this sketch.
*/

#include "thingProperties.h"

#include <SPI.h>
#include <DHT.h>
#include <RF24.h>
#include <nRF24L01.h>

// Définir le type de capteur (DHT22 ou AM2302)
#define DHT_TYPE DHT22

// Pin de connexion du capteur de température et d'humidité
#define DHT_PIN  0 // Utilisez n'importe quelle broche numérique disponible
#define CE_PIN 3
#define CSN_PIN 2

NBConnectionHandler conMan(SECRET_OPTIONAL_PIN);
NB nbAccess;

// Déclarer l'objet NBClient
NBClient nbClient;

// connection state
bool connected = false;
bool restart = false;

// Déclarer l'objet DHT
DHT dht(DHT_PIN, DHT_TYPE);

// Déclarer l'objet RF24 pour la communication sans fil
RF24 radio(3, 2,4000000); 

// Adresse du module NRF24L01 sur le réseau
const byte address[6] = "00008";

float data_R3[2];
float data_GSM[2];
bool newData = false;

unsigned long lastConnectionAttempt = 0; // Variable pour stocker le dernier moment où une tentative de connexion a été effectuée
const unsigned long connectionInterval = 60000; // Intervalle entre les tentatives de connexion en millisecondes (par exemple, 1 minute)

void setup() {
  // Initialize serial and wait for port to open:
  Serial.begin(9600);
  // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
  delay(1500); 

  // Defined in thingProperties.h
  initProperties();

  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  /*
     The following function allows you to obtain more information
     related to the state of network and IoT Cloud connection and errors
     the higher number the more granular information you’ll get.
     The default is 0 (only errors).
     Maximum is 4
 */
  setDebugMessageLevel(4);
  ArduinoCloud.printDebugInfo();
  
  // Initialisation du capteur DHT
  dht.begin();

  // Initialisation du module NRF24L01
  radio.begin();
  pinMode(0, INPUT);
  radio.setChannel(8);
  radio.setDataRate(RF24_2MBPS);
  radio.openReadingPipe(1, address);
  radio.setPALevel(RF24_PA_LOW); // Définir le niveau de puissance de transmission
  radio.startListening();
  
  ArduinoCloud.addCallback(ArduinoIoTCloudEvent::DISCONNECT, doThisOnDisconnect);
  conMan.addCallback(NetworkConnectionEvent::DISCONNECTED, doThisOnDisconnect);
}

void loop() {
  ArduinoCloud.update();
  
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  humidity_GSM = dht.readHumidity();
  // Read temperature as Celsius (the default)
  temperature_GSM = dht.readTemperature();
  
  // Check if any reads failed and exit early (to try again).
  if (isnan(humidity_GSM) || isnan(temperature_GSM)) {
    Serial.println(F("Failed to read from DHT sensor!"));
    return;
  } 
  data_GSM[0]=temperature_GSM;
  data_GSM[1]=humidity_GSM;
  Serial.print("Température GSM : ");
  Serial.print(data_GSM[0]);
  Serial.println("°C");
  Serial.print("Humidité GSM : ");
  Serial.print(data_GSM[1]);
  Serial.println("%");
  Serial.println("");
 
  getData();
  showData();
  conMan.check();
  verifyConnection();
  
  if (!connected && millis() - lastConnectionAttempt >= connectionInterval) {
    // Si le délai entre les tentatives de connexion est écoulé
    Serial.println("Attempting to reconnect to Arduino IoT Cloud...");
    lastConnectionAttempt = millis(); // Mettre à jour le dernier moment de la tentative de connexion

    // Tenter de se reconnecter à Arduino IoT Cloud
    if (ArduinoCloud.connected()) {
      Serial.println("Reconnected to Arduino IoT Cloud!");
      connected = true;
    } else {
      Serial.println("Failed to reconnect to Arduino IoT Cloud.");
      NVIC_SystemReset();
    }
  }
  delay(2000);
}

//==============

void getData() {
    if (radio.available()){
        radio.read(&data_R3, sizeof(data_R3));
        temperature_R3=data_R3[0];
        humidity_R3=data_R3[1];
        newData = true;
    }
}

void showData() {
    if (newData == true) {
        Serial.println("Data received ");
        Serial.print("Température R3 : ");
        Serial.print(data_R3[0]);
        Serial.println("°C");
        Serial.print("Humidité R3 : ");
        Serial.print(data_R3[1]);
        Serial.println("%");
        Serial.println("");
        newData = false;
    }
}

void doThisOnDisconnect() {
  Serial.println(">>>>doThisOnDisconnect connection au cloud perdu");
  // Effectuer un reset logiciel
  NVIC_SystemReset();
}

void verifyConnection() {
  if (nbAccess.isAccessAlive()) {
    connected = true;
  }
  else {
    NVIC_SystemReset();
  }
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.