/********* Rui Santos Complete instructions at https://RandomNerdTutorials.com/build-web-servers-ebook/ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. *********/ // Import required libraries #include #include #include #include #include "SPIFFS.h" #include #define VERSION "29-06-2025" //#define VERSION "22-02-2024" //VO #define VERSION "17-07-2022" int i,j,k; // OK avec reseau définit sur HotSpot Redmi note 11 //const char* ssid = "Redmi Note 11"; //const char* password = "k9m5jfurubn"; const char* ssid = "Freebox-5CF3C9"; const char* password = "mittebant-lacrymula.-fimbri-inmensum"; // Create AsyncWebServer object on port 80 AsyncWebServer server(80); // Create a WebSocket object AsyncWebSocket ws("/ws"); // Set number of outputs #define NUM_OUTPUTS 3 // 2=inbuilt Bleue // Assign each GPIO to an output 15= bleue 18=Led verte 19 =led Rouge int outputGPIOs[NUM_OUTPUTS] = {15,18,19}; // Initialize SPIFFS void initSPIFFS() { if (!SPIFFS.begin(true)) { Serial.println("An error has occurred while mounting SPIFFS"); } Serial.println("SPIFFS mounted successfully"); } // Initialize WiFi void initWiFi() { WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); Serial.print("Connecting to WiFi .."); while (WiFi.status() != WL_CONNECTED) { Serial.print('.'); delay(1000); } Serial.println(WiFi.localIP()); } String getOutputStates(){ JSONVar myArray; for (int i =0; ifinal && info->index == 0 && info->len == len && info->opcode == WS_TEXT) { data[len] = 0; if (strcmp((char*)data, "states") == 0) { notifyClients(getOutputStates()); } else{ int gpio = atoi((char*)data); Serial.println(gpio); digitalWrite(gpio, !digitalRead(gpio)); notifyClients(getOutputStates()); } } } void onEvent(AsyncWebSocket *server, AsyncWebSocketClient *client,AwsEventType type, void *arg, uint8_t *data, size_t len) { switch (type) { case WS_EVT_CONNECT: Serial.printf("WebSocket client #%u connected from %s\n", client->id(), client->remoteIP().toString().c_str()); break; case WS_EVT_DISCONNECT: Serial.printf("WebSocket client #%u disconnected\n", client->id()); break; case WS_EVT_DATA: handleWebSocketMessage(arg, data, len); break; case WS_EVT_PONG: case WS_EVT_ERROR: break; } } void initWebSocket() { ws.onEvent(onEvent); server.addHandler(&ws); } void setup(){ // Serial port for debugging purposes Serial.begin(115200); delay(1000); Serial.println("\r\n"); delay(500); Serial.println(" ESP32 Jaune DevkitV1 30 pins"); Serial.println(" _ESP32_Multiple_Ouputs_Websocket_2025-06.ino"); Serial.print(" Version :");Serial.println(VERSION); Serial.print(" Set GPIOs :"); for (int i =0; ihtml"); // Route for root / web page server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){ request->send(SPIFFS, "/index.html", "text/html",false); }); Serial.println("lance server de fichier"); server.serveStatic("/", SPIFFS, "/"); Serial.println(" Start Server"); server.begin(); j=0; } void loop() { j=j+1; Serial.printf("j=%5d Etats led 15= %1d, led 18=%1d ,Led 19=%1d\r\n",j, // digitalRead(15),digitalRead(18),digitalRead(19)); digitalRead(outputGPIOs[0]),digitalRead(outputGPIOs[1]),digitalRead(outputGPIOs[2])), delay(1000); ws.cleanupClients(); } /* ets Jul 29 2019 12:21:46 rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT) configsip: 0, SPIWP:0xee clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00 mode:DIO, clock div:1 load:0x3fff0030,len:1284 load:0x40078000,len:12836 load:0x40080400,len:3032 entry 0x400805e4 ESP32 Jaune DevkitV1 30 pins _ESP32_Multiple_Ouputs_Websocket_2025-06.ino Version :29-06-2025 Set GPIOs :151819 as outputs Init SPIFFS SPIFFS mounted successfully Init Wifi Connecting to WiFi .......192.168.1.3 Init Websocket start Web server et charge page fichier ->html lance server de fichier Start Server j= 1 Etats led 15= 0, led 18=0 ,Led 19=0 j= 2 Etats led 15= 0, led 18=0 ,Led 19=0 j= 3 Etats led 15= 0, led 18=0 ,Led 19=0 j= 4 Etats led 15= 0, led 18=0 ,Led 19=0 j= 5 Etats led 15= 0, led 18=0 ,Led 19=0 j= 6 Etats led 15= 0, led 18=0 ,Led 19=0 WebSocket client #1 connected from 192.168.1.4 j= 7 Etats led 15= 0, led 18=0 ,Led 19=0 19 j= 8 Etats led 15= 0, led 18=0 ,Led 19=1 15 j= 9 Etats led 15= 1, led 18=0 ,Led 19=1 j= 10 Etats led 15= 1, led 18=0 ,Led 19=1 18 19 j= 11 Etats led 15= 1, led 18=1 ,Led 19=0 j= 12 Etats led 15= 1, led 18=1 ,Led 19=0 j= 13 Etats led 15= 1, led 18=1 ,Led 19=0 */