Query the current TX spreading factor

Hi there,
I run a CubeCell AB01 in the TTN. How can the current TX spreading factor be queried in the program code?
Objective: Automatic adjustment of the duty cycle in the program in order to comply with the fair use policy in TTN.
Many thanks for your help

1 Like

Heltec: How can the current TX spreading factor be queried in the program code?

I’m not sure if this is exposed to the sketch level or if it is wrapped up in a class or namespace, but in the semtech code (LoraMAC) that underlies LoraWan_APP.h there is a function to query if a packet of a certain size can be sent - I’m assuming that the MAC would take your required duty cycle into account.

/*!
 * \brief   Queries the LoRaMAC if it is possible to send the next frame with
 *          a given payload size. The LoRaMAC takes scheduled MAC commands into
 *          account and reports, when the frame can be send or not.
 *
 * \param   [IN] size - Size of applicative payload to be send next
 *
 * \param   [OUT] txInfo - The structure \ref LoRaMacTxInfo_t contains
 *                         information about the actual maximum payload possible
 *                         ( according to the configured datarate or the next
 *                         datarate according to ADR ), and the maximum frame
 *                         size, taking the scheduled MAC commands into account.
 *
 * \retval  LoRaMacStatus_t Status of the operation. When the parameters are
 *          not valid, the function returns \ref LORAMAC_STATUS_PARAMETER_INVALID.
 *          In case of a length error caused by the applicative payload in combination
 *          with the MAC commands, the function returns \ref LORAMAC_STATUS_LENGTH_ERROR.
 *          Please note that if the size of the MAC commands which are in the queue do
 *          not fit into the payload size on the related datarate, the LoRaMAC will
 *          omit the MAC commands.
 *          In case the query is valid, and the LoRaMAC is able to send the frame,
 *          the function returns \ref LORAMAC_STATUS_OK.
 */
LoRaMacStatus_t LoRaMacQueryTxPossible( uint8_t size, LoRaMacTxInfo_t *txInfo );

If all you need to do is check if you can send a certain size packet you could perhaps simply:

if (LoRaMacQueryTxPossible(64, NULL ) == LORAMAC_STATUS_OK){
   appDataSize = 64;
   // fill appData with 64 bytes
} elseif (LoRaMacQueryTxPossible(32, NULL ) == LORAMAC_STATUS_OK){
   appDataSize = 32;
   // fill appData with 32 bytes
}

// etc

otherwise you are going to have to parse the output from LoRaMacQueryTxPossible() given in txInfo

this is already being done in the sendFrame() in LoRaWan_APP.c before the frame is sent…

although this part of the sendFrame() code looks wrong to me…the last piece of that function…

	if( LoRaMacMcpsRequest( &mcpsReq ) == LORAMAC_STATUS_OK )
	{
		return false;
	}
	return true;

i believe it should be :

if (LoRaMacMcpsRequest(&mcpsReq) == LORAMAC_STATUS_OK)
    {
        return LORAMAC_HANDLER_SUCCESS;
    }
    return LORAMAC_HANDLER_ERROR;

someone might correct me if i’m wrong…

Jay.

I’m not the original poster - but I get the feeling that the idea was to work out what the current spreading factor is, so that they can dynamically change either the payload size of what they send or the sleep time before waking up again or both.

That’s exactly what I intend to do with the request: Changing the sleep time based on the current send spreading factor.

1 Like