Stopping MKR NB 1500 if no network available

Hi everybody!

I am working with MKR NB 1500 board for a remote project where we perform environmental measurements. Sometimes the NB network is not available and the board keeps trying to connect discharging the batteries, and I need to avoid this condition. I have tried the code below, a minor modification of the standard code available online, but it does not work. Do you have any suggestion to solve this issue?
Thanks a lot in advance!

bool connected = false;
 int attempt = 0;
 while (!connected && (attempt < 2)) {
    if ((nbAccess.begin(PINNUMBER) == NB_READY) &&
        (gprs.attachGPRS() == GPRS_READY)) {
      connected = true;
    } else {
      Serial.println("Not connected");
      delay(1000);
    }
    attempt++;
  }

What does "doesn't work" mean?
How did you checked the code?

Hi! Thanks for the quick reply, appreciated!

We have installed one of those IoT station at the borders of the network coverage. The company that is supplying the SIM card kindly provides maps of the signal coverage. There is not signal also on our 4-5G phones when we go there with snowmobiles.

If I run the overall code with the extracted code above embedded, the IoT station does not run the remaining part of the code, sends data to Thingspeak, store data locally, and goes to in extremely low power mode. I can easily see that all the led are on. If I remove that part of the code, the IoT station performs measurements, and goes in extremely low power mode, but of course it does not send data over the IoT newtork, data are only stored locally.

I deduce that there is something wrong with the code above, like it still waits to be connected to the network but it never happens even if I am trying to break the while loop. Not sure if I wrote correctly the double conditions in the while loop.

What do you think?

The code above just trying to connect twice and then continue to run the rest of the code, whether the connections succeed or fail. Is it what you expected?

I couldn't say more with this snippet. There are no use of low power mode or transferring the data to the net in it.

Hi! Thanks again for the quick reply and sorry if my reply come a little bit late. I just wanted to test it again in an area without connection. I attach a simplified version of the entire code below. It is the core part that include NB IoT connection, SD, ThingSpeak, and low power mode.

What I´m trying to achieve is as follows: if there is no connection, after three attempts to connect do not try to connect again but perform the code below until going in low power mode with a TPL5110.

Nevertheless, if I am in an area without connection, or very weak connection, the code gets somehow stuck in the while loop. In area with good connection, it is just working fine.

Do you have any suggestion to solve this issue?
Thanks a lot in advance!


#include <MKRNB.h>
#include "ThingSpeak.h"
#include "arduino_secrets.h"
#include <DHT.h>
#include <Wire.h>
#include <RTClib.h>
#include <SD.h>
#include <SPI.h>

RTC_DS3231 rtc;
char t[32];
const int SDchipSelect = 4;
String logFile = "datalog.txt";
int Hours, Minutes, Seconds, Day, Month, Year;

#define DHTTYPE DHT22
#define DHTPIN 1
DHT dht(DHTPIN, DHTTYPE);
float hum;
float temp;

//TPL5110
int donePin = 3;

// Sensitive data
const char PINNUMBER[]     = SECRET_PINNUMBER;
const char GPRS_APN[]      = SECRET_GPRS_APN;
const char GPRS_LOGIN[]    = SECRET_GPRS_LOGIN;
const char GPRS_PASSWORD[] = SECRET_GPRS_PASSWORD;
unsigned long myChannelNumber = SECRET_CHANNEL_NUMBER;
const char * myWriteAPIKey = SECRET_API;

// NB IoT
NBClient client;
GPRS gprs;
NB nbAccess (true);

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(donePin, OUTPUT);
  Serial.begin(9600);
  dht.begin();
  Wire.begin();
  rtc.begin();
  delay(5000);

  if (rtc.lostPower()) {
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  }

  SD.begin(SDchipSelect);
  ThingSpeak.begin(client);  // Initialize ThingSpeak
}

void loop() {

  bool connected = false;
  int Try = 0;
  while (!connected && (Try < 2)) {
    if ((nbAccess.begin(PINNUMBER) == NB_READY) &&
        (gprs.attachGPRS() == GPRS_READY)) {
      connected = true;
      Serial.println("Connected");
    } else {
      Serial.println("Not connected");
      delay(1000);
    }
    Try++;
  }

  // Date and time
  DateTime now = rtc.now();
  Hours = now.hour();
  Minutes = now.minute();
  Seconds = now.second();
  Day = now.day();
  Month = now.month();
  Year = now.year();

 // Read sensor and battery
  hum = dht.readHumidity();
  temp = dht.readTemperature();

  int sensorvalue = analogRead(ADC_BATTERY);
  float voltage = sensorvalue * (4.3 / 1023.0);

  // SD card
  File dataFile = SD.open(logFile, FILE_WRITE);
  dataFile.print(Day);
  dataFile.print (" ");
  dataFile.print(Month);
  dataFile.print (" ");
  dataFile.print(Year);
  dataFile.print (" ");
  dataFile.print(Hours);
  dataFile.print (":");
  dataFile.print(Minutes);
  dataFile.print (":");
  dataFile.print(Seconds);
  dataFile.print (",");
  dataFile.print(hum);
  dataFile.print(",");
  dataFile.print(temp);
  dataFile.print(",");
  dataFile.println(voltage);

  dataFile.close();

  ThingSpeak.setField(1, hum);
  ThingSpeak.setField(2, temp);
  ThingSpeak.setField(3, voltage);
  int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
  client.stop();

  digitalWrite(donePin, LOW);
  digitalWrite(donePin, HIGH);

}

In contrary to this, your actual code is different - after three attempts to connect it going to the reading the sensors and write the values to the database. After performing this once, the code returns to it's attempts to connect to the net.
There is no work with low power mode in the code.

Hi! Thanks again for the quick reply! The circuit has a TPL5110 Low Power Timer Breakout, I wrote it above, sorry if it was confusing and you were expecting part of the code for the low power mode rather than cutting the power, I apologize. The last two lines of the code are for cutting the power.

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