项目简介
随着春天的远去,骄阳似火的夏天终于来临了。人们纷纷开启空调避暑纳凉,仿佛吹空调就是舒适;其实人体感觉最舒适的环境取决于温度和湿度,并受气温和气湿两者综合作用的结果。夏季,为了保证人们的身体健康和工作效率,同时又能够最大限度的节约能源,专家们推荐使用“黄金温度”,也就是27℃左右。因为在这一温度下人们感觉最舒适,而且也比较省电,因而被称作“黄金温度”。如果把空调开得很冷,室内温度过低,不仅不利于人们的身体健康,而且又浪费了大量宝贵的电力资源。另外,夏季空气中相对湿度较高也会令人不舒服,当室内温度高于30℃,相对湿度大于80%时,环境温度高于体表温度、人体皮肤温度很快升高,此时人的体温调节系统处于高负荷状态。为更好的监控室内温度和湿度,温湿度测量仪应运而生。
项目功能:
1.OLED时钟功能
2.实时温度、湿度显示
3.按钮控制输出测量打印结果
步骤1 硬件组装
1.树莓派扩展板插入树莓派;
2..热敏打印机USB连接树莓派;
3.按钮接扩展板5号口;
4.温湿度测量仪,和OLED屏幕连接扩展板的IIC接口
步骤2 程序调试
查看IIC硬件接口地址
运行命令 sudo i2cdetect -y 1
45是温湿度测量仪,3C是OLED屏幕
导入库,初始化oled屏幕
# -*- coding: UTF-8 -*-
#导入需要的库
from RPi import GPIO
import os
import subprocess
import smbus
import time
import Adafruit_SSD1306
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
#初始化oled屏幕
# Raspberry Pi pin configuration:
RST =24
# Note the following are only used with SPI:
DC = 23
SPI_PORT = 0
SPI_DEVICE = 0
# Note you can change the I2C address by passing an i2c_address parameter like:
disp = Adafruit_SSD1306.SSD1306_128_64(rst=RST, i2c_address=0x3C)
# Initialize library.
disp.begin()
# Clear display.
disp.clear()
disp.display()
# Create blank image for drawing.
# Make sure to create image with mode '1' for 1-bit color.
width = disp.width
height = disp.height
image = Image.new('1', (width, height))
font = ImageFont.load_default()
自定义函数GETIIC1,功能为初始化温湿度模块,获取温度、湿度值;通过os.environ函数赋值给echo语句;subprocess.call函数调用echo命令实现打印操作;
def GETIIC1():
bus = smbus.SMBus(1)
# SHT30 address, 0x45(68)
bus.write_i2c_block_data(0x45, 0x2C, [0x06])
time.sleep(0.5)
# SHT30 address, 0x44(68)
# Read data back from 0x00(00), 6 bytes
# Temp MSB, Temp LSB, Temp CRC, Humididty MSB, Humidity LSB, Humidity CRC
data = bus.read_i2c_block_data(0x45, 0x00, 6)
# Convert the data
temp = data[0] * 256 + data[1]
cTemp = -45 + (175 * temp / 65535.0)
fTemp = -49 + (315 * temp / 65535.0)
humidity = 100 * (data[3] * 256 + data[4]) / 65535.0
ctime=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
print(ctime)
print ("Temperature in Celsius is : %.2f C" %cTemp)
print ("Temperature in Fahrenheit is : %.2f F" %fTemp)
print ("Relative Humidity is : %.2f %%RH" %humidity)
cs='-----------------------------'
cs2=' '
os.environ['ctime']=str(ctime)
os.environ['tempc']=str(int(cTemp))
os.environ['tempf']=str(int(fTemp))
os.environ['humidity2']=str(int(humidity))
os.environ['cs']=str(cs)
os.environ['cs2']=str(cs2)
subprocess.call("echo $cs >/dev/ttyUSB0 ",shell=True)
subprocess.call("echo $ctime >/dev/ttyUSB0",shell=True)
subprocess.call("echo Temperature in Celsius is:$tempc C>/dev/ttyUSB0",shell=True)
subprocess.call("echo Temperature in Fahrenheitis:$tempf F >/dev/ttyUSB0",shell=True)
subprocess.call("echo Relative Humidity is:$humidity2 % >/dev/ttyUSB0",shell=True)
subprocess.call("echo $cs >/dev/ttyUSB0",shell=True)
subprocess.call("echo $cs2 >/dev/ttyUSB0",shell=True)
subprocess.call("echo $cs2 >/dev/ttyUSB0",shell=True)
自定义函数GETIIC2,功能为初始化温湿度模块,获取温度、湿度值;return回传 cTemp,fTemp,humidity三个值
def GETIIC2():
bus = smbus.SMBus(1)
# SHT30 address, 0x45(68)
bus.write_i2c_block_data(0x45, 0x2C, [0x06])
time.sleep(0.5)
# SHT30 address, 0x44(68)
# Read data back from 0x00(00), 6 bytes
# Temp MSB, Temp LSB, Temp CRC, Humididty MSB, Humidity LSB, Humidity CRC
data = bus.read_i2c_block_data(0x45, 0x00, 6)
# Convert the data
temp = data[0] * 256 + data[1]
cTemp = -45 + (175 * temp / 65535.0)
fTemp = -49 + (315 * temp / 65535.0)
humidity = 100 * (data[3] * 256 + data[4]) / 65535.0
return cTemp,fTemp,humidity
主程序:循环执行判断,按下按钮则,执行打印命令;松开则持续刷新OLED屏幕,显示相关信息。
while True:
if (GPIO.input(5)==GPIO.LOW):
print('正在打印,測試結果!')
GETIIC1()
time.sleep(0.3)
else :
print('請按按鈕,打印結果!')
GETIIC2()
a=GETIIC2()
tt=str(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) )
x=2
top=0
cT=str(int(a[0]))
fT=str(int(a[1]))
hT=str(int(a[2]))
draw = ImageDraw.Draw(image)
draw.rectangle((0,0,width,height), outline=0, fill=0)
draw.text((x, top), tt , font=font, fill=255)
draw.text((x, top+10), 'Temperature is : '+ cT+' C', font=font, fill=255)
draw.text((x, top+20), 'Fahrenheit is: '+fT+' T', font=font, fill=255)
draw.text((x, top+30), 'Humidity is : '+hT+' %', font=font, fill=255)
disp.image(image)
disp.display()
time.sleep(0.1)
刷新代码,测试硬件
步骤3 外壳组装,
在相机外壳基础上,扩大指示灯孔,安装温湿度测量模块
依次安装按钮、显示屏、树莓派、热敏打印机,运行程序测试结果
项目改进
1.屏幕可以升级为7寸
2.按钮控制可以调整为蓝牙或是无限控制
3.连接物联网方便自动控制温度和数据测量统计
评论