from machine import Pin
from machine import Pin, I2C
import utime
from mpu6050 import init_mpu6050, get_mpu6050_data
from time import sleep
import dht
i2c = I2C(0, scl=Pin(21), sda=Pin(20), freq=400000)
init_mpu6050(i2c)
# Create a DHT22 sensor object on GPIO Pin 22
sensor = dht.DHT22(Pin(22))
# Alternatively, you can use DHT11 by uncommenting the line below
# sensor = dht.DHT11(Pin(22))
def celsius_to_fahrenheit(temp_celsius):
# Convert temperature from Celsius to Fahrenheit
temp_fahrenheit = temp_celsius * (9/5) + 32
return temp_fahrenheit
while True:
try:
sleep(2)
# Measure temperature and humidity
sensor.measure()
temp = sensor.temperature()
hum = sensor.humidity()
# Convert temperature to Fahrenheit
temp_f = celsius_to_fahrenheit(temp)
# Print sensor readings
print('DHT Readings: ')
print('Temperature: %3.1f ºC' % temp)
print('Temperature: %3.1f ºF' % temp_f)
print('Humidity: %3.1f %%' % hum)
data = get_mpu6050_data(i2c)
print("Temperature: {:.2f} °C".format(data['temp']))
print("Acceleration: X: {:.2f}, Y: {:.2f}, Z: {:.2f} g".format(data['accel']['x'], data['accel']['y'], data['accel']['z']))
print("Gyroscope: X: {:.2f}, Y: {:.2f}, Z: {:.2f} °/s".format(data['gyro']['x'],data['gyro']['y'], data['gyro']['z']))
utime.sleep(0.5)
except OSError as e:
# Handle sensor reading errors
print('Failed to read sensor.')