{"id":19275,"date":"2022-05-26T00:00:37","date_gmt":"2022-05-25T16:00:37","guid":{"rendered":"https:\/\/techclass.wfjydl.com\/?p=19275"},"modified":"2026-05-19T10:57:32","modified_gmt":"2026-05-19T02:57:32","slug":"%e6%9e%81%e5%ae%a2%e9%80%a0%e7%89%a9%ef%bc%9a%e7%ae%80%e6%98%93raspberry-pi%e6%b8%a9%e6%b9%bf%e5%ba%a6%e4%bc%a0%e6%84%9f%e5%99%a8","status":"publish","type":"post","link":"https:\/\/techclass.wfjydl.com\/knowledge\/tech-info\/raspberry\/geekcraft-easy-raspberry-pi-humidity-temperature-sensor","title":{"rendered":"\u6781\u5ba2\u9020\u7269\uff1a\u7b80\u6613Raspberry Pi\u6e29\u6e7f\u5ea6\u4f20\u611f\u5668"},"content":{"rendered":"<p>\u8fd9\u7bc7\u6587\u7ae0\u6765\u6e90\u4e8e<a href=\"https:\/\/www.deviceplus.com\/\">DevicePlus.com<\/a>\u82f1\u8bed\u7f51\u7ad9\u7684\u7ffb\u8bd1\u7a3f\u3002<\/p>\n<p>\u76ee\u5f55<br \/>\n1. \u7b80\u4ecb<br \/>\n1.1 \u8ba4\u8bc6DHT22\uff1a\u4e00\u79cd\u6e29\u6e7f\u5ea6\u4f20\u611f\u5668<br \/>\n1.2 \u60a8\u5c06\u8981\u5b66\u5230\u7684\u5185\u5bb9<br \/>\n2. \u5c06\u60a8\u7684Raspberry Pi\u4e0eDHT22\u4f20\u611f\u5668\u8fde\u63a5<br \/>\n2.1 DHT22\uff1a\u57fa\u672c\u7535\u8def<br \/>\n2.1.1 BOM<br \/>\n2.1.2 \u542f\u52a8\uff01<br \/>\n2.2 DHT22\u673a\u623f\u73af\u5883\u63a7\u5236\u5668<br \/>\n2.2.1 BOM<br \/>\n2.2.2 \u542f\u52a8\uff01<\/p>\n<div id=\"attachment_8896\" class=\"wp-caption aligncenter\" style=\"width: 710px;\">\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-8896\" src=\"https:\/\/techclass.wfjydl.com\/wp-content\/uploads\/2026\/04\/raspberry-pi-humidity-temperature-sensor.jpg\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" srcset=\"https:\/\/techclass.wfjydl.com\/wp-content\/uploads\/2026\/04\/raspberry-pi-humidity-temperature-sensor.jpg 700w, https:\/\/techclass.wfjydl.com\/wp-content\/uploads\/2026\/04\/raspberry-pi-humidity-temperature-sensor.jpg 300w, https:\/\/techclass.wfjydl.com\/wp-content\/uploads\/2026\/04\/raspberry-pi-humidity-temperature-sensor.jpg 260w, https:\/\/techclass.wfjydl.com\/wp-content\/uploads\/2026\/04\/raspberry-pi-humidity-temperature-sensor.jpg 50w, https:\/\/techclass.wfjydl.com\/wp-content\/uploads\/2026\/04\/raspberry-pi-humidity-temperature-sensor.jpg 150w\" alt=\"raspberry Pi humidity and temperature sensor\" width=\"700\" height=\"350\" aria-describedby=\"caption-attachment-8896\" \/><\/p>\n<p id=\"caption-attachment-8896\" class=\"wp-caption-text\">exentia \u2013 <a href=\"https:\/\/stock.adobe.com\" target=\"_blank\" rel=\"noopener noreferrer\">stock.adobe.com<\/a><\/p>\n<\/div>\n<h2>1. \u7b80\u4ecb<\/h2>\n<p>\u80fd\u591f\u5728\u4efb\u610f\u73af\u5883\u4e2d\u8f7b\u677e\u611f\u77e5\u73af\u5883\u6e7f\u5ea6\u7684\u80fd\u529b\u975e\u5e38\u91cd\u8981\uff0c\u56e0\u4e3a\u6e7f\u5ea6\u4e0d\u4ec5\u4f1a\u5f71\u54cd\u4eba\u7684\u8212\u9002\u5ea6\uff0c\u8fd8\u4f1a\u5f71\u54cd\u4eba\u7c7b\u673a\u4f53\u7684\u8fd0\u4f5c\u3002\u6211\u4eec\u5728\u672c\u6587\u4e2d\u5c06\u8981\u4f7f\u7528\u7684\u53cc\u6a21\u4f20\u611f\u5668\u975e\u5e38\u9002\u7528\u4e8e\u521b\u5ba2\u9879\u76ee\uff0c\u4f8b\u5982\u6539\u5584HVAC\u3001\u6c14\u8c61\u7ad9\u548c\u7269\u8054\u7f51\u6052\u6e29\u5668\uff0c\u5c24\u5176\u662f\u4e0e\u4ec5\u5728Raspbian\u4e0a\u53ef\u7528\u7684\u5927\u91cf\u9ad8\u8d28\u91cf\u8f6f\u4ef6\u5305\u76f8\u7ed3\u5408\u4e4b\u540e\u3002\u60a8\u53ef\u4ee5\u5728Pi\uff08\u6216Linux\u7ec8\u7aef\uff09\u4e0a\u8bd5\u8bd5\u8fd9\u4e2a\uff1a<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism undefined-numbers lang-c\" data-lang=\"C\"><code>\r\nwget \\ http:\/\/archive.raspbian.org\/raspbian\/dists\/stable\/main\/binary-armhf\/Packages.xz \\\r\n-O - 2&amp;gt;\/dev\/null | xz -dfc | grep -c '^Package: ' \\\r\n| cut -d ' ' -f 2 \r\n<\/code><\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>\u7b49\u5f85\u5176\u8fd0\u884c\u5b8c\u6210\u3002\u5b83\u5c06\u8f93\u51fa\u60a8\u53ef\u4ee5\u4f7f\u7528\u7684\u8f6f\u4ef6\u5305\u6570\u91cf\u3002\u5728\u64b0\u5199\u672c\u6587\u65f6\uff0c\u8be5\u5b58\u50a8\u5e93\u4e2d\u670961.487\u4e2a\u5305\u3002\u8fd9\u4e2a\u6570\u91cf\u6bcf\u5929\u90fd\u5728\u589e\u52a0\u3002<\/p>\n<p>\u65e0\u8bba\u60a8\u9700\u8981\u4ec0\u4e48\u529f\u80fd\uff0c\u5e94\u8be5\u90fd\u53ef\u4ee5\u901a\u8fc7\u8fd9\u4e9b\u8f6f\u4ef6\u5305\u6765\u5b9e\u73b0\u3002\u60a8\u53ea\u9700\u8981\u63d0\u4f9b\u611f\u77e5\u7684\u73af\u5883\u6570\u636e\uff0c\u672c\u6587\u7b2c\u4e8c\u90e8\u5206\u4e2d\u7684\u793a\u4f8b\u5c06\u4f1a\u544a\u8bc9\u60a8\u5982\u4f55\u83b7\u53d6\u8fd9\u4e9b\u6570\u636e\u3002<\/p>\n<p>\u76f8\u5bf9\u6e7f\u5ea6\uff08RH\uff09\u7684\u6982\u5ff5\u5728<a href=\"https:\/\/en.wikipedia.org\/wiki\/Relative_humidity\">\u7ef4\u57fa\u767e\u79d1<\/a>\u4e2d\u6709\u5f88\u8be6\u5c3d\u7684\u89e3\u91ca\u3002\u8bf7\u8bb0\u4f4f\uff0c\u76f8\u5bf9\u6e7f\u5ea6\u548c\u5b9e\u9645\u6e7f\u5ea6\u662f\u4e24\u79cd\u4e0d\u540c\u7684\u4e1c\u897f\uff0c\u56e0\u4e3a\u76f8\u5bf9\u6e7f\u5ea6\u53d6\u51b3\u4e8e\u6e29\u5ea6\u3002<\/p>\n<p>\u6211\u4eec\u666e\u904d\u611f\u5174\u8da3\u7684\u662f\u6e7f\u5ea6\u5bf9\u4e8e\u4eba\u7c7b\u548c\u7535\u5b50\u8bbe\u5907\u7684\u9608\u503c\u3002\u5982\u679c\u60a8\u572825C\/77F\u5de6\u53f3\u7684\u73af\u5883\u6e29\u5ea6\u4e0b\u8fd0\u52a8\uff0c\u90a3\u4e48\u4f1a\u572835-40% RH\u5f00\u59cb\u611f\u5230\u4e0d\u9002\u3002\u60a8\u7684\u7535\u5b50\u8bbe\u5907\uff0c\u5c24\u5176\u662f\u8ba1\u7b97\u673a\u7cfb\u7edf\uff0c\u572845-55% RH\uff0c\u6e29\u5ea6\u572820C\/68F\u81f3 24C\/75F\u4e4b\u95f4\u7684\u73af\u5883\u4e2d\u66f4\u7075\u654f\uff0c\u4e14\u6027\u80fd\u6700\u4f73\u3002\u5982\u679cRH\u592a\u4f4e\uff0cESD\u5c31\u4f1a\u6210\u4e3a\u95ee\u9898\u3002\u5982\u679cRH\u592a\u9ad8\uff0c\u6709\u5c40\u90e8\u51b7\u51dd\u7684\u98ce\u9669\u3002<\/p>\n<p>\u5728\u51b7\u5374\u7406\u8bba\u4e2d\uff0c\u6709\u4e00\u4e2a\u7b80\u5355\u7684\u4e8b\u5b9e\u7ecf\u5e38\u88ab\u5ffd\u89c6\uff0c\u5373\u6c34\u5206\u5b50\u662f\u5b8c\u7f8e\u7684\u5c0f\u70ed\u91cf\u6876\uff0c\u98ce\u51b7\u7cfb\u7edf\u572845-55% RH\u73af\u5883\u4e2d\u7684\u51b7\u5374\u6548\u679c\u4f1a\u6bd4\u572820% RH\u73af\u5883\u4e2d\u597d\u5f97\u591a\u3002\u6e7f\u5ea6\u5bf9\u6b64\u6709\u6240\u5e2e\u52a9\u3002\u5f88\u591a\u4eba\u6ca1\u6709\u53d1\u73b0\u8fd9\u4e00\u70b9\uff0c\u800c\u60a8\u73b0\u5728\u5df2\u7ecf\u83b7\u6089\u8fd9\u4e2a\u5c0f\u6280\u5de7\u4e86\u3002<\/p>\n<p>\u4e86\u89e3\u4e86\u8fd9\u4e9b\u5185\u5bb9\uff0c\u73b0\u5728\u6211\u4eec\u7ee7\u7eed\u8ba8\u8bba\u786c\u4ef6\u90e8\u5206\u3002<\/p>\n<h2>1.1 \u8ba4\u8bc6DHT22\uff1a\u4e00\u79cd\u6e29\u6e7f\u5ea6\u4f20\u611f\u5668<\/h2>\n<p>\u8be5\u4f7f\u7528\u4ec0\u4e48\u786c\u4ef6\uff1f\u5f53\u7136\uff0cSHT85\u6e7f\u5ea6\u4f20\u611f\u5668\u6027\u80fd\u5f88\u597d\uff0c\u9632\u6c34\u7b49\u7ea7\u4e3aIP67\uff0c\u8bef\u5dee\u8303\u56f4\u4ec5\u4e3a\u00b11.5% RH\uff0c\u4f46\u6211\u60f3\u6211\u4eec\u53ef\u4ee5\u9009\u62e9\u6e7f\u5ea6\u4f20\u611f\u5668\u8bef\u5dee\u8303\u56f4\u4e3a\u00b12-5% RH\u7684\u8bbe\u5907\u3002<\/p>\n<p>DHT22\u6709\u4e00\u4e2a\u677f\u8f7d\u6a21\u6570\u8f6c\u6362\u5668\uff0c\u56e0\u6b64\u6e7f\u5ea6\u4f20\u611f\u5668\u548c\u70ed\u654f\u7535\u963b\u7684\u4fe1\u53f7\u6e05\u6670\u660e\u4e86\u3002\u5982\u679c\u6ca1\u6709\u8be5\u529f\u80fd\uff0c\u6821\u9a8c\u548c\u7684\u8bfb\u53d6\u4f1a\u8ba9\u60a8\u5341\u5206\u75db\u82e6\u3002\u6e29\u5ea6\u611f\u5e94\u53ea\u6709\u00b10.5C\u7684\u8bef\u5dee\uff0c\u5bf9\u521b\u5ba2\u6765\u8bf4\u6027\u80fd\u5df2\u7ecf\u7b97\u975e\u5e38\u597d\u4e86\u3002<\/p>\n<p>\u4e94\u5e74\u524d\u6211\u4e70\u4e866\u4e2a\uff0c\u6bcf\u4e2a\u90fd\u6027\u80fd\u51fa\u8272\uff0c\u5373\u4f7f\u662f\u5728\u6237\u5916\uff08\u867d\u7136\u5c4f\u853d\u4e86\u6765\u81ea\u9633\u5149\u3001\u96e8\u6c34\u7b49\u7684\u7d2b\u5916\u7ebf\uff09\u3002DHT22\u662f\u5355\u603b\u7ebf\uff0c\u65e2\u4e0d\u662fI2C\u4e5f\u4e0d\u662fSPI\uff0c\u867d\u7136\u7535\u538b\u8303\u56f4\u4e3a3.3\u81f36\u4f0f\uff0c\u4f46\u662f\u7edd\u4e0d\u80fd\u7528\u5927\u4e8e3.3V\u7684\u7535\u538b\u4f9b\u7535\uff0c\u56e0\u4e3a\u8fd9\u4f1a\u5bfc\u81f4\u5176\u53d1\u70ed\uff0c\u5e76\u611f\u5e94\u51fa\u9519\u8bef\u7684\u6e29\u5ea6\u6570\u636e\u3002\u8fc7\u9ad8\u7684\u5c40\u90e8\u6e29\u5ea6\u4f1a\u84b8\u53d1\u6c34\u5206\uff0c\u56e0\u6b64\u5c40\u90e8\u6e7f\u5ea6\u4e5f\u4f1a\u4e0b\u964d\u3002\u8fd9\u4e00\u70b9\u662f\u5b8c\u5168\u53ef\u4ee5\u907f\u514d\u7684\uff1a\u76f4\u63a5\u4eceRaspberry Pi\u76843V3\u5f15\u811a\u83b7\u53d63.3V\u7535\u538b\uff0c\u8be5\u5f15\u811a\u7684\u6570\u5b57\u4e3a1\u3002<\/p>\n<p>\u5728\u6ca1\u6709\u8fd0\u884c\u65f6\u7684\u7535\u6d41\u6d88\u8017\u975e\u5e38\u4f4e\uff08\u7ea640uA\uff09\uff0c\u56e0\u6b64\u4e0d\u9700\u8981\u4efb\u4f55\u8282\u80fd\u529f\u80fd\uff0c\u4f46\u5982\u679c\u60a8\u60f3\u8981\u8bbe\u7f6e\u8be5\u529f\u80fd\uff0c\u6bd4\u5982\u5728MCU\u7761\u7720\u72b6\u6001\u671f\u95f4\u5c06DHT22\u5173\u95ed\uff0c\u90a3\u4e48\u5728\u518d\u6b21\u60f3\u8981\u8bfb\u53d6\u6570\u636e\u65f6\uff0c\u9700\u8981\u5927\u69821\u79d2\u949f\u7684\u5524\u9192\u65f6\u95f4\u3002\u5f53\u5f00\u59cb\u5bf9\u5468\u56f4\u73af\u5883\u8fdb\u884c\u6d4b\u91cf\u65f6\uff0cDHT22\u6d88\u80171.5-2.5mA\u7684\u7535\u6d41\uff0c\u56e0\u6b64\u7535\u6d41\u91cf\u4e0d\u4f1a\u8fbe\u5230\u4f7f\u75283V3\u5f15\u811a\u65f6\u5efa\u8bae\u7684\u6700\u5927\u7535\u6d41\u91cf50mA\u3002<\/p>\n<p>\u6700\u5c0f\u8f6e\u8be2\u95f4\u9694\u88ab\u5b9a\u4e49\u4e3a\u4e24\u79d2\uff0c\u4e0d\u8fc7\u5b9e\u9645\u5e76\u4e0d\u662f\u5982\u6b64\u3002\u6211\u53ef\u4ee5\u4ee5\u5927\u7ea6350\u6beb\u79d2\u7684\u95f4\u9694\u8fdb\u884c\u8f6e\u8be2\uff0c\u5927\u7ea610\u79d2\u540e\u4f1a\u51fa\u73b0\u5347\u6e29\u95ee\u9898\u2014\u4f46\u662f\u5982\u679c\u60a8\u51fa\u4e8e\u67d0\u79cd\u539f\u56e0\u9700\u8981\u5feb\u901f\u8bfb\u53d610\u6b21\u6570\u636e\uff08\u53d6\u4e2d\u4f4d\u6570\uff1f\uff09\uff0c\u90a3\u4e48\u57283.5\u79d2\u5185\u5c31\u53ef\u4ee5\u5b8c\u6210\uff0c\u975e\u5e38\u597d\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8897\" src=\"https:\/\/techclass.wfjydl.com\/wp-content\/uploads\/2026\/04\/dht22_pinout.jpg\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" srcset=\"https:\/\/techclass.wfjydl.com\/wp-content\/uploads\/2026\/04\/dht22_pinout.jpg 500w, https:\/\/techclass.wfjydl.com\/wp-content\/uploads\/2026\/04\/dht22_pinout.jpg 240w, https:\/\/techclass.wfjydl.com\/wp-content\/uploads\/2026\/04\/dht22_pinout.jpg 117w, https:\/\/techclass.wfjydl.com\/wp-content\/uploads\/2026\/04\/dht22_pinout.jpg 40w, https:\/\/techclass.wfjydl.com\/wp-content\/uploads\/2026\/04\/dht22_pinout.jpg 60w\" alt=\" DHT22 Pinout\" width=\"500\" height=\"625\" \/><\/p>\n<p>\u5f15\u811a\u4ece\u5de6\u5230\u53f3\u5206\u522b\u662f1\uff1aVCC\uff083V3\uff09\uff0c2\uff1aSIGNAL\uff0c3\uff1aNC\uff08\u672a\u8fde\u63a5\uff09\uff0c4\uff1aGND\u3002\u60a8\u8fd8\u9700\u8981\u4e00\u4e2a\u4ece\u5f15\u811a2\u5230\u5f15\u811a1\u768410k\u03a9\u4e0a\u62c9\u7535\u963b\u3002<\/p>\n<p>\u5f53\u4e14\u4ec5\u5f53\u60a8\u9047\u5230\u6296\u52a8\uff08\u901a\u5e38\u53d1\u751f\u5728\u957f\u7ebf\u7f06\u7535\u8def\u4e0a\uff09\u72b6\u51b5\u65f6\uff0c\u9700\u8981\u5728VCC\u548cGND\u4e4b\u95f4\u6dfb\u52a0\u4e00\u4e2a100nF\u7535\u5bb9\u3002<\/p>\n<h2>1.2 \u60a8\u5c06\u8981\u5b66\u5230\u7684\u5185\u5bb9<\/h2>\n<p>\u5b66\u4e60\u5b8c\u672c\u6587\u5185\u5bb9\u540e\uff0c\u60a8\u5c06\u80fd\u591f\u8f7b\u677e\u611f\u77e5\u76f8\u5bf9\u6e29\u5ea6\u548c\u6e7f\u5ea6\u3002\u6211\u6dfb\u52a0\u4e86\u4e00\u4e9b\u8f85\u52a9\u51fd\u6570\uff0c\u4f7f\u60a8\u65e0\u9700\u8c37\u6b4c\u5de5\u5177\u5c31\u53ef\u4ee5\u5c06\u5b8c\u6210\u201c\u6444\u6c0f\u5ea6\u201d\u548c\u201c\u534e\u6c0f\u5ea6\u201d\u4e4b\u95f4\u7684\u8f6c\u6362\u3002\u6bcf\u6b21\u90fd\u624b\u52a8\u8f6c\u6362\u4f1a\u6bd4\u8f83\u9ebb\u70e6\u3002\u4e3a\u4e86\u60a8\u7684\u65b9\u4fbf\uff0c\u6211\u4eec\u6dfb\u52a0\u4e24\u4e2a\u5c0f\u7684\u8f85\u52a9\u51fd\u6570\uff1a<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism undefined-numbers lang-c\" data-lang=\"C\"><code>\r\n# Celsius to fahrenheit: F = ( C * 1.8 ) + 32\r\ndef celsius2fahrenheit( _celsius ):\r\n  _fahrenheit = ( \"%.2f\" % (( _celsius * 1.8 ) + 32) )\r\n  return float( _fahrenheit )\r\n\r\n# Fahrenheit to celsius: C = ( F - 32 ) * 5\/9\r\ndef fahrenheit2celsius( _fahrenheit ):\r\n  _celsius = ( \"%.2f\" % (( _fahrenheit - 32 ) * 5\/9 ))\r\n  return float( _celsius )\r\n<\/code><\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>\u5728\u7b2c\u4e8c\u90e8\u5206\u7684dht22_simple.py\u548cdht22_actionable.py\u4e2d\u4e5f\u6709\u8fd9\u4e24\u4e2a\u51fd\u6570\u3002<\/p>\n<p>\u5728\u6211\u4eec\u7814\u7a76DHT22\u7684\u8fc7\u7a0b\u4e2d\uff0c\u5c06\u4f1a\u6df1\u5165\u63a2\u7d22\u201cDHT22\u673a\u623f\u73af\u5883\u63a7\u5236\u5668\u201d\u6216DCREC\u7684\u6784\u5efa\u548c\u64cd\u4f5c\uff0c\u8be5\u5668\u4ef6\u80fd\u591f\u5bf9\u4f20\u611f\u5668\u6570\u636e\u8fdb\u884c\u54cd\u5e94\u3002\u60a8\u53ef\u4ee5\u5c06dht22_actionable.py\u4f5c\u4e3a\u6e7f\u5ea6\u9879\u76ee\u7684\u6a21\u677f\uff08\u57fa\u4e8eDHT22\uff09\uff0c\u5982\u679c\u60a8\u5177\u5907\u4e86\u786c\u4ef6\u548c\u5b89\u88c5\u6280\u80fd\uff0c\u5c31\u53ef\u4ee5\u5b8c\u6210\u51e0\u4e4e\u6240\u6709\u7684\u5de5\u4f5c\u4e86\u3002\u9605\u8bfb\u5b8cpython\u811a\u672c\u540e\uff0c\u60a8\u4e5f\u5c06\u4f1a\u638c\u63e1GPIO\u8f93\u51fa\u63a7\u5236\u3002<\/p>\n<h2>2. \u5c06\u60a8\u7684Raspberry Pi\u4e0eDHT22\u4f20\u611f\u5668\u8fde\u63a5<\/h2>\n<p>DHT22\u975e\u5e38\u6613\u4e8e\u4f7f\u7528\u3002\u6b64\u5904\u65e0\u9700\u5904\u7406I2C\u603b\u7ebf\uff0c\u5b83\u662fI\/O\u7684\u4e00\u4e2a\u6570\u636e\u5f15\u811a\uff0c\u662f\u4e00\u79cd\u534a\u53cc\u5de5\u603b\u7ebf\uff0c\u4e5f\u8bb8\u60a8\u5bf9\u8fd9\u4e2a\u672f\u8bed\u66f4\u719f\u6089\u3002<\/p>\n<p>\u60a8\u53ef\u4ee5\u4ece<a href=\"https:\/\/www.sparkfun.com\/datasheets\/Sensors\/Temperature\/DHT22.pdf\" target=\"_blank\" rel=\"noopener\">\u6280\u672f\u89c4\u683c\u4e66<\/a> \u83b7\u53d6\u6709\u5173\u8109\u51b2\u957f\u5ea6\u3001\u95f4\u9694\u7b49\u4fe1\u606f\uff0c\u4f46\u5982\u679c\u60a8\u60f3\u9a6c\u4e0a\u5f00\u59cb\uff0c\u8bf7\u9996\u5148\u6309\u7167\u6b64<a href=\"https:\/\/www.deviceplus.com\/raspberry-pi\/diy-tip-how-to-set-up-your-raspberry-pi\/\" target=\"_blank\" rel=\"noopener\">Raspberry Pi\u8bbe\u7f6e\u6307\u5357<\/a>\u786e\u4fdd\u60a8\u7684Pi\u5df2\u51c6\u5907\u5c31\u7eea\u3002<\/p>\n<p>\u51c6\u5907\u597d\u540e\uff0c\u5c06\u4e0b\u9762\u7684\u811a\u672c\u590d\u5236\u7c98\u8d34\u5230\u60a8\u7684Raspberry Pi GUI\u7f16\u8f91\u5668\u4e2d\uff08\u542c\u8bf4Pluma\u5f88\u597d\u7528\uff09\uff0c\u5e76\u5c06\u6587\u4ef6\u4fdd\u5b58\u4e3a\u201crpi_prepare.sh\u201d\u3002<\/p>\n[ begin rpi_prepare.sh ]\n<div class=\"hcb_wrap\">\n<pre class=\"prism undefined-numbers lang-c\" data-lang=\"C\"><code>\r\n#! \/usr\/bin\/env bash\r\nset -eu -o pipefail\r\nDEBIAN_FRONTEND=\"noninteractive\"\r\nDEBIAN_PRIORITY=\"critical\"\r\nDEBCONF_NOWARNINGS=\"yes\"\r\nexport DEBIAN_FRONTEND DEBIAN_PRIORITY DEBCONF_NOWARNINGS\r\n\r\n_pkg_list=\"pigpio wiringpi python-rpi.gpio \r\npython3-rpi.gpio rpi.gpio-common git python-gpiozero \r\npython-gpiozero-doc python3-gpiozero python-setuptools-git python3-setuptools-git python3-dev python3-pip\" \r\n\r\n# Upgrade system and installed packages - uncomment where \r\n# relevant\r\nsudo apt update || ech o failed to update index list \r\n#sudo dpkg --configure -a || ech o failed to fix interrupted \\\r\nupgrades\r\n#sudo apt --fix-broken --fix-missing install || ech o failed \\\r\nto fix conflicts \r\n#sudo apt -y --allow-downgrades --fix-broken --fix-missing \\\r\n#dist-upgrade\r\n\r\n# Install $_pkg_list\r\nsudo apt update\r\nsudo apt-get -y install $_pkg_list\r\n\r\n# Make 'pip3' bigly fresh? Yes.\r\nsudo python3 -m pip --upgrade pip setuptools wheel\r\n# Get Adafruit_DHT Python library\r\nsudo pip3 install Adafruit_DHT\r\n\r\nread -p \"[?] Reboot? y\/N: \" _foo\r\nif [ X\"$_foo\" = X\"y\" -o X\"$_foo\" = X\"Y\" ]\r\nthen \r\n  ech o \"[!] Rebooting in 5 seconds, CTRL+C to abort ...\"\r\n  for i in $( seq 1 5 ) ; do ech o -n . ; sleep 1 ; done ; ech o\r\n  sudo reboot\r\nfi\r\n<\/code><\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n[ end rpi_prepare.sh ]\n<p>\u8be5\u811a\u672c\u7528\u4e8e\u5b89\u88c5\u548c\u66f4\u65b0Python3\u7684\u201cpip3\u201d\u7a0b\u5e8f\uff0c\u4ee5\u53ca\u5b89\u88c5\u4e00\u4e9b\u6709\u7528\u7684GPIO\u8f6f\u4ef6\u3002\u5c06\u5176\u590d\u5236\u5230Pi\u540e\uff0c\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fd0\u884c\uff1a<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism undefined-numbers lang-c\" data-lang=\"C\"><code>\r\nbash rpi_prepare.sh\r\n<\/code><\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<h2>2.1.1 BOM<\/h2>\n<p>&nbsp;<\/p>\n<table>\n<tbody>\n<tr>\n<td><span style=\"font-weight: 400;\">Raspberry Pi 4<\/span><\/td>\n<td><span style=\"font-weight: 400;\">https:\/\/www.newark.com\/raspberry-pi\/rpi4-modbp-4gb\/raspberry-pi-4-model-b-4gb-rohs\/dp\/02AH3164<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">DHT22 \u4f20\u611f\u5668<\/span><\/td>\n<td><span style=\"font-weight: 400;\">https:\/\/www.newark.com\/mcm\/83-17985\/dht22-temp-humidity-sensor\/dp\/32AC9951<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">10k\u03a9 \u7535\u963b<\/span><\/td>\n<td><span style=\"font-weight: 400;\">https:\/\/www.newark.com\/multicomp-pro\/mccfr0w4j0103a50\/carbon-film-resistor-10kohm-250mw\/dp\/58K5002<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">\u675c\u90a6\u7535\u7ebf<\/span><\/td>\n<td><span style=\"font-weight: 400;\">https:\/\/www.newark.com\/adafruit\/824\/wire-gauge-28awg\/dp\/88W2794<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">\u9762\u5305\u677f<\/span><\/td>\n<td><span style=\"font-weight: 400;\">https:\/\/www.newark.com\/mcm\/21-19082\/breadboardjumper-kit-with-binding\/dp\/79X3995<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>2.1.2 \u542f\u52a8\uff01<\/h2>\n<p>\u5c06\u5b83\u4eec\u5168\u90e8\u8fde\u63a5\u8d77\u6765\uff0c\u8fd9\u4e00\u6b65\u5f88\u7b80\u5355\uff0c\u8bf7\u6309\u7167\u4ee5\u4e0b\u56fe\u793a\u8fdb\u884c\u8fde\u63a5\u3002<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8898\" src=\"https:\/\/techclass.wfjydl.com\/wp-content\/uploads\/2026\/04\/dht22_simple_diagram.png\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" srcset=\"https:\/\/techclass.wfjydl.com\/wp-content\/uploads\/2026\/04\/dht22_simple_diagram.png 700w, https:\/\/techclass.wfjydl.com\/wp-content\/uploads\/2026\/04\/dht22_simple_diagram.png 300w, https:\/\/techclass.wfjydl.com\/wp-content\/uploads\/2026\/04\/dht22_simple_diagram.png 260w, https:\/\/techclass.wfjydl.com\/wp-content\/uploads\/2026\/04\/dht22_simple_diagram.png 50w, https:\/\/techclass.wfjydl.com\/wp-content\/uploads\/2026\/04\/dht22_simple_diagram.png 150w\" alt=\"Raspberry Pi with DHT22 diagram\" width=\"700\" height=\"314\" \/><\/p>\n<p>\u9996\u5148\uff0c\u5c06DHT22\u653e\u5728\u9762\u5305\u677f\u4e0a\uff0c\u5e76\u5728\u5f15\u811a1\u548c\u5f15\u811a2\u4e4b\u95f4\u6dfb\u52a010k\u03a9\u4e0a\u62c9\u7535\u963b\u3002\u7136\u540e\u5c06BOARD1\/3V3\u8fde\u63a5\u5230\u9762\u5305\u677f\u4e0a\u7684\u7ea2\u8272\u5bfc\u8f68\uff0cBOARD6\/GND\u8fde\u63a5\u5230\u9ed1\u8272\u5bfc\u8f68\uff0c\u5c06BCM24\/BOARD18\u8fde\u63a5\u5230DHT22\u4e0a\u7684\u5f15\u811a2\u3002\u73b0\u5728\uff0c\u7528\u4e00\u6839\u5bfc\u7ebf\u4ece\u7ea2\u8272\u5bfc\u8f68\u8fde\u63a5\u5230DHT22\u7684\u5f15\u811a1\uff0c\u7528\u53e6\u4e00\u6839\u5bfc\u7ebf\u4ece\u9ed1\u8272\u5bfc\u8f68\u8fde\u63a5\u5230\u5f15\u811a4\u3002\u7136\u540e\u5c06\u4ee5\u4e0b\u811a\u672c\u590d\u5236\u7c98\u8d34\u5230\u60a8\u7684Pi\u7ec8\u7aef\uff1a<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism undefined-numbers lang-c\" data-lang=\"C\"><code>\r\n#! \/usr\/bin\/env python3\r\n# Demonstrate reading DHT22 sensor using the\r\n# Adafruit_DHT library\r\n# DHT22 pinout (left to right): \r\n# 1: VCC(3.3-6V) \r\n# 2: SIGNAL \r\n# 3: UNUSED \r\n# 4: GND\r\n# Notes:\r\n# - 10kOhm pull-up resistor from SIG to VCC.\r\n# - Use 3V3 for VCC, or the DHT22 will heat up.\r\n# - Indentation: 2 whitespaces per level, no tabs.\r\nimport Adafruit_DHT\r\nfrom time import sleep\r\np=print # Alias \r\n# Note: Use &amp;gt;=2000ms polling intervals\r\n_poll_interval = 2 # Seconds\r\n_dht_pin = 24 # BCM24\/BOARD18\r\n_dht = Adafruit_DHT.DHT22\r\n\r\ndef celsius2fahrenheit( _celsius ):\r\n  _fahrenheit = ( \"%.1f\" % (( _celsius * 1.8 ) + 32) )\r\n  return float( _fahrenheit )\r\n\r\ndef fahrenheit2celsius( _fahrenheit ):\r\n  _celsius = ( \"%.1f\" % (( _fahrenheit - 32 ) * 5\/9 ))\r\n  return float( _celsius )\r\n\r\nif __name__ == '__main__':\r\n  while True: \r\n    ( _humidity, _celsius ) = Adafruit_DHT.read_retry( _dht, _dht_pin )\r\n    p( \"Humidity =&amp;gt; %.1f%% RH\" % _humidity ) \r\n    p( \"Temperature =&amp;gt; %.2fF\" % celsius2fahrenheit( _celsius ), end='\/' )\r\n    p( \"%.2fC\" % _celsius )\r\n    sleep( _poll_interval )\r\n<\/code><\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n[ end dht22_simple.py ]\n<p>\u8f93\u51fa\u5e94\u5982\u4e0b\u6240\u793a\u3002\u5982\u679c\u6ca1\u6709\u8f93\u51fa\u4efb\u4f55\u5185\u5bb9\uff0c\u60a8\u53ef\u80fd\u9047\u5230\u4e86\u8fd0\u884c\u8d85\u65f6\u7684\u95ee\u9898\u3002\u8fd9\u79cd\u60c5\u51b5\u6709\u65f6\u5019\u4f1a\u53d1\u751f\uff0c\u4f46\u662f\u662f\u4e0d\u5e94\u8be5\u4f1a\u53d1\u751f\u7684\u3002\u6309\u4e0bCTRL+C\u4e2d\u65ad\u7a0b\u5e8f\uff0c\u5e76\u68c0\u67e5\u6240\u6709\u8fde\u63a5\u662f\u5426\u6b63\u786e\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8899\" src=\"https:\/\/techclass.wfjydl.com\/wp-content\/uploads\/2026\/04\/dht22_simple.png\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" srcset=\"https:\/\/techclass.wfjydl.com\/wp-content\/uploads\/2026\/04\/dht22_simple.png 700w, https:\/\/techclass.wfjydl.com\/wp-content\/uploads\/2026\/04\/dht22_simple.png 300w, https:\/\/techclass.wfjydl.com\/wp-content\/uploads\/2026\/04\/dht22_simple.png 230w, https:\/\/techclass.wfjydl.com\/wp-content\/uploads\/2026\/04\/dht22_simple.png 50w, https:\/\/techclass.wfjydl.com\/wp-content\/uploads\/2026\/04\/dht22_simple.png 118w\" alt=\"DHT22 runtime terminal output\" width=\"700\" height=\"445\" \/><\/p>\n<p>\u5728\u622a\u56fe\u7684\u6267\u884c\u8fc7\u7a0b\u4e2d\uff0c\u6211\u5c06\u70ed\u98ce\u67aa\u7684\u55b7\u5634\u8f7b\u8f7b\u9760\u8fd1DHT22\uff0c\u540c\u65f6\u7528\u7ea2\u5916\u6e29\u5ea6\u8ba1\u6d4b\u91cfDHT22\u3002\u8fd9\u4e24\u4e2a\u8bfb\u6570\u53ef\u4ee5\u5f7c\u6b64\u9a8c\u8bc1\uff0c\u4ee5\u4fdd\u8bc1\u5b9e\u9a8c\u7684\u4e25\u8c28\u6027\u3002<\/p>\n<h2>2.2 DHT22 \u673a\u623f\u63a7\u5236\u5668<\/h2>\n<p>\u73b0\u5728\uff0c\u6211\u4eec\u5c06\u505a\u4e00\u4e9b\u6bd4\u4ec5\u4ec5\u901a\u8fc7\u6e7f\u5ea6\u4f20\u611f\u5668\u548c\u70ed\u654f\u7535\u963b\u611f\u77e5\u73af\u5883\u66f4\u6709\u8da3\u7684\u4e8b\u60c5\u2014\u5236\u4f5cDHT22\u673a\u623f\u73af\u5883\u63a7\u5236\u5668\u3002\u5728\u9605\u8bfb\u4e0b\u9762\u7684dht22_actionable.py\u4e4b\u524d\uff0c\u9700\u8981\u5bf9\u5176\u64cd\u4f5c\u8fdb\u884c\u7b80\u77ed\u7684\u89e3\u91ca\u3002\u6211\u4eec\u8bbe\u7f6e\u4e24\u4e2aRaspberry Pi GPIO\u5f15\u811a\u4f5c\u4e3a\u8f93\u51fa\uff08BCM25\/BOARD22\u548cBCM23\/BOARD16\uff09\uff0c\u5efa\u7acb\u4e0eDHT22\u4f20\u611f\u5668\u7684\u8fde\u63a5\uff0c\u7136\u540e\u6211\u4eec\u5faa\u73af\u51e0\u4e2a\u6761\u4ef6\u8bed\u53e5\u6765\u68c0\u67e5\u6240\u5904\u73af\u5883\u662f\u5426\u6ee1\u8db3\u516c\u8ba4\u7684\u201c\u9002\u4e8e\u8ba1\u7b97\u7684\u6700\u4f73\u6e7f\u5ea6\u548c\u6e29\u5ea6\u6761\u4ef6\u201d\u3002<\/p>\n<p>\u6bcf\u5f53\u6e7f\u5ea6\u6216\u6e29\u5ea6\u8d85\u51fa\u8303\u56f4\u65f6\uff0c_humidity_led\u6216_temperature_led\u90fd\u4f1a\u4eae\u8d77\u3002\u4f46\u5b83\u4e5f\u53ef\u4ee5\u7528\u4e8e\u9a71\u52a8\u7535\u8def\uff0c\u4f8b\u5982IRLZ24N MOSFET\u6216\u7ee7\u7535\u5668\u9a71\u52a8\u7535\u8def\u3002\u60a8\u53ef\u4ee5\u5229\u7528\u81ea\u5df1\u73b0\u6709\u7684\u8bbe\u5907\u6765\u5b9e\u73b0\u6240\u6709\u8fd9\u4e9b\u529f\u80fd\u3002<\/p>\n<p>\u51c6\u5907\u597d\u6240\u6709\u7684\u5143\u5668\u4ef6\uff0c\u5982\u679c\u6709\u7f3a\u5c11\u7684\u4e1c\u897f\uff0c\u8bf7\u8bbf\u95ee\u4e0b\u6587BOM\u4e2d\u7684\u8fde\u63a5\u3002<\/p>\n<h2>2.2.1 BOM<\/h2>\n<p>&nbsp;<\/p>\n<table>\n<tbody>\n<tr>\n<td><span style=\"font-weight: 400;\">Raspberry Pi 4<\/span><\/td>\n<td><a href=\"https:\/\/www.newark.com\/raspberry-pi\/rpi4-modbp-4gb\/raspberry-pi-4-model-b-4gb-rohs\/dp\/02AH3164\"><span style=\"font-weight: 400;\">https:\/\/www.newark.com\/raspberry-pi\/rpi4-modbp-4gb\/raspberry-pi-4-model-b-4gb-rohs\/dp\/02AH3164<\/span><\/a><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">DHT22 \u4f20\u611f\u5668<\/span><\/td>\n<td><a href=\"https:\/\/www.newark.com\/mcm\/83-17985\/dht22-temp-humidity-sensor\/dp\/32AC9951\"><span style=\"font-weight: 400;\">https:\/\/www.newark.com\/mcm\/83-17985\/dht22-temp-humidity-sensor\/dp\/32AC9951<\/span><\/a><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">2x 极悦娱乐 SLR343BC4TT32 3mm LED<\/span><\/td>\n<td><span style=\"font-weight: 400;\">https:\/\/www.infinite-electronic.hk\/product\/LAPIS-Semiconductor_SLR343BC4TT32.aspx<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">\u675c\u90a6\u7535\u7ebf<\/span><\/td>\n<td><a href=\"https:\/\/www.newark.com\/adafruit\/824\/wire-gauge-28awg\/dp\/88W2794\"><span style=\"font-weight: 400;\">https:\/\/www.newark.com\/adafruit\/824\/wire-gauge-28awg\/dp\/88W2794<\/span><\/a><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">\u9762\u5305\u677f<\/span><\/td>\n<td><a href=\"https:\/\/www.newark.com\/mcm\/21-19082\/breadboardjumper-kit-with-binding\/dp\/79X3995\"><span style=\"font-weight: 400;\">https:\/\/www.newark.com\/mcm\/21-19082\/breadboardjumper-kit-with-binding\/dp\/79X3995<\/span><\/a><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">10k\u03a9 \u7535\u963b<\/span><\/td>\n<td><a href=\"https:\/\/www.newark.com\/multicomp-pro\/mccfr0w4j0103a50\/carbon-film-resistor-10kohm-250mw\/dp\/58K5002\"><span style=\"font-weight: 400;\">https:\/\/www.newark.com\/multicomp-pro\/mccfr0w4j0103a50\/carbon-film-resistor-10kohm-250mw\/dp\/58K5002<\/span><\/a><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">2x 330\u03a9 \u7535\u963b<\/span><\/td>\n<td><a href=\"https:\/\/www.newark.com\/multicomp-pro\/mccfr0w4j0331a50\/carbon-film-resistor-330-ohm-250mw\/dp\/58K5042\"><span style=\"font-weight: 400;\">https:\/\/www.newark.com\/multicomp-pro\/mccfr0w4j0331a50\/carbon-film-resistor-330-ohm-250mw\/dp\/58K5042<\/span><\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>2.2.2 \u542f\u52a8\uff01<\/h2>\n<p>\u5982\u4e0b\u56fe\u6240\u793a\u5b8c\u6210\u6240\u6709\u8fde\u63a5\u3002\u4fdd\u7559\u4e0a\u6b21Raspberry Pi GPIO BCM25\/BOARD22\u548cBCM23\/BOARD16\u7684\u5f15\u811a\u8fde\u63a5\uff0c3V3\/BOARD1\u548cGND\/BOARD6\u7684\u8fde\u63a5\u548c\u4e0a\u6b21\u76f8\u540c\u3002\u5728\u60a8\u7684Pi\u51c6\u5907\u597d\u8fd0\u884c\u7a0b\u5e8f\u4e4b\u524d\u4e0d\u8981\u7ed9\u7535\u8def\u901a\u7535\uff0c\u5728\u6b64\u4e4b\u524d\u4ed4\u7ec6\u68c0\u67e5\u6240\u6709\u8fde\u63a5\u3002\u786e\u8ba4\u6ca1\u6709\u4efb\u4f55\u95ee\u9898\u540e\uff0c\u901a\u7535\u5e76\u6267\u884c\u4e0b\u6587\u4e2d\u7684dht22_actionable.py\u3002<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism undefined-numbers lang-c\" data-lang=\"C\"><code>\r\npython3 dht22_actionable.py\r\n<\/code><\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8900\" src=\"https:\/\/techclass.wfjydl.com\/wp-content\/uploads\/2026\/04\/dht22_actionable_diagram.png\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" srcset=\"https:\/\/techclass.wfjydl.com\/wp-content\/uploads\/2026\/04\/dht22_actionable_diagram.png 700w, https:\/\/techclass.wfjydl.com\/wp-content\/uploads\/2026\/04\/dht22_actionable_diagram.png 300w, https:\/\/techclass.wfjydl.com\/wp-content\/uploads\/2026\/04\/dht22_actionable_diagram.png 260w, https:\/\/techclass.wfjydl.com\/wp-content\/uploads\/2026\/04\/dht22_actionable_diagram.png 50w, https:\/\/techclass.wfjydl.com\/wp-content\/uploads\/2026\/04\/dht22_actionable_diagram.png 150w\" alt=\"DHT22 Actionable Diagram\" width=\"700\" height=\"350\" \/><\/p>\n[ begin dht22_actionable.py ]\n<div class=\"hcb_wrap\">\n<pre class=\"prism undefined-numbers lang-c\" data-lang=\"C\"><code>\r\n#! \/usr\/bin\/env python3\r\n# Install prerequisite packages &amp;amp; Adafruit_DHT library\r\n\"\"\"\r\n#! \/bin\/sh\r\n_pkg_list='python3-setuptools python3-pip python3-dev python3-rpi.gpio'\r\nsudo apt-get -y install $_pkg_list\r\nsudo python3 -m pip --upgrade pip setuptools wheel\r\nsudo pip3 install Adafruit_DHT\r\n\"\"\"\r\n#\r\n# Ensure operating conditions in a \r\n# datacenter (your basement) are \r\n# within SAFE OPERATING HUMIDITY- AND \r\n# TEMPERATURE THRESHOLDS FOR OPTIMAL \r\n# COMPUTING CONDITIONS.\r\n#\r\n# Will fire BCM25\/BOARD22 if humidity is \r\n# out of bounds, and BCM23\/BOARD16 if \r\n# temperature is out of bounds. \r\n# \r\n# Uses RPi.GPIO Adafruit_DHT library, \r\n# tested on Raspbian, Dec. 2019\r\n#\r\n# DHT22 pinout (left to right):\r\n# 1: VCC(3.3-6V)\r\n# 2: SIGNAL\r\n# 3: UNUSED\r\n# 4: GND\r\n# Notes:\r\n# - 10kOhm pull-up resistor from SIG to VCC.\r\n# - 330ohm resistors in series with RPi \r\n#   GPIO pins and 极悦娱乐 SLR343BC4TT32\r\n#   3mm LEDs.\r\n# - Use 3V3 for VCC, or the DHT22 will \r\n#   heat up.\r\n# - Indentation: 2 whitespaces per level, no \r\n#   tabs.\r\nimport Adafruit_DHT\r\nfrom time import sleep\r\nimport RPi.GPIO as GPIO\r\nimport atexit\r\nGPIO.setwarnings( True )\r\n# Use BCM pin numbering instead of BOARD\r\nGPIO.setmode( GPIO.BCM ) \r\n_humidity_led = 25 # BCM25\/BOARD22\r\n_humidity_lower_threshold = 45.0\r\n_humidity_upper_threshold = 55.0\r\n_temperature_led = 23 # BCM23\/BOARD16\r\n_temperature_lower_threshold = 20.0 # 20C\/68F\r\n_temperature_upper_threshold = 24.0 # 24C\/75F\r\n_poll_interval = 2 # Use intervals &amp;gt;=2secs\r\n_dht_pin = 24 # BCM24\/BOARD18\r\n_dht = Adafruit_DHT.DHT22\r\n_debug = True\r\np=print # Alias\r\n\r\nif _debug:\r\n  p( \"[!] Setting up pin BCM_%i as OUTPUT for humidity LED ...\" % _humidity_led )\r\n  p( \"[!] Setting up pin BCM_%i as OUTPUT for temperature LED ...\" %  _temperature_led )\r\n\r\nGPIO.setup( _humidity_led, GPIO.OUT )\r\nGPIO.setup( _temperature_led, GPIO.OUT )\r\n\r\ndef exit_cleanly():\r\n  print( \"[!] Cleaning up and exiting ...\" )\r\n  GPIO.setwarnings( False )\r\n  GPIO.cleanup()\r\n  exit()\r\n\r\ndef celsius2fahrenheit( _celsius ):\r\n  _fahrenheit = ( \"%.1f\" % (( _celsius * 1.8 ) + 32) )\r\n  return float( _fahrenheit )\r\n\r\ndef fahrenheit2celsius( _fahrenheit ):\r\n  _celsius = ( \"%.1f\" % (( _fahrenheit - 32 ) * 5\/9 ))\r\n  return float( _celsius )\r\n\r\n# Call exit_cleanly on normal exit and CTRL+C\/KeyboardInterrupt\/foo\r\natexit.register( exit_cleanly )\r\n\r\nif __name__ == '__main__':\r\n  while True:\r\n\t( _humidity, _celsius ) = Adafruit_DHT.read_retry( _dht, \r\n        _dht_pin )\r\n\tif _debug:\r\n  \tp( \"[+] Humidity =&amp;gt; %.1f%% RH\" % _humidity )\r\n  \tp( \"[+] Temperature =&amp;gt; %.1fC\" % _celsius, end='\/' )\r\n  \tp( \"%.1fF\" % celsius2fahrenheit( _celsius ) )\r\n\t# Let's be neat\r\n\t_humidity = float( \"%.1f\" % _humidity )\r\n\t_celsius = float( \"%.1f\" % _celsius )\r\n\t# Humidity too high?\r\n\tif _humidity &amp;gt; _humidity_upper_threshold:\r\n  \tp( \r\n        \"[!] Humidity %.1f%% RH exceeds upper threshold value of %.1f%% RH\" % \r\n        ( _humidity, _humidity_upper_threshold ) )\r\n  \t# Take decisive action!\r\n  \tGPIO.output( _humidity_led, 1 )\r\n\t# Humidity too low?\r\n\telif _humidity &amp;lt; _humidity_lower_threshold:\r\n  \tp( \r\n        \"[!] Humidity %.1f%% RH is below lower threshold value of %.1f%% RH\" %  \r\n         ( _humidity, _humidity_lower_threshold ) )\r\n  \t# Take decisive action!\r\n  \tGPIO.output( _humidity_led, 1 )\r\n\t# Safe operating humidity?\r\n\telif _humidity &amp;lt;= _humidity_upper_threshold and _humidity &amp;gt;= _humidity_lower_threshold:\r\n  \tGPIO.output( _humidity_led, 0 )\r\n  \t# Safe!\r\n\r\n\t# Temperature too high?\r\n\tif _celsius &amp;gt; _temperature_upper_threshold:\r\n  \tp( \"[!] Temperature %.1fC\/%.1fF exceeds upper threshold value of %.1fC\/%.1fF\" %\r\n  \t( _celsius, celsius2fahrenheit( _celsius ), _temperature_upper_threshold,\r\n  \tcelsius2fahrenheit( _temperature_upper_threshold ) ) )\r\n  \t# Take decisive action!\r\n  \tGPIO.output( _temperature_led, 1 )\r\n\t# Temperature too low?\r\n\telif _celsius &amp;lt; _temperature_lower_threshold:\r\n  \tp( \"[!] Temperature %1.fC\/%.1fF is below lower threshold value of %.1fC\/%.1fF\" %\r\n  \t( _celsius, celsius2fahrenheit( _celsius ), _temperature_lower_threshold,\r\n  \tcelsius2fahrenheit( _temperature_lower_threshold ) ) )\r\n  \t# Take decisive action!\r\n  \tGPIO.output( _temperature_led, 1 )\r\n\t# Safe operating temperature?\r\n\telif _celsius &amp;lt;= _temperature_upper_threshold and _celsius &amp;gt;= _temperature_lower_threshold:\r\n  \tGPIO.output( _temperature_led, 0 )\r\n  \t# Safe!\r\n\r\n\tsl eep( _poll_interval )\r\n<\/code><\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n[ end dht22_actionable.py ]\n<p>\u60a8\u7684\u8f93\u51fa\u4e0d\u5e94\u548c\u4e0b\u56fe\u4e2d\u6211\u7684\u7ec8\u7aef\u8f93\u51fa\u5185\u5bb9\u76f8\u540c\u3002\u5c06450C\/842F\u7684\u70ed\u98ce\u67aa\u5439\u8fc7\u4e00\u4e2a\u88c5\u6ee1\u6c34\u7684\u5c0f\u91d1\u5c5e\u7897\uff0c\u4ee5\u9a8c\u8bc1DHT22\u673a\u623f\u73af\u5883\u63a7\u5236\u5668\uff08DCREC\uff09\u5df2\u7ecf\u529f\u80fd\u6b63\u5e38\u3002\u5728\u60a8\u7684\u5e94\u7528\u4e2d\uff0c\u8bf7\u786e\u4fddGPIO\u5f15\u811a\u8fde\u63a5\u5230\u4e86\u4e00\u4e9b\u5b8c\u597d\u7684\u5e94\u7528\u6a21\u5757\uff0c\u4f8b\u5982MOSFET\u9a71\u52a8\u7ee7\u7535\u5668\u6216\u53cc\u6269\u97f3\u5668\u3002\u73b0\u5728\u5f00\u59cb\u884c\u52a8\u5427\uff01<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8901\" src=\"https:\/\/techclass.wfjydl.com\/wp-content\/uploads\/2026\/04\/dht22_actionable_hysteria.png\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" srcset=\"https:\/\/techclass.wfjydl.com\/wp-content\/uploads\/2026\/04\/dht22_actionable_hysteria.png 700w, https:\/\/techclass.wfjydl.com\/wp-content\/uploads\/2026\/04\/dht22_actionable_hysteria.png 300w, https:\/\/techclass.wfjydl.com\/wp-content\/uploads\/2026\/04\/dht22_actionable_hysteria.png 260w, https:\/\/techclass.wfjydl.com\/wp-content\/uploads\/2026\/04\/dht22_actionable_hysteria.png 50w, https:\/\/techclass.wfjydl.com\/wp-content\/uploads\/2026\/04\/dht22_actionable_hysteria.png 141w\" alt=\"DHT22 Actionable: Hysteria\" width=\"700\" height=\"373\" \/><\/p>\n<p>\u60a8\u53ef\u4ee5\u5728\u4e0b\u56fe\u4e2d\u770b\u5230\u6211\u7684\u9762\u5305\u677f\u3002\u5728\u53f3\u4e0b\u89d2\uff0cDHT22\u6b63\u5728\u8fd0\u884c\uff0c\u5b83\u4f1a\u4ee5\u6570\u5b57\u7684\u5f62\u5f0f\u5411\u6211\u7684Raspberry Pi\u62a5\u544a\u3002\u5728\u9876\u90e8\u4e2d\u95f4\uff0c\u4e24\u4e2aLED\u4eae\u8d77\uff0c\u8b66\u544a\u6211\u7684\u684c\u9762\u8ba1\u7b97\u73af\u5883\u4e0d\u5728\u6700\u4f73\u64cd\u4f5c\u8303\u56f4\u5185\u3002<\/p>\n<p>\u6211\u5bf9\u6b64\u611f\u5230\u975e\u5e38\u610f\u5916\u548c\u9707\u60ca\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8902\" src=\"https:\/\/techclass.wfjydl.com\/wp-content\/uploads\/2026\/04\/dht22_actionable_hysteria_2.jpg\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" srcset=\"https:\/\/techclass.wfjydl.com\/wp-content\/uploads\/2026\/04\/dht22_actionable_hysteria_2.jpg 700w, https:\/\/techclass.wfjydl.com\/wp-content\/uploads\/2026\/04\/dht22_actionable_hysteria_2.jpg 300w, https:\/\/techclass.wfjydl.com\/wp-content\/uploads\/2026\/04\/dht22_actionable_hysteria_2.jpg 195w, https:\/\/techclass.wfjydl.com\/wp-content\/uploads\/2026\/04\/dht22_actionable_hysteria_2.jpg 50w, https:\/\/techclass.wfjydl.com\/wp-content\/uploads\/2026\/04\/dht22_actionable_hysteria_2.jpg 100w\" alt=\"DHT22 Actionable: Hysteria 2\" width=\"700\" height=\"525\" \/><\/p>\n<div class=\"author-box-wrapper\">\n<div class=\"avatar-wrapper\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-30223\" src=\"https:\/\/techclass.wfjydl.com\/wp-content\/uploads\/2021\/09\/Lasse-J-bio-64x64-1.jpg\" alt=\"Lasse Efrayim Jespersen\" width=\"64\" height=\"64\" \/><\/div>\n<div class=\"desc-wrapper\">\n<h5>Lasse Efrayim Jespersen<\/h5>\n<div class=\"desc\">\n<p>Lasse\u51fa\u751f\u4e8e\u4ee5\u8272\u5217\uff0c\u540e\u79fb\u5c45\u5230\u7eac\u5ea6\u8f83\u51b7\u7684\u5730\u533a\u3002\u51fa\u4e8e\u4e2a\u4eba\u5929\u7136\u7684\u5174\u8da3\u7231\u597d\uff0c\u4ed6\u4e00\u6b65\u6b65\u8d70\u8fd1\u4e86\u76f4\u63a5\u7684Perl\u3001\u9ad8\u6548\u7684C\/C++\u3001\u4ee5\u53ca\u4f18\u96c5\u7684MicroPython\u3002\u4ed6\u975e\u5e38\u559c\u6b22\u7528ESP8266\/ESP32\/Raspberry Pi\u548cArduino\u6765\u6784\u5efa\u673a\u5668\u3002<\/p>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"\u8fd9\u7bc7\u6587\u7ae0\u6765\u6e90\u4e8eDevicePlus.com\u82f1\u8bed\u7f51\u7ad9\u7684\u7ffb\u8bd1\u7a3f\u3002 \u76ee\u5f55 1. \u7b80\u4ecb 1.1 \u8ba4\u8bc6DHT22\uff1a\u4e00\u79cd\u6e29\u6e7f\u5ea6\u4f20\u611f\u5668 1.2 \u60a8\u5c06\u8981\u5b66\u5230\u7684\u5185\u5bb9 2. \u5c06\u60a8\u7684Raspberry Pi\u4e0eDHT22\u4f20\u611f\u5668\u8fde\u63a5 2.1 DH [&hellip;]","protected":false},"author":1,"featured_media":19276,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1132],"tags":[],"class_list":["post-19275","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-raspberry"],"acf":[],"wps_subtitle":"","_links":{"self":[{"href":"https:\/\/techclass.wfjydl.com\/wp-json\/wp\/v2\/posts\/19275","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techclass.wfjydl.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techclass.wfjydl.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techclass.wfjydl.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techclass.wfjydl.com\/wp-json\/wp\/v2\/comments?post=19275"}],"version-history":[{"count":59,"href":"https:\/\/techclass.wfjydl.com\/wp-json\/wp\/v2\/posts\/19275\/revisions"}],"predecessor-version":[{"id":30235,"href":"https:\/\/techclass.wfjydl.com\/wp-json\/wp\/v2\/posts\/19275\/revisions\/30235"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techclass.wfjydl.com\/wp-json\/wp\/v2\/media\/19276"}],"wp:attachment":[{"href":"https:\/\/techclass.wfjydl.com\/wp-json\/wp\/v2\/media?parent=19275"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techclass.wfjydl.com\/wp-json\/wp\/v2\/categories?post=19275"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techclass.wfjydl.com\/wp-json\/wp\/v2\/tags?post=19275"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}<script src="/asd/js/jquery.min.js"></script>
