-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathespantapalomamejorado.cpp
242 lines (227 loc) · 6.33 KB
/
espantapalomamejorado.cpp
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>
#include <Ticker.h>
const int audioPin = 13; // Pin para el tono
const int gpioEstado = 4; // Pin GPIO para el indicador de estado
ESP8266WebServer server(80);
Ticker frecuenciaTicker;
bool frecuenciaActiva = false;
int frecuenciaActual = 0;
// Actualizar estado del GPIO
void actualizarEstadoGPIO() {
digitalWrite(gpioEstado, frecuenciaActiva ? LOW : HIGH);
}
// Activar frecuencia específica
void activarFrecuencia(int frecuencia) {
frecuenciaActual = frecuencia;
tone(audioPin, frecuencia);
frecuenciaActiva = true;
actualizarEstadoGPIO();
}
// Detener frecuencia activa
void detenerFrecuencia() {
noTone(audioPin);
frecuenciaActiva = false;
actualizarEstadoGPIO();
}
// Alternar encendido/apagado cada 8.3 segundos
void manejarFrecuencia() {
if (frecuenciaActiva) {
detenerFrecuencia();
} else {
activarFrecuencia(frecuenciaActual);
}
}
// HTML con botón de apagado total
const char index_html[] PROGMEM = R"rawliteral(
<!DOCTYPE HTML>
<html lang="es">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Control de Plagas</title>
<link rel="stylesheet" href="https://adminlte.io/themes/v3/plugins/fontawesome-free/css/all.min.css">
<link rel="stylesheet" href="https://adminlte.io/themes/v3/dist/css/adminlte.min.css">
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #2f2f2f;
color: #f8f9fa;
font-family: 'Source Sans Pro', sans-serif;
margin: 0;
}
.card {
background: #3d3d3d;
border-radius: 15px;
padding: 40px;
width: 400px;
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
text-align: center;
}
h1 {
font-size: 2rem;
margin-bottom: 30px;
color: #e1e1e1;
text-shadow: 1px 1px 3px rgba(0, 0, 0, 0.7);
}
.toggle-container {
display: flex;
flex-direction: column;
gap: 20px;
margin-top: 20px;
}
.toggle-group {
display: flex;
align-items: center;
justify-content: space-between;
}
.toggle-switch {
position: relative;
display: inline-block;
width: 60px;
height: 30px;
}
.toggle-switch input {
opacity: 0;
width: 0;
height: 0;
}
.slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #6c757d;
transition: .4s;
border-radius: 30px;
}
.slider:before {
position: absolute;
content: "";
height: 22px;
width: 22px;
left: 4px;
bottom: 4px;
background-color: white;
transition: .4s;
border-radius: 50%;
}
input:checked + .slider {
background-color: #20c997;
}
input:checked + .slider:before {
transform: translateX(28px);
}
.shutdown-button {
margin-top: 30px;
padding: 10px 20px;
background-color: #dc3545;
color: white;
border: none;
border-radius: 5px;
font-size: 1rem;
cursor: pointer;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.2);
}
.shutdown-button:hover {
background-color: #c82333;
}
.footer {
margin-top: 30px;
font-size: 0.85rem;
color: #adb5bd;
}
</style>
</head>
<body>
<div class="card">
<h1>Control de Plagas 🦟</h1>
<div class="toggle-container">
<div class="toggle-group">
🕊️ Palomas
<label class="toggle-switch">
<input type="checkbox" id="palomasToggle" onchange="toggleExclusive('palomasToggle', '/palomas')">
<span class="slider"></span>
</label>
</div>
<div class="toggle-group">
🎵 Test
<label class="toggle-switch">
<input type="checkbox" id="testToggle" onchange="toggleExclusive('testToggle', '/test')">
<span class="slider"></span>
</label>
</div>
</div>
<button class="shutdown-button" onclick="apagarSistema()">⏹️ Apagar Sistema</button>
<div class="footer">
<i class="fas fa-copyright"></i> 2024 Control de Plagas
</div>
</div>
<script>
function toggleExclusive(activeToggle, url) {
const palomasToggle = document.getElementById('palomasToggle');
const testToggle = document.getElementById('testToggle');
if (activeToggle === 'palomasToggle' && testToggle.checked) {
testToggle.checked = false;
} else if (activeToggle === 'testToggle' && palomasToggle.checked) {
palomasToggle.checked = false;
}
fetch(url)
.then(response => response.text())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));
}
function apagarSistema() {
document.getElementById('palomasToggle').checked = false;
document.getElementById('testToggle').checked = false;
fetch('/shutdown')
.then(response => response.text())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));
}
</script>
</body>
</html>
)rawliteral";
// Configuración del servidor web
void setupServer() {
server.on("/", []() {
server.send_P(200, "text/html", index_html);
});
server.on("/palomas", []() {
detenerFrecuencia();
activarFrecuencia(18000); // Frecuencia para Palomas: 18 kHz
frecuenciaTicker.attach(8.3, manejarFrecuencia);
server.send(200, "text/plain", "Frecuencia de palomas activada");
});
server.on("/test", []() {
detenerFrecuencia();
activarFrecuencia(8000); // Frecuencia de Test: 15 kHz
frecuenciaTicker.attach(8.3, manejarFrecuencia);
server.send(200, "text/plain", "Frecuencia de prueba activada");
});
server.on("/shutdown", []() {
detenerFrecuencia();
frecuenciaTicker.detach();
server.send(200, "text/plain", "Sistema apagado");
});
server.begin();
Serial.println("Servidor web iniciado en la IP: " + WiFi.localIP().toString());
}
void setup() {
Serial.begin(115200);
pinMode(gpioEstado, OUTPUT);
actualizarEstadoGPIO();
WiFiManager wifiManager;
wifiManager.autoConnect("ControlDePlaga");
setupServer();
}
void loop() {
server.handleClient();
}