import smbus2 import bme280 import datetime import time import pygame from pygame.locals import * import sys import csv
BME280_PORT = 1 BME280_ADDRESS = 0x76 PYGAME_SCREEN_SIZE = (480, 320) LOG_INTERVAL = 10
bus = smbus2.SMBus(BME280_PORT) calibration_params = bme280.load_calibration_params(bus, BME280_ADDRESS)
pygame.init() screen = pygame.display.set_mode((PYGAME_SCREEN_SIZE), FULLSCREEN) pygame.display.set_caption("温度・湿度・気圧計") myfont1 = pygame.font.Font("ipag.ttf", 35) myfont2 = pygame.font.Font("ipag.ttf", 62)
old_time = time.perf_counter() while True: new_time = time.perf_counter() if new_time - old_time >= 1: old_time = new_time data = bme280.sample(bus, BME280_ADDRESS, calibration_params) timestamp_str = "{0:%Y年%m月%d日 %H時%M分%S秒}".format(data.timestamp) temperture_str = "温度:{0:>6.1f} ℃".format(data.temperature) humidity_str = "湿度:{0:>6.1f} %".format(data.humidity) pressure_str = "気圧:{0:>6.1f} hPa".format(data.pressure) timestamp = myfont1.render(timestamp_str, True, (255, 255, 255)) temperture = myfont2.render(temperture_str, True, (255, 255, 255)) humidity = myfont2.render(humidity_str, True, (255, 255, 255)) pressure = myfont2.render(pressure_str, True, (255, 255, 255)) screen.fill((0, 0, 0)) screen.blit(timestamp, (0, 10)) screen.blit(temperture, (0, 70)) screen.blit(humidity, (0, 155)) screen.blit(pressure, (0, 240)) pygame.display.update() if data.timestamp.second % 10 == 0: filename = "./BME280log/{0:%Y.%m.%d} BME280.log".format(datetime.date.today()) f = open(filename, "a") writer = csv.writer(f) csvrow = [] csvrow.append("{0:%Y/%m/%d %H:%M:%S}".format(data.timestamp)) csvrow.append("{0:.1f}".format(data.temperature)) csvrow.append("{0:.1f}".format(data.humidity)) csvrow.append("{0:.1f}".format(data.pressure)) writer.writerow(csvrow) f.close() for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() elif event.type == KEYDOWN and event.key == K_ESCAPE: pygame.quit() sys.exit() |