BobTheCoder

WiFi Person Counter Arduino Code

Sep 2nd, 2022 (edited)
341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.57 KB | Source Code | 0 0
  1. /*********
  2. Original Code By: Rui Santos
  3. Modified By: skywired
  4. *********/
  5. #include <ESP8266WiFi.h>
  6. int personsCounted = 0;
  7. // Replace with your network credentials
  8. const char* ssid = "YOUR SSID";
  9. const char* password = "YOUR PASSWORD";
  10. int x = 0;
  11. // Set web server port number to 80
  12. WiFiServer server(80);
  13.  
  14.  
  15.  
  16.  
  17. String header;
  18. bool led = false;
  19. const int trigPin = 4;
  20. const int echoPin = 5;
  21.  
  22.  
  23.  
  24.  
  25. long duration;
  26. int distance;
  27. int calDistance = 0;//Don't change
  28. // Current time
  29. unsigned long currentTime = millis();
  30. // Previous time
  31. unsigned long previousTime = 0;
  32. // Define timeout time in milliseconds (example: 2000ms = 2s)
  33. const long timeoutTime = 2000;
  34.  
  35.  
  36. void setup() {
  37. pinMode(12, OUTPUT);//Declaring GPIO 12 as an output (connects to LED)
  38.  
  39.  
  40. analogWrite(12, 3);//Write LED to 3 with PWM
  41.  
  42.  
  43. Serial.begin(9600); // Starts the serial communication
  44.  
  45.  
  46.  
  47.  
  48. //******************************************************
  49. //This code is used to start the web server
  50. //******************************************************
  51.  
  52.  
  53. // Connect to Wi-Fi network with SSID and password
  54. Serial.print("Connecting to ");
  55. Serial.println(ssid);
  56.  
  57.  
  58. WiFi.begin(ssid, password);
  59.  
  60.  
  61. while (WiFi.status() != WL_CONNECTED) {
  62. delay(500);//Wait .5 seconds before trying again to connect. This may take a few tries
  63. Serial.print(".");
  64. }
  65.  
  66.  
  67. // Print local IP address and start web server
  68. Serial.println("");
  69. Serial.println("WiFi connected.");
  70. Serial.println("IP address: ");
  71. Serial.println(WiFi.localIP());//REMEMBER THIS IP ADDRESS! YOU WILL NEED TO USE IT TO CONNECT TO IT!
  72.  
  73.  
  74. server.begin();//Start the server
  75.  
  76.  
  77. analogWrite(12, 0);//Turn the LED off
  78.  
  79.  
  80. pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  81. pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  82.  
  83.  
  84. }
  85.  
  86.  
  87.  
  88.  
  89. void loop() {
  90.  
  91.  
  92. //******************************************************
  93. // This code is for calculating distance on the HC-SR04
  94. //******************************************************
  95. // Clears the trigPin
  96. digitalWrite(trigPin, LOW);
  97. delayMicroseconds(2);
  98.  
  99.  
  100. // Sets the trigPin on HIGH state for 10 micro seconds
  101. digitalWrite(trigPin, HIGH);
  102. delayMicroseconds(10);
  103. digitalWrite(trigPin, LOW);
  104.  
  105.  
  106. // Reads the echoPin, returns the sound wave travel time in microseconds
  107. duration = pulseIn(echoPin, HIGH);
  108.  
  109.  
  110. // Calculating the distance
  111. distance = duration * 0.034 / 2;
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122. //******************************************************
  123. //This code detects if a user has connected to the ESP
  124. //******************************************************
  125. WiFiClient client = server.available(); // Listen for incoming clients
  126.  
  127.  
  128. if (client) { // If a new client connects,
  129. Serial.println("New Client."); // print a message out in the serial port
  130. String currentLine = ""; // make a String to hold incoming data from the client
  131. currentTime = millis();
  132. previousTime = currentTime;
  133. while (client.connected() && currentTime - previousTime <= timeoutTime) { // loop while the client's connected
  134. currentTime = millis();
  135. if (client.available()) { // if there's bytes to read from the client,
  136. char c = client.read(); // read a byte, then
  137. Serial.write(c); // print it out the serial monitor
  138. header += c;
  139. if (c == '\n') { // if the byte is a newline character
  140. // if the current line is blank, you got two newline characters in a row.
  141. // that's the end of the client HTTP request, so send a response:
  142. if (currentLine.length() == 0) {
  143. // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
  144. // and a content-type so the client knows what's coming, then a blank line:
  145. client.println("HTTP/1.1 200 OK");
  146. client.println("Content-type:text/html");
  147. client.println("Connection: close");
  148. client.println();
  149.  
  150.  
  151.  
  152.  
  153. if (header.indexOf("GET /update") >= 0) {
  154. Serial.println("Update");
  155.  
  156.  
  157. } else if (header.indexOf("GET /reset") >= 0) {
  158.  
  159.  
  160. Serial.println("Reset");
  161. personsCounted = 0;
  162.  
  163.  
  164.  
  165.  
  166. } else if (header.indexOf("GET /calibrate") >= 0) {
  167. digitalWrite(12, HIGH);
  168. Serial.println("calibrating!");
  169. digitalWrite(trigPin, LOW);
  170. delayMicroseconds(2);
  171.  
  172.  
  173. // Sets the trigPin on HIGH state for 10 micro seconds
  174. digitalWrite(trigPin, HIGH);
  175. delayMicroseconds(10);
  176. digitalWrite(trigPin, LOW);
  177.  
  178.  
  179. // Reads the echoPin, returns the sound wave travel time in microseconds
  180. duration = pulseIn(echoPin, HIGH);
  181.  
  182.  
  183. // Calculating the distance
  184. calDistance = duration * 0.034 / 2;
  185. calDistance = calDistance - 7;
  186. delay(1000);
  187. digitalWrite(12, LOW);
  188. }
  189.  
  190.  
  191. //******************************************************
  192. //This code is for making the website the ESP is hoting.
  193. //******************************************************
  194.  
  195.  
  196. //This is HTML code. You can modify it to your taste if you know what you are doing, but no modifications are required.
  197.  
  198.  
  199. client.println("<!DOCTYPE html><html>");
  200. client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
  201. client.println("<link rel=\"icon\" href=\"data:,\">");
  202. client.println("<script>");
  203. client.println("function cal() {");
  204. client.println("alert('Calibrated!');");
  205. client.println("");
  206.  
  207.  
  208. client.println("}");
  209. client.println("</script>");
  210. // CSS to style the on/off buttons
  211. // Feel free to change the background-color and font-size attributes to fit your preferences
  212. client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto;}");
  213. client.println(".button { background-color: #195B6A; border: none; color: white; padding: 16px 40px;");
  214. client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");
  215. client.println(".button2 {background-color: #77878A;}</style></head>");
  216. client.println("<body><br><center><u><h1>ESP8266 Person Counter Control Panel</h1></u>");
  217. client.println("<br>");
  218. client.println("<br>");
  219. client.println("<h2>People Counted:</h2>");
  220. client.println("<br>");
  221. client.println("<h1>");
  222. client.println(personsCounted);
  223. client.println("</h1>");
  224. client.println("<br>");
  225. client.println("<br>");
  226. client.println("<br>");
  227. client.println("<br>");
  228. client.println("<p><a href=\"/reset\"><button class=\"button\">Reset Counter</button></a></p>");
  229. client.println("<p><a href=\"/update\"><button class=\"button button2\">Update Counter</button></a></p>");
  230. client.print("<p><a href=\"/calibrate\" id = \"calibrateButton\" onclick =\"cal()\"><button class=\"button button2\">Calibrate Sensor</button></a></p>&nbsp;&nbsp;&nbsp;<br><p>Sensor Threshold: ");
  231. client.print(calDistance);
  232. client.println("</p>");
  233. client.println("<h3><a href=\"https://docs.google.com/presentation/d/1rl4b2HUcnA15HfbJAZuuupapFs2aXjrh8gPIO-D9RYU/present?slide=id.g14906e2b0e6_4_0\">Help and Instructions</a><h3>");
  234. client.println("</center></body></html>");
  235. client.println();
  236.  
  237.  
  238. // Break out of the while loop
  239. break;
  240. } else { // if you got a newline, then clear currentLine
  241. currentLine = "";
  242. }
  243. } else if (c != '\r') { // if you got anything else but a carriage return character,
  244. currentLine += c; // add it to the end of the currentLine
  245. }
  246. }
  247. }
  248. // Clear the header variable
  249. header = "";
  250. // Close the connection
  251. client.stop();
  252. Serial.println("Client disconnected.");
  253. Serial.println("");
  254. }
  255.  
  256.  
  257. //******************************************************
  258. //This code is for detecting people an flashing the LED
  259. //******************************************************
  260. delay(1);//Wait 1 ms
  261.  
  262.  
  263. x++;//Increase the variable 'X' by one every 1 ms
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270. if (x == 350 || x == 351 || x == 352 || x == 353 || x == 354 || x == 355 && led == false) {
  271. digitalWrite(12, HIGH);
  272. //Turn LED on for a bit WITHOUT pausing the program using delays
  273. led = true;//sets the boolean to true
  274. }
  275.  
  276.  
  277. //Serial.println(calDistance);
  278. if (x > 350) {
  279. digitalWrite(12, LOW);
  280. x = 0;//Sets the light flash variable to 0 again
  281. //Turn LED off for a bit WITHOUT pausing the program using delays
  282. led = false;//sets the boolean to false
  283. }
  284. if (calDistance > 1180) {
  285. calDistance = 60;
  286. client.println("<!DOCTYPE html><html>");
  287. client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
  288. client.println("<link rel=\"icon\" href=\"data:,\">");
  289. client.println("<script>");
  290. client.println("function cal() {");
  291. client.println("alert('calibrated!');");
  292. client.println("}");
  293. client.println("</script>");
  294. // CSS to style the buttons
  295. // Feel free to change the background-color and font-size attributes to fit your preferences
  296. client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto;}");
  297. client.println(".button { background-color: #195B6A; border: none; color: white; padding: 16px 40px;");
  298. client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");
  299. client.println(".button2 {background-color: #77878A;}</style></head>");
  300. client.println("<body><br><center><u><h1>ESP8266 Person Counter Control Panel</h1></u>");
  301. client.println("<br>");
  302. client.println("<br>");
  303. client.println("<h2>People Counted:</h2>");
  304. client.println("<br>");
  305. client.println("<h1>");
  306. client.println(personsCounted);
  307. client.println("</h1>");
  308. client.println("<br>");
  309. client.println("<br>");
  310. client.println("<br>");
  311. client.println("<br>");
  312. client.println("<p><a href=\"/reset\"><button class=\"button\">Reset Counter</button></a></p>");
  313. client.println("<p><a href=\"/update\"><button class=\"button button2\">Update Counter</button></a></p>");
  314. client.print("<p><a href=\"/calibrate\" onclick =\"cal()\"><button class=\"button button2\">Calibrate Sensor</button></a></p>&nbsp;&nbsp;&nbsp;<br><p>Sensor Threshold: ");
  315. client.print("Distance is INVALID! (Over 1180cm / limit) Please adjust! [Reset sensor threshold to deault, 60cm]");
  316. client.println("</p>");
  317. client.println("<h3><a href=\"https://docs.google.com/presentation/d/1rl4b2HUcnA15HfbJAZuuupapFs2aXjrh8gPIO-D9RYU/present?slide=id.g14906e2b0e6_4_0\">Help and Instructions</a><h3>");
  318. client.println("</center></body></html>");
  319. client.println();
  320. }
  321. if ( distance < calDistance && calDistance != 0) {
  322. personsCounted++;//Increase the amount of people counted by one
  323. digitalWrite(12, HIGH);//Turn on LED
  324. while (distance < calDistance && calDistance != 0) {
  325. delay(550);//Wait until person has passed.
  326. // Clears the trigPin
  327. digitalWrite(trigPin, LOW);
  328. delayMicroseconds(2);
  329.  
  330.  
  331. // Sets the trigPin on HIGH state for 10 micro seconds
  332. digitalWrite(trigPin, HIGH);
  333. delayMicroseconds(10);
  334. digitalWrite(trigPin, LOW);
  335.  
  336.  
  337. // Reads the echoPin, returns the sound wave travel time in microseconds
  338. duration = pulseIn(echoPin, HIGH);
  339.  
  340.  
  341. // Calculating the distance
  342. distance = duration * 0.034 / 2;
  343. }
  344. digitalWrite(12, LOW);
  345. }
  346. if (calDistance == 0) {
  347. for (int fadeValue = 0 ; fadeValue <= 150; fadeValue += 5) {
  348. // sets the value (range from 0 to 255):
  349. analogWrite(12, fadeValue);
  350. // wait for 30 milliseconds to see the dimming effect
  351. delay(30);
  352. }
  353. delay(500);
  354. // fade out from max to min in increments of 5 points:
  355. for (int fadeValue = 150 ; fadeValue >= 0; fadeValue -= 5) {
  356. // sets the value (range from 0 to 255):
  357. analogWrite(12, fadeValue);
  358. // wait for 30 milliseconds to see the dimming effect
  359. delay(30);
  360. }
  361. delay(500);
  362. }
  363.  
  364.  
  365. }
Advertisement
Add Comment
Please, Sign In to add comment