用于 Raspberry Pi 的 Sensirion SHT1x 系列温度和湿度传感器的 Python 3 库。
项目描述
pi-sht1x 包是一个 Python 3 库,用于通信和控制 Sensirion SHT1x 系列温度和湿度传感器。它主要用于 Raspberry Pi,并且依赖于RPi.GPIO库。
SHT1x(包括 SHT10、SHT11 和 SHT15)是 Sensirion 的表面贴装相对湿度和温度传感器系列。这些传感器将传感器元件和信号处理集成在一个很小的占地面积上,并提供完全校准的数字输出。一个独特的电容传感器元件用于测量相对湿度,而温度则由带隙传感器测量。
该软件包已使用 Raspberry Pi B+ 和 Raspberry Pi 2 进行了测试。在旧型号上运行它应该没有问题,但不能保证。如果您确实遇到任何问题,请让我知道或在 GitHub 项目页面上创建问题:
https://github.com/drohm/pi-sht1x
SHT1x 系列传感器的数据表可在此处找到:
http://bit.ly/1Pafs6j
该库提供以下功能:
进行温度测量
进行湿度测量
进行露点计算
改变供电电压(5V、4V、3.5V、3V、2.5V)
启用或禁用 CRC 检查
读取状态寄存器
写入状态寄存器,提供以下功能:
打开otp_no_reload(每次测量将节省大约 10 毫秒)
打开内部加热元件(有关功能分析,请参阅上面的数据表列表以获取更多信息)
更改测量分辨率,高(14 位温度和 12 位湿度)或低(12 位温度和 8 位湿度)
安装
安装非常简单:
pip3 install pi-sht1x
请注意,要将包安装到系统范围的 PATH 和站点包中,通常需要提升权限 (sudo)。您可以尝试使用install -user或virtualenv进行非特权安装。
用法
实例化 SHT1x 对象时,如果未指定,则使用以下默认值:
gpio_mode: GPIO.BOARD vdd: 3.5V resolution: High (14-bit temperature & 12-bit humidity) heater: False otp_no_reload: False crc_check: True
命令行 - REPL
您可以直接从 REPL 调用 SHT1x 类。为了使用该库,您需要导入包:
from pi_sht1x import SHT1x
现在您可以创建传感器对象并进行测量:
with SHT1x(18, 23, gpio_mode=GPIO.BCM) as sensor: temp = sensor.read_temperature() humidity = sensor.read_humidity(temp) sensor.calculate_dew_point(temp, humidity) print(sensor)
这将使用data_pin=18、sck_pin=23、gpio_mode=GPIO.BCM以及vdd (3.5V)、分辨率(High)、加热器(False)、otp_no_reload (False) 和crc_check (True )的默认值创建 SHT1x 对象)。输出将如下所示:
Temperature: 24.05°C [75.25°F] Humidity: 22.80% Dew Point: 1.38°C
请注意,此库应与with语句之类的上下文管理器一起使用。将它与上下文管理器一起使用将允许程序自行正确清理并将 GPIO 引脚重置回默认状态。
例子.py
该脚本位于示例文件夹中,包括多种使用 SHT1x 类进行温度、湿度和露点测量的方法。为了使用该脚本,请务必使用您在设置中本地使用的 pin 号更新文件顶部附近的DATA_PIN和SCK_PIN常量:
DATA_PIN = 18 SCK_PIN = 23
基于数据表推荐 3.3V 为传感器供电的事实,默认电压(如果在实例化对象时未指定)为 3.5V。如果您使用 5V 为传感器供电,请务必在创建对象时设置该值。要运行脚本:
sudo python3 examples/examples.py
运行该脚本会练习传感器的所有功能。请务必查看脚本以了解您可以做什么以及如何使用传感器进行自定义。样本输出:
$ sudo python3 examples/examples.py Test: using default values: 3.5V, High resolution, no heater, otp_no_reload off, CRC checking enabled... Temperature: 24.49°C [76.04°F] Humidity: 20.68% Dew Point: 0.47°C Temperature: 24.48°C [76.02°F] Humidity: 20.68% Dew Point: 0.46°C Temperature: 24.47°C [76.01°F] Humidity: 20.68% Dew Point: 0.45°C Temperature: 24.51°C [76.06°F] Humidity: 20.68% Dew Point: 0.47°C Temperature: 24.51°C [76.06°F] Humidity: 20.68% Dew Point: 0.47°C Test complete. Test: reading all measurements using GPIO.BCM mode, 3V, High resolution, heater off, otp_no_reload off, and CRC check on. Temperature: 24.48°C [76.02°F] Humidity: 20.61% Dew Point: 0.42°C Temperature: 24.46°C [75.98°F] Humidity: 20.61% Dew Point: 0.40°C Temperature: 24.46°C [75.98°F] Humidity: 20.61% Dew Point: 0.40°C Temperature: 24.48°C [76.02°F] Humidity: 20.68% Dew Point: 0.46°C Temperature: 24.48°C [76.02°F] Humidity: 20.65% Dew Point: 0.44°C Test complete. . . .
RPi.GPIO模块需要 root 权限才能与 Raspberry Pi 上的 GPIO 引脚通信,因此您需要以 root (sudo) 身份运行脚本。
传感器.py
该脚本可从终端调用,并且传感器参数被传递到脚本中。
sudo python3 sensor.py 18 23 -g 'BCM'
这将使用data_pin=18、sck_pin=23和gpio_mode=GPIO.BCM执行传感器脚本。然后,该脚本将创建 SHT1x 类的实例并读取温度、湿度并计算露点五次,每次测量之间休眠 2 秒。输出将如下所示:
$ sudo python3 examples/sensor.py 18 23 -g 'BCM' Temperature: 24.05°C [75.25°F] Humidity: 22.79% Dew Point: 1.37°C Temperature: 24.03°C [75.21°F] Humidity: 22.79% Dew Point: 1.36°C Temperature: 24.01°C [75.16°F] Humidity: 22.79% Dew Point: 1.33°C Temperature: 24.01°C [75.17°F] Humidity: 22.86% Dew Point: 1.38°C Temperature: 24.02°C [75.19°F] Humidity: 22.86% Dew Point: 1.39°C
要获取可以提供给脚本的所有参数的列表,请使用python3 sensor.py -h获取帮助:
$ sudo python3 examples/sensor.py -h usage: sensor.py [-h] [-g {BCM,BOARD}] [-v {5V,4V,3.5V,3V,2.5V}] [-r {HIGH,LOW}] [-e] [-o] [-c] data-pin sck-pin Reads the temperature and relative humidity from the SHT1x series of sensors using the pi_sht1x library. positional arguments: data-pin Data pin used to connect to the sensor. sck-pin SCK pin used to connect to the sensor. optional arguments: -h, --help show this help message and exit -g {BCM,BOARD}, --gpio-mode {BCM,BOARD} RPi.GPIO mode used, either GPIO.BOARD or GPIO.BCM. Defaults to GPIO.BCM. -v {5V,4V,3.5V,3V,2.5V}, --vdd {5V,4V,3.5V,3V,2.5V} Voltage used to power the sensor. Defaults to 3.5V. -r {HIGH,LOW}, --resolution {HIGH,LOW} Resolution used by the sensor, 14/12-bit or 12-8-bit. Defaults to High. -e, --heater Used to turn the internal heater on (used for calibration). -o, --otp-no-reload Used to enable OTP no reload, will save about 10ms per measurement. -c, --no-crc-check Performs CRC checking.
学分
这个模块是为了好玩,并学习如何使用 Python 和 Raspberry Pi 与串行设备进行通信。当我遇到一个绊脚石(有很多......)时,我不时提到以下项目:
项目详情
下载文件
下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。