{"id":286,"date":"2014-05-20T22:46:40","date_gmt":"2014-05-20T21:46:40","guid":{"rendered":"http:\/\/www.anginf.de\/?p=286"},"modified":"2015-12-16T16:05:13","modified_gmt":"2015-12-16T15:05:13","slug":"monitor-temperature-and-humidity-with-dht11-sensor-on-raspberry-pi","status":"publish","type":"post","link":"https:\/\/www.anginf.de\/?p=286","title":{"rendered":"Monitor temperature and humidity with DHT11 sensor on raspberry pi"},"content":{"rendered":"<p>The <a title=\"Google Suche nach DHT11\" href=\"https:\/\/www.google.de\/#q=dht11\" target=\"_blank\">DHT11<\/a> sensor includes a temperature and a humidity sensor. I connected it with my raspberry pi and transmit the data to an external cacti server, which polls the data regularly from the raspberry pi.<\/p>\n<h2>Step 1: Wire the sensor<\/h2>\n<p>While facing the open side of the sensor (NOT the one with the sticker on it), connect the pins as following<\/p>\n<ul>\n<li>Leftmost pin = V+ &#8212; connect with 3V3 (Pin 1) from raspberry pi<\/li>\n<li>Second pin = DATA &#8212; connect with GPIO4 (Pin 7) from raspberry pi <b>AND<\/b> via a 4.7k-10k resistor with 3V3<\/li>\n<li>Third pin = Do NOT connect<\/li>\n<li>Rightmost pin = GND &#8212; connect with GND (Pin 6) from raspberry pi<\/li>\n<\/ul>\n<figure id=\"attachment_251\" aria-describedby=\"caption-attachment-251\" style=\"width: 604px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/www.anginf.de\/wp-content\/uploads\/raspberry-pi-rev2-gpio-pinout.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.anginf.de\/wp-content\/uploads\/raspberry-pi-rev2-gpio-pinout-1024x714.jpg\" alt=\"Raspberry PI Rev2 GPIO pinout\" width=\"604\" height=\"421\" class=\"size-large wp-image-251\" srcset=\"https:\/\/www.anginf.de\/wp-content\/uploads\/raspberry-pi-rev2-gpio-pinout-1024x714.jpg 1024w, https:\/\/www.anginf.de\/wp-content\/uploads\/raspberry-pi-rev2-gpio-pinout-300x209.jpg 300w, https:\/\/www.anginf.de\/wp-content\/uploads\/raspberry-pi-rev2-gpio-pinout.jpg 1050w\" sizes=\"auto, (max-width: 604px) 100vw, 604px\" \/><\/a><figcaption id=\"caption-attachment-251\" class=\"wp-caption-text\">Raspberry PI Rev2 GPIO pinout<\/figcaption><\/figure>\n<h2>Step 2: Get the BCM2835 library and compile \/ install<\/h2>\n<p>The latest version can always be found here: <a href=\"http:\/\/www.airspayce.com\/mikem\/bcm2835\/\" title=\"BCM2\" target=\"_blank\">http:\/\/www.airspayce.com\/mikem\/bcm2835\/<\/a><br \/>\nAs the time of writing, this was <a href=\"http:\/\/www.airspayce.com\/mikem\/bcm2835\/bcm2835-1.36.tar.gz\" title=\"BCM2835 Library Version 1.36\" target=\"_blank\">1.36<\/a><\/p>\n<pre lang=\"bash\">\r\ncd\r\nmkdir -p work\/bcm2835\r\ncd work\/bcm2835\r\nwget http:\/\/www.open.com.au\/mikem\/bcm2835\/bcm2835-1.36.tar.gz\r\ntar xvfz bcm2835-1.36.tar.gz\r\ncd .\/bcm2835-1.36\r\n.\/configure\r\nmake\r\nsudo make install\r\n<\/pre>\n<h2>Step 3: Get the Adafruit python code<\/h2>\n<pre lang=\"bash\">\r\nsudo apt-get update\r\nsudo apt-get -y install git\r\ngit clone https:\/\/github.com\/adafruit\/Adafruit-Raspberry-Pi-Python-Code\r\ncd .\/Adafruit-Raspberry-Pi-Python-Code\r\ncd .\/Adafruit_DHT_Driver\r\nmake\r\n<\/pre>\n<p>After successfully compiling the Adafruit Driver, you can check if your sensor is already working. Keep in mind that the DHT11 is SLOW and won&#8217;t react if asked more than once within 2 or 3 seconds. If you don&#8217;t get any result after the first query, wait a few seconds (at least 3) and try again. If the problem persists, your wiring might be wrong.<\/p>\n<pre lang=\"bash\">\r\nsudo .\/Adafruit_DHT 11 4\r\n<\/pre>\n<h2>Step 4: Modify Adafruit python code<\/h2>\n<p>The original Adafruit code is giving too much information for simple cacti input parameters. A modification of the source code is necessary. Along with changing the output, I also removed all references to DHT22 and AM2302 to make the file a bit smaller:<\/p>\n<p><code>ada_sm.c<\/code><\/p>\n<pre lang=\"c\">\r\n\/\/  How to access GPIO registers from C-code on the Raspberry-Pi\r\n\/\/  Example program\r\n\/\/  15-January-2012\r\n\/\/  Dom and Gert\r\n\/\/ Access from ARM Running Linux\r\n#define BCM2708_PERI_BASE        0x20000000\r\n#define GPIO_BASE                (BCM2708_PERI_BASE + 0x200000) \/* GPIO controller *\/\r\n#include <stdio.h>\r\n#include <string.h>\r\n#include <stdlib.h>\r\n#include <dirent.h>\r\n#include <fcntl.h>\r\n#include <assert.h>\r\n#include <unistd.h>\r\n#include <sys\/mman.h>\r\n#include <sys\/types.h>\r\n#include <sys\/stat.h>\r\n#include <sys\/time.h>\r\n#include <bcm2835.h>\r\n#include <unistd.h>\r\n#define MAXTIMINGS 100\r\n\r\n#define DHT11 11\r\n\r\nint readDHT(int type, int pin);\r\n\r\nint main(int argc, char **argv)\r\n{\r\n  if (!bcm2835_init())\r\n        return 1;\r\n\r\n  if (argc != 3) {\r\n        printf(\"usage: %s [11] GPIOpin#\\n\", argv[0]);\r\n        printf(\"example: %s 11 4 - Read from an DHT11 connected to GPIO #4\\n\", argv[0]);\r\n        return 2;\r\n  }\r\n  int type = 0;\r\n  if (strcmp(argv[1], \"11\") == 0) type = DHT11;\r\n  if (type == 0) {\r\n        printf(\"Select 11 as type!\\n\");\r\n        return 3;\r\n  }\r\n  int dhtpin = atoi(argv[2]);\r\n  if (dhtpin <= 0) {\r\n        printf(\"Please select a valid GPIO pin #\\n\");\r\n        return 3;\r\n  }\r\n  readDHT(type, dhtpin);\r\n  return 0;\r\n} \/\/ main\r\n\r\nint bits[250], data[100];\r\nint bitidx = 0;\r\n\r\nint readDHT(int type, int pin) {\r\n  int counter = 0;\r\n  int laststate = HIGH;\r\n  int j=0;\r\n\r\n  \/\/ Set GPIO pin to output\r\n  bcm2835_gpio_fsel(pin, BCM2835_GPIO_FSEL_OUTP);\r\n  bcm2835_gpio_fsel(pin, BCM2835_GPIO_FSEL_OUTP);\r\n\r\n  bcm2835_gpio_write(pin, HIGH);\r\n  usleep(500000);  \/\/ 500 ms\r\n  bcm2835_gpio_write(pin, LOW);\r\n  usleep(20000);\r\n\r\n  bcm2835_gpio_fsel(pin, BCM2835_GPIO_FSEL_INPT);\r\n\r\n  data[0] = data[1] = data[2] = data[3] = data[4] = 0;\r\n\r\n  \/\/ wait for pin to drop?\r\n  while (bcm2835_gpio_lev(pin) == 1) {\r\n    usleep(1);\r\n  }\r\n\r\n  \/\/ read data!\r\n  for (int i=0; i< MAXTIMINGS; i++) {\r\n    counter = 0;\r\n    while ( bcm2835_gpio_lev(pin) == laststate) {\r\n        counter++;\r\n        \/\/nanosleep(1);         \/\/ overclocking might change this?\r\n        if (counter == 1000)\r\n          break;\r\n    }\r\n    laststate = bcm2835_gpio_lev(pin);\r\n    if (counter == 1000) break;\r\n    bits[bitidx++] = counter;\r\n\r\n    if ((i>3) && (i%2 == 0)) {\r\n      \/\/ shove each bit into the storage bytes\r\n      data[j\/8] <<= 1;\r\n      if (counter > 200)\r\n        data[j\/8] |= 1;\r\n      j++;\r\n    }\r\n  }\r\n\r\n  if ((j >= 39) &&\r\n      (data[4] == ((data[0] + data[1] + data[2] + data[3]) & 0xFF)) ) {\r\n     if (type == DHT11)\r\n        printf(\"temp:%d hum:%d\\n\", data[2], data[0]);\r\n    return 1;\r\n  }\r\n\r\n  return 0;\r\n}\r\n<\/pre>\n<p>Compile the file with this command line (if you saved the above .c-file as &#8222;ada_sm.c&#8220;)<\/p>\n<pre lang=\"bash\">\r\ngcc ada_sm.c -l bcm2835 -std=gnu99 -o ada_sm\r\n<\/pre>\n<p>The output should be compatible with cacti:<\/p>\n<pre lang=\"bash\">\r\n$ sudo .\/ada_sm 11 4\r\ntemp:23 hum:33\r\n<\/pre>\n<h2>Step 5: Add a socat service<\/h2>\n<p>I&#8217;ve explained how to achieve this in <a href=\"https:\/\/www.anginf.de\/?p=267\" title=\"Monitor Raspberry Pi temperature from an external server\" target=\"_blank\">another post<\/a>, you only need a new shell script which will make sure that the readouts were given:<\/p>\n<p><code>\/usr\/local\/bin\/dht11.sh<\/code><\/p>\n<pre lang=\"bash\">\r\n#!\/bin\/bash\r\nresult=\"\"\r\nwhile [ -z \"${result}\" ];\r\ndo\r\nresult=$(sudo \/home\/pi\/work\/dht11\/ada_sm 11 4)\r\nsleep 3\r\ndone\r\necho ${result}\r\n<\/pre>\n<p>Here the contents the configuration file for socat:<\/p>\n<p><code>\/etc\/default\/socat<\/code><\/p>\n<pre lang=\"ini\">\r\nOPTIONS=\"-T 30 -t 30 tcp-l:9889,reuseaddr,fork,crlf system:\\\"\/usr\/local\/bin\/dht11.sh\\\"\"\r\n<\/pre>\n<h2>Step 6: Read data from the socat service on the cacti host<\/h2>\n<p>This is explained in <a href=\"https:\/\/www.anginf.de\/?p=286\" title=\"Monitor temperature and humidity with DHT11 sensor on raspberry pi\" target=\"_blank\">another post<\/a> as well, but the needed script in the <code>site\/scripts<\/code>-directory is a bit different, here&#8217;s what I used. Keep in mind that you have to change &#8222;PI-IP&#8220; to your local DNS or IP-Adress of the Raspberry Pi.<\/p>\n<p><code>\/usr\/share\/cacti\/site\/scripts\/dht11.sh<\/code><\/p>\n<pre lang=\"bash\">\r\n#!\/bin\/bash\r\noutstring=$(\/bin\/nc PI-IP 9889  | tr -d '\\r')\r\necho \"${outstring}\"\r\n<\/pre>\n<h2>Resources on the internet<\/h2>\n<p>These links helped me set this up:<br \/>\n<a href=\"http:\/\/www.messtechniklabor.de\/artikel-h0000-temperatur_und_luftfeuchtigkeit_messen.html\" target=\"_blank\">http:\/\/www.messtechniklabor.de\/artikel-h0000-temperatur_und_luftfeuchtigkeit_messen.html<\/a><br \/>\n<a title=\"ADAfruit Information on DHT sensor access\" href=\"https:\/\/learn.adafruit.com\/dht-humidity-sensing-on-raspberry-pi-with-gdocs-logging\/wiring\" target=\"_blank\">https:\/\/learn.adafruit.com\/dht-humidity-sensing-on-raspberry-pi-with-gdocs-logging\/wiring<\/a><br \/>\n<a title=\"BCM2835 Library\" href=\"http:\/\/www.airspayce.com\/mikem\/bcm2835\/\" target=\"_blank\">http:\/\/www.airspayce.com\/mikem\/bcm2835\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The DHT11 sensor includes a temperature and a humidity sensor. I connected it with my raspberry pi and transmit the data to an external cacti server, which polls the data regularly from the raspberry pi. Step 1: Wire the sensor While facing the open side of the sensor (NOT the one with the sticker on &hellip; <a href=\"https:\/\/www.anginf.de\/?p=286\" class=\"more-link\"><span class=\"screen-reader-text\">Monitor temperature and humidity with DHT11 sensor on raspberry pi<\/span> weiterlesen <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,3,2],"tags":[],"class_list":["post-286","post","type-post","status-publish","format-standard","hentry","category-allgemein","category-cacti","category-raspberry-pi"],"_links":{"self":[{"href":"https:\/\/www.anginf.de\/index.php?rest_route=\/wp\/v2\/posts\/286","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.anginf.de\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.anginf.de\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.anginf.de\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.anginf.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=286"}],"version-history":[{"count":12,"href":"https:\/\/www.anginf.de\/index.php?rest_route=\/wp\/v2\/posts\/286\/revisions"}],"predecessor-version":[{"id":416,"href":"https:\/\/www.anginf.de\/index.php?rest_route=\/wp\/v2\/posts\/286\/revisions\/416"}],"wp:attachment":[{"href":"https:\/\/www.anginf.de\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=286"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.anginf.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=286"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.anginf.de\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=286"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}