first commit
This commit is contained in:
@@ -0,0 +1,242 @@
|
||||
/*******************************************************************************************************
|
||||
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 program tests the sleep mode and register retention of the lora device in sleep
|
||||
mode, it assumes an Atmel ATMega328P processor is in use. The LoRa settings to use are specified in the
|
||||
'Settings.h' file.
|
||||
|
||||
A packet is sent, containing the text 'Before Device Sleep' and the LoRa device and Atmel processor are put
|
||||
to sleep. The processor watchdog timer should wakeup the processor in 15 seconds (approx) and register
|
||||
values should be retained. The device then attempts to transmit another packet 'After Device Sleep'
|
||||
without re-loading all the LoRa settings. The receiver should see 'After Device Sleep' for the first
|
||||
packet and 'After Device Sleep' for the second.
|
||||
|
||||
Tested on a 'bare bones' ATmega328P board, the current in sleep mode was 6.5uA.
|
||||
|
||||
Serial monitor baud rate is set at 9600.
|
||||
*******************************************************************************************************/
|
||||
|
||||
#define Program_Version "V1.0"
|
||||
|
||||
#include <avr/wdt.h> //watchdog timer library, integral to Arduino IDE
|
||||
#include <SPI.h>
|
||||
#include <LowPower.h> //get the library here; https://github.com/rocketscream/Low-Power
|
||||
|
||||
#include <SX127XLT.h>
|
||||
#include "Settings.h"
|
||||
|
||||
SX127XLT LT;
|
||||
|
||||
bool SendOK;
|
||||
int8_t TestPower;
|
||||
uint8_t TXPacketL;
|
||||
|
||||
|
||||
void loop()
|
||||
{
|
||||
digitalWrite(LED1, HIGH);
|
||||
Serial.print(TXpower);
|
||||
Serial.print(F("dBm "));
|
||||
Serial.print(F("TestPacket1> "));
|
||||
Serial.flush();
|
||||
|
||||
if (Send_Test_Packet1())
|
||||
{
|
||||
packet_is_OK();
|
||||
}
|
||||
else
|
||||
{
|
||||
packet_is_Error();
|
||||
}
|
||||
Serial.println();
|
||||
delay(packet_delay);
|
||||
|
||||
LT.setSleep(CONFIGURATION_RETENTION); //preserve register settings in sleep.
|
||||
Serial.println(F("Sleeping zzzzz...."));
|
||||
Serial.println();
|
||||
Serial.flush();
|
||||
digitalWrite(LED1, LOW);
|
||||
|
||||
sleep1second(15); //goto sleep for 15 seconds
|
||||
|
||||
Serial.println(F("Awake !"));
|
||||
Serial.flush();
|
||||
digitalWrite(LED1, HIGH);
|
||||
LT.wake();
|
||||
|
||||
Serial.print(TXpower);
|
||||
Serial.print(F("dBm "));
|
||||
Serial.print(F("TestPacket2> "));
|
||||
Serial.flush();
|
||||
|
||||
if (Send_Test_Packet2())
|
||||
{
|
||||
packet_is_OK();
|
||||
}
|
||||
else
|
||||
{
|
||||
packet_is_Error();
|
||||
}
|
||||
Serial.println();
|
||||
delay(packet_delay);
|
||||
}
|
||||
|
||||
|
||||
void sleep1second(uint32_t sleeps)
|
||||
{
|
||||
//uses the lowpower library
|
||||
uint32_t index;
|
||||
|
||||
for (index = 1; index <= sleeps; index++)
|
||||
{
|
||||
LowPower.powerDown(SLEEP_1S, ADC_OFF, BOD_OFF); //sleep in 1 second steps
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void packet_is_OK()
|
||||
{
|
||||
Serial.print(F(" "));
|
||||
Serial.print(TXPacketL);
|
||||
Serial.print(F(" Bytes SentOK"));
|
||||
}
|
||||
|
||||
|
||||
void packet_is_Error()
|
||||
{
|
||||
uint16_t IRQStatus;
|
||||
IRQStatus = LT.readIrqStatus(); //get the IRQ status
|
||||
Serial.print(F("SendError,"));
|
||||
Serial.print(F("Length,"));
|
||||
Serial.print(TXPacketL);
|
||||
Serial.print(F(",IRQreg,"));
|
||||
Serial.print(IRQStatus, HEX);
|
||||
LT.printIrqStatus();
|
||||
digitalWrite(LED1, LOW); //this leaves the LED on slightly longer for a packet error
|
||||
}
|
||||
|
||||
|
||||
bool Send_Test_Packet1()
|
||||
{
|
||||
uint8_t bufffersize;
|
||||
|
||||
uint8_t buff[] = "Before Device Sleep";
|
||||
TXPacketL = sizeof(buff);
|
||||
buff[TXPacketL - 1] = '*';
|
||||
|
||||
if (sizeof(buff) > TXBUFFER_SIZE) //check that defined buffer is not larger than TX_BUFFER
|
||||
{
|
||||
bufffersize = TXBUFFER_SIZE;
|
||||
}
|
||||
else
|
||||
{
|
||||
bufffersize = sizeof(buff);
|
||||
}
|
||||
|
||||
TXPacketL = bufffersize;
|
||||
|
||||
LT.printASCIIPacket( (uint8_t*) buff, bufffersize);
|
||||
digitalWrite(LED1, HIGH);
|
||||
|
||||
if (LT.transmit( (uint8_t*) buff, TXPacketL, 10000, TXpower, WAIT_TX))
|
||||
{
|
||||
digitalWrite(LED1, LOW);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool Send_Test_Packet2()
|
||||
{
|
||||
uint8_t bufffersize;
|
||||
|
||||
uint8_t buff[] = "After Device Sleep";
|
||||
TXPacketL = sizeof(buff);
|
||||
buff[TXPacketL - 1] = '*';
|
||||
|
||||
if (sizeof(buff) > TXBUFFER_SIZE) //check that defined buffer is not larger than TX_BUFFER
|
||||
{
|
||||
bufffersize = TXBUFFER_SIZE;
|
||||
}
|
||||
else
|
||||
{
|
||||
bufffersize = sizeof(buff);
|
||||
}
|
||||
|
||||
TXPacketL = bufffersize;
|
||||
|
||||
LT.printASCIIPacket( (uint8_t*) buff, bufffersize);
|
||||
digitalWrite(LED1, HIGH);
|
||||
|
||||
if (LT.transmit( (uint8_t*) buff, TXPacketL, 10000, TXpower, WAIT_TX))
|
||||
{
|
||||
digitalWrite(LED1, LOW);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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(__TIME__);
|
||||
Serial.print(F(" "));
|
||||
Serial.println(__DATE__);
|
||||
Serial.println(F(Program_Version));
|
||||
Serial.println();
|
||||
Serial.println(F("5_LoRa_TX_Sleep_Timed_Wakeup_Atmel Starting"));
|
||||
|
||||
SPI.begin();
|
||||
|
||||
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 flash indicates device error
|
||||
}
|
||||
}
|
||||
|
||||
LT.setupLoRa(Frequency, Offset, SpreadingFactor, Bandwidth, CodeRate, Optimisation);
|
||||
|
||||
Serial.print(F("Transmitter ready - TXBUFFER_SIZE "));
|
||||
Serial.println(TXBUFFER_SIZE);
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
/*******************************************************************************************************
|
||||
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 = 10; //LoRa transmit power in dBm
|
||||
|
||||
const uint16_t packet_delay = 1000; //mS delay between packets
|
||||
|
||||
#define TXBUFFER_SIZE 32 //RX buffer size
|
||||
|
||||
|
||||
@@ -0,0 +1,224 @@
|
||||
/*******************************************************************************************************
|
||||
Programs for Arduino - Copyright of the author Stuart Robinson - 19/06/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 - The program listens for incoming packets using the LoRa settings in the 'Settings.h'
|
||||
file. The pins to access the lora device need to be defined in the 'Settings.h' file also.
|
||||
|
||||
When the program starts the LoRa device is setup to recieve packets with pin DIO0 set to go high when a
|
||||
packet arrives. The receiver remains powered (it cannot receive otherwise) and the processor
|
||||
(Atmel ATMega328P or 1284P) is put to sleep. When pin DIO0 does go high, indicating a packet is received,
|
||||
the processor wakes up and prints the packet. It then goes back to sleep.
|
||||
|
||||
There is a printout of the valid packets received, these are assumed to be in ASCII printable text.
|
||||
The LED will flash for each packet received and the buzzer will sound,if fitted.
|
||||
|
||||
Tested on a 'bare bones' ATmega328P board, the current in sleep mode was 13.07mA.
|
||||
|
||||
Serial monitor baud rate is set at 9600.
|
||||
*******************************************************************************************************/
|
||||
|
||||
#define Program_Version "V1.1"
|
||||
|
||||
#include <SPI.h>
|
||||
#include <SX127XLT.h>
|
||||
#include "Settings.h"
|
||||
|
||||
#include <avr/sleep.h>
|
||||
#include <avr/wdt.h>
|
||||
#include "PinChangeInterrupt.h" //get the library here; https://github.com/NicoHood/PinChangeInterrupt
|
||||
|
||||
#include <AtmelSleep.h>
|
||||
|
||||
SX127XLT LT;
|
||||
|
||||
uint32_t RXpacketCount;
|
||||
uint32_t errors;
|
||||
|
||||
uint8_t RXBUFFER[RXBUFFER_SIZE]; //create a buffer for the received packet
|
||||
|
||||
uint8_t RXPacketL; //stores length of packet received
|
||||
int16_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, 0, NO_WAIT); //setup LoRa device for receive with no timeout
|
||||
|
||||
Serial.println(F("Waiting for RX - Sleeping"));
|
||||
Serial.flush();
|
||||
|
||||
attachInterrupt(digitalPinToInterrupt(DIO0), wakeUp, HIGH);
|
||||
|
||||
atmelSleepPermanent(); //sleep the processor
|
||||
|
||||
detachInterrupt(digitalPinToInterrupt(DIO0));
|
||||
|
||||
//something has happened ?
|
||||
Serial.println(F("Awake"));
|
||||
digitalWrite(LED1, HIGH);
|
||||
|
||||
if (BUZZER > 0)
|
||||
{
|
||||
digitalWrite(BUZZER, HIGH);
|
||||
}
|
||||
|
||||
RXPacketL = LT.readPacket(RXBUFFER, RXBUFFER_SIZE); //now read in the received packet to the RX buffer
|
||||
|
||||
PacketRSSI = LT.readPacketRSSI();
|
||||
PacketSNR = LT.readPacketSNR();
|
||||
|
||||
if (RXPacketL == 0)
|
||||
{
|
||||
packet_is_Error();
|
||||
}
|
||||
else
|
||||
{
|
||||
packet_is_OK();
|
||||
}
|
||||
|
||||
digitalWrite(LED1, LOW);
|
||||
|
||||
if (BUZZER > 0)
|
||||
{
|
||||
|
||||
digitalWrite(BUZZER, LOW);
|
||||
}
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
|
||||
void wakeUp()
|
||||
{
|
||||
//handler for the interrupt
|
||||
}
|
||||
|
||||
|
||||
void packet_is_OK()
|
||||
{
|
||||
uint16_t IRQStatus, localCRC;
|
||||
|
||||
IRQStatus = LT.readIrqStatus();
|
||||
RXpacketCount++;
|
||||
|
||||
RXPacketL = LT.readPacket(RXBUFFER, RXBUFFER_SIZE); //now read in the received packet to the RX buffer
|
||||
|
||||
Serial.print(F("Packet> "));
|
||||
LT.printASCIIPacket(RXBUFFER, RXPacketL);
|
||||
|
||||
localCRC = LT.CRCCCITT(RXBUFFER, RXPacketL, 0xFFFF);
|
||||
Serial.print(F(" 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);
|
||||
Serial.println();
|
||||
led_Flash(2, 125); //LED flash for approx 10 seconds
|
||||
}
|
||||
|
||||
|
||||
void packet_is_Error()
|
||||
{
|
||||
uint16_t IRQStatus;
|
||||
IRQStatus = LT.readIrqStatus(); //get the IRQ status
|
||||
|
||||
if (IRQStatus & IRQ_RX_TIMEOUT)
|
||||
{
|
||||
Serial.println(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();
|
||||
Serial.println();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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(__TIME__);
|
||||
Serial.print(F(" "));
|
||||
Serial.println(__DATE__);
|
||||
Serial.println(F(Program_Version));
|
||||
Serial.println();
|
||||
Serial.println(F("62_LoRa_Wake_on_RX_Atmel Starting"));
|
||||
|
||||
if (BUZZER > 0)
|
||||
{
|
||||
pinMode(BUZZER, OUTPUT);
|
||||
digitalWrite(BUZZER, HIGH);
|
||||
delay(50);
|
||||
digitalWrite(BUZZER, LOW);
|
||||
}
|
||||
|
||||
SPI.begin();
|
||||
|
||||
if (LT.begin(NSS, NRESET, DIO0, LORA_DEVICE))
|
||||
{
|
||||
Serial.println(F("Radio Device found"));
|
||||
led_Flash(2, 125);
|
||||
delay(1000);
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.println(F("No device responding"));
|
||||
while (1)
|
||||
{
|
||||
led_Flash(50, 50);
|
||||
}
|
||||
}
|
||||
|
||||
LT.setupLoRa(Frequency, Offset, SpreadingFactor, Bandwidth, CodeRate, Optimisation);
|
||||
|
||||
Serial.print(F("Receiver ready - RXBUFFER_SIZE "));
|
||||
Serial.println(RXBUFFER_SIZE);
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
/*******************************************************************************************************
|
||||
Programs for Arduino - Copyright of the author Stuart Robinson - 19/06/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.
|
||||
*******************************************************************************************************/
|
||||
|
||||
//******* 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 BUZZER -1 //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 = 10; //LoRa transmit power in dBm
|
||||
|
||||
const uint16_t packet_delay = 1000; //mS delay between packets
|
||||
|
||||
#define RXBUFFER_SIZE 32 //RX buffer size
|
||||
const uint16_t packetCRCcheck = 0x3F83; //CRC to check RX packet for
|
||||
const uint8_t packetCRClengthcheck = 23; //packet length to check for
|
||||
|
||||
|
||||
@@ -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 lora device need to be defined in the 'Settings.h' file also.
|
||||
|
||||
When the program starts the LoRa device is setup to recieve packets with pin DIO0 set to go high when a
|
||||
packet arrives. The receiver remains powered (it cannot receive otherwise) and the processor
|
||||
(Atmel ATMega328P or 1284P) is put to sleep. When pin DIO0 does go high, indicating a packet is received,
|
||||
the processor wakes up and prints the packet. It then goes back to sleep.
|
||||
|
||||
There is a printout of the valid packets received, these are assumed to be in ASCII printable text.
|
||||
The LED will flash for each packet received and the buzzer will sound,if fitted.
|
||||
|
||||
Tested on a 'bare bones' ATmega328P board, the current in sleep mode was 12.26mA.
|
||||
|
||||
Serial monitor baud rate is set at 9600.
|
||||
*******************************************************************************************************/
|
||||
|
||||
#define Program_Version "V1.0"
|
||||
|
||||
#include <SPI.h>
|
||||
#include <SX127XLT.h>
|
||||
#include "Settings.h"
|
||||
|
||||
#include <avr/sleep.h>
|
||||
#include "PinChangeInterrupt.h" //get the library here; https://github.com/NicoHood/PinChangeInterrupt
|
||||
|
||||
SX127XLT LT;
|
||||
|
||||
uint32_t RXpacketCount;
|
||||
uint32_t errors;
|
||||
|
||||
uint8_t RXBUFFER[RXBUFFER_SIZE]; //create a buffer for the received packet
|
||||
|
||||
uint8_t RXPacketL; //stores length of packet received
|
||||
int16_t PacketRSSI; //stores RSSI of received packet
|
||||
int8_t PacketSNR; //stores signal to noise ratio of received packet
|
||||
|
||||
|
||||
void loop()
|
||||
{
|
||||
LT.fillSXBuffer(0, 13, '#'); //make sure the first part of FIFO is cleared, so we can tell its a fresh packet
|
||||
|
||||
RXPacketL = LT.receive(RXBUFFER, RXBUFFER_SIZE, 0, NO_WAIT); //setup LoRa device for receive and continue
|
||||
|
||||
//receive is setup
|
||||
|
||||
Serial.println(F("Going to sleep zzzz"));
|
||||
Serial.println();
|
||||
Serial.flush(); //make sure all serial has gone, it can wake up processor
|
||||
|
||||
sleep_permanent(); //put processor to sleep, with LoRa device listening, should
|
||||
//wakeup when DIO0 goes high
|
||||
|
||||
Serial.println(F("Awake !!!!"));
|
||||
digitalWrite(LED1, HIGH); //something has happened ?
|
||||
|
||||
if (BUZZER > 0)
|
||||
{
|
||||
digitalWrite(BUZZER, HIGH);
|
||||
}
|
||||
|
||||
RXPacketL = LT.readPacket(RXBUFFER, RXBUFFER_SIZE); //now read in the received packet to the RX buffer
|
||||
|
||||
PacketRSSI = LT.readPacketRSSI();
|
||||
PacketSNR = LT.readPacketSNR();
|
||||
|
||||
if (RXPacketL == 0)
|
||||
{
|
||||
packet_is_Error();
|
||||
}
|
||||
else
|
||||
{
|
||||
packet_is_OK();
|
||||
}
|
||||
|
||||
digitalWrite(LED1, LOW);
|
||||
|
||||
if (BUZZER > 0)
|
||||
{
|
||||
|
||||
digitalWrite(BUZZER, LOW);
|
||||
}
|
||||
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
|
||||
void sleep_permanent()
|
||||
{
|
||||
LT.clearIrqStatus(IRQ_RADIO_ALL); //ensure the DIO0 low is cleared, otherwise there could be an immediate wakeup
|
||||
attachPCINT(digitalPinToPCINT(DIO0), wakeUp, HIGH); //This is a hardware interrupt, the LoRa device is set for DIOo goes high on RXdone
|
||||
ADCSRA = 0; //disable ADC
|
||||
set_sleep_mode (SLEEP_MODE_PWR_DOWN);
|
||||
noInterrupts (); //timed sequence follows
|
||||
sleep_enable();
|
||||
|
||||
MCUCR = bit (BODS) | bit (BODSE); //turn on brown-out enable select
|
||||
MCUCR = bit (BODS); //this must be done within 4 clock cycles of above
|
||||
interrupts (); //guarantees next instruction executed
|
||||
|
||||
sleep_cpu (); //sleep within 3 clock cycles of above
|
||||
|
||||
/* wake up here */
|
||||
|
||||
sleep_disable();
|
||||
|
||||
detachPCINT(digitalPinToPCINT(DIO0));
|
||||
}
|
||||
|
||||
|
||||
void wakeUp()
|
||||
{
|
||||
//handler for the interrupt
|
||||
}
|
||||
|
||||
|
||||
|
||||
void packet_is_OK()
|
||||
{
|
||||
uint16_t IRQStatus, localCRC;
|
||||
|
||||
IRQStatus = LT.readIrqStatus();
|
||||
RXpacketCount++;
|
||||
|
||||
RXPacketL = LT.readPacket(RXBUFFER, RXBUFFER_SIZE); //now read in the received packet to the RX buffer
|
||||
|
||||
Serial.print(F("Packet> "));
|
||||
LT.printASCIIPacket(RXBUFFER, RXPacketL);
|
||||
|
||||
localCRC = LT.CRCCCITT(RXBUFFER, RXPacketL, 0xFFFF);
|
||||
Serial.print(F(" 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(); //get the IRQ status
|
||||
|
||||
if (IRQStatus & IRQ_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();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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(__TIME__);
|
||||
Serial.print(F(" "));
|
||||
Serial.println(__DATE__);
|
||||
Serial.println(F(Program_Version));
|
||||
Serial.println();
|
||||
Serial.println(F("6_LoRa_RX_and_Sleep_Atmel Starting"));
|
||||
Serial.println();
|
||||
|
||||
if (BUZZER > 0)
|
||||
{
|
||||
pinMode(BUZZER, OUTPUT);
|
||||
digitalWrite(BUZZER, HIGH);
|
||||
delay(50);
|
||||
digitalWrite(BUZZER, LOW);
|
||||
}
|
||||
|
||||
SPI.begin();
|
||||
|
||||
if (LT.begin(NSS, NRESET, DIO0, DIO1, DIO2, LORA_DEVICE))
|
||||
{
|
||||
Serial.println(F("Radio Device found"));
|
||||
led_Flash(2, 125);
|
||||
delay(1000);
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.println(F("No device responding"));
|
||||
while (1)
|
||||
{
|
||||
led_Flash(50, 50);
|
||||
}
|
||||
}
|
||||
|
||||
LT.setupLoRa(Frequency, Offset, SpreadingFactor, Bandwidth, CodeRate, Optimisation);
|
||||
|
||||
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 = 10; //LoRa transmit power in dBm
|
||||
|
||||
const uint16_t packet_delay = 1000; //mS delay between packets
|
||||
|
||||
#define RXBUFFER_SIZE 32 //RX buffer size
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,260 @@
|
||||
/*******************************************************************************************************
|
||||
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 program tests the sleep mode and register retention of the lora device in sleep
|
||||
mode, it assumes an Atmel ATMega328P processor is in use. The LoRa settings to use are specified in
|
||||
the 'Settings.h' file.
|
||||
|
||||
A packet is sent, containing the text 'Before Device Sleep' and the lora device and Atmel processor are put
|
||||
to sleep. The processor should remain asleep until the pin defined by SWITCH1 in the Settings.h file is
|
||||
connected to ground and the LoRa device register values should be retained. The LoRa device then
|
||||
attempts to transmit another packet 'After Device Sleep' without re-loading all the LoRa settings.
|
||||
The receiver should see 'After Device Sleep' for the first packet and 'After Device Sleep' for the second.
|
||||
|
||||
Tested on an bare bones ATmega328P board, the curent in sleep mode was 2.4uA.
|
||||
|
||||
Serial monitor baud rate is set at 9600.
|
||||
*******************************************************************************************************/
|
||||
|
||||
#define Program_Version "V1.0"
|
||||
|
||||
#include <avr/sleep.h>
|
||||
|
||||
#include <SPI.h>
|
||||
#include "PinChangeInterrupt.h" //get the library here; https://github.com/NicoHood/PinChangeInterrupt
|
||||
|
||||
#include <SX127XLT.h>
|
||||
#include "Settings.h"
|
||||
|
||||
SX127XLT LT;
|
||||
|
||||
uint8_t TXPacketL;
|
||||
|
||||
|
||||
void loop()
|
||||
{
|
||||
digitalWrite(LED1, HIGH);
|
||||
Serial.print(TXpower);
|
||||
Serial.print(F("dBm "));
|
||||
Serial.print(F("TestPacket1> "));
|
||||
Serial.flush();
|
||||
|
||||
if (Send_Test_Packet1())
|
||||
{
|
||||
packet_is_OK();
|
||||
}
|
||||
else
|
||||
{
|
||||
packet_is_Error();
|
||||
}
|
||||
Serial.println();
|
||||
delay(packet_delay);
|
||||
|
||||
LT.setSleep(CONFIGURATION_RETENTION); //preserve register settings in sleep.
|
||||
Serial.println(F("Sleeping zzzzz...."));
|
||||
Serial.println();
|
||||
Serial.flush();
|
||||
digitalWrite(LED1, LOW);
|
||||
|
||||
sleep_permanent(); //goto sleep till woken up by switch press
|
||||
|
||||
Serial.println(F("Awake !"));
|
||||
Serial.flush();
|
||||
digitalWrite(LED1, HIGH);
|
||||
LT.wake();
|
||||
|
||||
Serial.print(TXpower);
|
||||
Serial.print(F("dBm "));
|
||||
Serial.print(F("TestPacket2> "));
|
||||
Serial.flush();
|
||||
|
||||
if (Send_Test_Packet2())
|
||||
{
|
||||
packet_is_OK();
|
||||
}
|
||||
else
|
||||
{
|
||||
packet_is_Error();
|
||||
}
|
||||
Serial.println();
|
||||
delay(packet_delay);
|
||||
}
|
||||
|
||||
|
||||
void sleep_permanent()
|
||||
{
|
||||
attachPCINT(digitalPinToPCINT(SWITCH1), wakeUp, LOW); //This is a hardware interrupt
|
||||
|
||||
ADCSRA = 0; //disable ADC
|
||||
set_sleep_mode (SLEEP_MODE_PWR_DOWN);
|
||||
noInterrupts (); //timed sequence follows
|
||||
sleep_enable();
|
||||
|
||||
//turn off brown-out enable in software
|
||||
MCUCR = bit (BODS) | bit (BODSE); //turn on brown-out enable select
|
||||
MCUCR = bit (BODS); //this must be done within 4 clock cycles of above
|
||||
interrupts (); //guarantees next instruction executed
|
||||
|
||||
sleep_cpu (); //sleep within 3 clock cycles of above
|
||||
|
||||
/* wake up here */
|
||||
|
||||
sleep_disable();
|
||||
|
||||
detachPCINT(digitalPinToPCINT(SWITCH1));
|
||||
}
|
||||
|
||||
|
||||
void wakeUp()
|
||||
{
|
||||
//handler for the interrupt
|
||||
}
|
||||
|
||||
|
||||
void packet_is_OK()
|
||||
{
|
||||
Serial.print(F(" "));
|
||||
Serial.print(TXPacketL);
|
||||
Serial.print(F(" Bytes SentOK"));
|
||||
}
|
||||
|
||||
|
||||
void packet_is_Error()
|
||||
{
|
||||
uint16_t IRQStatus;
|
||||
IRQStatus = LT.readIrqStatus(); //get the IRQ status
|
||||
Serial.print(F("SendError,"));
|
||||
Serial.print(F("Length,"));
|
||||
Serial.print(TXPacketL);
|
||||
Serial.print(F(",IRQreg,"));
|
||||
Serial.print(IRQStatus, HEX);
|
||||
LT.printIrqStatus();
|
||||
digitalWrite(LED1, LOW); //this leaves the LED on slightly longer for a packet error
|
||||
}
|
||||
|
||||
|
||||
bool Send_Test_Packet1()
|
||||
{
|
||||
uint8_t bufffersize;
|
||||
|
||||
uint8_t buff[] = "Before Device Sleep";
|
||||
TXPacketL = sizeof(buff);
|
||||
buff[TXPacketL - 1] = '*';
|
||||
|
||||
if (sizeof(buff) > TXBUFFER_SIZE) //check that defined buffer is not larger than TX_BUFFER
|
||||
{
|
||||
bufffersize = TXBUFFER_SIZE;
|
||||
}
|
||||
else
|
||||
{
|
||||
bufffersize = sizeof(buff);
|
||||
}
|
||||
|
||||
TXPacketL = bufffersize;
|
||||
|
||||
LT.printASCIIPacket( (uint8_t*) buff, bufffersize);
|
||||
digitalWrite(LED1, HIGH);
|
||||
|
||||
if (LT.transmit( (uint8_t*) buff, TXPacketL, 10000, TXpower, WAIT_TX))
|
||||
{
|
||||
digitalWrite(LED1, LOW);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool Send_Test_Packet2()
|
||||
{
|
||||
uint8_t bufffersize;
|
||||
|
||||
uint8_t buff[] = "After Device Sleep";
|
||||
TXPacketL = sizeof(buff);
|
||||
buff[TXPacketL - 1] = '*';
|
||||
|
||||
if (sizeof(buff) > TXBUFFER_SIZE) //check that defined buffer is not larger than TX_BUFFER
|
||||
{
|
||||
bufffersize = TXBUFFER_SIZE;
|
||||
}
|
||||
else
|
||||
{
|
||||
bufffersize = sizeof(buff);
|
||||
}
|
||||
|
||||
TXPacketL = bufffersize;
|
||||
|
||||
LT.printASCIIPacket( (uint8_t*) buff, bufffersize);
|
||||
digitalWrite(LED1, HIGH);
|
||||
|
||||
if (LT.transmit( (uint8_t*) buff, TXPacketL, 10000, TXpower, WAIT_TX))
|
||||
{
|
||||
digitalWrite(LED1, LOW);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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
|
||||
pinMode(SWITCH1, INPUT_PULLUP); //setup switch pin, ground to activate
|
||||
|
||||
Serial.begin(9600);
|
||||
Serial.println();
|
||||
Serial.print(__TIME__);
|
||||
Serial.print(F(" "));
|
||||
Serial.println(__DATE__);
|
||||
Serial.println(F(Program_Version));
|
||||
Serial.println();
|
||||
|
||||
Serial.println(F("7_LoRa_TX_Sleep_Switch_Wakeup_Atmel Starting"));
|
||||
|
||||
SPI.begin();
|
||||
|
||||
if (LT.begin(NSS, NRESET, DIO0, DIO1, DIO2, LORA_DEVICE))
|
||||
{
|
||||
Serial.println(F("LoRa device found"));
|
||||
led_Flash(2, 125);
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.println(F("No device responding"));
|
||||
while (1)
|
||||
{
|
||||
led_Flash(50, 50); //long fast speed flash indicates device error
|
||||
}
|
||||
}
|
||||
|
||||
LT.setupLoRa(Frequency, Offset, SpreadingFactor, Bandwidth, CodeRate, Optimisation);
|
||||
|
||||
Serial.print(F("Transmitter ready - TXBUFFER_SIZE "));
|
||||
Serial.println(TXBUFFER_SIZE);
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
/*******************************************************************************************************
|
||||
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 SWITCH1 2 //switch pin, used to wake processor up
|
||||
|
||||
#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 TXBUFFER_SIZE 32 //RX buffer size
|
||||
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 43 KiB |
Binary file not shown.
Reference in New Issue
Block a user