import board import busio import digitalio from adafruit_bme280 import basic as adafruit_bme280 import datetime import mysql.connector import configparser i2c = busio.I2C(board.SCL, board.SDA) bme280 = adafruit_bme280.Adafruit_BME280_I2C(i2c) config = configparser.ConfigParser() config.read("/etc/weather.ini") db = mysql.connector.connect( host=config["database"]["host"], user=config["database"]["user"], password=config["database"]["password"], database=config["database"]["database"], ) def get_reading(): temp_c = bme280.temperature temp_f = temp_c * 9 / 5 + 32 return (datetime.datetime.now(), temp_f, bme280.humidity, bme280.pressure) def insert(table, timestamp, value): cursor = db.cursor() timestamp = timestamp.strftime('%Y-%m-%d %H:%M:%S') cursor.execute(f"insert into {table} (timestamp, value) values ('{timestamp}', '{value}')") timestamp, temperature, humidity, pressure = get_reading() insert("temperature", timestamp, temperature) insert("pressure", timestamp, pressure) insert("humidity", timestamp, humidity) db.commit()