ESP8266 Problem millis() in loop causes crash (Alexa Water Pump) [SOLVED]

Hi ladies and gentelman,

I am working on a Project that should read values from a humidty sensor and turn on an off a water pump on commands from an Alexa. All clear until there and my code also does work… only alexa responds that it doesnt although i can observe that it does.As far as I understand its a timing issue. So in order to solve this Problem I implemented a millis() in the code as below and it was working for a while, the alexa was telling me everything ok. Until I tried to upgrade my code so it would be able to change some variables through a web server, i failed at doing that, told myself lets start over. I changed everything back -or I think I did- And now the code crashes because of the watchdog timer -my conclusion from other things I read-. Here is the code, what are your thoughts?

#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#include <fauxmoESP.h>


#define pinC_m 5
//#define pin_mLED 4
//#define pin_sLED 0;
#define pinS_m A0
uint8_t sensor_TH = 650;
//#include "motor.h"

fauxmoESP fauxmo;

const char* ssid = "WLAN-236684-2.4Gplus";
const char* password = "notmyrealpassword";

const char* device_1 = "Bewaesserungssystem";
//const char* device_1 = "led";



unsigned int timer1 = 0;
unsigned int timer2 = 0;
unsigned int total_time = 0;
unsigned int timeLimit = 60000; // time limit for safety can be changed here
bool flag = false; // flag if motor on or not used in the echo response function


void setup() {
  Serial.begin(115200);
  pinMode(pinC_m, OUTPUT);
  digitalWrite(pinC_m, LOW);

  //pinMode(pin_mLED, OUTPUT);
   //digitalWrite(pin_mLED);

   //pinMode(pin_sLED, OUTPUT);
   //digitalWrite(pin_sLED);

  //motor *water_pump = new motor(controlPin,sensorPin);

  if (connectWifi()) {
    // Setup fauxmo
    Serial.printf("Adding device %s", device_1 );
    fauxmo.setPort(80);
    fauxmo.enable(true);
    fauxmo.addDevice(device_1);
  }

  fauxmo.onSetState([](unsigned char device_id, const char * device_name, bool state, unsigned char value) {

    // Callback when a command from Alexa is received.
    // You can use device_id or device_name to choose the element to perform an action onto (relay, LED,...)
    // State is a boolean (ON/OFF) and value a number from 0 to 255 (if you say "set kitchen light to 50%" you will receive a 128 here).
    // Just remember not to delay too much here, this is a callback, exit as soon as possible.
    // If you have to do something more involved here set a flag and process it in your main loop.



    Serial.printf("[MAIN] Device #%d (%s) state: %s value: %d\n", device_id, device_name, state ? "ON" : "OFF", value);

    // Checking for device_id is simpler if you are certain about the order they are loaded and it does not change.
    // Otherwise comparing the device_name is safer.

    if (strcmp(device_name, device_1) == 0)
    {

      if (analogRead(pinS_m) > sensor_TH && flag == false)
      {
        Serial.println(" Sensor reading above sensor threshold value, starting motor now...");
        digitalWrite(pinC_m, HIGH); // turn motor on
        //digitalWrite(pin_mLED, HIGH); // turns indicator LED on
        timer1 = millis(); // measures the time at which motor starts
        flag = true;
      }

      else if (analogRead(pinS_m) >= sensor_TH)
        Serial.printf("Sensor reading belove sensor threshold value. Sensor Reading : %s", analogRead(pinS_m));

      else Serial.println("Motor already running");


    }
    //digitalWrite(5, state ? HIGH : LOW);

  });
}
void loop() {

  fauxmo.handle();
  timer2 = millis();
  if (flag == true && timer2 - timer1 > 500) { // the timer line is to ensure safe feed back to alexa as it takes a while for alexa to confirm fesucess

    while (1)
    {
      timer2 = millis(); // measures the time at each pass in the loop
      total_time = timer2 - timer1; // assignes the total time lapse since motor start

      if (analogRead(pinS_m) <= sensor_TH)
      {
        Serial.printf("Sensor Threshhold has been triggered! Sensor Value: %d", analogRead(pinS_m));
        digitalWrite(pinC_m, LOW); // turns motor off
        //digitalWrite(pin_mLED,LOW);
        flag = false;
        break;

      }

      else if ( total_time >= timeLimit) // if a minute has passed and the value is still hasnt been reached asssumes there is a problem and stops
      {
        Serial.printf("Time Limit has been reached! Time lapse: %d , Sensor Reading: %d ... Safety stop triggered!",
                      total_time,
                      analogRead(pinS_m)
                     );
        digitalWrite(pinC_m, LOW); // turns motor off
        flag = false;
        break;
      }

      yield(); // stops the watchdog time from resetting the esp

    }

  }
  

}
boolean connectWifi() {
  // Let us connect to WiFi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println(".......");
  Serial.println("WiFi Connected....IP Address:");
  Serial.println(WiFi.localIP());
  return true;
}

the serial monitor shows this message that makes me think it is the watchdogtimer. I shortened it becasue of charachter limitations.

WiFi Connected....IP Address:
192.168.10.101
Adding device Bewaesserungssystem[MAIN] Device #0 (Bewaesserungssystem) state: ON value: 255
 Sensor reading above sensor threshold value, starting motor now...
[MAIN] Device #0 (Bewaesserungssystem) state: ON value: 254

Exception (28):
epc1=0x4020a942 epc2=0x00000000 epc3=0x00000000 excvaddr=0x000002f0 depc=0x00000000

>>>stack>>>

ctx: sys
sp: 3fffe8c0 end: 3fffffb0 offset: 01a0
3fffea60:  3fffeb60 000002f1 3fffeac0 402090b9  
3fffea70:  00000003 4023da9b 00000000 0000003f  
3fffea80:  3ffe8304 0000003f 3fffeb60 4020cf1e  
3fffea90:  3fffeb03 3ffe866f 00000000 4020d108  
3fffeaa0:  00000000 ffffffff ffffffff 3ffe86b0  
3fffeab0:  00000000 3ffe86af 3fffeb60 4020d34f  
3fffeac0:  00000000 ffffffff 00000000 00000000  
3fffead0:  00000003 0000003f 00302073 4020ce54  
3fffeae0:  003138e0 3fffeb80 3fffeb40 00000218  
3fffeaf0:  3ffef844 00000000 00000020 40100a88  
3fffeb00:  00343532 3fffec00 00000018 00000218  
3fffeb10:  00000274 0000005a 00000000 40218e04  
3fffeb20:  3fffec90 3fffec60 0000000c 3ffe8304  
3fffeb30:  00000003 00000009 00000001 4020f284  
3fffeb40:  3fffec82 2c9f0300 4000050c 00000001  
3fffeb50:  3ffee6b8 3ffe8304 00000040 4020a86d  
3fffeb60:  3fffec4f 00000030 00000000 ffff0208  
3fffeb70:  3fffec10 0000003f 00000002 fffff000  
3fffeb80:  00028002 3ff20a00 3ffee4f8 00000003  
3fffeb90:  00000009 fffff000 00000000 00000004  
3fffeba0:  3ffee6b8 3ffe84ec 00000001 00000030  
3fffebb0:  00000000 3ffefcc4 3fffece0 3ffef844  
3fffebc0:  3fffec4c 00000000 0a343532 402062b9  
3fffebd0:  3fffec4c 00000008 3ffee6b8 4020a8b0  
3fffebe0:  3fffec90 3fffec60 00000008 0000003c  
3fffebf0:  3ffee6b8 00000008 3fffec10 40204024  
3fffec00:  00000000 3ffefcc4 00000000 40204354  
3fffec10:  736e6553 7220726f 69646165 6220676e  
3fffec20:  766f6c65 65732065 726f736e 72687420  
3fffec30:  6f687365 7620646c 65756c61 6553202e  
3fffec40:  726f736e 61655220 676e6964 3f203a20  
3fffec50:  3fffec90 3fffec60 00000008 4023656e  
3fffec60:  402365cc 00000000 000002f1 fffffd0f  
3fffec70:  00000014 0000206f 05670567 05670567  
3fffec80:  3ffe866f 000002b3 3fffec60 3fffec90  
3fffec90:  40205a53 3ffefcc4 3fffece0 00000001  
3fffeca0:  3ffe84ec 3ffee6b8 3ffee6b8 402010c1  
3fffecb0:  3ffe8837 00000001 3fffed40 3fffed30  
3fffecc0:  3ffef674 3fffece0 3ffee610 40203850  
3fffecd0:  000000fe 4023f5bd 3fffed80 402047e2  
3fffece0:  73227b5b 65636375 3a227373 6c2f227b  
3fffecf0:  74686769 2f312f73 74617473 6e6f2f65  
3fffed00:  72743a22 7d7d6575 73227b2c 65636375  
3fffed10:  3a227373 6c2f227b 74686769 2f312f73  
3fffed20:  74617473 72622f65 323a2269 7d7d3435  
3fffed30:  3fff005d 0000000b 3fffed80 40204918  
3fffed40:  7d343500 00657400 80006570 40204a90  
3fffed50:  00000000 3ffefcc4 3fffed30 4023da19  
3fffed60:  3fffedd0 3fffeddc 3ffee610 3fffeddc  
3fffed70:  3fffedd0 3ffefcc4 3ffee610 40203920  
3fffed80:  3ffef904 000b000f 00ffedd0 3ffefc4c  
3fffed90:  003c003f 0000016d 0000016d 401007c8  
  
<<<stack<<<

 ets Jan  8 2013,rst cause:2, boot mode:(3,6)

load 0x4010f000, len 1392, room 16 
tail 0
chksum 0xd0
csum 0xd0
v3d128e5c
~ld
................
WiFi Connected....IP Address:
192.168.10.101
Adding device Bewaesserungssystem

I solved the problem. It was a logic error cause by not being carefull and fast typing. Change this

if (analogRead(pinS_m) > sensor_TH && flag == false)
      {
        Serial.println(" Sensor reading above sensor threshold value, starting motor now...");
        digitalWrite(pinC_m, HIGH); // turn motor on
        //digitalWrite(pin_mLED, HIGH); // turns indicator LED on
        timer1 = millis(); // measures the time at which motor starts
        flag = true;
      }

      else if (analogRead(pinS_m) >= sensor_TH)
        Serial.printf("Sensor reading below sensor threshold value. Sensor Reading : %s", analogRead(pinS_m));

      else Serial.println("Motor already running");

to this:

if (analogRead(pinS_m) > sensor_TH && flag == false)
      {
        Serial.println(" Sensor reading above sensor threshold value, starting motor now...");
        digitalWrite(pinC_m, HIGH); // turn motor on
        //digitalWrite(pin_mLED, HIGH); // turns indicator LED on
        timer1 = millis(); // measures the time at which motor starts
        flag = true;
      }

      else if (analogRead(pinS_m) <= sensor_TH) // changed here
        Serial.printf("Sensor reading below sensor threshold value. Sensor Reading : %s", analogRead(pinS_m));

      else Serial.println("Motor already running");