//des constantes pour traduire le voltage en Lux
const float GAMMA = 0.7;
const float RL10 = 85;
#include "DHTesp.h"
//import de librairie mpu6050 (gyro et accel) ↓↓
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>
Adafruit_MPU6050 mpu;
DHTesp dht;
int ledPin = 16; //pour mpu6050
// Threshold for detecting movement
float movementThreshold = 1.0;
float rotationThreshold = 0.1;
void setup() {
Serial.begin(115200);
//Initialize DHT22
dht.setup(32, DHTesp::DHT22);
//LED Du mouvement
pinMode(ledPin, OUTPUT);
// Initialize the MPU6050 sensor
if (!mpu.begin()) {
Serial.println("Failed to find MPU6050 chip");
while (1) {
delay(10);
}
}
// Give some time for the MPU6050 to start
delay(100);
}
void loop() {
//↓↓-----------------------ACCEL AND GYRO-----------------------↓↓
// Get new sensor events
sensors_event_t accel, gyro, temp;
mpu.getEvent(&accel, &gyro, &temp);
// Calculate total acceleration
float totalAccel = sqrt(sq(accel.acceleration.x) + sq(accel.acceleration.y) + sq(accel.acceleration.z));
float totalGyro = sqrt(sq(gyro.gyro.x) + sq(gyro.gyro.y) + sq(gyro.gyro.z));
// Check if movement exceeds the threshold
if (totalAccel > movementThreshold || totalGyro > rotationThreshold) {
// Turn on the LED if movement is detected
digitalWrite(ledPin, HIGH);
Serial.println("Movement detected!");
} else {
// Turn off the LED if no movement is detected
digitalWrite(ledPin, LOW);
Serial.println("No movement.");
}
//↓↓------------------------DHT22-------------------------------↓↓
float humidity = dht.getHumidity();
float temperature = dht.getTemperature();
Serial.print("La temperature = ");
Serial.print(temperature,2);
Serial.println(" °C");
Serial.print("L'humidité = ");
Serial.print(humidity,2);
Serial.println(" %");
//↓↓-----------------------PHOTORESISTOR---------------------------↓↓
int sensorValue = analogRead(15);
int sensorValue10bit = sensorValue / 4; //CHANGER LA RESOLUTION DE 12 BITS VERS 10 BITS POUR QUE LE LUX READING SOIT CORRECTE (4096/1024 = 1024)
float voltage = sensorValue10bit / 1023. * 3.3;
float resistance = 5000 * voltage / (1 - voltage / 3.3);
float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
Serial.print("Luminosité en lux = ");
Serial.println(lux);
Serial.println("-----------------------------------");
delay(2000);
}