通过文件获取温度
- 允许单总线接口
sudo vim /boot/config.txt
在最后一行添加
dtoverlay=w1-gpio,gpiopin=4
注:gpiopin=4
为BCM编码GPIO 对应 BOARD的7号引脚,默认为即为 gpiopin=4
也可指定其他通信引脚。
查看单总线是否启动成功
lsmod
载入模块
sudo modprobe w1-gpio
sudo modprobe w1-therm
进入设备列表,查看设备
cd /sys/bus/w1/devices/
ls
查看温度
cat 28-3c01f0958cf6/w1_slave
通过Python模块获取
安装
w1thermsensor
模块pip install w1thermsensor
python代码 ```python
导入 w1thermsensor
from w1thermsensor import W1ThermSensor, Sensor
配置传感器型号、设备号(进入设备列表查看的设备号去除 - 前的字符 即:28-3c01f0958cf6 在这里传入则为:3c01f0958cf6)
获取设备对象senor
senor = W1ThermSensor(Sensor.DS18B20, “3c01f0958cf6”)
通过设备对象获取温度
temperature = senor.get_temperature()
输出
print(temperature) ```