シェルスクリプトマガジン

Raspberry Pi Pico W/WHで始める電子工作(Vol.88掲載)

著者:米田 聡

本連載では、人気のマイコンボード「Raspberry Pi Pico W/WH」を活用していきます。同ボードは、無
線LANやBluetoothの通信機能を搭載し、入手しやすく価格も手頃なので、IoT機器を自作するのに最適なハードウエアです。第4回は、モノクロ有機ELディスプレイと湿温度センサー「DHT-11」で日本語表示の湿温度計を作ります。

シェルスクリプトマガジン Vol.88は以下のリンク先でご購入できます。

図9 温度と湿度を日本語で表示するプログラム(temp_hum.py)

from machine import Pin
from machine import I2C
from ssd1306 import SSD1306_I2C
from display import PinotDisplay
from pnfont import Font
import dht
import time

# ELパネル
i2c=I2C(0, freq=400000)
ssd = SSD1306_I2C(128, 32, i2c)
# pinotライブラリ
fnt = Font('/fonts/shnmk12u.pfn')
disp = PinotDisplay(panel = ssd, font = fnt)
# DHT-11センサー
sensor = dht.DHT11(Pin(15))

while True:
    os_error = False
    try:
        sensor.measure()
    except OSError as e:
        os_error = True
    
    line1 = "温度: {0:2d} C".format(sensor.temperature())
    line2 = "湿度: {0:2d} %".format(sensor.humidity())
    line3 = "DHT-11エラー" if os_error else "                "
    
    # 温度表示
    disp.locate(1, 0)
    disp.text(line1)
    # 湿度表示
    disp.locate(1,12)
    disp.text(line2)
    # エラー表示
    disp.locate(1,24)
    disp.text(line3)
    
    time.sleep(2)