How to make receive function return value #119
-
Hello, |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 2 replies
-
Your code makes no sense. Why would you call RadioRX(30) in a loop? RadioRX is none blocking, it will return immediately. (1) setup the callbacks for RX and TX ==> // Initialize the Radio callbacks Untested code snippet for RX only: // Define LoRa parameters
#define RF_FREQUENCY 868000000 // Hz
#define TX_OUTPUT_POWER 22 // dBm
#define LORA_BANDWIDTH 0 // [0: 125 kHz, 1: 250 kHz, 2: 500 kHz, 3: Reserved]
#define LORA_SPREADING_FACTOR 7 // [SF7..SF12]
#define LORA_CODINGRATE 1 // [1: 4/5, 2: 4/6, 3: 4/7, 4: 4/8]
#define LORA_PREAMBLE_LENGTH 8 // Same for Tx and Rx
#define LORA_SYMBOL_TIMEOUT 0 // Symbols
#define LORA_FIX_LENGTH_PAYLOAD_ON false
#define LORA_IQ_INVERSION_ON false
static RadioEvents_t RadioEvents;
bool has_data = false;
uint8_t received_data[256];
uint16_t received_data_size = 0;
void OnRxDone(uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr)
{
Serial.println("OnRxDone");
// Save received data
memcpy(received_data, payload, size);
received_data_size = size;
// Set flag
has_data = true;
}
void setup(void)
{
// Initialize Serial for debug output
Serial.begin(115200);
// Initialize the Radio callbacks (only RX here)
RadioEvents.RxDone = OnRxDone;
// Initialize the Radio
Radio.Init(&RadioEvents);
// Set Radio channel
Radio.SetChannel(RF_FREQUENCY);
// Set Radio TX configuration
Radio.SetTxConfig(MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH,
LORA_SPREADING_FACTOR, LORA_CODINGRATE,
LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON,
true, 0, 0, LORA_IQ_INVERSION_ON, TX_TIMEOUT_VALUE);
// Set Radio RX configuration
Radio.SetRxConfig(MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR,
LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH,
LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON,
0, true, 0, 0, LORA_IQ_INVERSION_ON, true);
}
void loop(void)
{
// Set LoRa transceiver to 30 seconds RX mode
RadioRx(30);
// Get start time of wait for RX
time_t rx_start = millis();
while (!has_data)
{
if ((millis() - rx_start) > 30000)
{
Serial.println("No data received in 30 seconds");
break;
}
}
if (has_data)
{
Serial.println("Received payload:");
for (int idx = 0; idx < received_data_size; idx++)
{
Serial.printf("%02X ", received_data[idx]);
}
Serial.println("");
has_data = false;
}
} |
Beta Was this translation helpful? Give feedback.
-
First conversion from Lora.h to SX126x.h is over. In my project I have to call receiver node several times. OnRxDone callback returns call received by node.
Later device is both listening and responding with status message.
...
The code works in present form, but I'd like to understand logic of SX126x. What happens when I start Radio.Send, while radio is set to receive with Radio.Rx(1200000)? Does it interrupt Radio.Rx(some time) timer? When sending the response is over, would Radio state be back to RX o stay in TX or go to Idle? Should I interrupt radio states with RadioStandby()? Am I properly handling state transitioning? |
Beta Was this translation helpful? Give feedback.
-
Would appreciate your critique. |
Beta Was this translation helpful? Give feedback.
-
If the SX1262 is in RX mode and you call Radio.Send(), it will stop RX mode and send the packet. Then the transceiver is set to sleep. There are callbacks for both RX and TX available. You can go back to RX within the TX callback function if you wish to do so. |
Beta Was this translation helpful? Give feedback.
Your code makes no sense. Why would you call RadioRX(30) in a loop? RadioRX is none blocking, it will return immediately.
There is no OnRxDone function, it is a callback when data has been received.
(1) setup the callbacks for RX and TX ==> // Initialize the Radio callbacks
(2) call RadioRX(30); // Sets radio in RX mode for 30 seconds, goes to sleep automatically after 30 seconds
(3) wait for RX callback called (if a packet was received) Function to be executed on Radio Rx Done event
(4) Handle received data in your loop(). To do this, you can set a flag in the callback and wait in the loop for that flag
Untested code snippet for RX only: