MQTT Microcontroller gets stuck

Hello Folks,

I have a problem with a lolin_s2_mini microcontroller which acts as a PIR Sensor and Publishes over MQTT.

It gets stuck once every 24h, and my lights do not turn on anymore.
Pressing reset and it works again.

The code is very straight forward:

#include <Arduino.h>
#include <WiFi.h>
#include <PubSubClient.h>
#include "config.h"

#define COOLDOWN_WAIT 5000 // Wait for 5 seconds
#define DURATION_PUBLISH_INTERVAL 1000 // Publish duration at most every 1 second
#define BUILTIN_LED 2 // Change this to the pin number of your board's built-in LED

WiFiClient espClient;
PubSubClient client(espClient);

unsigned long motionStartTime = 0;
unsigned long lastDurationPublishTime = 0;
bool motionDetected = false;

void setup_wifi() {
  delay(10);
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void reconnect() {
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    if (client.connect("ESP8266Client", mqtt_username, mqtt_password)) {
      Serial.println("connected");
      // Flash the built-in LED 3 times after connecting to the MQTT broker
      for (int i = 0; i < 3; i++) {
        digitalWrite(BUILTIN_LED, LOW); // Turn on the LED
        delay(200);
        digitalWrite(BUILTIN_LED, HIGH); // Turn off the LED
        delay(200);
      }
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      delay(5000);
    }
  }
}

void setup() {
  Serial.begin(115200);
  pinMode(BUILTIN_LED, OUTPUT);
  digitalWrite(BUILTIN_LED, HIGH); // Turn off the LED
  setup_wifi();
  client.setServer(mqtt_server, mqtt_port);
  pinMode(PIR_PIN, INPUT);
}

void loop() {
  if (!client.connected()) {
    reconnect();
  }
  client.loop();

  int pirState = digitalRead(PIR_PIN);

  if (pirState == HIGH && !motionDetected) {
    motionDetected = true;
    motionStartTime = millis();
    client.publish("PIR/Gang/state", "true");
    digitalWrite(BUILTIN_LED, LOW); // Turn on the LED
  }

  if (motionDetected && millis() - lastDurationPublishTime >= DURATION_PUBLISH_INTERVAL) {
    int duration = (millis() - motionStartTime) / 1000;
    char durationMsg[10];
    sprintf(durationMsg, "%d", duration);
    client.publish("PIR/Gang/duration", durationMsg);
    lastDurationPublishTime = millis();
  }

  if (pirState == LOW && motionDetected) {
    unsigned long cooldownStart = millis();
    while (millis() - cooldownStart < COOLDOWN_WAIT) {
      if (digitalRead(PIR_PIN) == HIGH) {
        return;
      }
    }
    motionDetected = false;
    client.publish("PIR/Gang/state", "false");
    digitalWrite(BUILTIN_LED, HIGH); // Turn off the LED
  }
}

What is the issue here?
Many thanks in advance!

why don’t you add few Serial.print messages as a debug and then see in the serial monitor?
there is no other way to check it
also: your sensor does not go to sleep - ask it to print millis() every now and then
debugging itself is an art but honestly: you just need to spend some time on it
the reasons for hanging is kind of gazyllion - starting with power supply, wifi, etc
let us know what you found out
cheers