#include <WiFi.h>
#include <ArtnetWifi.h>
#include <ModbusMaster.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
AsyncWebServer server(80);
const char* ssid = "NomDuReseauWiFi";
const char* password = "MotDePasseWiFi";
// Définir l'adresse IP statique
IPAddress staticIP(192, 168, 1, 10); // Remplacez ces valeurs par l'adresse IP souhaitée
IPAddress gateway(192, 168, 1, 1); // Remplacez ces valeurs par l'adresse de la passerelle
IPAddress subnet(255, 255, 255, 0); // Remplacez ces valeurs par le masque de sous-réseau
// Déclarer l'objet ArtnetWifi
ArtnetWifi artnet;
// Nombre de canaux DMX
#define NUM_CHANNELS 512
// Initialisation du DMX
#define DMX_START_ADDRESS 1
#define DMX_PACKET_SIZE (DMX_START_ADDRESS + NUM_CHANNELS)
uint8_t dmxData[NUM_CHANNELS];
// Broches GPIO pour la communication série avec le MAX485
#define RX_pin 32 // 16
#define TX_pin 33 // 17
// Broches GPIO pour le contrôle du MAX485
#define DE_pin 23 // 2
#define RE_pin 22 // 15
void setup() {
Serial.begin(115200);
// Configurer WiFi avec adresse IP statique
WiFi.config(staticIP, gateway, subnet);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connexion au réseau WiFi en cours...");
}
Serial.println("Connexion WiFi établie!");
// Initialiser Artnet
artnet.begin();
artnet.setArtDmxCallback(onArtNetFrame);
// Initialiser le serveur web
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
String html = "<html><head><style>body{background-color:#111;color:#fff;font-family:sans-serif;text-align:center;}";
html += "h1{margin-top:50px;}input{width:100%;padding:10px;margin:10px 0;border:none;background-color:#333;color:#fff;}";
html += "</style></head><body><h1>Hello, world!</h1>";
html += "<h2>Configuration</h2><h3>Changer l'adresse IP</h3>";
html += "<input type=\"text\" id=\"newIP\" placeholder=\"Nouvelle adresse IP\">";
html += "<button onclick=\"updateIP()\">Changer IP</button>";
html += "<h3>Changer le mot de passe WiFi</h3>";
html += "<input type=\"password\" id=\"newPassword\" placeholder=\"Nouveau mot de passe\">";
html += "<button onclick=\"updateWiFiPassword()\">Changer mot de passe</button>";
html += "<script>";
html += "function updateIP() {";
html += "const newIP = document.getElementById('newIP').value;";
html += "sendData('/update_ip', { ip: newIP });";
html += "}";
html += "function updateWiFiPassword() {";
html += "const newPassword = document.getElementById('newPassword').value;";
html += "sendData('/update_wifi_password', { password: newPassword });";
html += "}";
html += "function sendData(endpoint, data) {";
html += "fetch(endpoint, {";
html += "method: 'POST',";
html += "headers: { 'Content-Type': 'application/json' },";
html += "body: JSON.stringify(data)";
html += "})";
html += ".then(response => {";
html += "if (!response.ok) {";
html += "throw new Error('Erreur lors de la requête.');";
html += "}";
html += "return response.json();";
html += "})";
html += ".then(data => {";
html += "console.log('Réponse du serveur :', data);";
html += "})";
html += ".catch(error => {";
html += "console.error('Erreur:', error);";
html += "});";
html += "}";
html += "</script>";
html += "</body></html>";
request->send(200, "text/html", html);
});
server.on("/update_ip", HTTP_POST, [](AsyncWebServerRequest *request){
AsyncWebParameter* p = request->getParam(0);
String newIP = p->value();
Serial.print("Nouvelle adresse IP : ");
Serial.println(newIP);
// Mettez ici votre code pour changer l'adresse IP
request->send(200, "application/json", "{\"success\": true}");
});
server.on("/update_wifi_password", HTTP_POST, [](AsyncWebServerRequest *request){
AsyncWebParameter* p = request->getParam(0);
String newPassword = p->value();
Serial.print("Nouveau mot de passe WiFi : ");
Serial.println(newPassword);
// Mettez ici votre code pour changer le mot de passe WiFi
request->send(200, "application/json", "{\"success\": true}");
});
// Initialiser les broches GPIO pour la communication série avec le MAX485
Serial.begin(250000, SERIAL_8N1, RX_pin, TX_pin);
// Initialiser les broches GPIO pour le contrôle du MAX485
pinMode(DE_pin, OUTPUT);
pinMode(RE_pin, OUTPUT);
server.begin();
}
void loop() {
artnet.read(); // Lire les paquets Artnet
// Votre code loop ici
}
void onArtNetFrame(uint16_t universe, uint16_t length, uint8_t sequence, uint8_t* data) {
// Assurez-vous que les données reçues correspondent à l'univers DMX que vous souhaitez gérer
if (universe == 1) {
// Copiez les données Artnet dans le tampon DMX
for (int i = 0; i < length && i < NUM_CHANNELS; i++) {
dmxData[i] = data[i];
}
// Envoyer les données DMX
sendDmxData();
}
}
void sendDmxData() {
// Activer la transmission sur le MAX485
digitalWrite(DE_pin, HIGH);
// Code pour envoyer les données DMX au matériel DMX
// Assurez-vous d'utiliser la bibliothèque appropriée pour gérer la transmission DMX
// Désactiver la transmission sur le MAX485
digitalWrite(DE_pin, LOW);
}