【发布时间】:2019-08-27 16:45:28
【问题描述】:
我是 IoT 新手,我正在尝试使用 Raspberry Pi 读取土壤湿度传感器读数。我需要确切的水分值,而不仅仅是布尔值,因为水中是否存在。
我尝试使用 Arduino-UNO 读取确切的值,但不确定如何在 python 中使用 Raspberry Pi 进行读取。 我有在 Intructables 上找到的这段代码。 https://www.instructables.com/id/Soil-Moisture-Sensor-Raspberry-Pi/
#!/usr/bin/python
import RPi.GPIO as GPIO
import time
#GPIO SETUP
channel = 21
GPIO.setmode(GPIO.BCM)
GPIO.setup(channel, GPIO.IN)
def callback(channel):
if GPIO.input(channel):
print "no Water Detected!"
else:
print "Water Detected!"
GPIO.add_event_detect(channel, GPIO.BOTH, bouncetime=300) # let us know when the pin goes HIGH or LOW
GPIO.add_event_callback(channel, callback) # assign function to GPIO PIN, Run function on change
# infinite loop
while True:
time.sleep(1)
这段代码只是说明是否检测到水,但我需要实际的湿度值。
【问题讨论】:
-
GPIO.input(channel)返回什么? -
1 或 0 @Nick Brady
标签: python raspberry-pi iot sensors