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
Certainly! Here is some example code that should accomplish what you're asking:
codeimport machine import time import bme280 import ltr559 # Initialize the BME280 sensor i2c = machine.I2C(scl=machine.Pin(5), sda=machine.Pin(4)) bme = bme280.BME280(i2c=i2c) # Initialize the LTR559 sensor ltr559.init() while True: # Collect data from the BME280 sensor temperature = bme.temperature humidity = bme.humidity pressure = bme.pressure # Collect data from the LTR559 sensor lux = ltr559.get_lux() # Print the collected data print("Temperature: %0.1f C" % temperature) print("Humidity: %0.1f %%" % humidity) print("Pressure: %0.1f hPa" % (pressure / 100)) print("Lux: %d" % lux) # Sleep for 15 minutes time.sleep(900) # Put the microcontroller into deep sleep mode machine.deepsleep()This code assumes that you have a BME280 sensor connected to your microcontroller's I2C pins (SCL and SDA), and that you have an LTR559 sensor connected to your microcontroller as well. It will collect data from both sensors, print the data to the console, and then sleep for 15 minutes before repeating the process.
I hope this helps! Let me know if you have any questions.
- User
Thanks... But can you wrap the main part of the code into a neat feature
- Assistant
Certainly! Here is an updated version of the code that includes a function called
collect_and_print_datathat wraps the main part of the code:codeimport machine import time import bme280 import ltr559 # Initialize the BME280 sensor i2c = machine.I2C(scl=machine.Pin(5), sda=machine.Pin(4)) bme = bme280.BME280(i2c=i2c) # Initialize the LTR559 sensor ltr559.init() def collect_and_print_data(): # Collect data from the BME280 sensor temperature = bme.temperature humidity = bme.humidity pressure = bme.pressure
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.