MQTT Sender gets stucks

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!

do you see the output of this part of the code in the serial monitor ?

void reconnect() {
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    if (client.connect("S2_Gang", mqtt_username, mqtt_password)) {
      Serial.println("connected");
      // Flash the built-in LED 3 times after connecting to the MQTT broker
      flashLed(3, 300, 300);
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      flashLed(2, 100, 400); // Blink twice if failed to connect to MQTT broker
      delay(2000);
    }
  }
}

Whenever the arduino is stuck, it is blinking twice and rests for 10s or so. So it must be part of the else in void reconnect().

I will try to read out the Serial the next time is see it in this state.

09:50:06.673 -> failed, rc=-2 try again in 5 seconds

09:50:09.691 -> Attempting MQTT connection...failed, rc=-2 try again in 5 seconds

09:50:19.685 -> Attempting MQTT connection...failed, rc=-2 try again in 5 seconds

09:50:29.683 -> Attempting MQTT connection.....

(PRESS ON RESET BUTTON)

09:50:37.799 -> WiFi connected

09:50:37.799 -> IP address:

09:50:37.799 -> 192.168.0.196

09:50:37.799 -> Attempting MQTT connection...connected

possibly your DHCP lease expires and your board's IP needs changing - so needs a reconnect to the WiFi infrastructure?

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