measure.py

#!/usr/bin/env python

import board
import digitalio
from PIL import Image, ImageDraw, ImageFont
import adafruit_ssd1306
from time import sleep
from ccs811 import CCS811

ccs811 = CCS811()

# SSD1306のピン設定
DEVICE_ADR = 0x3C
DISP_WIDTH = 128
DISP_HEIGHT = 64

# Setting some variables for our reset pin etc.
RESET_PIN = digitalio.DigitalInOut(board.D4)

# Very important… This lets py-gaugette ʻknowʼ what pins to use in order to reset the display
i2c = board.I2C()
oled = adafruit_ssd1306.SSD1306_I2C(DISP_WIDTH, DISP_HEIGHT, i2c, addr=DEVICE_ADR, reset=RESET_PIN)

while(True):

try:

# Clear display.
oled.fill(0)
oled.show()

# Create blank image for drawing.
image = Image.new(“1”, (oled.width, oled.height))
draw = ImageDraw.Draw(image)

# Load a font in 2 different sizes.
font = ImageFont.truetype(“/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf”, 28)
font2 = ImageFont.truetype(“/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf”, 14)

# Draw CO2,TVOC
voc, co2 = ccs811.get()
draw.text((0, 0), “CO2:{}ppm”.format(co2), font=font, fill=255)
draw.text((0, 34), “TVOC:{}ppb”.format(voc), font=font, fill=255)

# Display image
oled.image(image)
oled.show()

sleep(10)

# Clear display.
oled.fill(0)
oled.show()

return

# voc, co2 = ccs811.get()
# print(f”TVOC:{voc:4d} ppb, eCO2:{co2:4d} ppm”)

sleep(1)

except OSError:
# i2c bus somtimes cannot access
continue
except KeyboardInterrupt:

シェアする

  • このエントリーをはてなブックマークに追加

フォローする