first commit
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
/*******************************************************************************************************
|
||||
Programs for Arduino - Copyright of the author Stuart Robinson - 06/02/20
|
||||
|
||||
This programs is supplied as is, it is up to the user of the program to decide if the programs are
|
||||
suitable for the intended purpose and free from errors.
|
||||
*******************************************************************************************************/
|
||||
|
||||
/*******************************************************************************************************
|
||||
Program Operation - This program blinks an LED connected the pin number defined below. The pin 13 LED,
|
||||
fitted to some Arduinos is blinked as well. The blinks should be close to one per second. messages are
|
||||
sent to the Serial Monitor also.
|
||||
|
||||
Serial monitor baud rate is set at 9600.
|
||||
*******************************************************************************************************/
|
||||
|
||||
#define LED1 8 //pin number for LED, set logic level high for on
|
||||
|
||||
#define Program_Version "V1.0"
|
||||
|
||||
uint16_t seconds; //used to display time elapsed on Serial Monitor
|
||||
|
||||
void loop()
|
||||
{
|
||||
Serial.print(seconds);
|
||||
Serial.println(F(" Seconds")); //this message should print on console at close to once per second
|
||||
seconds++;
|
||||
digitalWrite(LED1, HIGH);
|
||||
digitalWrite(13, HIGH);
|
||||
delay(100);
|
||||
digitalWrite(LED1, LOW);
|
||||
digitalWrite(13, LOW);
|
||||
delay(890); //should give approx 1 second flash
|
||||
}
|
||||
|
||||
|
||||
void led_Flash(uint16_t flashes, uint16_t delaymS)
|
||||
{
|
||||
//general purpose routine for flashing LED as indicator
|
||||
uint16_t index;
|
||||
|
||||
for (index = 1; index <= flashes; index++)
|
||||
{
|
||||
digitalWrite(LED1, HIGH); //LED on
|
||||
digitalWrite(13, HIGH); //Arduino board LED on
|
||||
delay(delaymS);
|
||||
digitalWrite(LED1, LOW); //LED off
|
||||
digitalWrite(13, LOW); //Arduino board LED off
|
||||
delay(delaymS);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void setup()
|
||||
{
|
||||
pinMode(LED1, OUTPUT); //setup pin as output for indicator LED
|
||||
pinMode(13, OUTPUT); //setup pin as output for some Arduino boards that include an LED on pin 13
|
||||
led_Flash(2, 125); //two quick LED flashes to indicate program start
|
||||
|
||||
Serial.begin(9600);
|
||||
Serial.println();
|
||||
Serial.print(__TIME__);
|
||||
Serial.print(F(" "));
|
||||
Serial.println(__DATE__);
|
||||
Serial.println(F(Program_Version));
|
||||
Serial.println();
|
||||
|
||||
Serial.println(F("1_LED_Blink Starting"));
|
||||
}
|
||||
|
||||
@@ -0,0 +1,401 @@
|
||||
/*******************************************************************************************************
|
||||
Programs for Arduino - Copyright of the author Stuart Robinson - 06/02/20
|
||||
|
||||
This program is supplied as is, it is up to the user of the program to decide if the program is
|
||||
suitable for the intended purpose and free from errors.
|
||||
*******************************************************************************************************/
|
||||
|
||||
/*******************************************************************************************************
|
||||
Program Operation - This program is stand alone, it is not necessary to install the SX12XX-LoRa library
|
||||
to use it. This test program is for the SX126X LoRa devices.
|
||||
|
||||
The program checks that a SX126X LoRa device can be accessed by doing a test register write and read.
|
||||
If there is no device found a message is printed on the serial monitor. The contents of the registers
|
||||
from 0x00 to 0x7F are printed, there is a copy of a typical printout below. Note that the read back
|
||||
changed frequency may be slightly different to the programmed frequency, there is a rounding error due
|
||||
to the use of floats to calculate the frequency.
|
||||
|
||||
The Arduino pin numbers that the NSS and NRESET pins on the LoRa device are connected to must be
|
||||
specified in the hardware definitions section below. The LoRa device type in use, SX1261, SX1262,
|
||||
or SX1268 must be specified also.
|
||||
|
||||
Typical printout;
|
||||
|
||||
2_Register_Test Starting
|
||||
Reset device
|
||||
LoRa Device found
|
||||
Reset device
|
||||
Registers at reset
|
||||
Reg 0 1 2 3 4 5 6 7 8 9 A B C D E F
|
||||
0x800 00 00 00 00 01 07 20 1E 00 10 19 04 0F FF 0F FF
|
||||
0x810 10 00 10 00 10 00 10 00 00 00 00 00 00 00 00 00
|
||||
0x820 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
|
||||
0x830 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
|
||||
0x840 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
|
||||
0x850 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
|
||||
0x860 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
|
||||
0x870 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
|
||||
0x880 03 00 00 5F 10 08 00 00 08 05 00 39 30 00 00 0C
|
||||
0x890 00 00 00 00 00 0F 0A 07 10 00 26 01 01 53 06 07
|
||||
0x8A0 10 00 AA 20 5A 04 F0 00 56 56 54 43 94 20 40 00
|
||||
0x8B0 00 83 11 00 01 04 0A 4C 14 0A 2F 01 6B FF FF 00
|
||||
0x8C0 00 A0 20 00 00 00 AC 00 1C 00 00 AB 05 30 00 00
|
||||
0x8D0 0C 14 14 40 06 00 00 10 C8 00 00 00 00 00 31 39
|
||||
0x8E0 90 39 0C 04 40 20 1C 18 03 00 05 04 03 02 01 01
|
||||
0x8F0 00 00 00 00 30 00 00 00 00 00 00 00 00 00 00 00
|
||||
0x900 30 00 00 00 00 00 00 00 00 00 00 00 24 04 47 04
|
||||
0x910 14 12 12 04 00 03 0A 00 15 35 09 00 02 1F 5F 08
|
||||
0x920 01 04 05 00 00 00 00 00 00 00 00 00 00 00 00 00
|
||||
0x930 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
|
||||
0x940 00 07 00 03 02 00 10 0E 0D 0C 03 04 03 70 0C 00
|
||||
0x950 00 00 00 04 00 00 00 00 00 00 00 00 00 00 00 00
|
||||
0x960 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
|
||||
0x970 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
|
||||
0x980 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
|
||||
0x990 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
|
||||
0x9A0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
|
||||
0x9B0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
|
||||
0x9C0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
|
||||
0x9D0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
|
||||
0x9E0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
|
||||
0x9F0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
|
||||
|
||||
|
||||
Frequency at reset 915000000
|
||||
Reg 0 1 2 3 4 5 6 7 8 9 A B C D E F
|
||||
0x880 03 00 00 5F 10 08 00 00 08 05 00 39 30 00 00 0C
|
||||
Change Frequency 434100000
|
||||
Reg 0 1 2 3 4 5 6 7 8 9 A B C D E F
|
||||
0x880 03 00 00 5F 10 08 00 00 08 05 00 1B 21 99 A0 0C
|
||||
Changed Frequency 434100000
|
||||
|
||||
Serial monitor baud rate is set at 9600.
|
||||
*******************************************************************************************************/
|
||||
|
||||
const uint16_t REG_RFFrequency31_24 = 0x088B;
|
||||
const uint16_t REG_RFFrequency23_16 = 0x088C;
|
||||
const uint16_t REG_RFFrequency15_8 = 0x088D;
|
||||
const uint16_t REG_RFFrequency7_0 = 0x088E;
|
||||
const uint8_t RADIO_WRITE_REGISTER = 0x0D;
|
||||
const uint8_t RADIO_READ_REGISTER = 0x1D;
|
||||
const uint8_t RADIO_SET_RFFREQUENCY = 0x86;
|
||||
|
||||
const uint8_t DEVICE_SX1261 = 0x01;
|
||||
const uint8_t DEVICE_SX1262 = 0x00;
|
||||
const uint8_t DEVICE_SX1268 = 0x02;
|
||||
|
||||
//********* Setup hardware definitions here ! *****************
|
||||
|
||||
//These are the pin definitions for one of the Tracker boards, be sure to change them to match your
|
||||
//own setup. You will also need to connect up the pins for the SPI bus
|
||||
|
||||
#define NSS 10 //SX126X device select
|
||||
#define NRESET 9 //SX126X reset pin
|
||||
#define RFBUSY -1 //SX126X busy pin
|
||||
#define SW -1 //SW pin on Dorji devices is used to turn RF switch on\off, set to -1 if not used
|
||||
|
||||
#define LORA_DEVICE DEVICE_SX1261 //define the device, DEVICE_SX1261, DEVICE_SX1262 or DEVICE_SX1268
|
||||
|
||||
//**************************************************************/
|
||||
|
||||
|
||||
#include <SPI.h>
|
||||
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(9600);
|
||||
Serial.println(F("2_Register_Test Starting"));
|
||||
|
||||
SPI.begin();
|
||||
SPI.beginTransaction(SPISettings(8000000, MSBFIRST, SPI_MODE0));
|
||||
|
||||
//The begin function setups the hardware pins used by device and then checks if device is found
|
||||
//the DIO1, DIO2 and DIO3 are not used in this example so are set to -1
|
||||
|
||||
if (begin(NSS, NRESET, RFBUSY, -1, -1, -1, SW, LORA_DEVICE))
|
||||
{
|
||||
Serial.println(F("LoRa Device found"));
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.println(F("No device responding"));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void loop()
|
||||
{
|
||||
uint32_t frequency;
|
||||
resetDevice(LORA_DEVICE); //reset the device
|
||||
Serial.println(F("Registers at reset")); //show the all registers following a reset
|
||||
printRegisters(0x800, 0x9FF);
|
||||
Serial.println();
|
||||
Serial.println();
|
||||
|
||||
frequency = getFreqInt(); //read the set frequency following a reset
|
||||
Serial.print(F("Frequency at reset "));
|
||||
Serial.println(frequency);
|
||||
printRegisters(0x0880, 0x088F); //show the registers before the frequency change
|
||||
setRfFrequency(434100000, 0); //change the frequency at reset, in hertz
|
||||
frequency = getFreqInt(); //read back the changed frequency
|
||||
Serial.print(F("Change Frequency "));
|
||||
Serial.println(frequency); //print the changed frequency, did the write work (allow for rounding errors) ?
|
||||
printRegisters(0x0880, 0x088F); //show the registers after frequency change
|
||||
frequency = getFreqInt(); //read the set frequency following a reset
|
||||
Serial.print(F("Changed Frequency "));
|
||||
Serial.println(frequency);
|
||||
|
||||
Serial.println();
|
||||
Serial.println();
|
||||
delay(5000);
|
||||
}
|
||||
|
||||
|
||||
void readRegisters(uint16_t address, uint8_t *buffer, uint16_t size)
|
||||
{
|
||||
|
||||
uint16_t index;
|
||||
uint8_t addr_l, addr_h;
|
||||
|
||||
addr_h = address >> 8;
|
||||
addr_l = address & 0x00FF;
|
||||
checkBusy();
|
||||
|
||||
digitalWrite(NSS, LOW);
|
||||
SPI.transfer(RADIO_READ_REGISTER);
|
||||
SPI.transfer(addr_h); //MSB
|
||||
SPI.transfer(addr_l); //LSB
|
||||
SPI.transfer(0xFF);
|
||||
for (index = 0; index < size; index++)
|
||||
{
|
||||
*(buffer + index) = SPI.transfer(0xFF);
|
||||
}
|
||||
|
||||
digitalWrite(NSS, HIGH);
|
||||
|
||||
checkBusy();
|
||||
}
|
||||
|
||||
|
||||
uint8_t readRegister(uint16_t address)
|
||||
{
|
||||
uint8_t data;
|
||||
|
||||
readRegisters(address, &data, 1);
|
||||
return data;
|
||||
}
|
||||
|
||||
|
||||
void writeRegisters(uint16_t address, uint8_t *buffer, uint16_t size)
|
||||
{
|
||||
uint8_t addr_l, addr_h;
|
||||
uint8_t i;
|
||||
|
||||
addr_l = address & 0xff;
|
||||
addr_h = address >> 8;
|
||||
checkBusy();
|
||||
|
||||
digitalWrite(NSS, LOW);
|
||||
SPI.transfer(RADIO_WRITE_REGISTER);
|
||||
SPI.transfer(addr_h); //MSB
|
||||
SPI.transfer(addr_l); //LSB
|
||||
|
||||
for (i = 0; i < size; i++)
|
||||
{
|
||||
SPI.transfer(buffer[i]);
|
||||
}
|
||||
|
||||
digitalWrite(NSS, HIGH);
|
||||
|
||||
checkBusy();
|
||||
}
|
||||
|
||||
|
||||
void writeRegister(uint16_t address, uint8_t value)
|
||||
{
|
||||
writeRegisters( address, &value, 1 );
|
||||
}
|
||||
|
||||
|
||||
uint32_t getFreqInt()
|
||||
{
|
||||
//get the current set device frequency from registers, return as long integer
|
||||
uint8_t MsbH, MsbL, Mid, Lsb;
|
||||
uint32_t uinttemp;
|
||||
float floattemp;
|
||||
MsbH = readRegister(REG_RFFrequency31_24);
|
||||
MsbL = readRegister(REG_RFFrequency23_16);
|
||||
Mid = readRegister(REG_RFFrequency15_8);
|
||||
Lsb = readRegister(REG_RFFrequency7_0);
|
||||
floattemp = ( (MsbH * 0x1000000ul) + (MsbL * 0x10000ul) + (Mid * 0x100ul) + Lsb);
|
||||
floattemp = ((floattemp * 0.95367431640625) / 1000000ul);
|
||||
uinttemp = (uint32_t)(floattemp * 1000000);
|
||||
return uinttemp;
|
||||
}
|
||||
|
||||
|
||||
void printRegisters(uint16_t Start, uint16_t End)
|
||||
{
|
||||
//prints the contents of SX126x registers to serial monitor
|
||||
|
||||
uint16_t Loopv1, Loopv2, RegData;
|
||||
|
||||
Serial.print(F("Reg 0 1 2 3 4 5 6 7 8 9 A B C D E F"));
|
||||
Serial.println();
|
||||
|
||||
for (Loopv1 = Start; Loopv1 <= End;) //32 lines
|
||||
{
|
||||
Serial.print(F("0x"));
|
||||
Serial.print((Loopv1), HEX); //print the register number
|
||||
Serial.print(F(" "));
|
||||
for (Loopv2 = 0; Loopv2 <= 15; Loopv2++)
|
||||
{
|
||||
RegData = readRegister(Loopv1);
|
||||
if (RegData < 0x10)
|
||||
{
|
||||
Serial.print(F("0"));
|
||||
}
|
||||
Serial.print(RegData, HEX); //print the register number
|
||||
Serial.print(F(" "));
|
||||
Loopv1++;
|
||||
}
|
||||
Serial.println();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void setRfFrequency( uint32_t frequency, int32_t offset )
|
||||
{
|
||||
//Note RF_Freq = freq_reg*32M/(2^25)-----> freq_reg = (RF_Freq * (2^25))/32
|
||||
|
||||
uint8_t Rf_Freq[4];
|
||||
|
||||
frequency = frequency + offset;
|
||||
|
||||
frequency = ( uint32_t )( ( double )frequency / ( double )0.95367431640625 );
|
||||
|
||||
checkBusy();
|
||||
|
||||
Rf_Freq[0] = (frequency >> 24) & 0xFF; //MSB
|
||||
Rf_Freq[1] = (frequency >> 16) & 0xFF;
|
||||
Rf_Freq[2] = (frequency >> 8) & 0xFF;
|
||||
Rf_Freq[3] = frequency & 0xFF;//LSB
|
||||
|
||||
writeCommand(RADIO_SET_RFFREQUENCY, Rf_Freq, 4);
|
||||
}
|
||||
|
||||
|
||||
void checkBusy()
|
||||
{
|
||||
uint8_t busy_timeout_cnt;
|
||||
busy_timeout_cnt = 0;
|
||||
|
||||
while (digitalRead(RFBUSY))
|
||||
{
|
||||
delay(1);
|
||||
busy_timeout_cnt++;
|
||||
|
||||
if (busy_timeout_cnt > 10) //wait 10mS for busy to complete
|
||||
{
|
||||
busy_timeout_cnt = 0;
|
||||
Serial.println(F("ERROR - Busy Timeout!"));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void resetDevice(uint8_t device)
|
||||
{
|
||||
if ( (device == DEVICE_SX1261) | (device == DEVICE_SX1262) | (device == DEVICE_SX1268) )
|
||||
{
|
||||
Serial.println(F("Reset device"));
|
||||
delay(10);
|
||||
digitalWrite(NRESET, LOW);
|
||||
delay(2);
|
||||
digitalWrite(NRESET, HIGH);
|
||||
delay(25);
|
||||
checkBusy();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool begin(int8_t pinNSS, int8_t pinNRESET, int8_t pinRFBUSY, int8_t pinDIO1, int8_t pinDIO2, int8_t pinDIO3, int8_t pinSW, uint8_t device)
|
||||
{
|
||||
pinMode(pinNSS, OUTPUT);
|
||||
digitalWrite(pinNSS, HIGH);
|
||||
pinMode(pinNRESET, OUTPUT);
|
||||
digitalWrite(pinNRESET, LOW);
|
||||
pinMode(pinRFBUSY, INPUT);
|
||||
|
||||
if (pinDIO1 >= 0)
|
||||
{
|
||||
pinMode( pinDIO1, INPUT);
|
||||
}
|
||||
|
||||
if (pinDIO2 >= 0)
|
||||
{
|
||||
pinMode(pinDIO2, INPUT);
|
||||
}
|
||||
|
||||
if (pinDIO3 >= 0)
|
||||
{
|
||||
pinMode(pinDIO3, INPUT);
|
||||
}
|
||||
|
||||
if (pinSW >= 0)
|
||||
{
|
||||
pinMode(pinSW, OUTPUT); //Dorji devices have an SW pin that needs to be set high to power antenna switch
|
||||
digitalWrite(pinSW, HIGH);
|
||||
}
|
||||
|
||||
resetDevice(device);
|
||||
if (checkDevice())
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
bool checkDevice()
|
||||
{
|
||||
//check there is a device out there, writes a register and reads back
|
||||
uint8_t Regdata1, Regdata2;
|
||||
Regdata1 = readRegister(0x88e); //low byte of frequency setting
|
||||
writeRegister(0x88e, (Regdata1 + 1));
|
||||
Regdata2 = readRegister(0x88e); //read changed value back
|
||||
writeRegister(0x88e, Regdata1); //restore register to original value
|
||||
|
||||
if (Regdata2 == (Regdata1 + 1))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void writeCommand(uint8_t Opcode, uint8_t *buffer, uint16_t size)
|
||||
{
|
||||
uint8_t index;
|
||||
checkBusy();
|
||||
|
||||
digitalWrite(NSS, LOW);
|
||||
SPI.transfer(Opcode);
|
||||
|
||||
for (index = 0; index < size; index++)
|
||||
{
|
||||
SPI.transfer(buffer[index]);
|
||||
}
|
||||
digitalWrite(NSS, HIGH);
|
||||
|
||||
checkBusy();
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,123 @@
|
||||
/*******************************************************************************************************
|
||||
Programs for Arduino - Copyright of the author Stuart Robinson - 02/03/20
|
||||
|
||||
This program is supplied as is, it is up to the user of the program to decide if the program is
|
||||
suitable for the intended purpose and free from errors.
|
||||
*******************************************************************************************************/
|
||||
|
||||
|
||||
/*******************************************************************************************************
|
||||
Program Operation - This is a minimum setup LoRa test transmitter. A packet containing the ASCII text
|
||||
"Hello World 1234567890" is sent using the frequency and LoRa settings specified in the LT.setupLoRa()
|
||||
command. The pins to access the lora device need to be defined at the top of the program also.
|
||||
|
||||
The details of the packet sent and any errors are shown on the Arduino IDE Serial Monitor, together with
|
||||
the transmit power used and the packet length. The matching receiver program, '4_LoRa_Receiver' can be used
|
||||
to check the packets are being sent correctly, the frequency and LoRa settings (in the LT.setupLoRa()
|
||||
commands) must be the same for the transmitter and receiver programs. Sample Serial Monitor output;
|
||||
|
||||
10dBm Packet> Hello World 1234567890* BytesSent,23 PacketsSent,6
|
||||
|
||||
For an example of a more detailed configuration for a transmitter, see program 103_LoRa_Transmitter.
|
||||
|
||||
Serial monitor baud rate is set at 9600
|
||||
*******************************************************************************************************/
|
||||
|
||||
#define Program_Version "V1.0"
|
||||
|
||||
#include <SPI.h> //the lora device is SPI based so load the SPI library
|
||||
#include <SX126XLT.h> //include the appropriate library
|
||||
|
||||
SX126XLT LT; //create a library class instance called LT
|
||||
|
||||
#define NSS 10 //select pin on LoRa device
|
||||
#define NRESET 9 //reset pin on LoRa device
|
||||
#define RFBUSY 7 //SX126X busy pin
|
||||
#define DIO1 4 //DIO1 pin on LoRa device, used for sensing RX and TX done
|
||||
#define SW 5 //SW pin on LoRa device, used to power antenna switch
|
||||
#define LORA_DEVICE DEVICE_SX1261 //we need to define the device we are using
|
||||
#define TXpower 10 //LoRa transmit power in dBm
|
||||
|
||||
uint8_t TXPacketL;
|
||||
uint32_t TXPacketCount;
|
||||
|
||||
uint8_t buff[] = "Hello World 1234567890"; //the message to send
|
||||
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(9600);
|
||||
Serial.println();
|
||||
Serial.println(F("3_LoRa_Transmitter Starting"));
|
||||
|
||||
SPI.begin();
|
||||
|
||||
if (LT.begin(NSS, NRESET, RFBUSY, DIO1, SW, LORA_DEVICE))
|
||||
{
|
||||
Serial.println(F("LoRa Device found"));
|
||||
delay(1000);
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.println(F("No device responding"));
|
||||
while (1);
|
||||
}
|
||||
|
||||
LT.setupLoRa(434000000, 0, LORA_SF7, LORA_BW_125, LORA_CR_4_5, LDRO_AUTO); //configure frequency and LoRa settings
|
||||
|
||||
Serial.print(F("Transmitter ready"));
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
|
||||
void loop()
|
||||
{
|
||||
Serial.print(TXpower); //print the transmit power defined
|
||||
Serial.print(F("dBm "));
|
||||
Serial.print(F("Packet> "));
|
||||
Serial.flush();
|
||||
|
||||
TXPacketL = sizeof(buff); //set TXPacketL to length of array
|
||||
buff[TXPacketL - 1] = '*'; //replace null character at buffer end so its visible on receiver
|
||||
|
||||
LT.printASCIIPacket(buff, TXPacketL); //print the buffer (the sent packet) as ASCII
|
||||
|
||||
if (LT.transmit(buff, TXPacketL, 10000, TXpower, WAIT_TX)) //will return packet length sent if OK, otherwise 0 if transmit error
|
||||
{
|
||||
TXPacketCount++;
|
||||
packet_is_OK();
|
||||
}
|
||||
else
|
||||
{
|
||||
packet_is_Error(); //transmit packet returned 0, there was an error
|
||||
}
|
||||
|
||||
Serial.println();
|
||||
delay(1000); //have a delay between packets
|
||||
}
|
||||
|
||||
|
||||
void packet_is_OK()
|
||||
{
|
||||
//if here packet has been sent OK
|
||||
Serial.print(F(" BytesSent,"));
|
||||
Serial.print(TXPacketL); //print transmitted packet length
|
||||
Serial.print(F(" PacketsSent,"));
|
||||
Serial.print(TXPacketCount); //print total of packets sent OK
|
||||
}
|
||||
|
||||
|
||||
void packet_is_Error()
|
||||
{
|
||||
//if here there was an error transmitting packet
|
||||
uint16_t IRQStatus;
|
||||
IRQStatus = LT.readIrqStatus(); //read the the interrupt register
|
||||
Serial.print(F(" SendError,"));
|
||||
Serial.print(F("Length,"));
|
||||
Serial.print(TXPacketL); //print transmitted packet length
|
||||
Serial.print(F(",IRQreg,"));
|
||||
Serial.print(IRQStatus, HEX); //print IRQ status
|
||||
LT.printIrqStatus(); //prints the text of which IRQs set
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,170 @@
|
||||
/*******************************************************************************************************
|
||||
Programs for Arduino - Copyright of the author Stuart Robinson - 02/03/20
|
||||
|
||||
This program is supplied as is, it is up to the user of the program to decide if the program is
|
||||
suitable for the intended purpose and free from errors.
|
||||
*******************************************************************************************************/
|
||||
|
||||
/*******************************************************************************************************
|
||||
Program Operation - This is a minimum setup LoRa test receiver. The program listens for incoming packets
|
||||
using the frequency and LoRa settings in the LT.setupLoRa() command. The pins to access the lora device
|
||||
need to be defined at the top of the program also.
|
||||
|
||||
There is a printout on the Arduino IDE serial monitor of the valid packets received, the packet is assumed
|
||||
to be in ASCII printable text, if it's not ASCII text characters from 0x20 to 0x7F, expect weird things to
|
||||
happen on the Serial Monitor. Sample serial monitor output;
|
||||
|
||||
8s Hello World 1234567890*,RSSI,-44dBm,SNR,9dB,Length,23,Packets,7,Errors,0,IRQreg,50
|
||||
|
||||
If there is a packet error it might look like this, which is showing a CRC error;
|
||||
|
||||
137s PacketError,RSSI,-89dBm,SNR,-8dB,Length,23,Packets,37,Errors,2,IRQreg,70,IRQ_HEADER_VALID,IRQ_CRC_ERROR,IRQ_RX_DONE
|
||||
|
||||
If there are no packets received in a 10 second period then you should see a message like this;
|
||||
|
||||
112s RXTimeout
|
||||
|
||||
For an example of a more detailed configuration for a receiver, see program 104_LoRa_Receiver.
|
||||
|
||||
Serial monitor baud rate is set at 9600.
|
||||
*******************************************************************************************************/
|
||||
|
||||
#define Program_Version "V1.1"
|
||||
|
||||
#include <SPI.h> //the lora device is SPI based so load the SPI library
|
||||
#include <SX126XLT.h> //include the appropriate library
|
||||
|
||||
SX126XLT LT; //create a library class instance called LT
|
||||
|
||||
#define NSS 10 //select pin on LoRa device
|
||||
#define NRESET 9 //reset pin on LoRa device
|
||||
#define RFBUSY 7 //SX126X busy pin
|
||||
#define DIO1 3 //DIO1 pin on LoRa device, used for RX and TX done
|
||||
#define SW 5 //SW pin on LoRa device, used to power antenna switch
|
||||
#define LORA_DEVICE DEVICE_SX1262 //we need to define the device we are using
|
||||
#define RXBUFFER_SIZE 32 //RX buffer size
|
||||
|
||||
uint32_t RXpacketCount;
|
||||
uint32_t errors;
|
||||
|
||||
uint8_t RXBUFFER[RXBUFFER_SIZE]; //create the buffer that received packets are copied into
|
||||
|
||||
uint8_t RXPacketL; //stores length of packet received
|
||||
int8_t PacketRSSI; //stores RSSI of received packet
|
||||
int8_t PacketSNR; //stores signal to noise ratio (SNR) of received packet
|
||||
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(9600);
|
||||
Serial.println();
|
||||
Serial.println(F("4_LoRa_Receiver Starting"));
|
||||
Serial.println();
|
||||
|
||||
SPI.begin();
|
||||
|
||||
if (LT.begin(NSS, NRESET, RFBUSY, DIO1, SW, LORA_DEVICE))
|
||||
{
|
||||
Serial.println(F("LoRa Device found"));
|
||||
delay(1000);
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.println(F("No device responding"));
|
||||
while (1);
|
||||
}
|
||||
|
||||
LT.setupLoRa(434000000, 0, LORA_SF7, LORA_BW_125, LORA_CR_4_5, LDRO_AUTO); //configure frequency and LoRa settings
|
||||
|
||||
Serial.print(F("Receiver ready - RXBUFFER_SIZE "));
|
||||
Serial.println(RXBUFFER_SIZE);
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
|
||||
void loop()
|
||||
{
|
||||
RXPacketL = LT.receive(RXBUFFER, RXBUFFER_SIZE, 60000, WAIT_RX); //wait for a packet to arrive with 60seconds (60000mS) timeout
|
||||
|
||||
PacketRSSI = LT.readPacketRSSI(); //read the received packets RSSI value
|
||||
PacketSNR = LT.readPacketSNR(); //read the received packets SNR value
|
||||
|
||||
if (RXPacketL == 0) //if the LT.receive() function detects an error RXpacketL is 0
|
||||
{
|
||||
packet_is_Error();
|
||||
}
|
||||
else
|
||||
{
|
||||
packet_is_OK();
|
||||
}
|
||||
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
|
||||
void packet_is_OK()
|
||||
{
|
||||
uint16_t IRQStatus;
|
||||
|
||||
RXpacketCount++;
|
||||
IRQStatus = LT.readIrqStatus(); //read the LoRa device IRQ status register
|
||||
printElapsedTime(); //print elapsed time to Serial Monitor
|
||||
|
||||
Serial.print(F(" "));
|
||||
LT.printASCIIPacket(RXBUFFER, RXPacketL); //print the packet as ASCII characters
|
||||
|
||||
Serial.print(F(",RSSI,"));
|
||||
Serial.print(PacketRSSI);
|
||||
Serial.print(F("dBm,SNR,"));
|
||||
Serial.print(PacketSNR);
|
||||
Serial.print(F("dB,Length,"));
|
||||
Serial.print(RXPacketL);
|
||||
Serial.print(F(",Packets,"));
|
||||
Serial.print(RXpacketCount);
|
||||
Serial.print(F(",Errors,"));
|
||||
Serial.print(errors);
|
||||
Serial.print(F(",IRQreg,"));
|
||||
Serial.print(IRQStatus, HEX);
|
||||
}
|
||||
|
||||
|
||||
void packet_is_Error()
|
||||
{
|
||||
uint16_t IRQStatus;
|
||||
IRQStatus = LT.readIrqStatus(); //read the LoRa device IRQ status register
|
||||
|
||||
printElapsedTime(); //print elapsed time to Serial Monitor
|
||||
|
||||
if (IRQStatus & IRQ_RX_TIMEOUT) //check for an RX timeout
|
||||
{
|
||||
Serial.print(F(" RXTimeout"));
|
||||
}
|
||||
else
|
||||
{
|
||||
errors++;
|
||||
Serial.print(F(" PacketError"));
|
||||
Serial.print(F(",RSSI,"));
|
||||
Serial.print(PacketRSSI);
|
||||
Serial.print(F("dBm,SNR,"));
|
||||
Serial.print(PacketSNR);
|
||||
Serial.print(F("dB,Length,"));
|
||||
Serial.print(LT.readRXPacketL()); //get the real packet length
|
||||
Serial.print(F(",Packets,"));
|
||||
Serial.print(RXpacketCount);
|
||||
Serial.print(F(",Errors,"));
|
||||
Serial.print(errors);
|
||||
Serial.print(F(",IRQreg,"));
|
||||
Serial.print(IRQStatus, HEX);
|
||||
LT.printIrqStatus(); //print the names of the IRQ registers set
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void printElapsedTime()
|
||||
{
|
||||
float seconds;
|
||||
seconds = millis() / 1000;
|
||||
Serial.print(seconds, 0);
|
||||
Serial.print(F("s"));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user