Heltec esp32 Lora receiver relay failsafe

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;
}

your code is unreadable at the moment

improve your code presentation by using code tags, e.g. click < CODE > and paste you code where it says 'type or paste code here

what does the code do at the moment? what should it do?
post serial monitor output (as text not a screen image)

So currently the transmitter reads a hall sensor and sends that data over and I have a button that sends the String "relay on" over. The receiver currently reads the hall sensor data and converts that string over to an int and controls the servo position for throttle control. The receiver reads the "relay on" data and turns the relay on which kills the motor. What I need now is for the receiver to have a failsafe where if there is a communication is loss then the relay will come on and will shut down the engine off.


#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;
}

if you don't receive anything within a certain time switch on relay
e.g. something along the lines of

void loop() {
  heltec_loop();
  static unsigned long relayTimer = millis();
  // if nothing received in 10 seconds
  if (millis() - relayTimer > 10000) {
    // switch relay ON
  }

  // If a packet was received, display it and the RSSI and SNR
  if (rxFlag) {
    relayTimer = millis();  // reset timer
    rxFlag = false;

Thanks! This works. Sorry for the delay I had to wait on a new relay to come in the mail.

good idea click the Solution button at the bottom of the reply that answered the question - this helps others with a similar question