| 1 | #!/usr/bin/env python |
| 2 | |
| 3 | import time |
| 4 | from bme280 import BME280 |
| 5 | |
| 6 | try: |
| 7 | from smbus2 import SMBus |
| 8 | except ImportError: |
| 9 | from smbus import SMBus |
| 10 | |
| 11 | import logging |
| 12 | |
| 13 | logging.basicConfig( |
| 14 | format='%(asctime)s.%(msecs)03d %(levelname)-8s %(message)s', |
| 15 | level=logging.INFO, |
| 16 | datefmt='%Y-%m-%d %H:%M:%S') |
| 17 | |
| 18 | logging.info("""weather.py - Print readings from the BME280 weather sensor. |
| 19 | |
| 20 | Press Ctrl+C to exit! |
| 21 | |
| 22 | """) |
| 23 | |
| 24 | bus = SMBus(1) |
| 25 | bme280 = BME280(i2c_dev=bus) |
| 26 | |
| 27 | while True: |
| 28 | temperature = bme280.get_temperature() |
| 29 | pressure = bme280.get_pressure() |
| 30 | humidity = bme280.get_humidity() |
| 31 | logging.info("""Temperature: {:05.2f} *C |
| 32 | Pressure: {:05.2f} hPa |
| 33 | Relative humidity: {:05.2f} % |
| 34 | """.format(temperature, pressure, humidity)) |
| 35 | time.sleep(1) |