通过文件获取温度

  1. 允许单总线接口

sudo vim /boot/config.txt
在最后一行添加

  1. dtoverlay=w1-gpio,gpiopin=4

注:gpiopin=4 为BCM编码GPIO 对应 BOARD的7号引脚,默认为即为 gpiopin=4 也可指定其他通信引脚。

  1. 查看单总线是否启动成功

    1. lsmod

    image.png

  2. 载入模块

    1. sudo modprobe w1-gpio
    2. sudo modprobe w1-therm
  3. 进入设备列表,查看设备

    1. cd /sys/bus/w1/devices/
    2. ls

    image.png

  4. 查看温度

    1. cat 28-3c01f0958cf6/w1_slave

    image.png

    通过Python模块获取

  5. 安装w1thermsensor模块

    1. pip install w1thermsensor
  6. python代码 ```python

    导入 w1thermsensor

    from w1thermsensor import W1ThermSensor, Sensor

配置传感器型号、设备号(进入设备列表查看的设备号去除 - 前的字符 即:28-3c01f0958cf6 在这里传入则为:3c01f0958cf6)

获取设备对象senor

senor = W1ThermSensor(Sensor.DS18B20, “3c01f0958cf6”)

通过设备对象获取温度

temperature = senor.get_temperature()

输出

print(temperature) ```