Deepsleep only every second time

I am using the HTCC-AB01 (v2) board.

I’m reading DS18B20 values every 3 minutes within the standard arduino LoRa example. Everything works fine but deepsleep current varies every second time (3 µA vs 35 µA). It should be the same every cycle, right?! I am running on battery (V_bat) and the sensor is connected to V_ext.

PowerProfiler readings for some cycles:

This is the code:

#include "LoRaWan_APP.h"

#include "Arduino.h"

#include <Wire.h>

#include <OneWire.h>

/*

 * set LoraWan_RGB to Active,the RGB active in loraWan

 * RGB red means sending;

 * RGB purple means joined done;

 * RGB blue means RxWindow1;

 * RGB yellow means RxWindow2;

 * RGB green means received done;

 */

/* OTAA para*/

uint8_t devEui[] = { XXX };

uint8_t appEui[] = { XXX };

uint8_t appKey[] = { XXX };

/* ABP para*/

uint8_t nwkSKey[] = { 0x15, 0xb1, 0xd0, 0xef, 0xa4, 0x63, 0xdf, 0xbe, 0x3d, 0x11, 0x18, 0x1e, 0x1e, 0xc7, 0xda,0x85 };

uint8_t appSKey[] = { 0xd7, 0x2c, 0x78, 0x75, 0x8c, 0xdc, 0xca, 0xbf, 0x55, 0xee, 0x4a, 0x77, 0x8d, 0x16, 0xef,0x67 };

uint32_t devAddr =  ( uint32_t )0x007e6ae1;

/*LoraWan channelsmask, default channels 0-7*/

uint16_t userChannelsMask[6]={ 0x00FF,0x0000,0x0000,0x0000,0x0000,0x0000 };

/*LoraWan region, select in arduino IDE tools*/

LoRaMacRegion_t loraWanRegion = ACTIVE_REGION;

/*LoraWan Class, Class A and Class C are supported*/

DeviceClass_t  loraWanClass = LORAWAN_CLASS;

/*the application data transmission duty cycle.  value in [ms].*/

uint32_t appTxDutyCycle = 60000*3;

/*OTAA or ABP*/

bool overTheAirActivation = LORAWAN_NETMODE;

/*ADR enable*/

bool loraWanAdr = LORAWAN_ADR;

/* set LORAWAN_Net_Reserve ON, the node could save the network info to flash, when node reset not need to join again */

bool keepNet = LORAWAN_NET_RESERVE;

/* Indicates if the node is sending confirmed or unconfirmed messages */

bool isTxConfirmed = LORAWAN_UPLINKMODE;

/* Application port */

uint8_t appPort = 2;

/*!

* Number of trials to transmit the frame, if the LoRaMAC layer did not

* receive an acknowledgment. The MAC performs a datarate adaptation,

* according to the LoRaWAN Specification V1.0.2, chapter 18.4, according

* to the following table:

*

* Transmission nb | Data Rate

* ----------------|-----------

* 1 (first)       | DR

* 2               | DR

* 3               | max(DR-1,0)

* 4               | max(DR-1,0)

* 5               | max(DR-2,0)

* 6               | max(DR-2,0)

* 7               | max(DR-3,0)

* 8               | max(DR-3,0)

*

* Note, that if NbTrials is set to 1 or 2, the MAC will not decrease

* the datarate, in case the LoRaMAC layer did not receive an acknowledgment

*/

uint8_t confirmedNbTrials = 4;

/* Prepares the payload of the frame */

OneWire  ds(GPIO1);  // on pin GPIO1 PIN 6 (a 4.7K resistor is necessary)

static void prepareTxFrame( uint8_t port )

{

  /*appData size is LORAWAN_APP_DATA_MAX_SIZE which is defined in "commissioning.h".

  *appDataSize max value is LORAWAN_APP_DATA_MAX_SIZE.

  *if enabled AT, don't modify LORAWAN_APP_DATA_MAX_SIZE, it may cause system hanging or failure.

  *if disabled AT, LORAWAN_APP_DATA_MAX_SIZE can be modified, the max value is reference to lorawan region and SF.

  *for example, if use REGION_CN470,

  *the max value for different DR can be found in MaxPayloadOfDatarateCN470 refer to DataratesCN470 and BandwidthsCN470 in "RegionCN470.h".

  */

  pinMode(Vext, OUTPUT);

  digitalWrite(Vext, LOW);

 

 byte i;

  byte present = 0;

  byte type_s;

  byte data[9];

  byte addr[8];

  float celsius, fahrenheit;

 

  if ( !ds.search(addr)) {

    Serial.println("No more addresses.");

    Serial.println();

    ds.reset_search();

    delay(250);

    return;

  }

 

  Serial.print("ROM =");

  for( i = 0; i < 8; i++) {

    Serial.write(' ');

    Serial.print(addr[i], HEX);

  }

  if (OneWire::crc8(addr, 7) != addr[7]) {

      Serial.println("CRC is not valid!");

      return;

  }

  Serial.println();

 

  // the first ROM byte indicates which chip

  switch (addr[0]) {

    case 0x10:

      Serial.println("  Chip = DS18S20");  // or old DS1820

      type_s = 1;

      break;

    case 0x28:

      Serial.println("  Chip = DS18B20");

      type_s = 0;

      break;

    case 0x22:

      Serial.println("  Chip = DS1822");

      type_s = 0;

      break;

    default:

      Serial.println("Device is not a DS18x20 family device.");

      return;

  }

  ds.reset();

  ds.select(addr);

  ds.write(0x44, 1);        // start conversion, with parasite power on at the end

 

  delay(1000);     // maybe 750ms is enough, maybe not

  // we might do a ds.depower() here, but the reset will take care of it.

 

  present = ds.reset();

  ds.select(addr);    

  ds.write(0xBE);         // Read Scratchpad

  Serial.print("  Data = ");

  Serial.print(present, HEX);

  Serial.print(" ");

  for ( i = 0; i < 9; i++) {           // we need 9 bytes

    data[i] = ds.read();

    Serial.print(data[i], HEX);

    Serial.print(" ");

  }

  Serial.print(" CRC=");

  Serial.print(OneWire::crc8(data, 8), HEX);

  Serial.println();

  // Convert the data to actual temperature

  // because the result is a 16 bit signed integer, it should

  // be stored to an "int16_t" type, which is always 16 bits

  // even when compiled on a 32 bit processor.

  int16_t raw = (data[1] << 8) | data[0];

  if (type_s) {

    raw = raw << 3; // 9 bit resolution default

    if (data[7] == 0x10) {

      // "count remain" gives full 12 bit resolution

      raw = (raw & 0xFFF0) + 12 - data[6];

    }

  } else {

    byte cfg = (data[4] & 0x60);

    // at lower res, the low bits are undefined, so let's zero them

    if (cfg == 0x00) raw = raw & ~7;  // 9 bit resolution, 93.75 ms

    else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms

    else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms

    //// default is 12 bit resolution, 750 ms conversion time

  }

  celsius = (float)raw / 16.0;

  Serial.print("  Temperature = ");

  Serial.print(celsius);

  Serial.print(" Celsius, ");

 

  digitalWrite(Vext, HIGH);

  uint16_t batteryVoltage = getBatteryVoltage();

 

  unsigned char *puc;

  puc = (unsigned char *)(&celsius);

  appDataSize = 6;

  appData[0] = puc[0];

  appData[1] = puc[1];

  appData[2] = puc[2];

  appData[3] = puc[3];

 

  appData[4] = (uint8_t)(batteryVoltage>>8);

  appData[5] = (uint8_t)batteryVoltage;

 

  Serial.print("Temperature: ");

  Serial.print(celsius);

  Serial.print(" C, ");

  Serial.print("BatteryVoltage:");

  Serial.println(batteryVoltage);

}

void setup() {

  Serial.begin(115200);

#if(AT_SUPPORT)

  enableAt();

#endif

  deviceState = DEVICE_STATE_INIT;

  LoRaWAN.ifskipjoin();

}

void loop()

{

  switch( deviceState )

  {

    case DEVICE_STATE_INIT:

    {

#if(LORAWAN_DEVEUI_AUTO)

      LoRaWAN.generateDeveuiByChipID();

#endif

#if(AT_SUPPORT)

      getDevParam();

#endif

      printDevParam();

      LoRaWAN.init(loraWanClass,loraWanRegion);

      deviceState = DEVICE_STATE_JOIN;

      break;

    }

    case DEVICE_STATE_JOIN:

    {

      LoRaWAN.join();

      break;

    }

    case DEVICE_STATE_SEND:

    {

      prepareTxFrame( appPort );

      LoRaWAN.send();

      deviceState = DEVICE_STATE_CYCLE;

      break;

    }

    case DEVICE_STATE_CYCLE:

    {

      // Schedule next packet transmission

      txDutyCycleTime = appTxDutyCycle + randr( 0, APP_TX_DUTYCYCLE_RND );

      LoRaWAN.cycle(txDutyCycleTime);

      deviceState = DEVICE_STATE_SLEEP;

      break;

    }

    case DEVICE_STATE_SLEEP:

    {

      LoRaWAN.sleep();

      break;

    }

    default:

    {

      deviceState = DEVICE_STATE_INIT;

      break;

    }

  }

}

Is there any suggestion, why this happens? Data is sent corretly every time.

Thank you all!

UPDATE 2023-06-08:

Ds.search() was called, so at the first run, the first sensor was found. There was no reset after sending the data, so at the second time, no more addresses were found.