#include <Arduino.h>
#include <WiFi.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Arduino_JSON.h>
#include "LittleFS.h" // <-- Usar LittleFS
// Credenciales WiFi
const char* ssid = "";
const char* password = "";
// Servidor
AsyncWebServer server(80);
AsyncEventSource events("/events");
// Variables JSON y temporizadores
JSONVar readings;
unsigned long lastTime = 0;
unsigned long lastTimeTemperature = 0;
unsigned long lastTimeAcc = 0;
unsigned long gyroDelay = 10;
unsigned long temperatureDelay = 1000;
unsigned long accelerometerDelay = 200;
// Sensor MPU6050
Adafruit_MPU6050 mpu;
sensors_event_t a, g, temp;
float gyroX, gyroY, gyroZ;
float accX, accY, accZ;
float temperature;
float gyroXerror = 0.07;
float gyroYerror = 0.03;
float gyroZerror = 0.01;
void initMPU() {
if (!mpu.begin()) {
Serial.println("No se encontró el chip MPU6050");
while (1) delay(10);
}
Serial.println("MPU6050 encontrado");
}
void initLittleFS() {
if (!LittleFS.begin()) {
Serial.println("Error al montar LittleFS");
} else {
Serial.println("LittleFS montado correctamente");
}
}
void initWiFi() {
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.print("Conectando a WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("");
Serial.print("Conectado. IP: ");
Serial.println(WiFi.localIP());
}
String getGyroReadings() {
mpu.getEvent(&a, &g, &temp);
float gx = g.gyro.x;
if (abs(gx) > gyroXerror) gyroX += gx / 50.0;
float gy = g.gyro.y;
if (abs(gy) > gyroYerror) gyroY += gy / 70.0;
float gz = g.gyro.z;
if (abs(gz) > gyroZerror) gyroZ += gz / 90.0;
readings["gyroX"] = String(gyroX);
readings["gyroY"] = String(gyroY);
readings["gyroZ"] = String(gyroZ);
return JSON.stringify(readings);
}
String getAccReadings() {
mpu.getEvent(&a, &g, &temp);
accX = a.acceleration.x;
accY = a.acceleration.y;
accZ = a.acceleration.z;
readings["accX"] = String(accX);
readings["accY"] = String(accY);
readings["accZ"] = String(accZ);
return JSON.stringify(readings);
}
String getTemperature() {
mpu.getEvent(&a, &g, &temp);
temperature = temp.temperature;
return String(temperature);
}
void setup() {
Serial.begin(115200);
initWiFi();
initLittleFS();
initMPU();
// Ruta principal
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
request->send(LittleFS, "/index.html", "text/html");
});
// Servir archivos estáticos desde LittleFS
server.serveStatic("/", LittleFS, "/");
// Rutas para resetear giroscopio
server.on("/reset", HTTP_GET, [](AsyncWebServerRequest *request){
gyroX = gyroY = gyroZ = 0;
request->send(200, "text/plain", "OK");
});
server.on("/resetX", HTTP_GET, [](AsyncWebServerRequest *request){
gyroX = 0;
request->send(200, "text/plain", "OK");
});
server.on("/resetY", HTTP_GET, [](AsyncWebServerRequest *request){
gyroY = 0;
request->send(200, "text/plain", "OK");
});
server.on("/resetZ", HTTP_GET, [](AsyncWebServerRequest *request){
gyroZ = 0;
request->send(200, "text/plain", "OK");
});
// Eventos SSE
events.onConnect([](AsyncEventSourceClient *client){
if(client->lastId()) {
Serial.printf("Cliente reconectado! Último ID: %u\n", client->lastId());
}
client->send("hello!", NULL, millis(), 10000);
});
server.addHandler(&events);
Serial.println("Iniciando servidor...");
server.begin();
Serial.println("Servidor iniciado.");
}
void loop() {
if ((millis() - lastTime) > gyroDelay) {
events.send(getGyroReadings().c_str(), "gyro_readings", millis());
lastTime = millis();
}
if ((millis() - lastTimeAcc) > accelerometerDelay) {
events.send(getAccReadings().c_str(), "accelerometer_readings", millis());
lastTimeAcc = millis();
}
if ((millis() - lastTimeTemperature) > temperatureDelay) {
events.send(getTemperature().c_str(), "temperature_reading", millis());
lastTimeTemperature = millis();
}
}