#include "DHT.h"
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#define DHTPIN 12
#define DHTTYPE 22
#define PHOTOPIN 27
const float GAMMA = 0.7;
const float RL10 = 50;
DHT dht(DHTPIN, DHTTYPE);
float temp, hum, voltage, resistance, lux;
int analogval;
Adafruit_MPU6050 mpu;
sensors_event_t acc, gyr, x;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32!");
dht.begin();
Serial.println("DHT22 ready!");
while (!mpu.begin()) {
Serial.println("MPU6050 not connected!");
delay(1000);
}
Serial.println("MPU6050 ready!");
mpu.setAccelerometerRange(MPU6050_RANGE_8_G);
mpu.setGyroRange(MPU6050_RANGE_250_DEG);
mpu.setFilterBandwidth(MPU6050_BAND_21_HZ);
}
void loop() {
// put your main code here, to run repeatedly:
// this speeds up the simulation
mpu.getEvent(&acc, &gyr, &x); // Acceleration, Rotation, Temperature (Unused)
temp = dht.readTemperature(); //Environment Temperature
hum = dht.readHumidity();
analogval = analogRead(PHOTOPIN);
voltage = analogval / 4096. * 5;
resistance = 2000 * voltage / (1 - voltage / 5);
lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA)); //Brightness measured in lux
if (lux == INFINITY){
Serial.println("Infinite Brightness");
}
Serial.print("Humidity: ");
Serial.println(hum);
Serial.print("Temperature: ");
Serial.println(temp);
Serial.print("Brightness: ");
Serial.println(lux);
Serial.print("X: ");
Serial.print(acc.acceleration.x);
Serial.print(", Y: ");
Serial.print(acc.acceleration.y);
Serial.print(", Z: ");
Serial.print(acc.acceleration.z);
Serial.println(" m/s^2");
Serial.print("X_rot: ");
Serial.print(gyr.gyro.x);
Serial.print(", Y_rot: ");
Serial.print(gyr.gyro.y);
Serial.print(", Z_rot: ");
Serial.print(gyr.gyro.z);
Serial.println(" radians");
Serial.println("----------------------------------");
delay(1000);
}