// il faut charger les bibliotheques *.h dans l'IDE #include #include #include #include #include #include #define SDA_PIN 4 #define SCL_PIN 5 WebServer server(80); WebSocketsServer webSocket = WebSocketsServer(81); MPU6050 mpu6050(Wire); unsigned long lastUpdate = 0; const unsigned long interval = 50; // Page HTML + JavaScript PUR (Aucune dépendance Internet) const char index_html[] PROGMEM = R"rawliteral( Horizon Artificiel & Compas ESP32-C3
Connexion en cours...
)rawliteral"; void handleRoot() { server.send(200, "text/html", index_html); } void setup() { Serial.begin(115200); delay(1000); Wire.begin(SDA_PIN, SCL_PIN); Wire.setClock(100000); mpu6050.begin(); mpu6050.calcGyroOffsets(true); WiFi.persistent(false); WiFi.mode(WIFI_AP); WiFi.setTxPower(WIFI_POWER_11dBm); WiFi.softAP("Mon-Horizon-ESP32C3", "12345678", 6); server.on("/", handleRoot); server.begin(); webSocket.begin(); } void loop() { server.handleClient(); webSocket.loop(); mpu6050.update(); unsigned long now = millis(); if (now - lastUpdate >= interval) { lastUpdate = now; if (webSocket.connectedClients() > 0) { float roll = mpu6050.getAngleX(); float pitch = mpu6050.getAngleY(); float yaw = mpu6050.getAngleZ(); if (!isnan(roll) && !isnan(pitch) && !isnan(yaw)) { StaticJsonDocument<128> doc; doc["roll"] = roll; doc["pitch"] = pitch; doc["yaw"] = yaw; String jsonString; serializeJson(doc, jsonString); webSocket.broadcastTXT(jsonString); } } } yield(); }