22#include < ESP32Servo.h>
33#include " webpage.h"
44#include " server.h"
5- #include < WiFi.h>
6- #include < WebServer.h>
5+ #include < WiFi.h>
6+ #include < WebServer.h>
77
88// input output pins
99#define ULTRA_ONE_TRIG_PIN 33 // ESP32 pin GIOP23 connected to Ultrasonic Sensor's TRIG pin
@@ -80,7 +80,7 @@ void setup() {
8080 pinMode (PIN_LED, OUTPUT);
8181
8282 // turn off led
83- // status_led = false;
83+ // status_led = false;
8484 digitalWrite (PIN_LED, status_led);
8585
8686 // configure the trigger pin to output mode
@@ -96,107 +96,107 @@ void setup() {
9696
9797
9898 // setup the server
99- Serial.println (" starting server" );
100- WiFi.softAP (AP_SSID, AP_PASS);
101- delay (100 );
102- WiFi.softAPConfig (PageIP, gateway, subnet);
103- delay (100 );
104- Actual_IP = WiFi.softAPIP ();
105- Serial.print (" IP address: " );
106- Serial.println (Actual_IP);
107- printWifiStatus ();
99+ Serial.println (" starting server" );
100+ WiFi.softAP (AP_SSID, AP_PASS);
101+ delay (100 );
102+ WiFi.softAPConfig (PageIP, gateway, subnet);
103+ delay (100 );
104+ Actual_IP = WiFi.softAPIP ();
105+ Serial.print (" IP address: " );
106+ Serial.println (Actual_IP);
107+ printWifiStatus ();
108108
109109
110110 // handle server requests
111- server.on (" /" , SendWebsite);
112- server.on (" /xml" , SendXML);
111+ server.on (" /" , SendWebsite);
112+ server.on (" /xml" , SendXML);
113113
114114 // upon ESP getting /UPDATE_SLIDER string, ESP will execute the UpdateSlider function
115115 // same notion for the following .on calls
116116 // add as many as you need to process incoming strings from your web page
117117 // as you can imagine you will need to code some javascript in your web page to send such strings
118- server.on (" /UPDATE_SLIDER" , UpdateSlider);
119- server.on (" /BUTTON_0" , ProcessButton_0);
120- server.on (" /BUTTON_1" , ProcessButton_1);
118+ server.on (" /UPDATE_SLIDER" , UpdateSlider);
119+ server.on (" /BUTTON_0" , ProcessButton_0);
120+ server.on (" /BUTTON_1" , ProcessButton_1);
121121 // finally begin the server
122- server.begin ();
122+ server.begin ();
123123}
124124
125125void loop () {
126126
127- if (system_status)
128- {
129-
127+ if (system_status)
128+ {
129+
130130 // Serial.println("Reading Sensors");
131131 SensorUpdate = millis ();
132132 exit_gate_distance = get_distance (ULTRA_TWO_TRIG_PIN, ULTRA_TWO_ECHO_PIN);
133133 entry_gate_distance = get_distance (ULTRA_ONE_TRIG_PIN, ULTRA_ONE_ECHO_PIN);
134-
135- if (entry_gate_distance <= 10 && cars_num < 10 && !entry_open) {
136- cars_num++;
137- entry_open_time = millis ();
138- entry_moving_time = millis ();
139- entry_open = true ;
140- }
141134
142- if (exit_gate_distance <= 10 && cars_num >= 1 && !exit_open ) {
143- cars_num-- ;
144- exit_open_time = millis ();
145- exit_moving_time = millis ();
146- exit_open = true ;
147- }
135+ if (entry_gate_distance <= 10 && cars_num < 10 && !entry_open ) {
136+ cars_num++ ;
137+ entry_open_time = millis ();
138+ entry_moving_time = millis ();
139+ entry_open = true ;
140+ }
148141
149- Serial.print (" Cars Number: " );
150- Serial.println (cars_num);
151-
142+ if (exit_gate_distance <= 10 && cars_num >= 1 && !exit_open) {
143+ cars_num--;
144+ exit_open_time = millis ();
145+ exit_moving_time = millis ();
146+ exit_open = true ;
147+ }
152148
149+ Serial.print (" Cars Number: " );
150+ Serial.println (cars_num);
153151
154- // Entry Gate Moving control
155- // check if the gate in open state to open it gradully
156- if (entry_open
157- // && (entry_gate_distance <= 10)
158- && entry_servo_pos < 90 ) {
159- if (millis () - entry_moving_time >= moveTime) {
160- entry_servo_pos = entry_servo_pos + 3 ;
161- entry_gate_servo.write (entry_servo_pos);
162- }
163- entry_moving_time = millis ();
164- }
165- // check to close the entry gate if it is open and time done
166- if (millis () - entry_open_time >= 7000 ) {
167- if (!(entry_gate_distance <= 10 ) && entry_servo_pos > 0 ) {
168- entry_open = false ;
152+
153+
154+ // Entry Gate Moving control
155+ // check if the gate in open state to open it gradully
156+ if (entry_open
157+ // && (entry_gate_distance <= 10)
158+ && entry_servo_pos < 90 ) {
169159 if (millis () - entry_moving_time >= moveTime) {
170- entry_servo_pos = entry_servo_pos - 3 ;
160+ entry_servo_pos = entry_servo_pos + 3 ;
171161 entry_gate_servo.write (entry_servo_pos);
172162 }
173163 entry_moving_time = millis ();
174164 }
175- }
176- // Exit Gate Moving control
177- // check if the gate in open state to open it gradully
178- if (exit_open
179- // && (entry_gate_distance <= 10)
180- && exit_servo_pos < 90 ) {
181- if (millis () - exit_moving_time >= moveTime) {
182- exit_servo_pos = exit_servo_pos + 3 ;
183- exit_gate_servo.write (exit_servo_pos);
165+ // check to close the entry gate if it is open and time done
166+ if (millis () - entry_open_time >= 7000 ) {
167+ if (!(entry_gate_distance <= 10 ) && entry_servo_pos > 0 ) {
168+ entry_open = false ;
169+ if (millis () - entry_moving_time >= moveTime) {
170+ entry_servo_pos = entry_servo_pos - 3 ;
171+ entry_gate_servo.write (entry_servo_pos);
172+ }
173+ entry_moving_time = millis ();
174+ }
184175 }
185- exit_moving_time = millis ();
186- }
187- // check to close the entry gate if it is open and time done
188- if (millis () - exit_open_time >= 7000 ) {
189- if (!(exit_gate_distance <= 10 ) && exit_servo_pos > 0 ) {
190- exit_open = false ;
191- if (millis () - entry_moving_time >= moveTime) {
192- exit_servo_pos = exit_servo_pos - 3 ;
176+ // Exit Gate Moving control
177+ // check if the gate in open state to open it gradully
178+ if (exit_open
179+ // && (entry_gate_distance <= 10)
180+ && exit_servo_pos < 90 ) {
181+ if (millis () - exit_moving_time >= moveTime) {
182+ exit_servo_pos = exit_servo_pos + 3 ;
193183 exit_gate_servo.write (exit_servo_pos);
194184 }
195185 exit_moving_time = millis ();
196186 }
187+ // check to close the entry gate if it is open and time done
188+ if (millis () - exit_open_time >= 7000 ) {
189+ if (!(exit_gate_distance <= 10 ) && exit_servo_pos > 0 ) {
190+ exit_open = false ;
191+ if (millis () - entry_moving_time >= moveTime) {
192+ exit_servo_pos = exit_servo_pos - 3 ;
193+ exit_gate_servo.write (exit_servo_pos);
194+ }
195+ exit_moving_time = millis ();
196+ }
197+ }
197198 }
198- }
199- server.handleClient ();
199+ server.handleClient ();
200200}
201201
202202// function managed by an .on method to handle slider actions on the web page
@@ -226,30 +226,43 @@ void UpdateSlider() {
226226// now process button_0 press from the web site. Typical applications are the used on the web client can
227227// turn on / off a light, a fan, disable something etc
228228void ProcessButton_0 () {
229- system_status=!system_status;
230- status_led = !status_led;
229+ if (system_status) {
230+ if (entry_servo_pos > 0 || exit_servo_pos > 0 ) {
231+ server.send (200 , " text/plain" , " false" ); // Send web page
232+ }
233+ else {
234+ system_status = false ;
235+ status_led = false ;
236+ server.send (200 , " text/plain" , " off" ); // Send web page
237+
238+ }
239+ }
240+ else {
241+ system_status = true ;
242+ status_led = true ;
243+ server.send (200 , " text/plain" , " on" ); // Send web page
244+ }
231245 digitalWrite (PIN_LED, status_led);
232- Serial.print (" Button 0 " ); Serial.println (status_led);
233- server.send (200 , " text/plain" , " " ); // Send web page
246+
234247
235248}
236249// same notion for processing button_1
237250void ProcessButton_1 () {
238251 // just a simple way to toggle a LED on/off. Much better ways to do this
239252 Serial.println (" Button 1 press" );
240253
241- // server.send(200, "text/plain", ""); //Send web page
254+ // server.send(200, "text/plain", ""); //Send web page
242255
243- if (!entry_open&& !exit_open) {
244- cars_num= 0 ;
245- Serial.println (" Cars number reset." );
256+ if (!entry_open && !exit_open) {
257+ cars_num = 0 ;
258+ Serial.println (" Cars number reset." );
246259 server.send (200 , " text/plain" , " true" ); // Send web page
247-
248- }
249- else {
260+
261+ }
262+ else {
250263 server.send (200 , " text/plain" , " false" ); // Send web page
251- }
252-
264+ }
265+
253266}
254267
255268
@@ -272,7 +285,7 @@ void SendXML() {
272285 // send distance_cm
273286 sprintf (buf, " <B0>%d</B0>\n " , cars_num);
274287 strcat (XML, buf);
275-
288+
276289 sprintf (buf, " <B1>%d</B1>\n " , entry_gate_distance);
277290 strcat (XML, buf);
278291
@@ -287,18 +300,18 @@ void SendXML() {
287300 else {
288301 strcat (XML, " <LED>0</LED>\n " );
289302 }
290- // if (!entry_open&&!exit_open) {
291- // strcat(XML, "<SWITCH>true</SWITCH>\n");
292- // }
293- // else {
294- // strcat(XML, "<SWITCH>false</SWITCH>\n");
295- // }
303+ // if (!entry_open&&!exit_open) {
304+ // strcat(XML, "<SWITCH>true</SWITCH>\n");
305+ // }
306+ // else {
307+ // strcat(XML, "<SWITCH>false</SWITCH>\n");
308+ // }
296309
297310 strcat (XML, " </Data>\n " );
298311 // wanna see what the XML code looks like?
299312 // actually print it to the serial monitor and use some text editor to get the size
300313 // then pad and adjust char XML[2048]; above
301- // Serial.println(XML);
314+ // Serial.println(XML);
302315
303316 // you may have to play with this value, big pages need more porcessing time, and hence
304317 // a longer timeout that 200 ms
0 commit comments