xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

How to use the Raspberry Pi and Python to control a DHT11 wet and temperate module All In One

How to use the Raspberry Pi and Python to control a DHT11 wet and temperate module All In One

如何使用树莓派和 Python 来控制温湿度模块

image

pip3 install python pacakge / library

$ sudo pip3 install adafruit-circuitpython-dht

# import adafruit_dht

https://github.com/adafruit/Adafruit_CircuitPython_DHT#installing-from-pypi

https://docs.circuitpython.org/projects/dht/en/latest/

依赖项目 libgpiod2, GPIO board

# new
$ sudo apt install libgpiod2
# old
$ sudo apt-get install libgpiod2

# import board

image

$ ./iot/dht11.py
开始读取温湿度 🌡 💦
🌡 华氏温度 Temperature: 75.2 °F
🌡 摄氏温度 Temperature: 24.0 °C
💦 湿度 Humidity: 55%
clear 🚀

代码优化: 精简,抽象,复用

#!/usr/bin/env python3
# coding: utf8

from time import sleep
import board
import adafruit_dht

dhtDevice = adafruit_dht.DHT11(board.D17)
# dhtDevice = adafruit_dht.DHT11(board.D18)
# dhtDevice = adafruit_dht.DHT11(board.D18, use_pulseio=False)
print('开始读取温湿度 🌡 💦')

def get_wed_temp():
  temperature_c = dhtDevice.temperature
  temperature_f = temperature_c * (9 / 5) + 32
  humidity = dhtDevice.humidity
  # print("Temperature: {:.1f} °F / {:.1f} °C".format(temperature_f, temperature_c))
  print("🌡 华氏温度 Temperature: {:.1f} °F".format(temperature_f))
  print("🌡 摄氏温度 Temperature: {:.1f} °C".format(temperature_c))
  print("💦 湿度 Humidity: {}%".format(humidity))

# once
def once():
  try:
    get_wed_temp()
  except KeyboardInterrupt:
    print('Ctrl + C 退出 ✅')
  except RuntimeError as error:
    print("error =", error, error.args[0])
    pass
  except Exception as error:
    # dhtDevice.exit()
    raise error
  finally:
    sleep(2.0)
    dhtDevice.exit()
    # cleanup
    print('clear 🚀')

# infinite loop
def infinite():
  while True:
    once()

infinite()

"""
DHT11 wet and temperate module
https://www.cnblogs.com/xgqfrms/p/17406481.html

https://stackoverflow.com/questions/74167188/get-rid-of-lost-access-to-message-queue-in-a-simple-python-script/76264450#76264450
"""

#!/usr/bin/env python3
# coding: utf8

from time import sleep
import board
import adafruit_dht

dhtDevice = adafruit_dht.DHT11(board.D17)
# dhtDevice = adafruit_dht.DHT11(board.D18)
# dhtDevice = adafruit_dht.DHT11(board.D18, use_pulseio=False)
print('开始读取温湿度 🌡 💦')

def cleanup():
  sleep(1)
  dhtDevice.exit()
  print('clear 🚀')

def get_wed_temp():
  temperature_c = dhtDevice.temperature
  temperature_f = temperature_c * (9 / 5) + 32
  humidity = dhtDevice.humidity
  # print("Temperature: {:.1f} °F / {:.1f} °C".format(temperature_f, temperature_c))
  print("🌡 华氏温度 Temperature: {:.1f} °F".format(temperature_f))
  print("🌡 摄氏温度 Temperature: {:.1f} °C".format(temperature_c))
  print("💦    湿度    Humidity: {}%".format(humidity), "\n")
  sleep(1.0)

# once
def once():
  try:
    get_wed_temp()
  except KeyboardInterrupt:
    print('Ctrl + C 退出 ✅')
  except RuntimeError as error:
    print("error =", error, error.args[0])
    pass
  except Exception as error:
    raise error
  finally:
    cleanup()

# infinite loop
def infinite():
  while True:
    get_wed_temp()

if __name__ == "__main__":
  try:
    once()
    # infinite()
  except KeyboardInterrupt:
    print('Ctrl + C 退出 ✅')
    cleanup()
  except RuntimeError as error:
    print("error =", error, error.args[0])
    cleanup()
    pass
  except Exception as error:
    cleanup()
    raise error

"""
DHT11 wet and temperate module
https://www.cnblogs.com/xgqfrms/p/17406481.html
https://stackoverflow.com/questions/74167188/get-rid-of-lost-access-to-message-queue-in-a-simple-python-script/76264450#76264450

# infinite
$ ./iot/dht11.py
# once
$ watch -n 1 ./iot/dht11.py
"""

image

todos

温湿度信息输出到 OLED 显示屏上

  1. dht11.py 把输出温湿度信息到文件中 wt_temp.txt
  2. oled.py 读取 wt_temp.txt,显示到 OLED 屏幕上


demos

image

#!/usr/bin/env python3
# coding: utf8

from time import sleep
import board
import adafruit_dht

dhtDevice = adafruit_dht.DHT11(board.D17)
# dhtDevice = adafruit_dht.DHT11(board.D18)
# dhtDevice = adafruit_dht.DHT11(board.D18, use_pulseio=False)

print('开始读取温湿度 🌡 💦')

try:
  temperature_c = dhtDevice.temperature
  temperature_f = temperature_c * (9 / 5) + 32
  humidity = dhtDevice.humidity
  # print("Temp: {:.1f} °F / {:.1f} °C".format(temperature_f, temperature_c))
  print("🌡 华氏温度 Temp: {:.1f} °F".format(temperature_f))
  print("🌡 摄氏温度 Temp: {:.1f} °C".format(temperature_c))
  print("💦 Humidity: {}%".format(humidity))
except KeyboardInterrupt:
  print('Ctrl + C 退出!')
except RuntimeError as error:
  print("error =", error, error.args[0])
  pass
except Exception as error:
  dhtDevice.exit()
  raise error
finally:
  sleep(2.0)
  # cleanup
  print('clear 🚀')

# while True:
#   try:
#     temperature_c = dhtDevice.temperature
#     temperature_f = temperature_c * (9 / 5) + 32
#     humidity = dhtDevice.humidity
#     # print("Temp: {:.1f} °F / {:.1f} °C".format(temperature_f, temperature_c))
#     print("华氏温度 Temp: {:.1f} °F".format(temperature_f))
#     print("摄氏温度 Temp: {:.1f} °C".format(temperature_c))
#     print("Humidity: {}%".format(humidity))
#   except KeyboardInterrupt:
#     print('Ctrl + C 退出!')
#   except RuntimeError as error:
#     print(error.args[0])
#     sleep(2.0)
#     continue
#   except Exception as error:
#     dhtDevice.exit()
#     raise error
#   finally:
#     sleep(2.0)
#     # cleanup
#     print('clear 🚀')

"""
https://www.cnblogs.com/xgqfrms/p/17406481.html

"""

#!/usr/bin/env python3
# coding: utf8

from time import sleep
import board
import adafruit_dht

dhtDevice = adafruit_dht.DHT11(board.D17)
# dhtDevice = adafruit_dht.DHT11(board.D18)
# dhtDevice = adafruit_dht.DHT11(board.D18, use_pulseio=False)
print('开始读取温湿度 🌡 💦')

# once
def once():
  try:
    temperature_c = dhtDevice.temperature
    temperature_f = temperature_c * (9 / 5) + 32
    humidity = dhtDevice.humidity
    # print("Temperature: {:.1f} °F / {:.1f} °C".format(temperature_f, temperature_c))
    print("🌡 华氏温度 Temperature: {:.1f} °F".format(temperature_f))
    print("🌡 摄氏温度 Temperature: {:.1f} °C".format(temperature_c))
    print("💦 湿度 Humidity: {}%".format(humidity))
  except KeyboardInterrupt:
    print('Ctrl + C 退出 ✅')
  except RuntimeError as error:
    print("error =", error, error.args[0])
    pass
  except Exception as error:
    # dhtDevice.exit()
    raise error
  finally:
    sleep(2.0)
    dhtDevice.exit()
    # cleanup
    print('clear 🚀')

# infinite loop
def infinite():
  while True:
    try:
      temperature_c = dhtDevice.temperature
      temperature_f = temperature_c * (9 / 5) + 32
      humidity = dhtDevice.humidity
      # print("Temperature: {:.1f} °F / {:.1f} °C".format(temperature_f, temperature_c))
      print("🌡 华氏温度 Temperature: {:.1f} °F".format(temperature_f))
      print("🌡 摄氏温度 Temperature: {:.1f} °C".format(temperature_c))
      print("💦 湿度 Humidity: {}%".format(humidity))
    except KeyboardInterrupt:
      print('Ctrl + C 退出 ✅')
    except RuntimeError as error:
      print("error =", error, error.args[0])
      pass
    except Exception as error:
      # dhtDevice.exit()
      raise error
    finally:
      sleep(2.0)
      dhtDevice.exit()
      # cleanup
      print('clear 🚀')

# once()
infinite()

"""
https://www.cnblogs.com/xgqfrms/p/17406481.html

https://stackoverflow.com/questions/74167188/get-rid-of-lost-access-to-message-queue-in-a-simple-python-script/76264450#76264450
"""

https://github.com/web-fullstack/Adafruit_CircuitPython_DHT11

(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问

https://www.cnblogs.com/xgqfrms/ 查看原创文章!

Adafruit_CircuitPython_DHT

PR

https://github.com/adafruit/Adafruit_CircuitPython_DHT/pull/91

Creating and sharing a CircuitPython library

https://learn.adafruit.com/creating-and-sharing-a-circuitpython-library/sharing-our-docs-on-readthedocs#sphinx-5-1

https://github.com/adafruit/Adafruit_CircuitPython_RGB_Display

https://cookiecutter.readthedocs.io/en/latest/

https://www.youtube.com/watch?v=4mkFfce46zE

https://learn.adafruit.com/modern-replacements-for-dht11-dht22-sensors

DHT11

Temperature & Humidity Sensor

image

image

https://wiki.seeedstudio.com/Grove-TemperatureAndHumidity_Sensor/

https://www.seeedstudio.com/Grove-Temperature-Humidity-Sensor-DHT11.html

Arduino Uno R3 $27.60

https://en.wikipedia.org/wiki/Arduino_Uno

image

https://www.arduino.cc/

https://store.arduino.cc/products/arduino-uno-rev3/

image

https://store-usa.arduino.cc/products/arduino-uno-rev3?selectedStore=us

refs

https://learn.adafruit.com/dht-humidity-sensing-on-raspberry-pi-with-gdocs-logging/python-setup

https://shumeipai.nxez.com/2014/10/10/raspberry-dht11-get-temperature-data.html

https://hive.blog/hive-105017/@oflyhigh/dht11



©xgqfrms 2012-2021

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2023-05-16 18:27  xgqfrms  阅读(52)  评论(9编辑  收藏  举报