If ledstate==high for 5 sec turn of wifi modem

Hi guys, i need some help on my program

-Im using a ultrasonic sensor sr04 to detect the movement

-If there is no movement ledState=HIGH, if there is a movment ledState=LOW

-If ledState=HIGH for 5 sec it will trigger modem to turn OFF

-If ledState=LOW for 5 sec it will restart the board

I've run the ultrasonic & wifi connecting code separately, its works well
but after combined the code with 'millis delay' to control the modem sleep time its seems the modem not going to sleep
can anyone enlighten me?

#include <ESP8266WiFi.h>


/*Ultrasonic Sensor*/
// defines Pin
int echoPin = D1;
int trigPin = D2; 
int ledPin = D3;
int ledState = HIGH;


// defines Ultrasonic variables
long duration;
int distance;

// define Ultrasonic range
int maximumRange = 10; // Maximum range needed
int minimumRange = 0; // Minimum range needed

/*Network*/
// WiFi Connection
const char* ssid = "xxxxx";
const char* password = "xx";
WiFiServer server(80);

// Millis Delay
unsigned long start;
unsigned long now;
const int  WifiOff = 5000;        // WifiOff...8second


void setup() {
  Serial.begin(115200);
  delay(10);

 //Ultrasonic Sensor
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

  //Unit Varible
  ledState = HIGH;

  //Unit LED output
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, ledState);

  // Connect to WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
 
  WiFi.begin(ssid, password);
  IPAddress ip(xx,x,x,xx);   
  IPAddress gateway(xx,x,x,x);   
  IPAddress subnet(xxx,xxx,xxx,x);   
  WiFi.config(ip, gateway, subnet);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
 
  // Start the server
  server.begin();
  Serial.println("Server started");
 
  // Print the IP address
  Serial.print("Use this URL to connect: ");
  Serial.print("http://");
  Serial.print(WiFi.localIP());
  Serial.println("/");
}
 
void loop() 

{


/* The following trigPin/echoPin cycle is used to determine the
 distance of the nearest object by bouncing soundwaves off of it. */ 
 digitalWrite(trigPin, LOW); 
 delayMicroseconds(2); 

 digitalWrite(trigPin, HIGH);
 delayMicroseconds(10); 
 
 digitalWrite(trigPin, LOW);
 duration = pulseIn(echoPin, HIGH);
 
 //Calculate the distance (in cm) based on the speed of sound.
 distance = duration/58.2;

 if (distance >= maximumRange || distance <= minimumRange){
 /* Send a negative number to computer and Turn LED ON 
 to indicate "out of range" */
 Serial.println("-1"); 
 ledState = HIGH;
 }

 if(ledState==HIGH)
 {
 Serial.print("NO movement!!");
 start = millis();
 }
 
  now=millis();
  if(now - start > WifiOff) 
 {
  WiFi.disconnect();
  WiFi.forceSleepBegin();
  delay(1);
  Serial.print("WifiOff");
 }
 
 else {
 /* Send the distance to the computer using Se
  *  rial protocol, and
 turn LED OFF to indicate successful reading. */
 Serial.println(distance);
 ledState = LOW;
 ESP.restart();
 
 }
 // Update the LED based on ledState
 digitalWrite(ledPin, ledState);
 
 delay(50);


 
}

-Im using a ultrasonic sensor sr04 to detect the movement

An ultrasonic sensor measures distance. How does that relate, in you mind, to movement?

I completely fail to see why a distance reading out of range causes setting a variable named ledState to anything.

I fail to see where ledState is set to any other value, when the distance is valid.

its seems the modem not going to sleep

How have you reached that conclusion?

Thanks for your reply!

My intention is to ON the wifi when someone is stand there and block the ultrasound sensor for 5sec

I set the led to LOW when the distance is valid.

 else {
 /* Send the distance to the computer using Se
  *  rial protocol, and
 turn LED OFF to indicate successful reading. */
 Serial.println(distance);
 ledState = LOW;
 ESP.restart();
[code]

To detect the modem sleep/no sleep i use network app to detect whether it is still exist in my network.

I set the led to LOW when the distance is valid.

Since the value in ledState has nothing to do with the state of an LED, you might as well give the variable a name like farmerBrownsCow. That name is equally meaningless, and just as useless.

What do your serial print statements tell you is happening? If the problem is that the ESP's actions do not match the serial print statements, that is one thing. If the problem is that the distance sensor is not working, that's a much different problem.

Hello Zhong
I am new with arduino and cannot help you with the code. however, i will suggest you use photo detector (transmitter and receiver). 4p38 receiver
good luck