diff --git a/app/routes/index.js b/app/routes/index.js index ea5f51ece470ad67acf6c232cde41ebe5f64f22d..e177386bc1de813a35121b1d36959723a8bc8d74 100644 --- a/app/routes/index.js +++ b/app/routes/index.js @@ -41,8 +41,30 @@ wss.on('connection', (ws) => { console.log('Nouvelle connexion WebSocket établie.'); // Écoutez les messages du client WebSocket - ws.on('message', (message) => { + ws.on('message', async (message) => { + if (message instanceof Buffer) { + message = message.toString('utf-8'); + } + console.log(`Message reçu du client: ${message}`); + let distanceCapteur = parseInt(message); + + if (distanceCapteur && typeof distanceCapteur === 'number' && distanceCapteur > 0) { + + const distanceReelle = distanceCapteur - distanceSupplementaire; + if (distanceReelle > 0) { + + const hauteurEau = hauteurInitialeEau - distanceReelle; + const volumeEau = Math.PI * rayonSeau ** 2 * hauteurEau; + const consommationEau = volumeEauInitial - volumeEau; + + const consom = new Consommation({ + consommation: consommationEau + }); + + await consom.save(); + } + } }); // Envoyez un message au client WebSocket diff --git a/arduino/abreuvoire.c b/arduino/abreuvoire.c index 9d02f5accfa3d7d79500fb4f96c5729c7ac2b9a9..73e110c78b483c9c5c0dfb7870eea4e18aee96b6 100644 --- a/arduino/abreuvoire.c +++ b/arduino/abreuvoire.c @@ -5,7 +5,7 @@ int pinEcho = 8; int ledPinGreen = 11; int ledPinRed = 12; int limitGreen = 10; -int limitRed = 10; +int limitRed = 13; int pinBuzzer = 6; String methodeNotification = "leds"; @@ -61,15 +61,15 @@ void loop() { Serial.print(distance); Serial.println(" cm"); - if (distance < limitGreen) { + if (distance < limitRed) { digitalWrite(ledPinGreen, LOW); if (methodeNotification == "sound"){ - digitalWrite(ledPinRed, LOW); - tone(pinBuzzer, 1000); + digitalWrite(ledPinRed, LOW); + tone(pinBuzzer, 1000); } else if (methodeNotification == "leds"){ - noTone(pinBuzzer); - digitalWrite(ledPinRed, HIGH); + digitalWrite(ledPinRed, HIGH); + noTone(pinBuzzer); }else{ digitalWrite(ledPinRed, LOW); digitalWrite(ledPinGreen, LOW); @@ -82,6 +82,8 @@ void loop() { noTone(pinBuzzer); } else { + digitalWrite(ledPinGreen, LOW); + digitalWrite(ledPinRed, LOW); noTone(pinBuzzer); } }