Well, this is a bit complicated, cause my soluction require modification on Semtech default SX1262 chip library. But the adventage is that you can use the same method on other LoRa board with Semtech LoRa Chip(not only heltec one)
- You need to localize the esp32 lorawan library so that the change wont affect other sensor or produce error when library update. To do this, just copy the whole library into your working directory and include it in the .ino. For local library, you should change from #include “ESP32_LoRaWAN.h” to #include “src/ESP32_LoRaWAN/ESP32_LoRaWAN.h”
- in LoRaMac.c, create you own function like this:
int gettxpower(void){
return LoRaMacParams.ChannelsTxPower;
}
void settxpower(int txpower){
LoRaMacParams.ChannelsTxPower=txpower;
}
-
add the function to ESP32_LoRaWAN.h and LoRaMac.h like this:
extern int gettxpower(void);
extern void settxpower(int txpower);
-
call the function in your main problem
The problem of this method is that the tx power is reset everytime, you must call it and change the tx power everytime before you send.
Another method is passsing a TX power constant into sx1276.c (at my time it was sx1276, but it was the same for newer chip)
- In SX1276-board.c, you will find a function called SX1276SetTxConfig, for SX1262, it should located at src/driver/sx126x.c with function name SX126xSetTxParams. You are able to control all Tx parameters by modify this function. (Tx power called SX1276SetRfTxPower / )
The last method is a bit risky, which involve writing to register, but the benfit is easy to use and manage.
void setFrequency(long frequency)
{
uint64_t frf = ((uint64_t)frequency << 19) / 32000000;
writeRegister(REG_FRF_MSB, (uint8_t)(frf >> 16));
writeRegister(REG_FRF_MID, (uint8_t)(frf >> 8));
writeRegister(REG_FRF_LSB, (uint8_t)(frf >> 0));
}
void setTxPower(int8_t power)
{
uint8_t paConfig = 0;
paConfig = readRegister( REG_PA_CONFIG );
paConfig = ( paConfig & RF_PACONFIG_PASELECT_MASK );
paConfig = ( paConfig & RF_PACONFIG_MAX_POWER_MASK ) | 0x70;
if ( ( paConfig & RF_PACONFIG_PASELECT_PABOOST ) == RF_PACONFIG_PASELECT_PABOOST )
{
if ( power < 2 )
{
power = 2;
}
if ( power > 17 )
{
power = 17;
}
paConfig = ( paConfig & RF_PACONFIG_OUTPUTPOWER_MASK ) | ( uint8_t )( ( uint16_t )( power - 2 ) & 0x0F );
writeRegister( REG_PA_CONFIG, paConfig );
}
}