{"id":836,"date":"2020-09-08T23:10:17","date_gmt":"2020-09-08T15:10:17","guid":{"rendered":"https:\/\/www.iotch.xyz\/?p=836"},"modified":"2020-09-08T23:10:17","modified_gmt":"2020-09-08T15:10:17","slug":"%e6%a0%91%e8%8e%93%e6%b4%be%e9%a9%b1%e5%8a%a8ssd1306-oeld%e5%b1%8f%e5%b9%95%e6%98%be%e7%a4%ba%e6%b8%a9%e5%ba%a6%ef%bc%8cip%e5%9c%b0%e5%9d%80%ef%bc%8c%e6%97%a5%e6%9c%9f","status":"publish","type":"post","link":"https:\/\/www.iotch.xyz\/?p=836","title":{"rendered":"\u6811\u8393\u6d3e\u9a71\u52a8ssd1306 oeld\u5c4f\u5e55\u663e\u793a\u6e29\u5ea6\uff0cip\u5730\u5740\uff0c\u65e5\u671f"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u8bdd\u4e0d\u591a\u8bf4\uff0c\u5148\u4e0a\u56fe<img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/img-blog.csdnimg.cn\/20191129172256584.jpg?x-oss-process=image\/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xpb253ZXJzb24=,size_5,color_FFFFFF,t_70\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" width=\"600\" height=\"400\"><\/h2>\n\n\n\n<h2 class=\"wp-block-heading\">\u6211\u7684oled\u5c4f\u5e55\u662fssd 1306 \u82af\u7247\u9a71\u52a8\u7684\uff0c4\u9488\u811a\uff0c\u53ea\u652f\u6301iic,\u4e0d\u652f\u6301spi,\u53cc\u8272\u7684\u3002<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code># Copyright (c) 2017 Adafruit Industries\n# Author: Tony DiCola &amp; James DeVito\n#\n# Permission is hereby granted, free of charge, to any person obtaining a copy\n# of this software and associated documentation files (the \"Software\"), to deal\n# in the Software without restriction, including without limitation the rights\n# to use, copy, modify, merge, publish, distribute, sublicense, and\/or sell\n# copies of the Software, and to permit persons to whom the Software is\n# furnished to do so, subject to the following conditions:\n#\n# The above copyright notice and this permission notice shall be included in\n# all copies or substantial portions of the Software.\n#\n# THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\n# THE SOFTWARE.\nimport time\n\nimport Adafruit_GPIO.SPI as SPI\nimport Adafruit_SSD1306\nimport Adafruit_DHT\n\nfrom PIL import Image\nfrom PIL import ImageDraw\nfrom PIL import ImageFont\n\nimport subprocess\n\np=0\nsensor=Adafruit_DHT.DHT11\ngpio=27\nhumidity,temperature = Adafruit_DHT.read_retry(sensor,gpio)\n\n\n# Raspberry Pi pin configuration:\nRST = None     # on the PiOLED this pin isnt used\n# Note the following are only used with SPI:\nDC = 23\nSPI_PORT = 0\nSPI_DEVICE = 0\n\n\ndisp = Adafruit_SSD1306.SSD1306_128_64(rst=RST)\n\n\ndisp.begin()\n\n# Clear display.\ndisp.clear()\ndisp.display()\n\n# Create blank image for drawing.\n# Make sure to create image with mode '1' for 1-bit color.\nwidth = disp.width\nheight = disp.height\nimage = Image.new('1', (width, height))\n\n# Get drawing object to draw on image.\ndraw = ImageDraw.Draw(image)\n\n# Draw a black filled box to clear the image.\ndraw.rectangle((0,0,width,height), outline=0, fill=0)\n\n# Draw some shapes.\n# First define some constants to allow easy resizing of shapes.\npadding = -2\ntop = padding\nbottom = height-padding\n# Move left to right keeping track of the current x position for drawing shapes.\nx = 0\n\n\n\nfont = ImageFont.load_default()\n\n\nwhile True:\n\n    # Draw a black filled box to clear the image.\n    draw.rectangle((0,0,width,height), outline=0, fill=0)\n\n    # Shell scripts for system monitoring from here : https:\/\/unix.stackexchange.com\/questions\/119126\/command-to-display-memory-usage-disk-usage-and-cpu-load\n    cmd = \"hostname -I | cut -d\\' \\' -f1\"\n    IP = subprocess.check_output(cmd, shell = True )\n    cmd = \"top -bn2 -d 0.1 | grep %Cpu | awk 'NR==2{printf \\\"CPU: %.2f%%\\\",100-$8}'\"\n    CPU = subprocess.check_output(cmd, shell = True )\n    cmd = \"free -m | awk 'NR==2{printf \\\"Mem: %s\/%sMB %.2f%%\\\", $3,$2,$3*100\/$2 }'\"\n    MemUsage = subprocess.check_output(cmd, shell = True )\n    cmd = \"df -h | awk '$NF==\\\"\/\\\"{printf \\\"Disk: %d\/%dGB %s\\\", $3,$2,$5}'\"\n    Disk = subprocess.check_output(cmd, shell = True )\n    humidity,temperature = Adafruit_DHT.read_retry(sensor,gpio)\n    cmd = \"date | awk '{printf \\\"%s %s %s %s \\\", $1,$2,$3,$4}'\"\n    date = subprocess.check_output(cmd, shell = True )\n    cmd = \"cat \/sys\/class\/thermal\/thermal_zone0\/temp | awk '{printf \\\"%.1f\\\", $0\/1000}'\"\n    cput = subprocess.check_output(cmd, shell= True)\n\n\n\n    # Write two lines of text.\n    draw.text((x, top),       \"IP: \" + str(IP),  font=font, fill=255)\n    draw.text((x, top+8),     str(CPU), font=font, fill=255)\n    draw.text((x+80, top+8),  str(cput),font=font, fill=255)\n    draw.text((x, top+16),    str(MemUsage),  font=font, fill=255)\n    draw.text((x, top+25),    str(Disk),  font=font, fill=255)\n    draw.text((x, top+34),    \"temperature: \" + str(temperature),font=font,fill=255)\n    draw.text((x, top+43),    \"humidity: \" + str(humidity),font=font,fill=255)\n    draw.text((x, top+54),    str(date),  font=font, fill=255)\n\n    disp.image(image)\n    disp.display()\n    time.sleep(.1)\n\n\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u5728\u9a71\u52a8\u793a\u4f8b\u6587\u4ef6\u4e0a\uff0c\u6211\u52a0\u4e0a\u4e86DHT11\u6a21\u5757\uff0c\u6765\u663e\u793a\u6e29\u5ea6\u548c\u6e7f\u5ea6\uff0c\u5c4f\u5e55\u7684scl\u548csda\u5f15\u811a\u8fde\u63a5\u5230\u4e86\u6811\u8393\u6d3e\u76843\u53f7\u548c5\u53f7\u5f15\u811a\uff0cDHT11\u7684out\u5f15\u811a\u8fde\u63a5\u523013\u53f7\u5f15\u811a<\/h2>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/img-blog.csdnimg.cn\/20191129174229852.jpg?x-oss-process=image\/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xpb253ZXJzb24=,size_16,color_FFFFFF,t_70\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">\u6811\u8393\u6d3e\u8981\u9a71\u52a8iic,\u5fc5\u987b\u8981\u73b0\u5728\u914d\u7f6e\u754c\u9762\u5f00\u542fiic\u901a\u4fe1\uff01<\/h2>\n\n\n\n<p>\u7528\u6811\u8393\u6d3e\u914d\u7f6e\u5de5\u5177\u5f00\u542fiic<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>raspi-config\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/img-blog.csdnimg.cn\/20191129175829293.jpg?x-oss-process=image\/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xpb253ZXJzb24=,size_16,color_FFFFFF,t_70\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">\u9009\u62e9\u56fe\u4e2d\u7684\u9009\u9879<\/h2>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/img-blog.csdnimg.cn\/20191129180058674.jpg?x-oss-process=image\/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xpb253ZXJzb24=,size_16,color_FFFFFF,t_70\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">\u9009\u62e9I2C<\/h2>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/img-blog.csdnimg.cn\/20191129180140560.jpg?x-oss-process=image\/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xpb253ZXJzb24=,size_16,color_FFFFFF,t_70\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">\u9009\u62e9yes<\/h2>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/img-blog.csdnimg.cn\/20191129180217899.jpg?x-oss-process=image\/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xpb253ZXJzb24=,size_16,color_FFFFFF,t_70\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">\u53ef\u4ee5\u770b\u5230 I2C\u5df2\u7ecf\u5f00\u542f\u4e86\uff01<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo i2cdetect -y 1\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u53ef\u4ee5\u7528\u8fd9\u4e2a\u547d\u4ee4\u6765\u67e5\u770b\u662f\u5426\u5df2\u7ecf\u5f00\u542f\u3002<img decoding=\"async\" src=\"https:\/\/img-blog.csdnimg.cn\/20191129180425669.jpg?x-oss-process=image\/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xpb253ZXJzb24=,size_16,color_FFFFFF,t_70\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\"><\/h2>\n\n\n\n<h2 class=\"wp-block-heading\">\u6811\u8393\u6d3e\u6211\u53ea\u8fde\u63a5\u4e86\u4e00\u4e2a\u5c4f\u5e55\uff0c\u4ece\u4e0a\u56fe\u53ef\u4ee5\u770b\u5230\u3002<\/h2>\n\n\n\n<h2 class=\"wp-block-heading\">\u53ef\u4ee5\u8bbe\u7f6e\u4e3a\u5f00\u673a\u542f\u52a8\u3002<\/h2>\n\n\n\n<p>\u539f\u6587\u94fe\u63a5\uff1a<a href=\"https:\/\/blog.csdn.net\/lionwerson\/article\/details\/103314825\" target=\"_blank\"  rel=\"nofollow\" >https:\/\/blog.csdn.net\/lionwerson\/article\/details\/103314825<\/a><\/p>\n<button class=\"simplefavorite-button\" data-postid=\"836\" data-siteid=\"1\" data-groupid=\"1\" data-favoritecount=\"0\" style=\"\">\u6536\u85cf <i class=\"sf-icon-star-empty\"><\/i><\/button>","protected":false},"excerpt":{"rendered":"<p>\u8bdd\u4e0d\u591a\u8bf4\uff0c\u5148\u4e0a\u56fe \u6211\u7684oled\u5c4f\u5e55\u662fssd 1306 \u82af\u7247\u9a71\u52a8\u7684\uff0c4\u9488\u811a\uff0c\u53ea\u652f\u6301iic,\u4e0d\u652f\u6301spi,\u53cc\u8272\u7684\u3002 \u5728\u9a71\u52a8\u793a\u4f8b\u6587\u4ef6\u4e0a\uff0c &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"class_list":["post-836","post","type-post","status-publish","format-standard","hentry","category-10"],"_links":{"self":[{"href":"https:\/\/www.iotch.xyz\/index.php?rest_route=\/wp\/v2\/posts\/836","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.iotch.xyz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.iotch.xyz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.iotch.xyz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.iotch.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=836"}],"version-history":[{"count":0,"href":"https:\/\/www.iotch.xyz\/index.php?rest_route=\/wp\/v2\/posts\/836\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.iotch.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=836"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.iotch.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=836"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.iotch.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=836"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}