Bonjour J-M-L
Je me suis amusé (même bien amusé
) à l'exercice avec la "complicité" de ChatGPT pour la partie application Web et ça donne ceci:
Le temps d'allumage du casier est défini dans le tableau;
ledOffTempoDef ledOn[] = // Si tempo = 0, On/Off/On/Off ...
{
{ledOnTempo, 0}, {ledOnTempo, 0}, {ledOnTempo, 0}, {0, 0},
{ledOnTempo, 0}, {ledOnTempo, 0}, {ledOnTempo, 0}, {ledOnTempo, 0},
{ledOnTempo, 0}, {ledOnTempo, 0}, {ledOnTempo, 0}, {ledOnTempo, 0},
{ledOnTempo, 0}, {ledOnTempo, 0}, {ledOnTempo, 0}, {ledOnTempo, 0},
};
On voit que le casier

est en allumage On/Off.
Il faut ajuster ces paramètres à son propre réseau:
// Configuration réseau wifi
const char* wifiSsid = "xxxxxxxxx";
const char* wifiPassword = "xxxxxxxxxx";
const char* serveurHostName = "galien-casiers";
Le bus des LED est:
const int ledBusPin = 23; // Connexion du bus des LED
Le programme:
/*
Name: AF_galien_RgmtCasierWeb.ino
Created: 11.03.2025
Author: jpbbricole/ChatGPT
Remarque: Commande de LED pour casier de rangement
Serveur Web
https://forum.arduino.cc/t/aide-pour-developper-app-telephone-arduino-leds/1362675
14.03.2025 Première version #9
14.03.2025 Indexation des position des LED #9
15.03.2025 Wifi avec DHCP et DNS #9
*/
//------------------------------------- Wifi
#include <WiFi.h>
#include <WebServer.h>
#include <ESPmDNS.h>
// Configuration réseau wifi
const char* wifiSsid = "xxxxxxxxx";
const char* wifiPassword = "xxxxxxxxx";
const char* serveurHostName = "galien-casiers";
WebServer server(80);
//------------------------------------- LED
#include <Adafruit_NeoPixel.h>
const int ledBusPin = 23; // Connexion du bus des LED
const int ledPosInStrips[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}; // Position des LED sur le strips
const int ledNombre = sizeof(ledPosInStrips) / sizeof(ledPosInStrips[0]); // Nombre de LED
int ledOnTempo = 4000; // Durée d'allumage de la LED, si 0 = On/Off
struct ledOffTempoDef {unsigned long tempo; unsigned long millis;};
ledOffTempoDef ledOn[] = // Si tempo = 0, On/Off/On/Off ...
{
{ledOnTempo, 0}, {ledOnTempo, 0}, {ledOnTempo, 0}, {0, 0},
{ledOnTempo, 0}, {ledOnTempo, 0}, {ledOnTempo, 0}, {ledOnTempo, 0},
{ledOnTempo, 0}, {ledOnTempo, 0}, {ledOnTempo, 0}, {ledOnTempo, 0},
{ledOnTempo, 0}, {ledOnTempo, 0}, {ledOnTempo, 0}, {ledOnTempo, 0},
};
const int stripsLedNombre = 24; // Nombre de LED sur le strips
Adafruit_NeoPixel leds(stripsLedNombre, ledBusPin, NEO_GRB + NEO_KHZ800);
enum ledlColorsIndex {ledsColOff, ledsColGreen, ledsColBlue, ledsColRed, ledsColOrange, ledsColYellow, ledsColWithe, ledsColNombre};
uint32_t ledsColors[ledsColNombre];// Valeurs RGB pour les couleurs (Tableau)
const int ledsBrightMax = 25;// LED Luminosité maximum
// Couleurs des cases
const String webColors[16] = {"green", "blue", "red", "orange", "yellow", "white", "blue", "red", "orange", "yellow", "white", "blue", "red", "orange", "yellow", "white"};
const String webColorsPalette[] = {"green", "blue", "red", "orange", "yellow", "white"};
const int webColorNombre = 6; // 6 couleurs dans la palette
void setup()
{
Serial.begin(115200);
WiFi.begin(wifiSsid, wifiPassword);
while (WiFi.status() != WL_CONNECTED)
{
delay(1000);
Serial.println("Connexion au WiFi...");
}
Serial.print("WiFi connected ");
Serial.println(WiFi.localIP());
// Attribution hostname
if (!MDNS.begin(serveurHostName))
{
Serial.println("Erreur lors de la configuration mDNS!");
}
else
{
Serial.print("mDNS OK ");
Serial.println(serveurHostName);
}
server.on("/", wifiHandleRoot);
server.on("/led", wifiHandleLED);
server.on("/allOFF", handleAllOFF);
server.begin();
signLedInitialisation(); // Démarrage des LED
}
void loop()
{
server.handleClient();
// Extinction des LED
for (int l = 0; l < ledNombre; l ++)
{
if (ledOn[l].millis != 0) // Si temporisation active
{
if (millis() - ledOn[l].millis >= ledOn[l].tempo) // Si temporisation terminée
{
ledExtinction(l);
ledOn[l].millis = 0; // Arrêt de la temporisation
}
}
}
}
//------------------------------------- Serveur Web
void wifiHandleRoot()
{
String html = "<html><head><style>";
html += "body { text-align: center; font-family: Arial; } ";
html += ".grid { display: grid; grid-template-columns: repeat(4, 80px); grid-gap: 10px; justify-content: center; } ";
html += ".cell { width: 80px; height: 80px; display: flex; align-items: center; justify-content: center; font-size: 20px; cursor: pointer; transition: background 0.5s; } ";
html += ".cell:hover { opacity: 0.8; }";
html += ".off-button { margin-top: 20px; padding: 15px 0; font-size: 18px; background: red; color: white; border: none; cursor: pointer; width: calc(4 * 80px + 30px); }";
html += ".off-button:hover { background: darkred; }";
html += "</style></head><body>";
html += "<h1>galien's casiers couleur</h1><div class='grid'>";
for (int i = 0; i < 16; i++)
{
html += "<div class='cell' id='cell" + String(i) + "' style='background:" + webColors[i] + "' onclick=\"changeColor(" + String(i) + ", '" + webColors[i] + "')\">" + String(i+1) + "</div>";
}
html += "</div><br><button class='off-button' onclick=\"location.href='/allOFF'\">OFF</button>";
html += "<script>";
html += "function changeColor(index, originalColor) {";
html += " var cell = document.getElementById('cell' + index);";
html += " cell.style.background = 'gray';";
html += " setTimeout(function() { cell.style.background = originalColor; }, 1000);";
html += " location.href='/led?num=' + index + '&color=' + originalColor;";
html += "}";
html += "</script></body></html>";
server.send(200, "text/html", html);
}
void wifiHandleLED()
{
if (server.hasArg("num") && server.hasArg("color"))
{
int caseNum = server.arg("num").toInt();
String caseColor = server.arg("color");
if (caseNum >= 0 && caseNum < 16)
{
ledAllumage(caseNum, caseColor);
}
}
server.sendHeader("Location", "/");
server.send(303);
}
void handleAllOFF()
{
Serial.println("All LEDs OFF");
leds.clear();
leds.show();
server.sendHeader("Location", "/");
server.send(303);
}
//------------------------------------- LED
void ledAllumage(int ledNum, String ledCouleur)
{
Serial.print("Allumage LED " + String(ledNum));
Serial.print("\tcouleur " + ledCouleur);
Serial.println("\tLed pos:" + String(ledPosInStrips[ledNum]));
int couleurIndex = ledColorIndex(ledCouleur) +1;
if (ledOn[ledNum].tempo > 0) // S'il y a temporisation sur cette LED
{
leds.setPixelColor(ledPosInStrips[ledNum], ledsColors[couleurIndex]); // Allumage de la LED
leds.show();
ledOn[ledNum].millis = millis(); // Démarrage du chrono
}
else // Si LED On/Off
{
if (leds.getPixelColor(ledPosInStrips[ledNum]) != 0) // Si LED déjà allumée
{
leds.setPixelColor(ledPosInStrips[ledNum], ledsColors[ledsColOff]); // Eteindre la LED
}
else
{
leds.setPixelColor(ledPosInStrips[ledNum], ledsColors[couleurIndex]); // Allumer la LED
}
leds.show();
}
}
void ledExtinction(int ledNum)
{
leds.setPixelColor(ledPosInStrips[ledNum], ledsColors[ledsColOff]); // Extinction de la LED
leds.show();
}
int ledColorIndex(String color) // Retourne l'index de la couleur en fonction de son nom
{
for (int c = 0; c < webColorNombre; c ++)
{
if (color == webColorsPalette[c]) // Si couleur trouvée dans la palette
{
return c;
}
}
return -1; // Couleur pas trouvée
}
void signLedInitialisation() // Initialisation des leds et des couleurs
{
ledsColors[ledsColOff] = leds.Color(0, 0, 0);
ledsColors[ledsColRed] = leds.Color(255, 0, 0); // Définition des couleurs
ledsColors[ledsColGreen] = leds.Color(0, 255, 0);
ledsColors[ledsColBlue] = leds.Color(0, 0, 255);
ledsColors[ledsColYellow] = leds.Color(255, 255, 0);
ledsColors[ledsColOrange] = leds.Color(255, 165, 0);
ledsColors[ledsColWithe] = leds.Color(255, 255, 255);
leds.begin();
leds.setBrightness(ledsBrightMax);
leds.clear();
leds.show();
}
Bonne soirée
jpbbricole