Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*********
- Original Code By: Rui Santos
- Modified By: skywired
- *********/
- #include <ESP8266WiFi.h>
- int personsCounted = 0;
- // Replace with your network credentials
- const char* ssid = "YOUR SSID";
- const char* password = "YOUR PASSWORD";
- int x = 0;
- // Set web server port number to 80
- WiFiServer server(80);
- String header;
- bool led = false;
- const int trigPin = 4;
- const int echoPin = 5;
- long duration;
- int distance;
- int calDistance = 0;//Don't change
- // Current time
- unsigned long currentTime = millis();
- // Previous time
- unsigned long previousTime = 0;
- // Define timeout time in milliseconds (example: 2000ms = 2s)
- const long timeoutTime = 2000;
- void setup() {
- pinMode(12, OUTPUT);//Declaring GPIO 12 as an output (connects to LED)
- analogWrite(12, 3);//Write LED to 3 with PWM
- Serial.begin(9600); // Starts the serial communication
- //******************************************************
- //This code is used to start the web server
- //******************************************************
- // Connect to Wi-Fi network with SSID and password
- Serial.print("Connecting to ");
- Serial.println(ssid);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);//Wait .5 seconds before trying again to connect. This may take a few tries
- Serial.print(".");
- }
- // Print local IP address and start web server
- Serial.println("");
- Serial.println("WiFi connected.");
- Serial.println("IP address: ");
- Serial.println(WiFi.localIP());//REMEMBER THIS IP ADDRESS! YOU WILL NEED TO USE IT TO CONNECT TO IT!
- server.begin();//Start the server
- analogWrite(12, 0);//Turn the LED off
- pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
- pinMode(echoPin, INPUT); // Sets the echoPin as an Input
- }
- void loop() {
- //******************************************************
- // This code is for calculating distance on the HC-SR04
- //******************************************************
- // Clears the trigPin
- digitalWrite(trigPin, LOW);
- delayMicroseconds(2);
- // Sets the trigPin on HIGH state for 10 micro seconds
- digitalWrite(trigPin, HIGH);
- delayMicroseconds(10);
- digitalWrite(trigPin, LOW);
- // Reads the echoPin, returns the sound wave travel time in microseconds
- duration = pulseIn(echoPin, HIGH);
- // Calculating the distance
- distance = duration * 0.034 / 2;
- //******************************************************
- //This code detects if a user has connected to the ESP
- //******************************************************
- WiFiClient client = server.available(); // Listen for incoming clients
- if (client) { // If a new client connects,
- Serial.println("New Client."); // print a message out in the serial port
- String currentLine = ""; // make a String to hold incoming data from the client
- currentTime = millis();
- previousTime = currentTime;
- while (client.connected() && currentTime - previousTime <= timeoutTime) { // loop while the client's connected
- currentTime = millis();
- if (client.available()) { // if there's bytes to read from the client,
- char c = client.read(); // read a byte, then
- Serial.write(c); // print it out the serial monitor
- header += c;
- if (c == '\n') { // if the byte is a newline character
- // if the current line is blank, you got two newline characters in a row.
- // that's the end of the client HTTP request, so send a response:
- if (currentLine.length() == 0) {
- // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
- // and a content-type so the client knows what's coming, then a blank line:
- client.println("HTTP/1.1 200 OK");
- client.println("Content-type:text/html");
- client.println("Connection: close");
- client.println();
- if (header.indexOf("GET /update") >= 0) {
- Serial.println("Update");
- } else if (header.indexOf("GET /reset") >= 0) {
- Serial.println("Reset");
- personsCounted = 0;
- } else if (header.indexOf("GET /calibrate") >= 0) {
- digitalWrite(12, HIGH);
- Serial.println("calibrating!");
- digitalWrite(trigPin, LOW);
- delayMicroseconds(2);
- // Sets the trigPin on HIGH state for 10 micro seconds
- digitalWrite(trigPin, HIGH);
- delayMicroseconds(10);
- digitalWrite(trigPin, LOW);
- // Reads the echoPin, returns the sound wave travel time in microseconds
- duration = pulseIn(echoPin, HIGH);
- // Calculating the distance
- calDistance = duration * 0.034 / 2;
- calDistance = calDistance - 7;
- delay(1000);
- digitalWrite(12, LOW);
- }
- //******************************************************
- //This code is for making the website the ESP is hoting.
- //******************************************************
- //This is HTML code. You can modify it to your taste if you know what you are doing, but no modifications are required.
- client.println("<!DOCTYPE html><html>");
- client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
- client.println("<link rel=\"icon\" href=\"data:,\">");
- client.println("<script>");
- client.println("function cal() {");
- client.println("alert('Calibrated!');");
- client.println("");
- client.println("}");
- client.println("</script>");
- // CSS to style the on/off buttons
- // Feel free to change the background-color and font-size attributes to fit your preferences
- client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto;}");
- client.println(".button { background-color: #195B6A; border: none; color: white; padding: 16px 40px;");
- client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");
- client.println(".button2 {background-color: #77878A;}</style></head>");
- client.println("<body><br><center><u><h1>ESP8266 Person Counter Control Panel</h1></u>");
- client.println("<br>");
- client.println("<br>");
- client.println("<h2>People Counted:</h2>");
- client.println("<br>");
- client.println("<h1>");
- client.println(personsCounted);
- client.println("</h1>");
- client.println("<br>");
- client.println("<br>");
- client.println("<br>");
- client.println("<br>");
- client.println("<p><a href=\"/reset\"><button class=\"button\">Reset Counter</button></a></p>");
- client.println("<p><a href=\"/update\"><button class=\"button button2\">Update Counter</button></a></p>");
- client.print("<p><a href=\"/calibrate\" id = \"calibrateButton\" onclick =\"cal()\"><button class=\"button button2\">Calibrate Sensor</button></a></p> <br><p>Sensor Threshold: ");
- client.print(calDistance);
- client.println("</p>");
- client.println("<h3><a href=\"https://docs.google.com/presentation/d/1rl4b2HUcnA15HfbJAZuuupapFs2aXjrh8gPIO-D9RYU/present?slide=id.g14906e2b0e6_4_0\">Help and Instructions</a><h3>");
- client.println("</center></body></html>");
- client.println();
- // Break out of the while loop
- break;
- } else { // if you got a newline, then clear currentLine
- currentLine = "";
- }
- } else if (c != '\r') { // if you got anything else but a carriage return character,
- currentLine += c; // add it to the end of the currentLine
- }
- }
- }
- // Clear the header variable
- header = "";
- // Close the connection
- client.stop();
- Serial.println("Client disconnected.");
- Serial.println("");
- }
- //******************************************************
- //This code is for detecting people an flashing the LED
- //******************************************************
- delay(1);//Wait 1 ms
- x++;//Increase the variable 'X' by one every 1 ms
- if (x == 350 || x == 351 || x == 352 || x == 353 || x == 354 || x == 355 && led == false) {
- digitalWrite(12, HIGH);
- //Turn LED on for a bit WITHOUT pausing the program using delays
- led = true;//sets the boolean to true
- }
- //Serial.println(calDistance);
- if (x > 350) {
- digitalWrite(12, LOW);
- x = 0;//Sets the light flash variable to 0 again
- //Turn LED off for a bit WITHOUT pausing the program using delays
- led = false;//sets the boolean to false
- }
- if (calDistance > 1180) {
- calDistance = 60;
- client.println("<!DOCTYPE html><html>");
- client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
- client.println("<link rel=\"icon\" href=\"data:,\">");
- client.println("<script>");
- client.println("function cal() {");
- client.println("alert('calibrated!');");
- client.println("}");
- client.println("</script>");
- // CSS to style the buttons
- // Feel free to change the background-color and font-size attributes to fit your preferences
- client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto;}");
- client.println(".button { background-color: #195B6A; border: none; color: white; padding: 16px 40px;");
- client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");
- client.println(".button2 {background-color: #77878A;}</style></head>");
- client.println("<body><br><center><u><h1>ESP8266 Person Counter Control Panel</h1></u>");
- client.println("<br>");
- client.println("<br>");
- client.println("<h2>People Counted:</h2>");
- client.println("<br>");
- client.println("<h1>");
- client.println(personsCounted);
- client.println("</h1>");
- client.println("<br>");
- client.println("<br>");
- client.println("<br>");
- client.println("<br>");
- client.println("<p><a href=\"/reset\"><button class=\"button\">Reset Counter</button></a></p>");
- client.println("<p><a href=\"/update\"><button class=\"button button2\">Update Counter</button></a></p>");
- client.print("<p><a href=\"/calibrate\" onclick =\"cal()\"><button class=\"button button2\">Calibrate Sensor</button></a></p> <br><p>Sensor Threshold: ");
- client.print("Distance is INVALID! (Over 1180cm / limit) Please adjust! [Reset sensor threshold to deault, 60cm]");
- client.println("</p>");
- client.println("<h3><a href=\"https://docs.google.com/presentation/d/1rl4b2HUcnA15HfbJAZuuupapFs2aXjrh8gPIO-D9RYU/present?slide=id.g14906e2b0e6_4_0\">Help and Instructions</a><h3>");
- client.println("</center></body></html>");
- client.println();
- }
- if ( distance < calDistance && calDistance != 0) {
- personsCounted++;//Increase the amount of people counted by one
- digitalWrite(12, HIGH);//Turn on LED
- while (distance < calDistance && calDistance != 0) {
- delay(550);//Wait until person has passed.
- // Clears the trigPin
- digitalWrite(trigPin, LOW);
- delayMicroseconds(2);
- // Sets the trigPin on HIGH state for 10 micro seconds
- digitalWrite(trigPin, HIGH);
- delayMicroseconds(10);
- digitalWrite(trigPin, LOW);
- // Reads the echoPin, returns the sound wave travel time in microseconds
- duration = pulseIn(echoPin, HIGH);
- // Calculating the distance
- distance = duration * 0.034 / 2;
- }
- digitalWrite(12, LOW);
- }
- if (calDistance == 0) {
- for (int fadeValue = 0 ; fadeValue <= 150; fadeValue += 5) {
- // sets the value (range from 0 to 255):
- analogWrite(12, fadeValue);
- // wait for 30 milliseconds to see the dimming effect
- delay(30);
- }
- delay(500);
- // fade out from max to min in increments of 5 points:
- for (int fadeValue = 150 ; fadeValue >= 0; fadeValue -= 5) {
- // sets the value (range from 0 to 255):
- analogWrite(12, fadeValue);
- // wait for 30 milliseconds to see the dimming effect
- delay(30);
- }
- delay(500);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment