This code behaves exactly like my application (same functions, not changing a comma). I just removed the uplink payload because it’s irrelevant to the analysis.
With all inputs open, sleep consumption is ~5uA. By placing the D1 input to GND, the consumption jumps to exactly ~88uA.
I didn’t use api.system.sleep.setup(RUI_WAKEUP_FALLING_EDGE, D1_PIN); because I could only wake up the microcontroller for a single input (I couldn’t count pulses on several inputs simultaneously, during sleep mode).
Code:
#define OTAA_PERIOD (60000)
#define OTAA_BAND (RAK_REGION_AU915)
#define OTAA_DEVEUI {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x88}
#define OTAA_APPEUI {0x6a, 0x22, 0x2b, 0x60, 0x3f, 0x2e, 0xd0, 0xba}
#define OTAA_APPKEY {0x6a, 0x22, 0x2b, 0x60, 0x3f, 0x2e, 0xd0, 0xba, 0x43, 0x3e, 0xfb, 0x10, 0x05, 0xb4, 0x4b, 0x1c}
#define D1_PIN PA4
#define D2_PIN PA5
#define D3_PIN PA6
#define Tamper_PIN PA7
#define Bateria_PIN PB3
/** Packet buffer for sending */
uint8_t collected_data[64] = { 0 };
volatile uint32_t pulsosD1;
volatile uint32_t pulsosD2;
volatile uint32_t pulsosD3;
volatile unsigned long last_timeD1;
volatile unsigned long last_timeD2;
volatile unsigned long last_timeD3;
long deb_timeD1 = 200; // debounce time(ms) canal 1
long deb_timeD2 = 200; // debounce time(ms) canal 2
long deb_timeD3 = 200; // debounce time(ms) canal 3
static bool set_timer_sleep = true;
struct pulsosVar{
uint8_t D1[8];
uint8_t D2[8];
uint8_t D3[8];
bool tamper;
};
void recvCallback(SERVICE_LORA_RECEIVE_T * data)
{
if (data->BufferSize > 0) {
Serial.println("Something received!");
for (int i = 0; i < data->BufferSize; i++) {
Serial.printf("%x", data->Buffer[i]);
}
Serial.print("\r\n");
}
}
void joinCallback(int32_t status)
{
Serial.printf("Join status: %d\r\n", status);
}
void sendCallback(int32_t status)
{
if (status == 0) {
Serial.println("Successfully sent");
} else {
Serial.println("Sending failed");
}
}
void uplink_routine()
{
/** Payload of Uplink */
uint8_t data_len = 0;
collected_data[data_len++] = (uint8_t) 't';
collected_data[data_len++] = (uint8_t) 'e';
collected_data[data_len++] = (uint8_t) 's';
collected_data[data_len++] = (uint8_t) 't';
Serial.println("Data Packet:");
for (int i = 0; i < data_len; i++) {
Serial.printf("0x%02X ", collected_data[i]);
}
Serial.println("");
/** Send the data package */
if (api.lorawan.send(data_len, (uint8_t *) & collected_data, 2, true, 1)) {
Serial.println("Sending is requested");
} else {
Serial.println("Sending failed");
}
}
void D1_HANDLER()
{
if(millis() - last_timeD1 > deb_timeD1) // Debounce Time
{
pulsosD1++;
Serial.printf(" D1++ %i\r\n", pulsosD1);
last_timeD1 = millis();
}
}
void D2_HANDLER()
{
if(millis() - last_timeD2 > deb_timeD2)
{
pulsosD2++;
Serial.printf(" D2++ %i\r\n", pulsosD2);
last_timeD2 = millis();
}
}
void D3_HANDLER()
{
if(millis() - last_timeD3 > deb_timeD3)
{
pulsosD3++;
Serial.printf(" D3++ %i\r\n", pulsosD3);
last_timeD3 = millis();
}
}
void setup()
{
Serial.begin(115200, RAK_AT_MODE);
Serial.println("RAKwireless LoRaWan OTAA Example");
Serial.println("------------------------------------------------------");
if(api.lorawan.nwm.get() != 1)
{
Serial.printf("Set Node device work mode %s\r\n",
api.lorawan.nwm.set(1) ? "Success" : "Fail");
api.system.reboot();
}
// OTAA Device EUI MSB first
uint8_t node_device_eui[8] = OTAA_DEVEUI;
// OTAA Application EUI MSB first
uint8_t node_app_eui[8] = OTAA_APPEUI;
// OTAA Application Key MSB first
uint8_t node_app_key[16] = OTAA_APPKEY;
if (!api.lorawan.appeui.set(node_app_eui, 8)) {
Serial.printf("LoRaWan OTAA - set application EUI is incorrect! \r\n");
return;
}
if (!api.lorawan.appkey.set(node_app_key, 16)) {
Serial.printf("LoRaWan OTAA - set application key is incorrect! \r\n");
return;
}
if (!api.lorawan.deui.set(node_device_eui, 8)) {
Serial.printf("LoRaWan OTAA - set device EUI is incorrect! \r\n");
return;
}
if (!api.lorawan.band.set(OTAA_BAND)) {
Serial.printf("LoRaWan OTAA - set band is incorrect! \r\n");
return;
}
if (!api.lorawan.deviceClass.set(RAK_LORA_CLASS_A)) {
Serial.printf("LoRaWan OTAA - set device class is incorrect! \r\n");
return;
}
if (!api.lorawan.njm.set(RAK_LORA_OTAA)) // Set the network join mode to OTAA
{
Serial.printf("LoRaWan OTAA - set network join mode is incorrect! \r\n");
return;
}
if (!api.lorawan.join()) // Join to Gateway
{
Serial.printf("LoRaWan OTAA - join fail! \r\n");
return;
}
/** Wait for Join success */
while (api.lorawan.njs.get() == 0) {
Serial.print("Wait for LoRaWAN join...");
api.lorawan.join();
delay(10000);
}
if (!api.lorawan.adr.set(true)) {
Serial.printf("LoRaWan OTAA - set adaptive data rate is incorrect! \r\n");
return;
}
if (!api.lorawan.rety.set(1)) {
Serial.printf("LoRaWan OTAA - set retry times is incorrect! \r\n");
return;
}
if (!api.lorawan.cfm.set(1)) {
Serial.printf("LoRaWan OTAA - set confirm mode is incorrect! \r\n");
return;
}
/** Check LoRaWan Status*/
Serial.printf("Duty cycle is %s\r\n", api.lorawan.dcs.get()? "ON" : "OFF"); // Check Duty Cycle status
Serial.printf("Packet is %s\r\n", api.lorawan.cfm.get()? "CONFIRMED" : "UNCONFIRMED"); // Check Confirm status
uint8_t assigned_dev_addr[4] = { 0 };
api.lorawan.daddr.get(assigned_dev_addr, 4);
Serial.printf("Device Address is %02X%02X%02X%02X\r\n", assigned_dev_addr[0], assigned_dev_addr[1], assigned_dev_addr[2], assigned_dev_addr[3]); // Check Device Address
Serial.printf("Uplink period is %ums\r\n", OTAA_PERIOD);
Serial.println("");
api.lorawan.registerRecvCallback(recvCallback);
api.lorawan.registerJoinCallback(joinCallback);
api.lorawan.registerSendCallback(sendCallback);
// initialize interrupt pins D1 D2 D3 as an input with the internal pull-up resistor enabled
pinMode(D1_PIN, INPUT); //INPUT_PULLUP
pinMode(D2_PIN, INPUT); //INPUT_PULLUP
pinMode(D3_PIN, INPUT); //INPUT_PULLUP
pinMode(Tamper_PIN, INPUT); //INPUT_PULLUP
// trigger interrupt execute handlers when the pin 1 goes from high to low
attachInterrupt(D1_PIN, D1_HANDLER, FALLING);
attachInterrupt(D2_PIN, D2_HANDLER, FALLING);
attachInterrupt(D3_PIN, D3_HANDLER, FALLING);
for(int i=0;i<5;i++) {
delay(3000);
uplink_routine();
}
}
void loop() {
static uint64_t last = 0;
static uint64_t elapsed;
if ((elapsed = millis() - last) > (OTAA_PERIOD)) {
uplink_routine();
set_timer_sleep = true;
last = millis();
}
if(set_timer_sleep) {
delay(20);
api.system.sleep.all(OTAA_PERIOD);
set_timer_sleep = false;
}
else {
api.system.sleep.all();
}
}