RAK3172 Low Power Mode with timer and button: source code example

Hey!
I spent some time to find out how to make LPM with timers and keep board sleep rest of the time and this is an example:

uint8_t ledPin = PA8; // set your pin
uint8_t buttonPin = PA9; // set your pin

void setup()
{
  Serial.begin(115200);
 
  delay(5000);

  if (!api.system.lpm.set(1)) 
  {
	Serial.println("Set low power mode is incorrect! \r\n");
	return;
  }

  //LED
  pinMode(ledPin, OUTPUT);

  // BUTTON
  pinMode(buttonPin, INPUT_PULLUP);
  attachInterrupt(buttonPin, button_click, FALLING);

  if (api.system.timer.create(RAK_TIMER_0, (RAK_TIMER_HANDLER)sensor_handler, RAK_TIMER_PERIODIC) != true) 
  {
    Serial.printf("Creating timer failed.\r\n");
    return;
  }
  
  if (api.system.timer.start(RAK_TIMER_0, 1000 * 5 , NULL) != true) 
  {
    Serial.printf("Starting timer failed.\r\n");
    return;
  }

  digitalWrite(ledPin, HIGH);
  delay(2000);
  sensor_handler(NULL);
}

void button_click()
{
  sensor_handler(NULL);
}

void sensor_handler(void *)
{
    Serial.println("TICK");
    digitalWrite(ledPin, HIGH);
    delay(50);
    digitalWrite(ledPin, LOW);
}

void loop()
{
  api.system.sleep.all();
  api.system.scheduler.task.destroy();
}

Energy consumption:

I hope it will help someone.

1 Like

Can you implement with LoRaWAN together ?

Sorry, I do not use LoWAN, only Phy

@tcpipchip
Here is low power RUI3 example with LoRaWAN

If you wonder why there are only a few API calls for LoRaWAN in it, I setup my devices with AT commands or WisToolBox. That way I can use the same code on multiple devices without the need to change the AppEUI, DevEUI and AppKey every time.

I checked your code in github and it looks almost the same like mine, except one moment: after LoRa done with sending your message you need explicitly change LoRa back to Sending mode, otherwise it will keep waiting in loop for the next incoming message and will NOT go back into LPM.

I did it like this:

void send_callback(void)
{
  Serial.printf("P2P finish Rx mode %s\r\n", api.lorawan.precv(0) ? "Success" : "Fail");
}

@beegee
wIch the at command to define interval ?
for example, 10 seconds ?
AT+SENDINT=00 00 00 0a ?

simpler!

AT+SENDINT=10 sets send interval to 10 seconds.

for me i got
AT_COMMAND_NOT_FOUND

RAKwireless RUI3 Node


Setup the device with WisToolBox or AT commands before using it

RUI3 RUI_4.0.2_RAK3172-E


[SETUP] Confirmed disabled

[SETUP] Retry = 0

[SETUP] DR = 2

Current Work Mode: LoRaWAN.

Sorry, custom AT commands need ATC instead of AT

It is ATC+SENDINT=10

i will test

btw, in the example that you sent me, must i send a AT+JOIN ?

Looks that the user must to send…

Once device is setup, you can use AT_JOIN=1:1, which will tell the device to automatically join on power-up or reset.

Hey, if you use
AT+JOIN=1:1<----
And RESET, never JOIN

But, if you use
AT+JOIN=1:0<----
And RESET, and AT+JOIN=1:0, always JOIN

test there

RAK - YouTube