Reduce current consumption of Lora WiFi V3 node

Hi,
I’ve just successfully made my ESP32 Lora WiFi V3 node connected to LoraWan Gateway HT-M01S (V2.0). The node wakes up every 1min to deliver dummy data and the every currrent is around 9mA. The sleep current is normal (=19uA). I notice that every wakeup cycle will occupy around 5s on time, no matter my setting of “appTxDutyCycle” (15s, 5s or 0.5s). There will also a long wakeup every 4 cycles, which will sustain for around 10seconds.
Previously, I used the same node to transmit non-LoraWan packets and the average current can be down to 3mA.
Below shows my LoraWan node code. Could you please advise any hint to improve the current consumption?
Thanks!!
#include “LoRaWan_APP.h”
#include <ArduinoJson.h> // JSON library

#define ADC_CTL 37
#define V_EXT 36
#define LED 35
#define BATT 1
#define SIZE 100

StaticJsonDocument<100> Jsondata; // Create a JSON document of 100 characters max
float temp_value,hum_value,battery_lvl;
int sun_value = 0;
byte status = 0;
char msg[SIZE];

/* OTAA para*/
uint8_t devEui[] = { 0x70, 0xB3, 0xD5, 0x7E, 0xD0, 0x05, 0xAA, 0xB6 };
uint8_t appEui[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
uint8_t appKey[] = { 0xB1, 0x2C, 0x03, 0x01, 0xEA, 0xCD, 0x3B, 0x7C, 0xF7, 0xA9, 0xB6, 0xA6, 0x8A, 0x15, 0xB9, 0xE8 };
/* ABP para*/
uint8_t nwkSKey[] = { 0xC1, 0xAE, 0x15, 0x9D, 0x98, 0x59, 0x0E, 0xD3, 0x68, 0x39, 0x51, 0x5A, 0xF1, 0x2F, 0x93, 0x81 };
uint8_t appSKey[] = { 0xC1, 0xAE, 0x15, 0x9D, 0x98, 0x59, 0x0E, 0xD3, 0x68, 0x39, 0x51, 0x5A, 0xF1, 0x2F, 0x93, 0x81 };
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 = CLASS_A; //Class A

/the application data transmission duty cycle. value in [ms]./
uint32_t appTxDutyCycle = 100;

/OTAA or ABP/
bool overTheAirActivation = true; //OTAA selected

/ADR enable/
bool loraWanAdr = true;

/* Indicates if the node is sending confirmed or unconfirmed messages */
bool isTxConfirmed = true;

/* 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 */
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”.
*/
appDataSize = strlen(msg);
for (int j = 0; j<appDataSize; j++){
appData[j] = msg[j];
}
}

//if true, next uplink will add MOTE_MAC_DEVICE_TIME_REQ

void create_data(){
temp_value = random(15,40);
hum_value = random(40,80);

battery_lvl =0;
digitalWrite(ADC_CTL,LOW); //Enable battery level resistor network
for (int i = 0; i<3; i++){
delay(50); //delay to get stable voltage
battery_lvl += float(analogRead(BATT))/40963.3/0.2041.06; //Adjustment for unit
}
battery_lvl = battery_lvl/3;
digitalWrite(ADC_CTL,HIGH); //Disable battery level resistor network

sun_value = random(30,100);
status = int(random(0, 2));

//Create JSON message
Jsondata[“Temp”] = temp_value; //temp_value read from DHT11
Jsondata[“Hum”] = hum_value; //humidity value read from DHT11
Jsondata[“Batt”] = battery_lvl; //battery_lvl read by “analogRead(xx)/4096*3.3”
Jsondata[“Sun”] = sun_value; //sunshine level ready by “analogRead(xx)/4096 * 100” in %
if (status ==0) Jsondata[“Status”] = “Discharging”;
else Jsondata[“Status”] = “Charging”;

//Packing the JSON message into msg
serializeJson(Jsondata, msg);
Serial.println(msg);
Serial.println(strlen(msg));
}

void setup() {
Serial.begin(115200);
pinMode(LED,OUTPUT);
pinMode(ADC_CTL, OUTPUT);
Mcu.begin();
deviceState = DEVICE_STATE_INIT;
create_data();
}

void loop()
{
switch( deviceState )
{
case DEVICE_STATE_INIT:
{
#if(LORAWAN_DEVEUI_AUTO)
LoRaWAN.generateDeveuiByChipID();
#endif
LoRaWAN.init(loraWanClass,loraWanRegion);
break;
}
case DEVICE_STATE_JOIN:
{
LoRaWAN.join();
break;
}
case DEVICE_STATE_SEND:
{
digitalWrite(LED, HIGH); //turn on LED
Serial.println(“Sending”);
prepareTxFrame(appPort);
LoRaWAN.send();
deviceState = DEVICE_STATE_CYCLE;
break;
}
case DEVICE_STATE_CYCLE:
{
// Schedule next packet transmission
//txDutyCycleTime = appTxDutyCycle + randr( -APP_TX_DUTYCYCLE_RND, APP_TX_DUTYCYCLE_RND );
txDutyCycleTime = 60000;
LoRaWAN.cycle(txDutyCycleTime);
digitalWrite(LED, LOW); //turn off LED
deviceState = DEVICE_STATE_SLEEP;
Serial.println(“Going to Sleep”);
break;
}
case DEVICE_STATE_SLEEP:
{
LoRaWAN.sleep(loraWanClass);
break;
}
default:
{
deviceState = DEVICE_STATE_INIT;
break;
}
}
}

It looks like your code is configured to wait for an ACK after sending a message.


/* Indicates if the node is sending confirmed or unconfirmed messages */
bool isTxConfirmed = true;

Since isTxConfirmed is true, the device will wait 5 seconds for a response.
Change the value to false and see if your 5 second delay goes away.

Sopwith

Thanks for your advice!
I’ve tried but only find that the big long pulse every 4 cycles now vanish. Every on-time is still up to 5seconds.

Could you please post your code on github or similar including the header files.
My compile wants the #include <ArduinoJson.h> // JSON library and I have downloaded a few similar named files but none seem to work

Hi smbunn,

Below is the shared link containing the code and the zip library:
LoRaWan-test
Thanks!!

Awesome, all working now.