Hi Guys,
I am setting up to Dragino LoRa mini Dev boards (I believe they are based on UNO) to talk to each other
I have got everything working except for turning off a relay. I have the sketch setup to turn on a relay (via pin A0) when it receives LoRa data.
I would like it to turn the relay off again, when it stops receiving LoRa data, but I don't want it do continuosly cycle the relay.
The code I have at the moment, will turn the relay on, but not off.
Any thoughts on where I went wrong, or a better approach?
Thanks
Dave
#include <SPI.h>
#include <LoRa.h>
const int RelayPin = A0; // the number of the Relay pin
int count = 0;
void setup() {
pinMode(RelayPin, OUTPUT);
digitalWrite(RelayPin, LOW);
Serial.begin(9600);
while (!Serial);
Serial.println("LoRa Receiver");
if (!LoRa.begin(433E6)) {
Serial.println("Starting LoRa failed!");
while (1);
}
}
void loop() {
count = 0;
LoRa.setSpreadingFactor(10); // ranges from 6-12,default 7 see API docs - perhaps use a jumper pin to set this
LoRa.setSyncWord(0x23); // ranges from 0-0xFF, default 0x34, see API docs
LoRa.setTxPower(20); // Supported values are `2` to `20` for `PA_OUTPUT_PA_BOOST_PIN`, and `0` to `14` for `PA_OUTPUT_RFO_PIN`. Most modules have the PA output pin connected to PA BOOST,
// LoRa.setSignalBandwidth(62.5E3); // * `signalBandwidth` - signal bandwidth in Hz, defaults to `125E3`. Supported values are `7.8E3`, `10.4E3`, `15.6E3`, `20.8E3`, `31.25E3`, `41.7E3`, `62.5E3`, `125E3`, and `250E3`.// try to parse packet
LoRa.setCodingRate4(5); //- `codingRateDenominator` - denominator of the coding rate, defaults to `5`Supported values are between `5` and `8`, these correspond to coding rates of `4/5` and `4/8`. The coding rate numerator is fixed at `4`.
int packetSize = LoRa.parsePacket();
if (packetSize) {
// received a packet
Serial.print("Received packet '");
// read packet
while (LoRa.available()) {
Serial.print((char)LoRa.read());
}
// print RSSI of packet
Serial.print("' with RSSI ");
Serial.println(LoRa.packetRssi());
Serial.print(" and SNR ");
Serial.println(LoRa.packetSnr());
// print SNR of packet
digitalWrite(RelayPin, HIGH);
delay(25);
} else {
count++;
if (count > 100){
Serial.println(count);
// We need to write pin low, but need some sort of a delay to stop relay switching on and off
digitalWrite(RelayPin, LOW);
}
}
}