first commit
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
/*******************************************************************************************************
|
||||
Programs for Arduino - Copyright of the author Stuart Robinson - 16/12/19
|
||||
|
||||
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_STM32 Starting"));
|
||||
}
|
||||
|
||||
@@ -0,0 +1,300 @@
|
||||
/*******************************************************************************************************
|
||||
|
||||
Programs for Arduino - Copyright of the author Stuart Robinson - 30/12/19
|
||||
|
||||
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.
|
||||
|
||||
The program checks that a SX127X 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, SX1272, SX1276,
|
||||
SX1277, SX1278 or SX1279 must be specified also.
|
||||
|
||||
Typical printout;
|
||||
|
||||
2_Register_Test Starting
|
||||
SX1276-79 Selected
|
||||
LoRa Device found
|
||||
Device version 0x12
|
||||
|
||||
Frequency at reset 434000000
|
||||
Registers at reset
|
||||
Reg 0 1 2 3 4 5 6 7 8 9 A B C D E F
|
||||
0x00 00 09 1A 0B 00 52 6C 80 00 4F 09 2B 20 08 02 0A
|
||||
0x10 FF 6F 15 0B 28 0C 12 47 32 3E 00 00 00 00 00 40
|
||||
0x20 00 00 00 00 05 00 03 93 55 55 55 55 55 55 55 55
|
||||
0x30 90 40 40 00 00 0F 00 00 00 F5 20 82 00 02 80 40
|
||||
0x40 00 00 12 24 2D 00 03 00 04 23 00 09 05 84 32 2B
|
||||
0x50 14 00 00 12 00 00 00 0F E0 00 0C 00 08 00 5C 78
|
||||
0x60 00 19 0C 4B CC 0F 01 20 04 47 AF 3F CF 00 53 0B
|
||||
0x70 D0 01 10 00 00 00 00 00 00 00 00 00 00 00 00 00
|
||||
|
||||
Changed Frequency 434099968
|
||||
Reg 0 1 2 3 4 5 6 7 8 9 A B C D E F
|
||||
0x00 00 09 1A 0B 00 52 6C 86 66 4F 09 2B 20 08 02 0A
|
||||
0x10 FF 6F 15 0B 28 0C 12 47 32 3E 00 00 00 00 00 40
|
||||
0x20 00 00 00 00 05 00 03 93 55 55 55 55 55 55 55 55
|
||||
0x30 90 40 40 00 00 0F 00 00 00 F5 20 82 00 02 80 40
|
||||
0x40 00 00 12 24 2D 00 03 00 04 23 00 09 05 84 32 2B
|
||||
0x50 14 00 00 12 00 00 00 0F E0 00 0C 00 08 00 5C 78
|
||||
0x60 00 19 0C 4B CC 0F 01 20 04 47 AF 3F CF 00 53 0B
|
||||
0x70 D0 01 10 00 00 00 00 00 00 00 00 00 00 00 00 00
|
||||
|
||||
Serial monitor baud rate is set at 9600.
|
||||
*******************************************************************************************************/
|
||||
|
||||
const uint8_t REG_FRMSB = 0x06; //register number for setting setting and reading frequency, high byte
|
||||
const uint8_t REG_FRMID = 0x07; //register number for setting setting and reading frequency, mid byte
|
||||
const uint8_t REG_FRLSB = 0x08; //register number for setting setting and reading frequency, low byte
|
||||
const uint8_t REG_VERSION = 0x42; //register containg version number of device
|
||||
|
||||
const uint8_t DEVICE_SX1272 = 0x10; //SX1272
|
||||
const uint8_t DEVICE_SX1276 = 0x11; //SX1276
|
||||
const uint8_t DEVICE_SX1277 = 0x12; //SX1277
|
||||
const uint8_t DEVICE_SX1278 = 0x13; //SX1278
|
||||
const uint8_t DEVICE_SX1279 = 0x14; //SX1279
|
||||
|
||||
//********* 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, which on an Arduino Pro Mini are
|
||||
//SCK pin 13, MISO pin 12, and MOSI pin 11.
|
||||
|
||||
#define NSS 10 //SX127X device select
|
||||
#define NRESET 9 //SX127X reset pin
|
||||
#define DIO0 -1 //DIO0 pin on LoRa device, not used here so set to -1
|
||||
#define DIO1 -1 //DIO1 pin on LoRa device, normally not used so set to -1
|
||||
#define DIO2 -1 //DIO2 pin on LoRa device, normally not used so set to -1
|
||||
|
||||
#define LORA_DEVICE DEVICE_SX1278 //defines the type of LoRa device used, needed for correct program operation
|
||||
|
||||
//**************************************************************/
|
||||
|
||||
|
||||
#include <SPI.h>
|
||||
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(9600);
|
||||
Serial.println(F("2_Register_Test_STM32 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 DIO0, DIO1 and DIO2 pins are not used in this example so are set to -1
|
||||
//the LT.begin fuction can define the pins and device type directly in this way (for SX1278);
|
||||
//LT.begin(10, 9, -1, -1, -1, DEVICE_SX1278)
|
||||
|
||||
if (begin(NSS, NRESET, -1, -1, -1, LORA_DEVICE))
|
||||
{
|
||||
Serial.println(F("LoRa Device found"));
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.println(F("No device responding"));
|
||||
}
|
||||
|
||||
Serial.print(F("Device version 0x"));
|
||||
uint8_t deviceversion = readRegister(REG_VERSION);
|
||||
if (deviceversion < 0x10)
|
||||
{
|
||||
Serial.print(F("0"));
|
||||
}
|
||||
Serial.println(deviceversion, HEX);
|
||||
}
|
||||
|
||||
|
||||
void loop()
|
||||
{
|
||||
uint32_t frequency;
|
||||
|
||||
frequency = getFreqInt(); //read the set frequency following a reset
|
||||
Serial.print(F("Frequency at reset "));
|
||||
Serial.println(frequency);
|
||||
|
||||
Serial.println(F("Registers at reset")); //show the all registers following a reset
|
||||
printRegisters(0x00, 0x7F);
|
||||
|
||||
Serial.println();
|
||||
Serial.println();
|
||||
|
||||
setRfFrequency(434100000, 0); //change the frequency at reset, in hertz
|
||||
frequency = getFreqInt(); //read back the changed frequency
|
||||
Serial.print(F("Changed Frequency "));
|
||||
Serial.println(frequency); //print the changed frequency, did the write work (allow for rounding errors) ?
|
||||
printRegisters(0x00, 0x7F); //show the registers after frequency change
|
||||
Serial.println();
|
||||
delay(5000);
|
||||
resetDevice(LORA_DEVICE); //reset the device and start again
|
||||
}
|
||||
|
||||
|
||||
uint8_t readRegister(uint8_t address)
|
||||
{
|
||||
uint8_t regdata;
|
||||
digitalWrite(NSS, LOW); //set NSS low
|
||||
SPI.transfer(address & 0x7F); //mask address for read
|
||||
regdata = SPI.transfer(0); //read the byte
|
||||
digitalWrite(NSS, HIGH); //set NSS high
|
||||
return regdata;
|
||||
}
|
||||
|
||||
|
||||
void writeRegister(uint8_t address, uint8_t value)
|
||||
{
|
||||
digitalWrite(NSS, LOW); //set NSS low
|
||||
SPI.transfer(address | 0x80); //mask address for write
|
||||
SPI.transfer(value); //write the byte
|
||||
digitalWrite(NSS, HIGH); //set NSS high
|
||||
}
|
||||
|
||||
|
||||
uint32_t getFreqInt()
|
||||
{
|
||||
//get the current set LoRa device frequency, return as long integer
|
||||
|
||||
uint8_t Msb, Mid, Lsb;
|
||||
uint32_t uinttemp;
|
||||
float floattemp;
|
||||
Msb = readRegister(REG_FRMSB);
|
||||
Mid = readRegister(REG_FRMID);
|
||||
Lsb = readRegister(REG_FRLSB);
|
||||
floattemp = ((Msb * 0x10000ul) + (Mid * 0x100ul) + Lsb);
|
||||
floattemp = ((floattemp * 61.03515625) / 1000000ul);
|
||||
uinttemp = (uint32_t)(floattemp * 1000000);
|
||||
return uinttemp;
|
||||
}
|
||||
|
||||
|
||||
void printRegisters(uint16_t Start, uint16_t End)
|
||||
{
|
||||
//prints the contents of SX127x 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;)
|
||||
{
|
||||
Serial.print(F("0x"));
|
||||
if (Loopv1 < 0x10)
|
||||
{
|
||||
Serial.print(F("0"));
|
||||
}
|
||||
Serial.print((Loopv1), HEX);
|
||||
Serial.print(F(" "));
|
||||
for (Loopv2 = 0; Loopv2 <= 15; Loopv2++)
|
||||
{
|
||||
RegData = readRegister(Loopv1);
|
||||
if (RegData < 0x10)
|
||||
{
|
||||
Serial.print(F("0"));
|
||||
}
|
||||
Serial.print(RegData, HEX);
|
||||
Serial.print(F(" "));
|
||||
Loopv1++;
|
||||
}
|
||||
Serial.println();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void setRfFrequency(uint64_t freq64, int32_t offset)
|
||||
{
|
||||
freq64 = freq64 + offset;
|
||||
freq64 = ((uint64_t)freq64 << 19) / 32000000;
|
||||
writeRegister(REG_FRMSB, (uint8_t)(freq64 >> 16));
|
||||
writeRegister(REG_FRMID, (uint8_t)(freq64 >> 8));
|
||||
writeRegister(REG_FRLSB, (uint8_t)(freq64 >> 0));
|
||||
}
|
||||
|
||||
|
||||
void resetDevice(uint8_t device)
|
||||
{
|
||||
if (device == DEVICE_SX1272)
|
||||
{
|
||||
digitalWrite(NRESET, HIGH);
|
||||
delay(2);
|
||||
digitalWrite(NRESET, LOW);
|
||||
delay(20);
|
||||
Serial.println(F("SX1272 Selected"));
|
||||
}
|
||||
else
|
||||
{
|
||||
digitalWrite(NRESET, LOW);
|
||||
delay(2);
|
||||
digitalWrite(NRESET, HIGH);
|
||||
delay(20);
|
||||
Serial.println(F("SX1276-79 Selected"));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool begin(int8_t pinNSS, int8_t pinNRESET, int8_t pinDIO0, int8_t pinDIO1, int8_t pinDIO2, uint8_t device)
|
||||
{
|
||||
pinMode(pinNSS, OUTPUT);
|
||||
digitalWrite(pinNSS, HIGH);
|
||||
pinMode(pinNRESET, OUTPUT);
|
||||
digitalWrite(pinNRESET, LOW);
|
||||
|
||||
if (pinDIO0 >= 0)
|
||||
{
|
||||
pinMode( pinDIO0, INPUT);
|
||||
}
|
||||
|
||||
if (pinDIO1 >= 0)
|
||||
{
|
||||
pinMode( pinDIO1, INPUT);
|
||||
}
|
||||
|
||||
if (pinDIO2 >= 0)
|
||||
{
|
||||
pinMode( pinDIO2, INPUT);
|
||||
}
|
||||
|
||||
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(REG_FRMID); //low byte of frequency setting
|
||||
writeRegister(REG_FRMID, (Regdata1 + 1));
|
||||
Regdata2 = readRegister(REG_FRMID); //read changed value back
|
||||
writeRegister(REG_FRMID, Regdata1); //restore register to original value
|
||||
|
||||
if (Regdata2 == (Regdata1 + 1))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,182 @@
|
||||
/*******************************************************************************************************
|
||||
Programs for Arduino - Copyright of the author Stuart Robinson - 16/12/19
|
||||
|
||||
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 simple LoRa test transmitter. A packet containing ASCII text is sent
|
||||
according to the frequency and LoRa settings specified in the 'Settings.h' file. The pins to access
|
||||
the SX127X need to be defined in the 'Settings.h' file also.
|
||||
|
||||
The details of the packet sent and any errors are shown on the Serial Monitor, together with the transmit
|
||||
power used, the packet length and the CRC of the packet. The matching receive program, '4_LoRa_Receive'
|
||||
can be used to check the packets are being sent correctly, the frequency and LoRa settings (in Settings.h)
|
||||
must be the same for the Transmit and Receive program. Sample Serial Monitor output;
|
||||
|
||||
10dBm Packet> {packet contents*} BytesSent,23 CRC,DAAB TransmitTime,54mS PacketsSent,1
|
||||
|
||||
Serial monitor baud rate is set at 9600
|
||||
*******************************************************************************************************/
|
||||
|
||||
#define Program_Version "V1.0"
|
||||
|
||||
#include <SPI.h> //the SX127X device is SPI based so load the SPI library
|
||||
#include <SX127XLT.h> //include the appropriate library
|
||||
#include "Settings.h" //include the setiings file, frequencies, LoRa settings etc
|
||||
|
||||
SX127XLT LT; //create a library class instance called LT
|
||||
|
||||
uint8_t TXPacketL;
|
||||
uint32_t TXPacketCount, startmS, endmS;
|
||||
|
||||
uint8_t buff[] = "Hello World 1234567890";
|
||||
|
||||
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 reciver
|
||||
|
||||
LT.printASCIIPacket(buff, TXPacketL); //print the buffer (the sent packet) as ASCII
|
||||
|
||||
digitalWrite(LED1, HIGH);
|
||||
startmS = millis(); //start transmit timer
|
||||
if (LT.transmit(buff, TXPacketL, 10000, TXpower, WAIT_TX)) //will return packet length sent if OK, otherwise 0 if transmit error
|
||||
{
|
||||
endmS = millis(); //packet sent, note end time
|
||||
TXPacketCount++;
|
||||
packet_is_OK();
|
||||
}
|
||||
else
|
||||
{
|
||||
packet_is_Error(); //transmit packet returned 0, there was an error
|
||||
}
|
||||
|
||||
digitalWrite(LED1, LOW);
|
||||
Serial.println();
|
||||
delay(packet_delay); //have a delay between packets
|
||||
}
|
||||
|
||||
|
||||
void packet_is_OK()
|
||||
{
|
||||
//if here packet has been sent OK
|
||||
uint16_t localCRC;
|
||||
|
||||
Serial.print(F(" BytesSent,"));
|
||||
Serial.print(TXPacketL); //print transmitted packet length
|
||||
localCRC = LT.CRCCCITT(buff, TXPacketL, 0xFFFF);
|
||||
Serial.print(F(" CRC,"));
|
||||
Serial.print(localCRC, HEX); //print CRC of sent packet
|
||||
Serial.print(F(" TransmitTime,"));
|
||||
Serial.print(endmS - startmS); //print transmit time of packet
|
||||
Serial.print(F("mS"));
|
||||
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
|
||||
}
|
||||
|
||||
|
||||
void led_Flash(uint16_t flashes, uint16_t delaymS)
|
||||
{
|
||||
uint16_t index;
|
||||
for (index = 1; index <= flashes; index++)
|
||||
{
|
||||
digitalWrite(LED1, HIGH);
|
||||
delay(delaymS);
|
||||
digitalWrite(LED1, LOW);
|
||||
delay(delaymS);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void setup()
|
||||
{
|
||||
pinMode(LED1, OUTPUT); //setup pin as output for indicator LED
|
||||
led_Flash(2, 125); //two quick LED flashes to indicate program start
|
||||
|
||||
Serial.begin(9600);
|
||||
Serial.println();
|
||||
Serial.print(F(__TIME__));
|
||||
Serial.print(F(" "));
|
||||
Serial.println(F(__DATE__));
|
||||
Serial.println(F(Program_Version));
|
||||
Serial.println();
|
||||
Serial.println(F("3_LoRa_Transmitter_STM32 Starting"));
|
||||
|
||||
SPI.begin();
|
||||
|
||||
//SPI beginTranscation is normally part of library routines, but if it is disabled in library
|
||||
//a single instance is needed here, so uncomment the program line below
|
||||
//SPI.beginTransaction(SPISettings(8000000, MSBFIRST, SPI_MODE0));
|
||||
|
||||
//setup hardware pins used by device, then check if device is found
|
||||
if (LT.begin(NSS, NRESET, DIO0, DIO1, DIO2, LORA_DEVICE))
|
||||
{
|
||||
Serial.println(F("LoRa Device found"));
|
||||
led_Flash(2, 125); //two further quick LED flashes to indicate device found
|
||||
delay(1000);
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.println(F("No device responding"));
|
||||
while (1)
|
||||
{
|
||||
led_Flash(50, 50); //long fast speed LED flash indicates device error
|
||||
}
|
||||
}
|
||||
|
||||
//The function call list below shows the complete setup for the LoRa device using the information defined in the
|
||||
//Settings.h file.
|
||||
//The 'Setup LoRa device' list below can be replaced with a single function call;
|
||||
//LT.setupLoRa(Frequency, Offset, SpreadingFactor, Bandwidth, CodeRate, Optimisation);
|
||||
|
||||
//***************************************************************************************************
|
||||
//Setup LoRa device
|
||||
//***************************************************************************************************
|
||||
LT.setMode(MODE_STDBY_RC); //got to standby mode to configure device
|
||||
LT.setPacketType(PACKET_TYPE_LORA); //set for LoRa transmissions
|
||||
LT.setRfFrequency(Frequency, Offset); //set the operating frequency
|
||||
LT.calibrateImage(0); //run calibration after setting frequency
|
||||
LT.setModulationParams(SpreadingFactor, Bandwidth, CodeRate, LDRO_AUTO); //set LoRa modem parameters
|
||||
LT.setBufferBaseAddress(0x00, 0x00); //where in the SX buffer packets start, TX and RX
|
||||
LT.setPacketParams(8, LORA_PACKET_VARIABLE_LENGTH, 255, LORA_CRC_ON, LORA_IQ_NORMAL); //set packet parameters
|
||||
LT.setSyncWord(LORA_MAC_PRIVATE_SYNCWORD); //syncword, LORA_MAC_PRIVATE_SYNCWORD = 0x12, or LORA_MAC_PUBLIC_SYNCWORD = 0x34
|
||||
LT.setHighSensitivity(); //set for highest sensitivity at expense of slightly higher LNA current
|
||||
LT.setDioIrqParams(IRQ_RADIO_ALL, IRQ_TX_DONE, 0, 0); //set for IRQ on RX done
|
||||
//***************************************************************************************************
|
||||
|
||||
Serial.println();
|
||||
LT.printModemSettings(); //reads and prints the configured LoRa settings, useful check
|
||||
Serial.println();
|
||||
LT.printOperatingSettings(); //reads and prints the configured operating settings, useful check
|
||||
Serial.println();
|
||||
Serial.println();
|
||||
LT.printRegisters(0x00, 0x4F); //print contents of device registers, normally 0x00 to 0x4F
|
||||
Serial.println();
|
||||
Serial.println();
|
||||
|
||||
Serial.print(F("Transmitter ready"));
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
/*******************************************************************************************************
|
||||
Programs for Arduino - Copyright of the author Stuart Robinson - 16/12/19
|
||||
|
||||
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.
|
||||
*******************************************************************************************************/
|
||||
|
||||
//******* Setup hardware pin definitions here ! ***************
|
||||
|
||||
//These are the pin definitions for one of my own boards, the Easy Pro Mini,
|
||||
//be sure to change the definitions to match your own setup. Some pins such as DIO1,
|
||||
//DIO2, BUZZER may not be in used by this sketch so they do not need to be
|
||||
//connected and should be included and be set to -1.
|
||||
|
||||
#define NSS 10 //select pin on LoRa device
|
||||
#define NRESET 9 //reset pin on LoRa device
|
||||
#define LED1 8 //on board LED, high for on
|
||||
#define DIO0 3 //DIO0 pin on LoRa device, used for RX and TX done
|
||||
#define DIO1 -1 //DIO1 pin on LoRa device, normally not used so set to -1
|
||||
#define DIO2 -1 //DIO2 pin on LoRa device, normally not used so set to -1
|
||||
|
||||
#define LORA_DEVICE DEVICE_SX1278 //we need to define the device we are using
|
||||
|
||||
|
||||
//******* Setup LoRa Parameters Here ! ***************
|
||||
|
||||
//LoRa Modem Parameters
|
||||
const uint32_t Frequency = 434000000; //frequency of transmissions in hertz
|
||||
const uint32_t Offset = 0; //offset frequency for calibration purposes
|
||||
|
||||
const uint8_t Bandwidth = LORA_BW_125; //LoRa bandwidth
|
||||
const uint8_t SpreadingFactor = LORA_SF7; //LoRa spreading factor
|
||||
const uint8_t CodeRate = LORA_CR_4_5; //LoRa coding rate
|
||||
const uint8_t Optimisation = LDRO_AUTO; //low data rate optimisation setting, normally set to auto
|
||||
|
||||
const int8_t TXpower = 10; //LoRa transmit power in dBm
|
||||
|
||||
const uint16_t packet_delay = 1000; //mS delay between packets
|
||||
|
||||
@@ -0,0 +1,247 @@
|
||||
/*******************************************************************************************************
|
||||
Programs for Arduino - Copyright of the author Stuart Robinson - 16/12/19
|
||||
|
||||
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 - The program listens for incoming packets using the LoRa settings in the 'Settings.h'
|
||||
file. The pins to access the SX127X need to be defined in the 'Settings.h' file also.
|
||||
|
||||
There is a printout of the valid packets received, the packet is assumed to be in ASCII printable text,
|
||||
if its not ASCII text characters from 0x20 to 0x7F, expect weird things to happen on the Serial Monitor.
|
||||
The LED will flash for each packet received and the buzzer will sound, if fitted.
|
||||
|
||||
Sample serial monitor output;
|
||||
|
||||
1109s Hello World 1234567890*,CRC,DAAB,RSSI,-61dBm,SNR,9dB,Length,23,Packets,1026,Errors,0,IRQreg,50
|
||||
|
||||
If there is a packet error it might look like this, which is showing a CRC error,
|
||||
|
||||
1189s PacketError,RSSI,-111dBm,SNR,-12dB,Length,0,Packets,1126,Errors,1,IRQreg,70,IRQ_HEADER_VALID,IRQ_CRC_ERROR,IRQ_RX_DONE
|
||||
|
||||
Serial monitor baud rate is set at 9600.
|
||||
*******************************************************************************************************/
|
||||
|
||||
#define Program_Version "V1.0"
|
||||
|
||||
#include <SPI.h> //the SX127X device is SPI based so load the SPI library
|
||||
#include <SX127XLT.h> //include the appropriate library
|
||||
#include "Settings.h" //include the setiings file, frequencies, LoRa settings etc
|
||||
|
||||
SX127XLT LT; //create a library class instance called LT
|
||||
|
||||
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 of received packet
|
||||
|
||||
|
||||
void loop()
|
||||
{
|
||||
RXPacketL = LT.receive(RXBUFFER, RXBUFFER_SIZE, 60000, WAIT_RX); //wait for a packet to arrive with 60seconds (60000mS) timeout
|
||||
|
||||
digitalWrite(LED1, HIGH); //something has happened
|
||||
|
||||
if (BUZZER > 0) //turn buzzer on
|
||||
{
|
||||
digitalWrite(BUZZER, HIGH);
|
||||
}
|
||||
|
||||
PacketRSSI = LT.readPacketRSSI(); //read the recived RSSI value
|
||||
PacketSNR = LT.readPacketSNR(); //read the received SNR value
|
||||
|
||||
if (RXPacketL == 0) //if the LT.receive() function detects an error, RXpacketL == 0
|
||||
{
|
||||
packet_is_Error();
|
||||
}
|
||||
else
|
||||
{
|
||||
packet_is_OK();
|
||||
}
|
||||
|
||||
if (BUZZER > 0)
|
||||
{
|
||||
digitalWrite(BUZZER, LOW); //buzzer off
|
||||
}
|
||||
|
||||
digitalWrite(LED1, LOW); //LED off
|
||||
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
|
||||
void packet_is_OK()
|
||||
{
|
||||
uint16_t IRQStatus, localCRC;
|
||||
|
||||
IRQStatus = LT.readIrqStatus(); //read the LoRa device IRQ status register
|
||||
|
||||
RXpacketCount++;
|
||||
|
||||
printElapsedTime(); //print elapsed time to Serial Monitor
|
||||
Serial.print(F(" "));
|
||||
LT.printASCIIPacket(RXBUFFER, RXPacketL); //print the packet as ASCII characters
|
||||
|
||||
localCRC = LT.CRCCCITT(RXBUFFER, RXPacketL, 0xFFFF); //calculate the CRC, this is the external CRC calculation of the RXBUFFER
|
||||
Serial.print(F(",CRC,")); //contents, not the LoRa device internal CRC
|
||||
Serial.print(localCRC, HEX);
|
||||
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
|
||||
}
|
||||
|
||||
delay(250); //gives a longer buzzer and LED flash for error
|
||||
|
||||
}
|
||||
|
||||
|
||||
void printElapsedTime()
|
||||
{
|
||||
float seconds;
|
||||
seconds = millis() / 1000;
|
||||
Serial.print(seconds, 0);
|
||||
Serial.print(F("s"));
|
||||
}
|
||||
|
||||
|
||||
void led_Flash(uint16_t flashes, uint16_t delaymS)
|
||||
{
|
||||
uint16_t index;
|
||||
|
||||
for (index = 1; index <= flashes; index++)
|
||||
{
|
||||
digitalWrite(LED1, HIGH);
|
||||
delay(delaymS);
|
||||
digitalWrite(LED1, LOW);
|
||||
delay(delaymS);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void setup()
|
||||
{
|
||||
pinMode(LED1, OUTPUT); //setup pin as output for indicator LED
|
||||
led_Flash(2, 125); //two quick LED flashes to indicate program start
|
||||
|
||||
Serial.begin(9600);
|
||||
Serial.println();
|
||||
Serial.print(F(__TIME__));
|
||||
Serial.print(F(" "));
|
||||
Serial.println(F(__DATE__));
|
||||
Serial.println(F(Program_Version));
|
||||
Serial.println();
|
||||
Serial.println(F("4_LoRa_Receiver Starting"));
|
||||
Serial.println();
|
||||
|
||||
if (BUZZER > 0)
|
||||
{
|
||||
pinMode(BUZZER, OUTPUT);
|
||||
digitalWrite(BUZZER, HIGH);
|
||||
delay(50);
|
||||
digitalWrite(BUZZER, LOW);
|
||||
}
|
||||
|
||||
SPI.begin();
|
||||
|
||||
//SPI beginTranscation is normally part of library routines, but if it is disabled in the library
|
||||
//a single instance is needed here, so uncomment the program line below
|
||||
//SPI.beginTransaction(SPISettings(8000000, MSBFIRST, SPI_MODE0));
|
||||
|
||||
//setup hardware pins used by device, then check if device is found
|
||||
if (LT.begin(NSS, NRESET, DIO0, DIO1, DIO2, LORA_DEVICE))
|
||||
{
|
||||
Serial.println(F("LoRa Device found"));
|
||||
led_Flash(2, 125);
|
||||
delay(1000);
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.println(F("No device responding"));
|
||||
while (1)
|
||||
{
|
||||
led_Flash(50, 50); //long fast speed LED flash indicates device error
|
||||
}
|
||||
}
|
||||
|
||||
//The function call list below shows the complete setup for the LoRa device using the information defined in the
|
||||
//Settings.h file.
|
||||
//The 'Setup LoRa device' list below can be replaced with a single function call;
|
||||
//LT.setupLoRa(Frequency, Offset, SpreadingFactor, Bandwidth, CodeRate, Optimisation);
|
||||
|
||||
//***************************************************************************************************
|
||||
//Setup LoRa device
|
||||
//***************************************************************************************************
|
||||
LT.setMode(MODE_STDBY_RC); //got to standby mode to configure device
|
||||
LT.setPacketType(PACKET_TYPE_LORA); //set for LoRa transmissions
|
||||
LT.setRfFrequency(Frequency, Offset); //set the operating frequency
|
||||
LT.calibrateImage(0); //run calibration after setting frequency
|
||||
LT.setModulationParams(SpreadingFactor, Bandwidth, CodeRate, LDRO_AUTO); //set LoRa modem parameters
|
||||
LT.setBufferBaseAddress(0x00, 0x00); //where in the SX buffer packets start, TX and RX
|
||||
LT.setPacketParams(8, LORA_PACKET_VARIABLE_LENGTH, 255, LORA_CRC_ON, LORA_IQ_NORMAL); //set packet parameters
|
||||
LT.setSyncWord(LORA_MAC_PRIVATE_SYNCWORD); //syncword, LORA_MAC_PRIVATE_SYNCWORD = 0x12, or LORA_MAC_PUBLIC_SYNCWORD = 0x34
|
||||
LT.setHighSensitivity(); //set for highest sensitivity at expense of slightly higher LNA current
|
||||
LT.setDioIrqParams(IRQ_RADIO_ALL, IRQ_RX_DONE, 0, 0); //set for IRQ on RX done
|
||||
//***************************************************************************************************
|
||||
|
||||
|
||||
Serial.println();
|
||||
LT.printModemSettings(); //reads and prints the configured LoRa settings, useful check
|
||||
Serial.println();
|
||||
LT.printOperatingSettings(); //reads and prints the configured operting settings, useful check
|
||||
Serial.println();
|
||||
Serial.println();
|
||||
LT.printRegisters(0x00, 0x4F); //print contents of device registers, normally 0x00 to 0x4F
|
||||
Serial.println();
|
||||
Serial.println();
|
||||
|
||||
Serial.print(F("Receiver ready - RXBUFFER_SIZE "));
|
||||
Serial.println(RXBUFFER_SIZE);
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
/*******************************************************************************************************
|
||||
Programs for Arduino - Copyright of the author Stuart Robinson - 16/12/19
|
||||
|
||||
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.
|
||||
*******************************************************************************************************/
|
||||
|
||||
//******* Setup hardware pin definitions here ! ***************
|
||||
|
||||
//These are the pin definitions for one of my own boards, the Easy Pro Mini,
|
||||
//be sure to change the definitions to match your own setup. Some pins such as DIO1,
|
||||
//DIO2, BUZZER may not be in used by this sketch so they do not need to be
|
||||
//connected and should be included and be set to -1.
|
||||
|
||||
#define NSS 10 //select pin on LoRa device
|
||||
#define NRESET 9 //reset pin on LoRa device
|
||||
#define LED1 8 //on board LED, high for on
|
||||
#define DIO0 3 //DIO0 pin on LoRa device, used for RX and TX done
|
||||
#define DIO1 -1 //DIO1 pin on LoRa device, normally not used so set to -1
|
||||
#define DIO2 -1 //DIO2 pin on LoRa device, normally not used so set to -1
|
||||
#define BUZZER 4 //pin for buzzer, on when logic high
|
||||
|
||||
#define LORA_DEVICE DEVICE_SX1278 //we need to define the device we are using
|
||||
|
||||
|
||||
//******* Setup LoRa Parameters Here ! ***************
|
||||
|
||||
//LoRa Modem Parameters
|
||||
const uint32_t Frequency = 434000000; //frequency of transmissions in hertz
|
||||
const uint32_t Offset = 0; //offset frequency for calibration purposes
|
||||
|
||||
const uint8_t Bandwidth = LORA_BW_125; //LoRa bandwidth
|
||||
const uint8_t SpreadingFactor = LORA_SF7; //LoRa spreading factor
|
||||
const uint8_t CodeRate = LORA_CR_4_5; //LoRa coding rate
|
||||
const uint8_t Optimisation = LDRO_AUTO; //low data rate optimisation setting, normally set to auto
|
||||
|
||||
const int8_t TXpower = 2; //LoRa transmit power in dBm
|
||||
|
||||
const uint16_t packet_delay = 1000; //mS delay between packets
|
||||
|
||||
#define RXBUFFER_SIZE 32 //RX buffer size
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user