This is driving me crazy … It worked,and I (foolishly) made a bunch of changes without using a Source Control Management System … so I know that there’s something I’m doing wrong, but for the life of me I cannot figure it out!!
It should be really easy, right?
I have a process which is llistening on the LoRa P2P network, when it receives a message it does some tinkering with it, and then sends it out on a different frequency: what could possibly go wrong? … well, I ask you that because I’ve been hitting my head up against a brick wall for approximately 48 hrs now and I’m starting to get a headache.
Here is some code:
#include <Arduino.h>
#include <Wire.h>
#include <SX126x-RAK4630.h>
#define LORA_SPREADING_FACTOR 7 // [SF7..SF12]
#define LORA_BANDWIDTH 0 // [0: 125 kHz, 1: 250 kHz, 2: 500 kHz, 3: Reserved]
/**
* 7.8Khz, 10.4Khz, 15.6Khz, 20.8Khz, 31.25Khz, 41.7Khz, 62.5Khz, 125Khz, 250Khz, 500Khz.
* A low bandwidth signal, such as 7.8Khz will give the best (longest distance) performance
*/
#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
#define TX_OUTPUT_POWER 22
#define TX_TIMEOUT_VALUE 300
#define RX_TIMEOUT_VALUE 200
#define TRANSMIT_FREQUENCY 433450000
#define RECEIVE_FREQUENCY 433600000
RadioEvents_t _radioEvents;
void EnableReceiver(void)
{
Serial.print(F("\tEnabling Receiver on Frequency "));
Serial.println(RECEIVE_FREQUENCY);
Radio.SetChannel(RECEIVE_FREQUENCY);
Radio.Rx(RX_TIMEOUT_VALUE);
}
void Channel_ActivityDetected(bool detected) { Serial.printf("Channel activity detected = %s\n", detected? "true" : "false"); }
void Tranceiver_TxCompleted(void) { Serial.println(F("Transmission completed")); EnableReceiver(); }
void Tranceiver_TxTimedOut(void) { Serial.println(F("TransmissionTimedOut")); }
void EnableTransmitter(void)
{
Serial.printf("\tEnabling Transmitter on Frequency %u\n", TRANSMIT_FREQUENCY);
Radio.SetChannel(TRANSMIT_FREQUENCY);
}
void Transmit(char* message)
{
EnableTransmitter();
Serial.printf("Transmitting message : %s\n", message);
Radio.Send((uint8_t*)message, strlen(message));
digitalToggle(LED_GREEN);
digitalToggle(LED_GREEN);
}
void Tranceiver_RxCompleted(uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr)
{
digitalToggle(LED_BLUE);
digitalToggle(LED_BLUE);
Serial.print("Receive complete ->"); Serial.println((char*)payload);
Transmit((char*)payload);
}
void Tranceiver_RxTimedOut(void) { Serial.println(F("Rx TimedOut ")); Radio.Rx(RX_TIMEOUT_VALUE); }
void Tranceiver_RxError(void) { Serial.println(F("Rx Error ")); Radio.Rx(RX_TIMEOUT_VALUE); }
void setup()
{
pinMode(LED_BLUE, OUTPUT); pinMode(LED_GREEN, OUTPUT);
digitalWrite(LED_BLUE, LOW); digitalWrite(LED_GREEN, LOW);
time_t timeout = millis();
Serial.begin(115200);
while (!Serial && ((millis() - timeout) < 5000)) { digitalToggle(LED_BLUE); }
digitalWrite(LED_BLUE, LOW);
lora_rak4630_init();
Wire.begin();
_radioEvents.CadDone = Channel_ActivityDetected;
_radioEvents.TxDone = Tranceiver_TxCompleted;
_radioEvents.TxTimeout = Tranceiver_TxTimedOut;
_radioEvents.RxDone = Tranceiver_RxCompleted;
_radioEvents.RxTimeout = Tranceiver_RxTimedOut;
_radioEvents.RxError = Tranceiver_RxError;
Radio.Init(&_radioEvents);
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);
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, false);
EnableReceiver();
}
void loop() {
delay(50);
}
and here is my platformio.ini file
[env:wiscore_rak4631]
platform = [email protected]
board = wiscore_rak4631
framework = arduino
monitor_speed = 115200
upload_speed = 115200
lib_deps =
beegee-tokyo/SX126x-Arduino@^2.0.3
In order to see it working you will need two devices, one running the above code and another one which just sits there sending messages on the RECEIVE_FREQUENCY
above.
I have one sitting around sending out a message once per second, and here is a sample of what happens:
It’s not hardware related because: the unit left busy just listening receives all expected messages, or left busy transmitting, quite happily transmits all expected messages … it’s just the switching channels and function (i.e. Rx/Tx), as you will see when the unit is powered up, it receives a message, however after switching channel and transmitting that message, the subsequent channel switch either is not happening or something screwy is going down
Please help me @beegee I want to go to bed and I can’t until I’ve got it working
UPDATE:
One experiment that I tried was to just switch the channels and not actually perform the Tx of the received data, this gives me a much nicer readout in my serial console (as you can see, it does receive the messages every second) however, there’s obviously no Tx going on form this unit -which is less than helpful