While loop inside void loop() causing problems

Hi,
I am trying to run a while loop inside the void loop().

But I am getting a WDT reset.

WDT rest.
cause: 2  error(3,6)

and as soon as i remove the while loop, it runs fine.

here is the code

int abc == 0;

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

  while (abc == 0){
  if(digitalRead(pirPin) == HIGH){
       digitalWrite(ledPin, HIGH);
        
       if(lockLow){
         lockLow = false;           
         Serial.println("motion detected ");
         digitalWrite(14, HIGH);
         mqttClient.publish("XYZ/MotionSensor","Motion Detected");
         }        
         takeLowTime = true;
       }
 
     if(digitalRead(pirPin) == LOW){      
       digitalWrite(ledPin, LOW);  
 
       if(takeLowTime){
        lowIn = millis();          
        takeLowTime = false;       
        }
       
       if(!lockLow && millis() - lowIn > pause){ 
           
           lockLow = true;                       
           Serial.println("motion ended");
           digitalWrite(14, LOW);      
           abc == 1;
           mqttClient.publish("XYZ/MotionSensor","Motion Ended");
           }
       }
}
}

Basically, I receive a command using internet to start the sensor. The sensor starts. And I need it to run in the loop TILL the time motion is detected. And then fall out of the while loop and wait for command from internet again.

But why is the device getting s WDT reset?

Cause: 2 refers to the reset pin. But my reset pin is untouched.

But why is the device getting s WDT reset?

Because you spend too much time in loop()

The purpose of watchdog timer (WDT) is to recover program when loop() hangs out for more than.. 8sec I think.

You should either change code to return from loop() quickly, by preserving status in global variables, or disable WDT which is not recommended

blimpyway:
The purpose of watchdog timer (WDT) is to recover program when loop() hangs out for more than.. 8sec I think.

Eight seconds is the longest configurable watchdog timeout - it could be as little as 16 milliseconds

So replace it with if(abc==0) {. My quick reading of your code looks like it will work exactly the same, with the added benefit that the MQTT client gets more opportunities to do the jobs it needs to do.

The Problem with the IF option that u presented is that...once the IF loop executes, it will return to MQTTclient.loop and again I will have to give a command from MQTT to start the motion sensor for it to run again.

Which i don't want. What I want is...Once I give a command and the motion sensor initializes, it should run in a loop TILL the time motion is detected. And then exit the loop.

Anyways..I figured out a way to do this task. I just declared another function and called it from within the void loop(). Theoretically, its the same thing and the problem should persist. But it seems to work.

Now another major problem I am facing is the False Detections!! The PIR motion sensor gives soooo much False Detection!! Its annoying really!

Are you using an ESP8266? If so, you need to insert some delays. Or you could make use of the yield() function.

Yes! i am using the ESP8266 module.