Example of downlink to modify tx interval?

Hi everyone, just wondering if there are any examples of code to send a downlink to modify the sleep time or LORAWAN_APP_INTERVAL of a wisblock module? I have successfully set up a 4-20mA pressure transducer with deep sleep, and would like the ability to modify the interval it sends an uplink…thanks in advance!

Hi Andy,

There is no ready to use example that would do that. But it is not that difficult to do it.

NOT TESTED, JUST OUT OF MY HEAD:

Step 1: Change LORAWAN_APP_INTERVAL from a define to a uint32_t

#define LORAWAN_APP_INTERVAL 20000

to

uint32_t LORAWAN_APP_INTERVAL = 20000;

Step 2: decode the downlink packet in void lorawan_rx_handler(lmh_app_data_t *app_data), set LORAWAN_APP_INTERVAL to the new value, set the timer to the new value and restart the timer.

void lorawan_rx_handler(lmh_app_data_t *app_data)
{
    Serial.printf("LoRa Packet received on port %d, size:%d, rssi:%d, snr:%d, data:%s\n",
          app_data->port, app_data->buffsize, app_data->rssi, app_data->snr, app_data->buffer);
    // Assuming the new time is encoded as 4 bytes. e.g. 3000000=> 0x00, 0x2D, 0xC6, 0xC0
    uint32_t new_time = app_data->buffer[0] << 24;
    new_time |= app_data->buffer[1] << 16;
    new_time |= app_data->buffer[2] << 8;
    new_time |= app_data->buffer[3];
    LORAWAN_APP_INTERVAL = new_time;
    TimerStop(&appTimer);
    TimerSetValue(&appTimer, LORAWAN_APP_INTERVAL);
    TimerStart(&appTimer);
}

Not sure what LoRaWAN Networks Server you use, but for example in Chirpstack the value needs to be encoded in Base64. 3000000 would be AC3GwA==
I use Base64 to hex to create the encoded data for Chirpstack.

As I said, above is not tested, but should work.
It might be a good idea to extend the downlink packet with some markers to make sure that the downlink is really a new interval value.

1 Like

Hi @beegee ,

I gave this a go this morning in the deep sleep example.

Had to change the SLEEP_TIME definition to LORAWAN_APP_INTERVAL which was no big deal.

But then getting the error

exit status 1
'appTimer' was not declared in this scope`


Any ideas?

I did try adding
TimerEvent_t appTimer;
up the top of lora_handler.cpp but then get this weird error which I have seen before when trying to edit code and don’t know what it means.

collect2.exe: error: ld returned 1 exit status
exit status 1
Error compiling for board WisBlock RAK4631.

Ah, different example.
In that code it is different.

In file main.h change

#define SLEEP_TIME 2 * 60 * 1000

to

uint32_t SLEEP_TIME = 2*60*1000;

In file lora_handler.cpp put the decoding into the function static void lorawan_rx_handler(lmh_app_data_t *app_data)

    case LORAWAN_APP_PORT:
      // Copy the data into loop data buffer
      memcpy(rcvdLoRaData, app_data->buffer, app_data->buffsize);
      rcvdDataLen = app_data->buffsize;
      eventType = 0;

      // Assuming the new time is encoded as 4 bytes. e.g. 3000000=> 0x00, 0x2D, 0xC6, 0xC0
      uint32_t new_time = app_data->buffer[0] << 24;
      new_time |= app_data->buffer[1] << 16;
      new_time |= app_data->buffer[2] << 8;
      new_time |= app_data->buffer[3];
      SLEEP_TIME = new_time;

      taskWakeupTimer.stop();
      taskWakeupTimer.setPeriod(SLEEP_TIME);
      taskWakeupTimer.start();

      // Notify task about the event
      if (taskEvent != NULL)
      {
#ifndef MAX_SAVE
        Serial.println("Waking up loop task");
#endif
        xSemaphoreGive(taskEvent);
      }

Thanks,

just changed those two things and get a compile error

multiple definition of SLEEP_TIME’; sketch\lora_handler.cpp.o:sketch/main.h:20: first defined here`

and then

collect2.exe: error: ld returned 1 exit status
exit status 1
Error compiling for board WisBlock RAK4631.

In main.h change

uint32_t SLEEP_TIME = 2*60*1000;

to

extern uint32_t SLEEP_TIME;

and then in lora_handler.cpp add

uint32_t SLEEP_TIME = 2*60*1000;

below the #include "main.h"

Great, thanks that is working once i worked out the port is 2

Perfect, thanks for the assistance @beegee!!

Hi, @beegee, I just came across another issue with my code, the 5801 isn’t going to sleep. The sensor itself seems to, but is there a way to shut the interface down? Current during sleep is in the order of 2mA, I would assume I should be able to get it down to 20 or so uA? When I physically disconnect it during operation, that’s about where it sits whilst in deep sleep.
Is there anything else I may need to do to reduce power consumption whilst in Deep Sleep?
This may be worthy of a new topic, which I’m happy to do if deemed appropriate.

This may be the fix…hadn’t started/stopped the 5801.
Is there anything else I may need to consider? Now sitting on around 22uA.

image

If you are at 22uA sleep current that is the best you can get. I never got my WisBlocks lower when using Arduino BSP which runs FreeRTOS in the background.

Excellent, thanks @beegee, hopefully my siblings christmas presents can now be completed!! Your help is as always much appreciated.
Andrew.