Wifi lora 32 v2 reset after sometime

Hi guys, I have a helteck wifi lora 32 v2 and Im trying to do a communication between 2 of them. The first one is only sending messages by Lora but the second is receveing messages by Lora and sending it by MQTT. I have some functions to reconnection and to check the connections between the devices. All works good, but after a while both devices reboot (not at the same time). How can I do? I'm going to paste my codes. I hope you can help me. Thank you.

/*------LIBRERIAS--------------------*/
#include <WiFi.h>
#include <PubSubClient.h>
#include <EEPROM.h>
#include "heltec.h"
/*----VARIABLES INTERNET ---------------*/
const char* ssid = "Nexxt_B5486C";
const char* password = "di-control";
const char* mqtt_server = "192.168.0.102";
const char* deviceID = "ESP32_Modulo1";
const char* topic_prueba = "esp32/m1/in";
const char* topic_out = "esp32/m1/out";
const char* topic_validacion = "watchdog";
/*-------OBJETOS DE LIBRERIAS ----------*/
WiFiClient espClient;
PubSubClient client(espClient);
#define BAND 915E6
/*------- VARIABLES FISICAS -----------*/
const int gpio13Led = 25;    //LED COMUNICACION WIFI
const int gpioLora = 32;     //LED COMUNICACION LORA
const int gpio12Relay = 17;  //SALIDA
const int gpio33boton = 33;  //ENTRADA
int estadoboton = LOW;
/*------- VARIABLE CALLBACK------------*/
int topic_size = 40;  //variable a modificar dependiendo el largo del topic en Node red al que esta inscrito
char msg[40];
char topico[40];
char publish[40];
/*--------VARIABLES SEND DATA MQTT----------*/
unsigned long currentMillis;
unsigned long reloj = 0;
int tempo = 0;
int rest = 3;  //Manda msj cada 5 seg
/*--------VARIABLES WATCHDOG MQTT----------*/
unsigned long reloj2 = 0;
int tempo2 = 0;
int rest2 = 20;  //Sino recibe señal del watchdog en 20 seg
/*--------VARIABLES WATCHDOG LORA----------*/
unsigned long reloj3 = 0;
int tempo3 = 0;
int rest3 = 20;  //Sino recibe señal del watchdog en 20 seg
/*--------VARIABLES SEND DATA LORA----------*/
unsigned long reloj4 = 0;
int tempo4 = 0;
int rest4 = 3;  //Sino recibe señal del watchdog en 5 seg
/*-------VARIABLES CONVERSION LORA --------*/
String entrada = "";
char inChar = 0;
char aux[10];
char* p = 0;
int cuenta_receive = 0;
//-----OLED---------//
String rssi = "";
/*-------CIERRE DE VARIABLES --------*/

void setup_wifi() {
  delay(10);
  digitalWrite(gpio13Led, LOW);
  WiFi.disconnect();
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  //Serial.println("Setup wifi");

  for (int i = 0; i < 120; i++) {
    if (WiFi.status() != WL_CONNECTED) {
      delay(100);
      digitalWrite(gpio13Led, LOW);
    }
  }
  digitalWrite(gpio13Led, HIGH);
  //Serial.println("IP address: ");
  //Serial.println(WiFi.localIP());
}

void callback(char* topic, byte* payload, unsigned int length) {

  memcpy(msg, payload, length);
  msg[length] = '\0';
  topic[topic_size] = '\0';


  if (strncmp(topic, topic_prueba, topic_size) == 0) {
    if (strncmp(msg, "ON", length) == 0) {
      digitalWrite(gpio12Relay, HIGH);
    } else if (strncmp(msg, "OFF", length) == 0) {
      digitalWrite(gpio12Relay, LOW);
    }
  }

  if (strncmp(topic, topic_validacion, topic_size) == 0) {
    if (strncmp(msg, "HEY", length) == 0) {
      tempo2 = 0;
    }
  }

  for (int i = 0; i <= length; i++) {
    payload[i] = '\0';
  }

  for (int i = 0; i <= length; i++) {
    msg[i] = '\0';
  }

  for (int i = 0; i <= topic_size; i++) {
    topic[i] = '\0';
  }
}

void reconnect() {
  while (!client.connected()) {
    if (client.connect(deviceID)) {
      client.subscribe(topic_prueba);
      client.subscribe(topic_validacion);
      digitalWrite(gpio13Led, HIGH);  //Para observar el estado de conexion
    } else {
      digitalWrite(gpio13Led, LOW);
    }
  }
}

void OLED() {
  Heltec.display->init();
  //Heltec.display->flipScreenVertically();
  Heltec.display->setFont(ArialMT_Plain_10);
  Heltec.display->clear();

  Heltec.display->drawString(0, 0, "DICONTROL");
  Heltec.display->drawString(0, 10, "Creamos soluciones tecno");
  Heltec.display->drawString(0, 20, "4779149289");
  Heltec.display->display();
  delay(1000);
}

void LoRaData() {
  Heltec.display->clear();
  Heltec.display->setTextAlignment(TEXT_ALIGN_LEFT);
  Heltec.display->setFont(ArialMT_Plain_10);
  Heltec.display->drawString(0, 15, "Mensajes Recib: " + String(cuenta_receive));
  rssi = "Fuerza Señal Recib: " + String(LoRa.packetRssi(), DEC);
  Heltec.display->drawString(0, 0, rssi);
  Heltec.display->display();
}

void onReceive(int packetSize) {
  for (int i = 0; i < packetSize; i++) {
    inChar = LoRa.read();
    aux[i] = inChar;
    entrada = entrada + aux;
  }

  if (aux[0] == 'T') {
    p = strtok(aux, "T*");
    cuenta_receive = atoi(p);
  }
  tempo3 = 0;
  //Serial.print("RECEIVER: ");
  //Serial.println(cuenta_receive);
  inChar = 0;
  *p = 0;
  entrada = "";
  for (int i = 0; i < 10; i++) {
    aux[i] = '\0';
  }
}

void setup(void) {
  //EEPROM.begin(512);
  pinMode(gpio13Led, OUTPUT);
  pinMode(gpio12Relay, OUTPUT);
  pinMode(gpioLora, OUTPUT);
  setup_wifi();
  client.setServer(mqtt_server, 1884);
  client.setCallback(callback);
  Heltec.begin(true /*DisplayEnable Enable*/, true /*Heltec.LoRa Disable*/, true /*Serial Enable*/, true /*PABOOST Enable*/, BAND /*long BAND*/);
  LoRa.onReceive(onReceive);
  LoRa.receive();
  OLED();
}

void loop(void) {

  currentMillis = millis();
  LoRaData();

  if (WiFi.status() != WL_CONNECTED) {
    setup_wifi();  //se cambio el setup y se agregaron estas condiciones
  }

  //TIMER SEND DATA MQTT
  if (currentMillis - reloj >= 1000) {
    tempo++;
    reloj = currentMillis;
  }
  if (tempo >= rest) {
    itoa(cuenta_receive,publish,10);
    client.publish(topic_out, publish);
    tempo = 0;
  }
  //TIMER SEND DATA MQTT

  //TIMER WATCHDOG MQTT
  if (currentMillis - reloj2 >= 1000) {
    tempo2++;
    reloj2 = currentMillis;
  }
  if (tempo2 >= rest2) {
    digitalWrite(gpio13Led, LOW);
  } else {
    digitalWrite(gpio13Led, HIGH);
  }
  //TIMER WATCHDOG MQTT

  //TIMER WATCHDOG LORA
  if (currentMillis - reloj3 >= 1000) {
    tempo3++;
    reloj3 = currentMillis;
  }
  if (tempo3 >= rest3) {
    digitalWrite(gpioLora, LOW);
  } else {
    digitalWrite(gpioLora, HIGH);
  }
  //TIMER WATCHDOG LORA

  if (currentMillis - reloj4 >= 1000) {
    tempo4++;
    reloj4 = currentMillis;
  }
  
  if (tempo4 >= rest4) {
    LoRa.beginPacket();
    LoRa.setTxPower(14, RF_PACONFIG_PASELECT_PABOOST);  //ESTO HABILITA MANDAR LA INFO
    LoRa.print("T");
    LoRa.print(cuenta_receive);
    LoRa.endPacket("*");
    //Serial.print("SENDER: ");
    //Serial.println(cuenta_receive);
    LoRa.receive();  //ESTO HABILITA RECIBIR LOS DATOS
    rest4 = random(2) + 3;
    tempo4 = 0;
  }
  //TIMER SEND DATA LORA//

  if (!client.connected()) {
    reconnect();
  }
  if (!client.loop())
    client.connect(deviceID);
}
//-------LIBRERIA--------//
#include "heltec.h"
//-------OBJETO DE LIBRERIA------//
#define BAND 915E6
//-------VARIABLE PARA INT DESDE RECEIVER-------//
String entrada = "";
char inChar = 0;
char aux[10];
int cuenta_receive = 0;
char *p = 0;
//------ENTRADA DEBOUNCE----------//
const int botonIncremento = 33;
int cuenta = 0;
int estadoBotonIncremento;
int estadoBotonAnteriorIncremento;
int contador = 0;
//--------SALIDA---------//
const int salida = 25;
//-----TIMER SEND DATA---------//
unsigned long currentMillis;
unsigned long reloj = 0;
int tempo = 0;
int rest = 3;  //Manda msj cada 3 seg y se recibe igual
//-----TIMER COMPROBACION DATOS---------//
unsigned long reloj2 = 0;
int tempo2 = 0;
int rest2 = 5;  //Sino recibe msj en 10 seg se apaga led
//-----OLED---------//
String rssi = "";
//------ FIN DE VARIABLES------//

void counter_plus() {
  estadoBotonIncremento = digitalRead(botonIncremento);
  if (estadoBotonIncremento != estadoBotonAnteriorIncremento) {
    if (estadoBotonIncremento == HIGH) {
      contador++;
      delay(10);
    } 
  }
  estadoBotonAnteriorIncremento = estadoBotonIncremento;  // guardamos el estado del botón
  if (contador >= 30000) {
    contador = 0;
  }
}

void OLED() {
  Heltec.display->init();
  //Heltec.display->flipScreenVertically();
  Heltec.display->setFont(ArialMT_Plain_10);
  Heltec.display->clear();

  Heltec.display->drawString(0, 0, "DICONTROL");
  Heltec.display->drawString(0, 10, "Creamos soluciones tecno");
  Heltec.display->drawString(0, 20, "4779149289");
  Heltec.display->display();
  delay(1000);
}

void LoRaData() {
  Heltec.display->clear();
  Heltec.display->setTextAlignment(TEXT_ALIGN_LEFT);
  Heltec.display->setFont(ArialMT_Plain_10);
  Heltec.display->drawString(0, 15, "Msj mandado: " + String(contador));
  Heltec.display->drawString(0, 30, "Msj comprobado: " + String(cuenta_receive)); 
  rssi = "Fuerza Señal Recib: " + String(LoRa.packetRssi(), DEC);
  Heltec.display->drawString(0, 0, rssi);
  Heltec.display->display();
}

void onReceive(int packetSize) {
  for (int i = 0; i < packetSize; i++) {
    inChar = LoRa.read();
    aux[i] = inChar;
    entrada = entrada + aux;
  }

  if (aux[0] == 'T'){
    p = strtok (aux,"T*");
    cuenta_receive = atoi(p);
  }
  counter_plus();
  Serial.print("RECEIVER: ");
  Serial.println(cuenta_receive);
  inChar = 0;
  *p = 0;
  entrada = "";
  for (int i = 0; i < 10; i++) {
    aux[i] = '\0';
  }
}


void setup() {
  Heltec.begin(true /*DisplayEnable Enable*/, true /*LoRa Disable*/, true /*Serial Enable*/, true /*PABOOST Enable*/, BAND /*long BAND*/);
  LoRa.onReceive(onReceive);
  LoRa.receive();  //ACTIVA MODO RECEPCION
  pinMode(salida, OUTPUT);
  pinMode(botonIncremento, INPUT);
  OLED();
}

void loop() {
  currentMillis = millis();
  counter_plus();
  LoRaData();

  if (currentMillis - reloj >= 1000) {
    tempo++;
    reloj = currentMillis;
  }
  if (tempo >= rest) {
    LoRa.beginPacket();
    LoRa.setTxPower(14, RF_PACONFIG_PASELECT_PABOOST);  //ESTO HABILITA MANDAR LA INFO
    LoRa.print("T");
    LoRa.print(contador);
    LoRa.print("*");
    LoRa.endPacket();
    Serial.print("SENDER: ");
    Serial.println(contador);
    LoRa.receive();  //ESTO HABILITA RECIBIR LOS DATOS
    rest = random(2) + 3;
    tempo = 0;
  }

  if (currentMillis - reloj2 >= 1000) {
    tempo2++;
    reloj2 = currentMillis;
  }
  if (tempo2 >= rest2) {
    if (contador - 10 < cuenta_receive) {
      digitalWrite(salida, HIGH);
    } else {
      digitalWrite(salida, LOW);
    }
    tempo2 = 0;
  }
}


This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.