-
Notifications
You must be signed in to change notification settings - Fork 1
/
esp32-web.ino
197 lines (151 loc) · 6.03 KB
/
esp32-web.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include <WiFi.h>
#include <WebServer.h>
#include <ESP32Servo.h>
#define SERVO_PIN 18 // Change to your servo's connected pin
#define MAX_DEGREE 180 // Maximum degree for servo
#define MIN_US 500 // Minimum pulse width for servo in microseconds
#define MAX_US 2400 // Maximum pulse width for servo in microseconds
const char* ssid = "Wifi_SSID";
const char* password = "Wifi Password";
WebServer server(80);
Servo myServo;
int currentPosition = 90;
// Initial value for demonstration
int value = 15;
int progressBarWidth;
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
// After connecting to WiFi
myServo.attach(SERVO_PIN, MIN_US, MAX_US);
moveServo(0);
int servoPin = 18; // GPIO pin connected to the servo motor
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi..");
}
Serial.println("Connected to the WiFi network");
server.on("/", HTTP_GET, []() {
sendMainPage();
});
server.on("/increment", HTTP_GET, []() {
if (value < 30){
value++; // Increment the value
moveServo(6);
// Calculate the new width percentage
progressBarWidth = (value * 100) / 30;
// Generate the updated progress bar HTML snippet
String volumeValue =
"<div id=\"barCount\">"
"<div class=\"progress\" role=\"progressbar\" aria-valuemin=\"0\" aria-valuemax=\"60\" aria-valuenow=\"0\" aria-labelledby=\"pblabel\">"
"<div id=\"pb\" class=\"progress-bar\" style=\"width:" + String(progressBarWidth) + "%\">" + String(progressBarWidth) + "%</div>"
"</div>"
"</div>";
server.send(200, "text/html", volumeValue );
}
});
server.on("/decrement", HTTP_GET, []() {
if (value > 0){
value--; // Decrement the value
//server.send(200, "text/html", "Volume: " + String(value)); // Send back only the updated value
moveServo(-6);
// Calculate the new width percentage
progressBarWidth = (value * 100) / 30;
// Generate the updated progress bar HTML snippet
String volumeValue =
"<div id=\"barCount\">"
"<div class=\"progress\" role=\"progressbar\" aria-valuemin=\"0\" aria-valuemax=\"60\" aria-valuenow=\"0\" aria-labelledby=\"pblabel\">"
"<div id=\"pb\" class=\"progress-bar\" style=\"width:" + String(progressBarWidth) + "%\">" + String(progressBarWidth) + "%</div>"
"</div>"
"</div>";
server.send(200, "text/html", volumeValue );
}
});
server.begin();
}
void loop() {
server.handleClient();
}
void sendMainPage() {
// Dynamic part for the value display
int progressBarWidth = (value * 100) / 30;
String html =
"<!DOCTYPE html><html>"
"<head>"
"<meta name='viewport' content='width=device-width, initial-scale=1'>"
"<link rel='icon' href='data:,'>"
"<script src='https://unpkg.com/htmx.org'></script>"
"<link rel=\"stylesheet\" href=\"https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css\">"
"<style>"
"html, body { "
" height: 100%; " // Ensure the body takes full viewport height
" margin: 0; "
" display: flex; "
" justify-content: center; "
" align-items: center; "
" background-image: url('https://background.com'); " // Replace with your image URL
" background-size: cover; "
" background-position: center; "
"}"
".container { "
" background-color: rgba(255, 255, 255, 0.8); "
" border-radius: 10px; "
" padding: 20px; "
" box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); "
"}"
".btn { margin: 5px; }"
".progress {"
" height: 20px;"
" margin-bottom: 20px;"
" overflow: hidden;"
" background-color: #f5f5f5;"
" border-radius: 4px;"
" box-shadow: inset 0 1px 2px rgba(0,0,0,.1);"
"}"
".progress-bar {"
" float: left;"
" width: 0%;"
" height: 100%;"
" font-size: 12px;"
" line-height: 20px;"
" color: #fff;"
" text-align: center;"
" background-color: #337ab7;"
" -webkit-box-shadow: inset 0 -1px 0 rgba(0,0,0,.15);"
" box-shadow: inset 0 -1px 0 rgba(0,0,0,.15);"
" -webkit-transition: width .6s ease;"
" -o-transition: width .6s ease;"
" transition: width .6s ease;"
"}"
"</style>"
"</head>"
"<body>"
"<div class=\"container my-5 bg-dark text-white\">"
" <div class=\"row justify-content-center\">"
" <div class=\"col-12 col-md-8 text-center\">"
" <h1>Volume Control</h1>"
" <div id=\"control-panel\" class=\"my-4\">"
" <button class=\"btn btn-success\" hx-get=\"/increment\" hx-target=\"#barCount\" hx-swap=\"outerHTML\">Up</button>"
" <div id=\"barCount\">"
"<div class=\"progress\" role=\"progressbar\" hx-target=\"#this\" hx-swap=\"outerHTML\" aria-valuemin=\"0\" aria-valuemax=\"60\" aria-valuenow=\"0\" aria-labelledby=\"pblabel\">"
"<div id=\"pb\" class=\"progress-bar\" style=\"width:" + String(progressBarWidth) + "%\">" + String(progressBarWidth) + "%</div>"
"</div>"
"</div>"
" <button class=\"btn btn-danger\" hx-get=\"/decrement\" hx-target=\"#barCount\" hx-swap=\"outerHTML\">Down</button>"
" </div>"
" </div>"
" </div>"
"</div>"
"</body></html>";
server.send(200, "text/html", html);
}
void moveServo(int delta) {
// Update currentPosition based on the input delta
currentPosition -= delta;
// Clamp the currentPosition to ensure it's within 0 to MAX_DEGREE degrees
currentPosition = max(0, min(currentPosition, MAX_DEGREE));
// Move the servo to the new currentPosition
myServo.write(currentPosition);
Serial.print("Moving servo to ");
Serial.println(currentPosition);
}