-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSERVIDOR_ESP32.ino
77 lines (63 loc) · 2.26 KB
/
SERVIDOR_ESP32.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
#include <WiFi.h>
#include <WebServer.h>
#include <FS.h> // Biblioteca para el sistema de archivos SPIFFS
const char *ssid = "upsrj_PROFESORES_campus";
const char *password = "D0c9nt9$2022*#";
WebServer server(80);
void setup() {
Serial.begin(115200);
// Inicialización de la conexión WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Conectando a la red WiFi...");
}
Serial.println("Conexión WiFi establecida.");
Serial.print("Dirección IP: ");
Serial.println(WiFi.localIP());
// Inicialización del sistema de archivos SPIFFS
if (!SPIFFS.begin(true)) {
Serial.println("Error al montar el sistema de archivos SPIFFS");
return;
}
// Inicialización del servidor web
server.on("/", HTTP_GET, []() {
String content = "<h1>Bienvenido al ESP32 Web Server</h1>";
content += "<button onclick=\"guardarDatos()\">Guardar datos</button>";
content += "<script>function guardarDatos() {"
"var xhttp = new XMLHttpRequest();"
"xhttp.open('GET', '/guardar', true);"
"xhttp.send();}</script>";
server.send(200, "text/html", content);
});
// Ruta para guardar datos
server.on("/guardar", HTTP_GET, []() {
guardarDatosLocalmente();
server.send(200, "text/plain", "Datos guardados localmente");
});
server.begin();
Serial.println("Servidor HTTP iniciado.");
}
void loop() {
server.handleClient();
}
void guardarDatosLocalmente() {
// Abre un archivo en modo de escritura (si no existe, se crea)
File archivo = FS.open("/datos.txt", "a");
if (!archivo) {
Serial.println("Error al abrir el archivo de datos");
return;
}
// Obtén la hora actual para registrarla junto con los datos
String datosAGuardar = obtenerHoraActual() + ": Botón presionado\n";
// Escribe los datos en el archivo
archivo.println(datosAGuardar);
archivo.close();
Serial.println("Datos guardados localmente en el archivo datos.txt");
}
String obtenerHoraActual() {
// Obtén la hora actual del sistema
// Aquí deberías implementar tu lógica para obtener la hora actual
// Por simplicidad, retornaremos una cadena vacía en este ejemplo
return "";
}