1
1
// Load Wi-Fi library
2
2
#include < WiFi.h>
3
+ #include < ESP32Servo.h>
3
4
4
5
// Network credentials
5
- const char * ssid = " Moti" ;
6
- const char * password = " 18101965" ;
6
+ const char * ssid = " " ;
7
+ const char * password = " " ;
8
+ // Set Static IP address
9
+ IPAddress local_IP ();
10
+ // Set Gateway IP address
11
+ IPAddress gateway ();
12
+ // Subnet mask
13
+ IPAddress subnet ();
14
+
15
+
16
+
7
17
8
18
// Set web server port number to 80
9
- WiFiServer server (80 );
19
+ WiFiServer server ();
10
20
11
21
// Variable to store the HTTP request
12
22
String header;
13
23
14
24
// Auxiliar variables to store the current output state
15
- String gateState = " off " ;
25
+ String gateState = " CLOSED " ;
16
26
17
- // Assign output variables to GPIO pins
27
+ // Assign output variables to IO pin
18
28
const int output26 = 26 ;
19
29
30
+ // Servo def
31
+ Servo motor;
32
+ int pos = 0 ;
33
+
20
34
// Current time
21
35
unsigned long currentTime = millis();
22
36
// Previous time
@@ -25,15 +39,19 @@ unsigned long previousTime = 0;
25
39
const long timeoutTime = 2000 ;
26
40
27
41
void setup () {
42
+ // Configures static IP address
43
+ if (!WiFi.config (local_IP, gateway, subnet)) {
44
+ Serial.println (" STA Failed to configure" );
45
+ }
28
46
Serial.begin (115200 );
29
47
// Initialize the output variables as outputs
30
- pinMode (output26, OUTPUT);
48
+ // pinMode(output26, OUTPUT);
31
49
pinMode (2 , OUTPUT);
50
+ motor.attach (output26); // attach the servo to the IO
32
51
33
52
// Set outputs to LOW
34
- digitalWrite (output26, LOW);
53
+ // digitalWrite(output26, LOW);
35
54
36
-
37
55
// Connect to Wi-Fi network with SSID and password
38
56
Serial.print (" Connecting to " );
39
57
Serial.println (ssid);
@@ -45,8 +63,17 @@ void setup() {
45
63
// Print local IP address and start web server
46
64
Serial.println (" " );
47
65
Serial.println (" WiFi connected." );
48
- Serial.println ( " IP address: " );
66
+ Serial.print ( " Local IP address: " );
49
67
Serial.println (WiFi.localIP ());
68
+ Serial.print (" Subnet Mask: " );
69
+ Serial.println (WiFi.subnetMask ());
70
+ Serial.print (" Gateway IP address: " );
71
+ Serial.println (WiFi.gatewayIP ());
72
+ Serial.print (" DNS 1: " );
73
+ Serial.println (WiFi.dnsIP (0 ));
74
+ Serial.print (" DNS 2: " );
75
+ Serial.println (WiFi.dnsIP (1 ));
76
+
50
77
server.begin ();
51
78
}
52
79
@@ -75,40 +102,41 @@ void loop(){
75
102
client.println (" Connection: close" );
76
103
client.println ();
77
104
78
- // turns the GPIOs on and off
79
- if (header.indexOf (" GET /26/on " ) >= 0 ) {
80
- Serial. println ( " GPIO 26 on " ) ;
81
- gateState = " on " ;
82
- digitalWrite (output26 , HIGH);
83
- digitalWrite ( 2 , HIGH); // turn the LED on (HIGH is the voltage level)
84
- } else if (header.indexOf (" GET /26/off " ) >= 0 ) {
85
- Serial. println ( " GPIO 26 off " ) ;
86
- gateState = " off " ;
87
- digitalWrite (output26 , LOW);
88
- digitalWrite ( 2 , LOW); // turn the LED off by making the voltage LOW
105
+ // turns the IOs on and off
106
+ if (header.indexOf (" GET /open " ) >= 0 ) {
107
+ gateState = " OPEN " ;
108
+ // digitalWrite(output26, HIGH) ;
109
+ // digitalWrite(2 , HIGH); // turn the LED on (HIGH is the voltage level)
110
+ motor. write ( 180 );
111
+ } else if (header.indexOf (" GET /close " ) >= 0 ) {
112
+ gateState = " CLOSED " ;
113
+ // digitalWrite(output26, LOW) ;
114
+ // digitalWrite(2 , LOW); // turn the LED off by making the voltage LOW
115
+ motor. write ( 0 );
89
116
}
90
117
91
118
// Display the HTML web page
92
119
client.println (" <!DOCTYPE html><html>" );
120
+ client.println (" <body style=background-color:#212121;>" );
93
121
client.println (" <head><meta name=\" viewport\" content=\" width=device-width, initial-scale=1\" >" );
94
122
client.println (" <link rel=\" icon\" href=\" data:,\" >" );
95
123
// CSS to style the on/off buttons
96
- // Feel free to change the background-color and font-size attributes to fit your preferences
97
124
client.println (" <style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}" );
98
125
client.println (" .button { background-color: #4CAF50; border: none; color: white; padding: 16px 40px;" );
99
126
client.println (" text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}" );
100
- client.println (" .button2 {background-color: #555555 ;}</style></head>" );
127
+ client.println (" .button2 {background-color: #AF4C50 ;}</style></head>" );
101
128
102
129
// Web Page Heading
103
- client.println (" <body><h1>ESP32 Web Server</h1>" );
130
+ client.println (" <body><h1 style=color:white;>Home Gate</h1>" );
131
+
104
132
105
- // Display current state, and ON/OFF buttons for GPIO 26
106
- client.println (" <p>GPIO 26 - State " + gateState + " </p>" );
107
- // If the output26State is off , it displays the ON button
108
- if (gateState==" off " ) {
109
- client.println (" <p><a href=\" /26/on \" ><button class=\" button\" >ON </button></a></p>" );
133
+ // Display current state, and ON/OFF buttons for IO
134
+ client.println (" <p style=color:white;>The gate is " + gateState + " </p>" );
135
+ // If the gate is CLOSED , it displays the OPEN the gate button
136
+ if (gateState==" CLOSED " ) {
137
+ client.println (" <p><a href=\" /open \" ><button class=\" button\" >OPEN the gate </button></a></p>" );
110
138
} else {
111
- client.println (" <p><a href=\" /26/off \" ><button class=\" button button2\" >OFF </button></a></p>" );
139
+ client.println (" <p><a href=\" /close \" ><button class=\" button button2\" >CLOSE the gate </button></a></p>" );
112
140
}
113
141
114
142
0 commit comments