I am using the CubeCell Dev Board (V1) for LoRa transmission.
I have 1 board setup as a transmitter, and 1 board setup as a receiver.
The code is based on the CubCell LoRa Examples
The aim is to have the receiver turn on a relay while the transmitter is transmitting, and as soon as the receiver doesn’t receive a valid code from the transmitter, it switches off the relay.
I would like to have the delay between switching off the transmitter, and the relay switching off, to be no more than 500ms
I have setup the sender to transmit at a random time, anywhere up to 250ms. This all appears to be working, however, the receiver is only receiving about 1 packet per second, so misses anywhere from 6 to 9 packets each time.
Is there a way I can get the receiver to receive more packets, or is this a limitation of the LoRaWan_APP library?
I have previously setup something similar using a Dragino Lora Shield, and LoRa.h, which worked well, but LoRa.h doesn’t seem to be compatible with the CubeCell
I am fairly new to LoRa and arduino, so stumbling my way through everything
Transmitter Code:
#include "LoRaWan_APP.h"
#include "Arduino.h"
/*
* set LoraWan_RGB to 1,the RGB active in loraWan
* RGB red means sending;
* RGB green means received done;
*/
#ifndef LoraWan_RGB
#define LoraWan_RGB 0
#endif
#define RF_FREQUENCY 433000000 // Hz
#define TX_OUTPUT_POWER 21 // dBm
#define LORA_BANDWIDTH 0 // [0: 125 kHz,
// 1: 250 kHz,
// 2: 500 kHz,
// 3: Reserved]
#define LORA_SPREADING_FACTOR LoRaSF // [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
#define RX_TIMEOUT_VALUE 1000
#define BUFFER_SIZE 16 // Define the payload size here
char txpacket[BUFFER_SIZE];
char rxpacket[BUFFER_SIZE];
static RadioEvents_t RadioEvents;
float txNumber;
bool lora_idle=true;
int LoRaSF = 8;
int Transmitter_ID = 100; // Set to 100 so if something is wrong with jumpers, nothing will happen.
void setup() {
Serial.begin(115200);
txNumber=0;
#define J1 GPIO1 // Jumper 1 for remote coding
#define J2 GPIO2 // Jumper 2 for remote coding
#define J3 GPIO3 // Jumper 3 for LoRa Spreading Factor
#define J4 GPIO4 // Jumper 4 for Remote Function
#define J5 GPIO5 // Jumper 5 for Remote Function
#define RB GPIO0 // Input for remote button - may not be needed
#define LED 0 // Output for LED
int counter = 0;
int delay_time = 0;
pinMode(J1, INPUT);
pinMode(J2, INPUT);
pinMode(J3, INPUT);
pinMode(J4, INPUT);
pinMode(J5, INPUT);
pinMode(RB, INPUT);
pinMode(LED, OUTPUT); // Not doing anything with this yet, but should turn on when button pressed, and flash if voltage low.
// calculate Transmitter number based on J1, J2, J3
Trans_ID(); // Call the Trans_ID function - calculates the transmitter ID based on jumpers
// Set LoRa Spreading Factor
if (digitalRead(J3) == HIGH) {
LoRaSF = 11; // ranges from 6-12,default 7 see API docs
}
if (digitalRead(J3) == LOW) {
LoRaSF = 12; // ranges from 6-12,default 7 see API docs
}
RadioEvents.TxDone = OnTxDone;
RadioEvents.TxTimeout = OnTxTimeout;
Radio.Init( &RadioEvents );
Radio.SetChannel( RF_FREQUENCY );
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, 3000 );
}
void loop() {
Transmitter_ID ++;
Serial.print("Sending packet: ");
Serial.println(Transmitter_ID);
Serial.print("SF");
Serial.println(LORA_SPREADING_FACTOR);
sprintf(txpacket,"%u",Transmitter_ID); //start a package
Radio.Send( (uint8_t *)txpacket, strlen(txpacket) ); //send the package out
int delay_time = random(250); // set random delay to prevent interferring with other transmitters
delay(delay_time);
}
void Trans_ID() { // Calculates the transmitter number based on State of jumpers J1, J2, J4 & J5. (J3 is used for spreading factor not ID)
int Sum = 0;
if (digitalRead(J1) == HIGH) {
Sum += 1;
}
if (digitalRead(J2) == HIGH) {
Sum += 2;
}
if (digitalRead(J4) == HIGH) {
Sum += 4;
}
if (digitalRead(J5) == HIGH) {
Sum += 8;
}
Transmitter_ID = Sum;
}
void OnTxDone( void )
{
turnOffRGB();
Serial.println("TX done......");
lora_idle = true;
}
void OnTxTimeout( void )
{
turnOffRGB();
Radio.Sleep( );
Serial.println("TX Timeout......");
lora_idle = true;
}
Receiver Code
/* Heltec Automation Receive communication test example
*
* Function:
* 1. Receive the same frequency band lora signal program
*
*
* this project also realess in GitHub:
* https://github.com/HelTecAutomation/ASR650x-Arduino
* */
#include "LoRaWan_APP.h"
#include "Arduino.h"
/*
* set LoraWan_RGB to 1,the RGB active in loraWan
* RGB red means sending;
* RGB green means received done;
*/
#ifndef LoraWan_RGB
#define LoraWan_RGB 0
#endif
#define RF_FREQUENCY 433000000 // Hz
#define TX_OUTPUT_POWER 21 // dBm
#define LORA_BANDWIDTH 0 // [0: 125 kHz,
// 1: 250 kHz,
// 2: 500 kHz,
// 3: Reserved]
#define LORA_SPREADING_FACTOR 12 // [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
#define RX_TIMEOUT_VALUE 100
#define BUFFER_SIZE 16 // Define the payload size here
char txpacket[BUFFER_SIZE];
char rxpacket[BUFFER_SIZE];
static RadioEvents_t RadioEvents;
int16_t txNumber;
int16_t rssi,rxSize;
bool lora_idle = true;
void setup() {
Serial.begin(115200);
txNumber=0;
rssi=0;
RadioEvents.RxDone = OnRxDone;
Radio.Init( &RadioEvents );
Radio.SetChannel( RF_FREQUENCY );
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()
{
if(lora_idle)
{
turnOffRGB();
lora_idle = false;
Serial.println("into RX mode");
Radio.Rx(0);
}
}
void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr )
{
rssi=rssi;
rxSize=size;
memcpy(rxpacket, payload, size );
rxpacket[size]='\0';
turnOnRGB(COLOR_RECEIVED,0);
Radio.Sleep( );
Serial.printf("\r\nreceived packet \"%s\" with rssi %d , length %d\r\n",rxpacket,rssi,rxSize);
lora_idle = true;
}