I was wondering if someone could help me set up a connection failsafe lora relay control. I've created a lora servo throttle control for a hang glider winch and I'd like to have it set up so that when a connection is lost between the two Heltec Esp32 Lora devices a relay would come on and shut the engine off on the receiver side. This is my current file.
Thanks,
Lynn
#include <ESP32Servo.h>
#define HELTEC_POWER_BUTTON // must be before "#include <heltec_unofficial.h>"
#include <heltec_unofficial.h>
#define PAUSE 1
#define FREQUENCY 905.2 // for US
#define BANDWIDTH 250.0
#define SPREADING_FACTOR 9
#define TRANSMIT_POWER 0
String rxdata;
volatile bool rxFlag = false;
long counter = 0;
uint64_t last_tx = 0;
uint64_t tx_time;
uint64_t minimum_pause;
int myint = rxdata.toInt();
Servo myservo1;
int newval1, oldval1;
int RelayOn = 45;
void setup() {
myservo1.attach(35);
pinMode(RelayOn, OUTPUT);
digitalWrite(RelayOn,LOW);
Serial.begin(115200);
// Allow allocation of all timers
ESP32PWM::allocateTimer(0);
ESP32PWM::allocateTimer(1);
ESP32PWM::allocateTimer(2);
ESP32PWM::allocateTimer(3);
myservo1.setPeriodHertz(50); // standard 50 hz servo
myservo1.attach(35, 300, 2400); // attaches the servo on pin 18 to the servo object
// using default min/max of 1000us and 2000us
// different servos may require different min/max settings
// for an accurate 0 to 180 sweep
heltec_setup();
both.println("Radio init");
RADIOLIB_OR_HALT(radio.begin());
// Set the callback function for received packets
radio.setDio1Action(rx);
// Set radio parameters
both.printf("Frequency: %.2f MHz\n", FREQUENCY);
RADIOLIB_OR_HALT(radio.setFrequency(FREQUENCY));
both.printf("Bandwidth: %.1f kHz\n", BANDWIDTH);
RADIOLIB_OR_HALT(radio.setBandwidth(BANDWIDTH));
both.printf("Spreading Factor: %i\n", SPREADING_FACTOR);
RADIOLIB_OR_HALT(radio.setSpreadingFactor(SPREADING_FACTOR));
both.printf("TX power: %i dBm\n", TRANSMIT_POWER);
RADIOLIB_OR_HALT(radio.setOutputPower(TRANSMIT_POWER));
// Start receiving
RADIOLIB_OR_HALT(radio.startReceive(RADIOLIB_SX126X_RX_TIMEOUT_INF));
}
void loop() {
heltec_loop();
// If a packet was received, display it and the RSSI and SNR
if (rxFlag) {
rxFlag = false;
radio.readData(rxdata);
if (_radiolib_status == RADIOLIB_ERR_NONE) {
both.println(rxdata.toInt());
both.println(rxdata.c_str());
//both.printf(" RSSI: %.2f dBm\n", radio.getRSSI());
//both.printf(" SNR: %.2f dB\n", radio.getSNR());
newval1 = rxdata.toInt();
newval1 = map(newval1,0,500,-45,180);
myservo1.write(newval1);
//Serial.print(newval1);
//Serial.print(myint);
//oldval1 = newval1;
//}
if(String(rxdata) == "Relay On "){
digitalWrite(RelayOn, HIGH);
both.println(String(rxdata));
delay(5000);
}
}
else {
both.printf("fail (%i)\n",_radiolib_status);
}
RADIOLIB_OR_HALT(radio.startReceive(RADIOLIB_SX126X_RX_TIMEOUT_INF));
}
}
// Can't do Serial or display things here, takes too much time for the interrupt
void rx() {
rxFlag = true;
}