0% found this document useful (0 votes)
12 views2 pages

11111

This document contains an Arduino sketch for controlling an LED using an ESP32 microcontroller and WebSocket communication. It sets up a Wi-Fi access point, serves a web page for user interaction, and allows clients to toggle the LED state through WebSocket messages. The LED state is communicated back to all connected clients in real-time using JSON format.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views2 pages

11111

This document contains an Arduino sketch for controlling an LED using an ESP32 microcontroller and WebSocket communication. It sets up a Wi-Fi access point, serves a web page for user interaction, and allows clients to toggle the LED state through WebSocket messages. The LED state is communicated back to all connected clients in real-time using JSON format.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

#include <WiFi.

h>
#include <ESPAsyncWebServer.h>
#include <AsyncTCP.h>
#include <ArduinoJson.h>

// Identifiants du point d'accès


const char* ssid = "ESP32_LED";
const char* password = "12345678";

AsyncWebServer server(80);
AsyncWebSocket ws("/ws");

const int ledPin = 2; // LED intégrée

// Fonction pour envoyer l'état de la LED


void notifyClients() {
StaticJsonDocument<100> doc;
doc["led"] = digitalRead(ledPin) == HIGH ? "ON" : "OFF";
String msg;
serializeJson(doc, msg);
ws.textAll(msg);
}

// Gestion des messages WebSocket


void handleWebSocketMessage(void *arg, uint8_t *data, size_t len) {
AwsFrameInfo *info = (AwsFrameInfo*)arg;
if (info->final && info->index == 0 && info->len == len && info->opcode ==
WS_TEXT) {
String msg = String((char*)data);
if (msg == "toggle") {
digitalWrite(ledPin, !digitalRead(ledPin));
notifyClients();
}
}
}

// Événements WebSocket
void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client,
AwsEventType type, void *arg, uint8_t *data, size_t len) {
if (type == WS_EVT_CONNECT) {
Serial.println("Client connecté");
notifyClients();
} else if (type == WS_EVT_DISCONNECT) {
Serial.println("Client déconnecté");
} else if (type == WS_EVT_DATA) {
handleWebSocketMessage(arg, data, len);
}
}

// Page HTML avec WebSocket


const char index_html[] PROGMEM = R"rawliteral(
<!DOCTYPE html>
<html>
<head>
<title>Contrôle LED ESP32</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script>
var socket;
function toggleLED() {
socket.send("toggle");
}
window.onload = function() {
socket = new WebSocket("ws://" + location.host + "/ws");
socket.onmessage = function(event) {
var data = JSON.parse(event.data);
document.getElementById("state").innerText = data.led;
};
};
</script>
</head>
<body>
<h2>Contrôle LED via WebSocket</h2>
<p>État de la LED : <strong id="state">...</strong></p>
<button onclick="toggleLED()">Basculer</button>
</body>
</html>
)rawliteral";

void setup() {
Serial.begin(115200);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);

// Lancer le point d'accès


WiFi.softAP(ssid, password);
Serial.println("AP lancé : " + WiFi.softAPIP().toString());

// WebSocket
ws.onEvent(onWsEvent);
server.addHandler(&ws);

// Route racine
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send_P(200, "text/html", index_html);
});

server.begin();
}

void loop() {
ws.cleanupClients(); // nettoie les clients déconnectés
}

You might also like