임베디드

[라즈베리파이] OLED와 BMP280 을 이용한 온도계 제작

히똔 2022. 4. 26. 15:02
728x90
반응형

개요

BMP 280으로 현재 온도를 얻어오고, 현재시간을 포함하여 OLED에 실시간으로 출력한다.

 

회로구성

준비물 : 라즈베리파이, OLED, BMP280 

 

API 설치

https://www.waveshare.com/wiki/0.96inch_OLED_(B)

 

0.96inch OLED (B) - Waveshare Wiki

 

www.waveshare.com

공식 사이트 document의 demo code를 주소 복사해서 wget으로 다운로드 및 압축풀기로 API를 다운받는다.

 

코드

from bmp280 import BMP280
import time
from smbus import SMBus

import sys
import os
picdir = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), 'pic')
libdir = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), 'lib')
if os.path.exists(libdir):
    sys.path.append(libdir)

import logging    
from time import sleep
import traceback
from waveshare_OLED import OLED_0in96
from PIL import Image,ImageDraw,ImageFont\

import datetime

disp = OLED_0in96.OLED_0in96() # Initialize library.
disp.Init() # Clear display.
disp.clear()

font1 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 18)
font2 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 24)

bus = SMBus(1)
bmp280 = BMP280(i2c_dev=bus)

while True :
    # Create blank image for drawing.
    image1 = Image.new('1', (disp.width, disp.height), "WHITE")
    draw = ImageDraw.Draw(image1)
    
    temperature = bmp280.get_temperature()
    pressure = bmp280.get_pressure()
    print('{:05.2f}*C {:05.2f}hPa' .format(temperature, pressure))
    draw.text((20,0), '{:05.2f}*C ' .format(temperature), font=font2,fill=0)
    
    today = datetime.datetime.today()
    print(today.hour)
    print(today.minute)
    draw.text((20,30),'%s : %s' %(today.hour, today.minute), font=font2,fill=0)
    
    disp.ShowImage(disp.getbuffer(image1))
    sleep(3)

 

결과

 

728x90
반응형