Skip to content

Commit d2379ee

Browse files
authored
add static private ip, tested new esp32
1 parent 58df27e commit d2379ee

File tree

1 file changed

+57
-29
lines changed

1 file changed

+57
-29
lines changed

Arduino_ESP32dev_Webserver.ino

+57-29
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,36 @@
11
// Load Wi-Fi library
22
#include <WiFi.h>
3+
#include <ESP32Servo.h>
34

45
// 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+
717

818
// Set web server port number to 80
9-
WiFiServer server(80);
19+
WiFiServer server();
1020

1121
// Variable to store the HTTP request
1222
String header;
1323

1424
// Auxiliar variables to store the current output state
15-
String gateState = "off";
25+
String gateState = "CLOSED";
1626

17-
// Assign output variables to GPIO pins
27+
// Assign output variables to IO pin
1828
const int output26 = 26;
1929

30+
// Servo def
31+
Servo motor;
32+
int pos = 0;
33+
2034
// Current time
2135
unsigned long currentTime = millis();
2236
// Previous time
@@ -25,15 +39,19 @@ unsigned long previousTime = 0;
2539
const long timeoutTime = 2000;
2640

2741
void setup() {
42+
// Configures static IP address
43+
if (!WiFi.config(local_IP, gateway, subnet)) {
44+
Serial.println("STA Failed to configure");
45+
}
2846
Serial.begin(115200);
2947
// Initialize the output variables as outputs
30-
pinMode(output26, OUTPUT);
48+
//pinMode(output26, OUTPUT);
3149
pinMode(2, OUTPUT);
50+
motor.attach(output26); // attach the servo to the IO
3251

3352
// Set outputs to LOW
34-
digitalWrite(output26, LOW);
53+
//digitalWrite(output26, LOW);
3554

36-
3755
// Connect to Wi-Fi network with SSID and password
3856
Serial.print("Connecting to ");
3957
Serial.println(ssid);
@@ -45,8 +63,17 @@ void setup() {
4563
// Print local IP address and start web server
4664
Serial.println("");
4765
Serial.println("WiFi connected.");
48-
Serial.println("IP address: ");
66+
Serial.print("Local IP address: ");
4967
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+
5077
server.begin();
5178
}
5279

@@ -75,40 +102,41 @@ void loop(){
75102
client.println("Connection: close");
76103
client.println();
77104

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);
89116
}
90117

91118
// Display the HTML web page
92119
client.println("<!DOCTYPE html><html>");
120+
client.println("<body style=background-color:#212121;>");
93121
client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
94122
client.println("<link rel=\"icon\" href=\"data:,\">");
95123
// CSS to style the on/off buttons
96-
// Feel free to change the background-color and font-size attributes to fit your preferences
97124
client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}");
98125
client.println(".button { background-color: #4CAF50; border: none; color: white; padding: 16px 40px;");
99126
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>");
101128

102129
// Web Page Heading
103-
client.println("<body><h1>ESP32 Web Server</h1>");
130+
client.println("<body><h1 style=color:white;>Home Gate</h1>");
131+
104132

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>");
110138
} 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>");
112140
}
113141

114142

0 commit comments

Comments
 (0)