write me some MicroPython code to collect temperature, humidity, air pressure, lux level…
- User
write me some MicroPython code to collect temperature, humidity, air pressure, lux level once every 15 minutes then put the micro controller into deep sleep mode
- Assistant
Sure, I'd be happy to help you with that! Here's a MicroPython code snippet that should do what you're looking for. This example assumes you're using an ESP32 microcontroller with a BME280 sensor for temperature, humidity, and air pressure, and a BH1750 sensor for lux level. Make sure to install the necessary libraries for these sensors. You can use the
upiptool to install them:pythonimport time import machine from machine import I2C, Pin, RTC from bme280 import BME280 from bh1750 import BH1750 # Set up I2C and sensors i2c = I2C(scl=Pin(22), sda=Pin(21), freq=100000) bme280 = BME280(i2c=i2c) bh1750 = BH1750(i2c) # Function to collect sensor data def collect_data(): temperature = bme280.temperature humidity = bme280.humidity pressure = bme280.pressure lux = bh1750.luminance(BH1750.ONCE_HIRES_MODE) return temperature, humidity, pressure, lux # Function to put the microcontroller into deep sleep mode def deep_sleep(duration_seconds): rtc = RTC() rtc.irq(trigger=RTC.ALARM0, wake=machine.DEEPSLEEP) rtc.alarm(rtc.ALARM0, duration_seconds * 1000) machine.deepsleep() # Main loop while True: temperature, humidity, pressure, lux = collect_data() print(f"Temperature: {temperature}°C, Humidity: {humidity}%, Pressure: {pressure}hPa, Lux: {lux}lx") deep_sleep(15 * 60) # Sleep for 15 minutesThis code collects temperature, humidity, air pressure, and lux level once every 15 minutes and then puts the microcontroller into deep sleep mode. When the microcontroller wakes up, it collects the data again and repeats the process.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.