
树莓派第35讲:智能温度测量系统的综合实验
5星
- 浏览量: 0
- 大小:None
- 文件类型:None
简介:
本课程为树莓派系列教程的第35讲,主要内容是通过树莓派构建一个智能温度测量系统,并进行相关综合实验。参与者将学习如何使用传感器采集数据及实现自动化监控功能。
第35讲:智能温度测量系统(综合实验)
```python
import RPi.GPIO as GPIO
import importlib
import time
LedR = 11
LedG = 12
LedB = 13
Buzz = 15
joystick_module = importlib.import_module(15_joystick_PS2)
ds18b20_module = importlib.import_module(26_ds18b20)
beep_module = importlib.import_module(10_active_buzzer)
rgb_led_module = importlib.import_module(02_rgb_led)
joystick_setup_func = joystick_module.setup
ds18b20_setup_func = ds18b20_module.setup
beep_setup_func = beep_module.setup
rgb_led_setup_func = rgb_led_module.setup
color_dict = {Red: 0xFF0000, Green: 0x00FF00, Blue: 0x0000FF}
low_limit_temp = 29
high_limit_temp = 31
def setup():
global low_limit_temp, high_limit_temp
joystick_setup_func()
ds18b20_setup_func()
beep_setup_func(Buzz)
rgb_led_setup_func(LedR, LedG, LedB)
def edge_detection():
global low_limit_temp, high_limit_temp
direction = joystick_module.direction()
if direction == Pressed:
destroy()
sys.exit(0)
if direction == up and low_limit_temp < high_limit_temp - 1:
high_limit_temp += 1
elif direction == down and low_limit_temp >= -5:
high_limit_temp -= 1
elif direction == right and high_limit_temp <= 125:
low_limit_temp += 1
elif direction == left and low_limit_temp < high_limit_temp - 1:
low_limit_temp -= 1
def loop():
while True:
edge_detection()
current_temperature = ds18b20_module.read()
print(fThe lower limit of temperature : {low_limit_temp})
print(fThe upper limit of temperature : {high_limit_temp})
print(fCurrent temperature : {current_temperature})
if float(current_temperature) < low_limit_temp:
rgb_led_module.setColor(color_dict[Blue])
for i in range(3):
beep_module.beep(0.5)
elif float(low_limit_temp) <= current_temperature < high_limit_temp:
rgb_led_module.setColor(color_dict[Green])
else: # temperature >= high limit
rgb_led_module.setColor(color_dict[Red])
for i in range(3):
beep_module.beep(0.1)
def destroy():
beep_setup_func.destroy()
joystick_setup_func.destroy()
ds18b20_setup_func.destroy()
rgb_led_setup_func.destroy()
GPIO.cleanup()
if __name__ == __main__:
try:
setup()
loop()
except KeyboardInterrupt: # Ctrl+C
destroy()
```
全部评论 (0)


