Re-bonjour,
Merci pour toutes vos réponses.J'ai enfin fini ( il y a eu quelles que modifications dans mon projet) .
Je vous joint les 2 codes.
Émetteur:
#include <WiFi.h>
#include <ArduinoHttpClient.h>
#include <Wire.h>
#include <Adafruit_TCS34725.h>
#include <BH1750.h>
#include <LiquidCrystal_I2C.h>
// Capteurs
Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_50MS, TCS34725_GAIN_4X);
BH1750 lightMeter;
LiquidCrystal_I2C lcd(0x27, 16, 2); // Adresse typique des LCD I2C
const char* ssid = "ESP32_AP";
const char* password = "12345678";
const char* serverAddress = "192.168.4.1"; // IP du récepteur
int port = 80;
WiFiClient wifi;
HttpClient client = HttpClient(wifi, serverAddress, port);
void setup() {
Serial.begin(115200);
Wire.begin();
// Initialisation LCD
lcd.init();
lcd.backlight();
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Init WiFi...");
WiFi.begin(ssid, password);
Serial.print("Connexion au réseau");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println(" Connecté!");
lcd.clear();
lcd.setCursor(0,0);
lcd.print("WiFi Connecte");
delay(1000);
lcd.clear();
if (!tcs.begin()) {
Serial.println("Erreur TCS34725");
lcd.setCursor(0,0);
lcd.print("TCS34725 ERR");
while (1);
} else {
lcd.setCursor(0,0);
lcd.print("TCS34725 OK");
}
delay(1000);
lcd.clear();
if (!lightMeter.begin(BH1750::CONTINUOUS_HIGH_RES_MODE)) {
Serial.println("Erreur BH1750");
lcd.setCursor(0,0);
lcd.print("BH1750 ERR");
while (1);
} else {
lcd.setCursor(0,0);
lcd.print("BH1750 OK");
}
delay(1000);
lcd.clear();
}
void loop() {
// Lecture capteurs
uint16_t r, g, b, c;
tcs.getRawData(&r, &g, &b, &c);
float X = (-0.14282f * r) + (1.54924f * g) + (-0.95641f * b);
float Y = (-0.32466f * r) + (1.57837f * g) + (-0.73191f * b);
float Z = (-0.68202f * r) + (0.77073f * g) + (0.56332f * b);
float xc, yc;
if ((X + Y + Z) == 0) {
xc = 0;
yc = 0;
} else {
xc = X / (X + Y + Z);
yc = Y / (X + Y + Z);
}
float n = (xc - 0.3320f) / (0.1858f - yc);
float CCT = (449.0f * pow(n, 3)) + (3525.0f * pow(n, 2)) + (6823.3f * n) + 5520.33f;
if (CCT < 2700) CCT = 2700;
if (CCT > 6000) CCT = 6000;
float lux = lightMeter.readLightLevel();
// Affichage LCD
lcd.clear();
lcd.setCursor(0,0);
lcd.print("CCT:");
lcd.print((int)CCT);
lcd.print("K");
lcd.setCursor(0,1);
lcd.print("Lux:");
lcd.print((int)lux);
// Construction requête GET
String url = "/?cct=" + String((int)CCT) + "&lux=" + String((int)lux);
Serial.println("Envoi GET : " + url);
client.get(url);
int statusCode = client.responseStatusCode();
String response = client.responseBody();
Serial.print("Status code: ");
Serial.println(statusCode);
Serial.print("Réponse: ");
Serial.println(response);
delay(30000);
}
Récepteur:
#include <WiFi.h>
#include <WebServer.h>
#include <LiquidCrystal_I2C.h>
const char* ssid = "ESP32_AP";
const char* password = "12345678";
WebServer server(80);
// Pins PWM
const int pwmWarmPin = 12; // GPIO12 exemple PWM
const int pwmCoolPin = 13; // GPIO13 exemple PWM
LiquidCrystal_I2C lcd(0x27, 16, 2); // Adresse LCD classique 0x27, 16 colonnes, 2 lignes
void handleRoot() {
if (server.hasArg("cct") && server.hasArg("lux")) {
float CCT = server.arg("cct").toFloat();
float lux = server.arg("lux").toFloat();
float ratioCool = (CCT - 2700) / (6000 - 2700);
float ratioWarm = 1.0 - ratioCool;
float intensity = lux / 1000.0;
if (intensity > 1.0) intensity = 1.0;
int pwmWarm = (int)(ratioWarm * intensity * 255);
int pwmCool = (int)(ratioCool * intensity * 255);
analogWrite(pwmWarmPin, pwmWarm);
analogWrite(pwmCoolPin, pwmCool);
// Affichage sur LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("CCT:");
lcd.print((int)CCT);
lcd.print("K");
lcd.setCursor(0, 1);
lcd.print("Lux:");
lcd.print((int)lux);
String response = "Received: CCT=" + String(CCT) + ", Lux=" + String(lux);
server.send(200, "text/plain", response);
Serial.println(response);
} else {
server.send(400, "text/plain", "Missing parameters");
}
}
void setup() {
Serial.begin(115200);
pinMode(pwmWarmPin, OUTPUT);
pinMode(pwmCoolPin, OUTPUT);
// Initialisation LCD
lcd.init();
lcd.backlight();
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Starting AP...");
// Démarrage Access Point
WiFi.softAP(ssid, password);
IPAddress IP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(IP);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("IP:");
lcd.print(IP);
// Démarrage serveur
server.on("/", handleRoot);
server.begin();
Serial.println("HTTP server started");
}
void loop() {
server.handleClient();
}