七仔的博客

七仔的博客GithubPages分博

0%

【“魔镜”系列】DHT11传感器温湿度的获取与显示

通过DHT11获取温湿度信息然后通过Queue发送数据到显示进程再使用selenium执行js语句显示

【“魔镜”系列】DHT11传感器温湿度的获取与显示

通过DHT11获取温湿度信息

这里有我参考别人修改的获取温湿度的py文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/python3
import RPi.GPIO as GPIO
import time


channel = 17 # 设置树莓派端口号(BCM编码)
j = 0 # 初始化计数器为0
data = [] # 初始化数据存储数组


# 初始化
def initialize():
global channel
GPIO.setmode(GPIO.BCM) # 设置gpio引脚编号模式,有两种编号模式可供选择,自己随意设置就好
time.sleep(1) # 程序暂停一秒钟
GPIO.setup(channel, GPIO.OUT) # 设置6号口为输出模式
GPIO.output(channel, GPIO.LOW) # 6号口输出低电平,即发送触发信号
time.sleep(0.02) # 低电平维持0.02秒
GPIO.output(channel, GPIO.HIGH) # 输出高电平,标志低电平结束
GPIO.setup(channel, GPIO.IN) # 设置端口模式为输入
while GPIO.input(channel) is GPIO.LOW: # 跳过初始状态的低电平
pass
while GPIO.input(channel) is GPIO.HIGH: # 跳过初始状态的高电平
pass


# 获取数据
def get_data():
global j, data, channel
# 进入循环
while j < 40: # 仅仅存储40个数据
k = 0
while GPIO.input(channel) is GPIO.LOW: # 跳过低电平
continue

while GPIO.input(channel) is GPIO.HIGH: # 如果是高电平,则进入循环,高电平结束时停止
k += 1 # 计数器自增
if k > 100: # 如果高电平计数器大于100,则跳出循环,数据错误
break
if k < 8: # 如果计数器数值小于8次,则认为值为0
data.append(0)
else: # 否则认为值为1
data.append(1)
j += 1 # 数据位数计数器加一


# 数据处理
def data_processing():
global data
# 按位切割数据
humidity_bit = data[0:8]
humidity_point_bit = data[8:16]
temperature_bit = data[16:24]
temperature_point_bit = data[24:32]
check_bit = data[32:40]

humidity = 0
humidity_point = 0
temperature = 0
temperature_point = 0
check = 0
# 计算各个数据结果和校验值
for i in range(8):
humidity += humidity_bit[i] * 2 ** (7 - i)
humidity_point += humidity_point_bit[i] * 2 ** (7 - i)
temperature += temperature_bit[i] * 2 ** (7 - i)
temperature_point += temperature_point_bit[i] * 2 ** (7 - i)
check += check_bit[i] * 2 ** (7 - i)
# 计算检查值
tmp = humidity + humidity_point + temperature + temperature_point
return temperature, humidity, check, tmp


# 获取温湿度数据
def get_temperature_humidity(setting_channel):
global channel
channel = setting_channel
initialize()
get_data()
temperature, humidity, check, tmp = data_processing()
GPIO.cleanup()
return temperature, humidity, check, tmp # 温度、湿度、校验值、检查值

使用时直接调用get_temperature_humidity并传入树莓派端口号(引脚)即可

新版本获取温湿度信息

Home Assistant有个获取DHT传感器信息的插件,配置如下:

1
2
3
4
5
6
7
sensor:
platform: dht
sensor: DHT11
pin: 17
monitored_conditions:
- temperature
- humidity

引脚为17号,然后GET

1
http://树莓派ip地址:8123/api/states/sensor.dht_sensor_humidity

并加上Bearer形式的token即可获取到湿度信息,温度同理

显示到屏幕上

环境进程通过Queue队列发送数据,显示进程获取到数据再通过selenium执行javascript语句显示到屏幕上,其中温湿度的显示是使用两个ECharts表盘显示的,我封装成了个Vue插件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<template>
<div class="sensor_line" style="margin-top: 50px;">
<div id="chart" class="sensor_meter"></div>
<span id="hide_temp" style="display:none">0</span>
<span id="hide_humidity" style="display:none">0</span>
</div>
</template>
<script>
export default {
name: "Sensor",
data() {
return {
temp: 0,
humidity: 0
}
},
mounted() {
this.init();
setInterval(this.refresh_data, 3000);
},
methods:{
init(){
var chart = this.$echarts.init(document.getElementById("chart"));
var option = {
title: {
text: '温度 湿度',
x: 'center',
textStyle: {
color: '#fff',
fontSize: 24
}
},
grid: {
x: 20,
y: 30,
x2: 20,
y2: 10,
},
series: [{
name: '温度',
type: 'gauge',
center: ['25%', '50%'],
min: 0,
max: 60,
splitNumber: 8,
axisLine: {
lineStyle: {
color: [
[12 / 400, '#668888'],
[35.4 / 400, '#888888'],
[55.4 / 400, '#aa8888'],
[150.4 / 400, '#cc8888'],
[250.4 / 400, '#dd8888'],
[400 / 400, '#ff8888']
],
width: 3,
}
},
axisLabel: {
show: false,
},
detail: {
formatter: '{value}℃',
offsetCenter: [0, '60%'],
},
data: [{
value: this.temp
}]
},{
name: '湿度',
type: 'gauge',
center: ['75%', '50%'],
min: 0,
max: 100,
splitNumber: 8,
axisLine: {
lineStyle: {
color: [
[12 / 400, '#668888'],
[35.4 / 400, '#888888'],
[55.4 / 400, '#aa8888'],
[150.4 / 400, '#cc8888'],
[250.4 / 400, '#dd8888'],
[400 / 400, '#ff8888']
],
width: 3,
}
},
axisLabel: {
show: false,
},
detail: {
formatter: '{value}%',
offsetCenter: [0, '60%'],
},
data: [{
value: this.humidity
}]
}]
};
chart.setOption(option);
},
refresh_data(){
this.temp = document.getElementById("hide_temp").innerText;
this.humidity = document.getElementById("hide_humidity").innerText;
this.init();
}
}
}
</script>
<style scoped>
.sensor_meter {
width:500px;
height:250px;
}
</style>

此为博主副博客,留言请去主博客,转载请注明出处:https://www.baby7blog.com/myBlog/76.html

欢迎关注我的其它发布渠道