Trouble with RAK4631 Rx/Tx switching

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 :sob: :sob: :sob:

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 :rofl:

@billr
Works just fine for me after I added Radio.Standby() before

  • switching between RX and TX
  • switching frequencies

Do you work with RAK4631(L)? I guess not, because we are not selling it officially. You might have range problems if you use a RAK4631 on the 433MHz bands.

Combined your code to be RX or TX depending on build flag.
platformio.ini:

[env:Bill-1]
platform = nordicnrf52
board = wiscore_rak4631
framework = arduino
upload_port = COM33
lib_deps = 
	beegee-tokyo/SX126x-Arduino

[env:Bill-2]
platform = nordicnrf52
board = wiscore_rak4631
framework = arduino
upload_port = COM20
build_flags =
	-DTX_DEV=1
lib_deps = 
	beegee-tokyo/SX126x-Arduino

Code part:

#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
#ifdef TX_DEV
#define TRANSMIT_FREQUENCY 915900000
#define RECEIVE_FREQUENCY 915800000
#else
#define TRANSMIT_FREQUENCY 915800000
#define RECEIVE_FREQUENCY 915900000
#endif

RadioEvents_t _radioEvents;

void EnableReceiver(void)
{
	Serial.print(F("\tEnabling Receiver on Frequency "));
	Serial.println(RECEIVE_FREQUENCY);

	Radio.Standby();
	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.Standby();
	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.Standby();
	Radio.Rx(RX_TIMEOUT_VALUE);
}

void Tranceiver_RxError(void)
{
	Serial.println(F("Rx Error "));
	Radio.Standby();
	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);

#ifdef TX_DEV
	EnableTransmitter();
#else
	EnableReceiver();
#endif
}

void loop()
{
	delay(50);
#ifdef TX_DEV
	Transmit("Hello Listener");
	delay(1000);
#endif
}

Log from RX device:

	Enabling Receiver on Frequency 915900000
Receive complete ->Hello Listener
	Enabling Transmitter on Frequency 915800000
Transmitting message : Hello Listener
Transmission completed
	Enabling Receiver on Frequency 915900000
Receive complete ->Hello Listener
	Enabling Transmitter on Frequency 915800000
Transmitting message : Hello Listener
Transmission completed
	Enabling Receiver on Frequency 915900000
...

Log from TX device:

	Enabling Transmitter on Frequency 915900000
Transmitting message : Hello Listener
Transmission completed
	Enabling Receiver on Frequency 915800000
Receive complete ->Hello Listener
	Enabling Transmitter on Frequency 915900000
Transmitting message : Hello Listener
Transmission completed
	Enabling Receiver on Frequency 915800000
Receive complete ->Hello Listener
	Enabling Transmitter on Frequency 915900000
...

That was it my friend, I had removed the calls to Radio.Standby()

You know, how you get so involved in a problem, that you start thinking “I’ve tried all of the basics, so it has to be something esoteric”

Yes we are using the 4631(L) core units -if you remember we had a little assistance from you during the placing of our order for them, because as you mention, they are not strictly speaking available fromt he store at the moment :smiley:, though to be fair back here in the “workshop” I’m just using the regular 4631 core for the R&D exercise.

:handshake: :+1:

Another happy customer, now I can start my weekend.

1 Like

you’re knocking off late … go grab some fun and swing it around by the ears for a couple of days @beegee; something tells me that you’ve earned it

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.