核桃派
直播中

张峻巍

1年用户 42经验值
擅长:嵌入式技术 控制/MCU
私信 关注
[讨论]

【核桃派ZeroW开发板体验连载】小小气象站 优化1

最终阶段

这个作品马上来到了最终阶段,可是使用面包板连接十分麻烦。于是我们设计了PCB板,打板,接着焊接组装上上一篇的代码(再放一次是为了防止找不到了):

'''
实验名称:小小气象站雏形
实验平台:核桃派ZeroW
'''
import time,board,busio,adafruit_ssd1306,adafruit_ahtx0,adafruit_bmp280
from digitalio import DigitalInOut
from PIL import Image, ImageDraw, ImageFont


i2c = busio.I2C(board.SCL1, board.SDA1)
sensor = adafruit_ahtx0.AHTx0(i2c)
# #构建BMP280,模块I2C地址为默认的0x76.
bmp280 = adafruit_bmp280.Adafruit_BMP280_I2C(i2c,address=0x76)
disp = adafruit_ssd1306.SSD1306_I2C(128, 64, i2c, addr=0x3C)
bmp280.sea_level_pressure = 1013.25
# 清屏
disp.fill(0)
disp.show()

width = disp.width
height = disp.height
# 单色位图
image = Image.new("1", (width, height))

# 定义绘图对象
draw = ImageDraw.Draw(image)

# 绘制一个黑色填充的矩形以清屏
draw.rectangle((0, 0, width, height), outline=0, fill=0)
top = -2
# 定义字体
font = ImageFont.load_default()
while True:
    draw.rectangle((0, 0, width, height), outline=0, fill=0)
    draw.text((0, top + 0), "temp: %.2fC"  %(sensor.temperature), font=font, fill=255)
    draw.text((0, top + 8), "humi: %.2f"  %(sensor.temperature), font=font, fill=255)
    draw.text((0, top + 16), "Press: %.2fhPa"  %(sensor.temperature), font=font, fill=255)
    draw.text((0, top + 25), "Alti: %.2fmeters"  %(sensor.temperature), font=font, fill=255)
    # 显示信息
    disp.image(image)
    disp.show()
    time.sleep(1)

优化

接着整个项目,做完了发现人不在的时候一直在显示,十分费电,于是我加上了人体感应传感器,做到人走息屏的效果,代码如下:

'''
实验名称:小小气象站
实验平台:核桃派ZeroW
'''
import time,board,busio,adafruit_ssd1306,adafruit_ahtx0,adafruit_bmp280
from digitalio import DigitalInOut, Direction, Pull
from PIL import Image, ImageDraw, ImageFont

i = 0
#构建按键对象和初始化
irq = DigitalInOut(board.PC8) #定义引脚编号
irq.direction = Direction.INPUT #IO为输入
i2c = busio.I2C(board.SCL1, board.SDA1)
sensor = adafruit_ahtx0.AHTx0(i2c)
# #构建BMP280,模块I2C地址为默认的0x76.
bmp280 = adafruit_bmp280.Adafruit_BMP280_I2C(i2c,address=0x76)
disp = adafruit_ssd1306.SSD1306_I2C(128, 64, i2c, addr=0x3C)
bmp280.sea_level_pressure = 1013.25
# 清屏
disp.fill(0)
disp.show()

width = disp.width
height = disp.height
# 单色位图
image = Image.new("1", (width, height))

# 定义绘图对象
draw = ImageDraw.Draw(image)

# 绘制一个黑色填充的矩形以清屏
draw.rectangle((0, 0, width, height), outline=0, fill=0)
top = -2
# 定义字体
font = ImageFont.load_default()
while True:
    if irq.value == 1:
        i = 0
        draw.rectangle((0, 0, width, height), outline=0, fill=0)
        draw.text((0, top + 0), "temp: %.2fC"  %(sensor.temperature), font=font, fill=255)
        draw.text((0, top + 8), "humi: %.2f"  %(sensor.temperature)+'%', font=font, fill=255)
        draw.text((0, top + 16), "Press: %.2fhPa"  %(sensor.temperature), font=font, fill=255)
        draw.text((0, top + 25), "Alti: %.2fmeters"  %(sensor.temperature), font=font, fill=255)
        # 显示信息
        disp.image(image)
        disp.show()
    elif irq.value == 0:
        if i<=0:
            disp.fill(0)
            disp.show()
        else:
            pass
        i+=1
    time.sleep(1)

效果:

优化1

更多回帖

发帖
×
20
完善资料,
赚取积分