#include <WiFi.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <EEPROM.h>
#include <esp_wifi.h>
const char* ssid = "Mi_Red_ESP32";
const char* password = "Contrasao";
AsyncWebServer server(80);
const int EEPROM_SIZE = 4096; // Tamaño total de la EEPROM
const int MAX_NOTE_LENGTH = 100; // Longitud máxima de cada nota
const int MAX_NOTES = 50; // Número máximo de notas
struct Note {
char text[MAX_NOTE_LENGTH];
};
Note notes[MAX_NOTES]; // Array para almacenar las notas
// HTML blog de notas
const char* rootPage = R"(
<html>
<head>
<title>Blog de Notas</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background-color: #f0f0f0;
}
.container {
max-width: 800px;
margin: 20px auto;
padding: 20px;
background-color: #fff;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0,0,0,0.1);
}
h1 {
font-size: 24px;
color: #333;
margin-bottom: 20px;
}
form textarea {
width: 100%;
height: 200px;
margin-bottom: 20px;
padding: 10px;
border: 1px solid #ccc;
border-radius: 4px;
}
input[type="submit"] {
background-color: #4CAF50;
color: white;
padding: 10px 20px;
border: none;
border-radius: 4px;
cursor: pointer;
}
input[type="submit"]:hover {
background-color: #45a049;
}
</style>
</head>
<body>
<div class="container">
<h1>Blog de Notas</h1>
<form action="/notes" method="POST">
<textarea name="notes" placeholder="Escribe tus notas aquí"></textarea>
<br>
<input type="submit" value="Guardar">
</form>
<hr>
<h2>Notas guardadas:</h2>
<ul>%NOTES%</ul>
<!-- Botón para resetear la memoria -->
<form action="/reset" method="GET">
<input type="submit" value="Borrar Notas y Resetear Memoria">
</form>
<!-- Enlace para ver dispositivos conectados -->
<a href="/devices">Ver dispositivos conectados</a>
</div>
</body>
</html>
)";
// HTML dispositivos conectados
const char* devicesPage = "<html><head><title>Dispositivos Conectados</title></head><body><h1>Dispositivos Conectados</h1><ul>%DEVICES%</ul></body></html>";
void saveNotesToEEPROM() {
EEPROM.begin(EEPROM_SIZE);
for (int i = 0; i < MAX_NOTES; i++) {
int addr = i * sizeof(Note);
EEPROM.put(addr, notes[i]);
}
EEPROM.commit();
}
void loadNotesFromEEPROM() {
EEPROM.begin(EEPROM_SIZE);
for (int i = 0; i < MAX_NOTES; i++) {
int addr = i * sizeof(Note);
EEPROM.get(addr, notes[i]);
}
EEPROM.end();
}
void handleRoot(AsyncWebServerRequest *request) {
String notesList;
bool foundNote = false;
for (int i = 0; i < MAX_NOTES; i++) {
if (strlen(notes[i].text) > 0) {
foundNote = true;
notesList += "<li>" + String(notes[i].text) + "</li>";
} else if (foundNote) {
// Si ya se encontró una nota, no mostrar notas vacías o con datos residuales al principio
break;
}
}
// Reemplazar %NOTES% en el HTML con las notas guardadas
String page = String(rootPage);
page.replace("%NOTES%", notesList);
// Enviar la página al cliente
request->send(200, "text/html", page);
}
void handleNotes(AsyncWebServerRequest *request) {
// Obtener la nueva nota enviada en el formulario
if (request->hasArg("notes")) {
String newNote = request->arg("notes");
// Encontrar un espacio vacío para almacenar la nueva nota
for (int i = 0; i < MAX_NOTES; i++) {
if (strlen(notes[i].text) == 0) {
strncpy(notes[i].text, newNote.c_str(), MAX_NOTE_LENGTH);
break;
}
}
// Guardar las notas en la EEPROM
saveNotesToEEPROM();
}
// Redirigir de vuelta a la página de inicio
request->redirect("/");
}
void handleReset(AsyncWebServerRequest *request) {
// Borrar todas las notas en la EEPROM
for (int i = 0; i < MAX_NOTES; i++) {
memset(notes[i].text, 0, MAX_NOTE_LENGTH);
}
saveNotesToEEPROM(); // Guardar el estado actual en la EEPROM
request->redirect("/"); // Redirigir de vuelta a la página de inicio
}
void handleDeviceInfo(AsyncWebServerRequest *request) {
// Construir la lista de dispositivos conectados en formato HTML
String devicesList;
wifi_sta_list_t stationList;
memset(&stationList, 0, sizeof(stationList));
esp_wifi_ap_get_sta_list(&stationList);
for (int i = 0; i < stationList.num; i++) {
devicesList += "<li>" + MACAddressToString(stationList.sta[i].mac) + "</li>";
}
// Reemplazar %DEVICES% en el HTML con los dispositivos conectados
String page = String(devicesPage);
page.replace("%DEVICES%", devicesList);
// Enviar la página al cliente
request->send(200, "text/html", page);
}
String MACAddressToString(uint8_t mac[6]) {
String macStr;
for (int i = 0; i < 6; i++) {
macStr += String(mac[i], HEX);
if (i < 5) macStr += ':';
}
return macStr;
}
void setup() {
Serial.begin(115200);
// Inicializar la EEPROM
EEPROM.begin(EEPROM_SIZE);
// Cargar notas desde la EEPROM al iniciar
loadNotesFromEEPROM();
// Configurar como punto de acceso Wi-Fi
WiFi.softAP(ssid, password);
IPAddress IP = WiFi.softAPIP();
Serial.print("Dirección IP del ESP32 (punto de acceso): ");
Serial.println(IP);
// Configurar manejadores de ruta para el servidor web
server.on("/", HTTP_GET, handleRoot);
server.on("/notes", HTTP_POST, handleNotes);
server.on("/reset", HTTP_GET, handleReset); // Manejador para el reseteo de la memoria
server.on("/devices", HTTP_GET, handleDeviceInfo); // Manejador para la información de dispositivos
// Iniciar el servidor web
server.begin();
}
void loop() {
}