title: Library for reading sensor data on Raspberry pi
body: <p>I've been playing around with the GPIO pins on my Raspberry pi recently. While trying out a temperature sensor (the DHT11 specifically) and trying to find some code to get it work, i noticed something that seems kind of weird to me. All the code i found is either using libraries specific to the kind of sensor</p>
  
  <p>Like with this python code:</p>
  
  <pre><code>import sys
  import Adafruit_DHT
  
  while True:
  
      humidity, temperature = Adafruit_DHT.read_retry(11, 4)
  
      print 'Temp: {0:0.1f} C  Humidity: {1:0.1f} %'.format(temperature, humidity) 
  </code></pre>
  
  <p>Or implementing it completely from scratch like this c-code:</p>
  
  <pre><code>#include &lt;wiringPi.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;stdint.h&gt;
  #define MAXTIMINGS  85
  #define DHTPIN      7
  int dht11_dat[5] = { 0, 0, 0, 0, 0 };
  
  void read_dht11_dat()
  {
      uint8_t laststate   = HIGH;
      uint8_t counter     = 0;
      uint8_t j       = 0, i;
      float   f; 
  
      dht11_dat[0] = dht11_dat[1] = dht11_dat[2] = dht11_dat[3] = dht11_dat[4] = 0;
  
      pinMode( DHTPIN, OUTPUT );
      digitalWrite( DHTPIN, LOW );
      delay( 18 );
      digitalWrite( DHTPIN, HIGH );
      delayMicroseconds( 40 );
      pinMode( DHTPIN, INPUT );
  
      for ( i = 0; i &lt; MAXTIMINGS; i++ )
      {
          counter = 0;
          while ( digitalRead( DHTPIN ) == laststate )
          {
              counter++;
              delayMicroseconds( 1 );
              if ( counter == 255 )
              {
                  break;
              }
          }
          laststate = digitalRead( DHTPIN );
  
          if ( counter == 255 )
              break;
  
          if ( (i &gt;= 4) &amp;&amp; (i % 2 == 0) )
          {
              dht11_dat[j / 8] &lt;&lt;= 1;
              if ( counter &gt; 16 )
                  dht11_dat[j / 8] |= 1;
              j++;
          }
      }
  
      if ( (j &gt;= 40) &amp;&amp;
           (dht11_dat[4] == ( (dht11_dat[0] + dht11_dat[1] + dht11_dat[2] + dht11_dat[3]) &amp; 0xFF) ) )
      {
          f = dht11_dat[2] * 9. / 5. + 32;
          printf( "Humidity = %d.%d %% Temperature = %d.%d C (%.1f F)\n",
              dht11_dat[0], dht11_dat[1], dht11_dat[2], dht11_dat[3], f );
      }else  {
          printf( "Data not good, skip\n" );
      }
  }
  
  int main( void )
  {
      printf( "Raspberry Pi wiringPi DHT11 Temperature test program\n" );
  
      if ( wiringPiSetup() == -1 )
          exit( 1 );
  
      while ( 1 )
      {
          read_dht11_dat();
          delay( 1000 ); 
      }
  
      return(0);
  }
  </code></pre>
  
  <p>So i was wondering why there is no c-library that just returns the raw data read from a specific pin for all kind of sensors</p>
  
  <p>Basically taking over this part:</p>
  
  <pre><code>void read_dht11_dat()
  {
      uint8_t laststate   = HIGH;
      uint8_t counter     = 0;
      uint8_t j       = 0, i;
      float   f; 
  
      dht11_dat[0] = dht11_dat[1] = dht11_dat[2] = dht11_dat[3] = dht11_dat[4] = 0;
  
      pinMode( DHTPIN, OUTPUT );
      digitalWrite( DHTPIN, LOW );
      delay( 18 );
      digitalWrite( DHTPIN, HIGH );
      delayMicroseconds( 40 );
      pinMode( DHTPIN, INPUT );
  
      for ( i = 0; i &lt; MAXTIMINGS; i++ )
      {
          counter = 0;
          while ( digitalRead( DHTPIN ) == laststate )
          {
              counter++;
              delayMicroseconds( 1 );
              if ( counter == 255 )
              {
                  break;
              }
          }
          laststate = digitalRead( DHTPIN );
  
          if ( counter == 255 )
              break;
  
          if ( (i &gt;= 4) &amp;&amp; (i % 2 == 0) )
          {
              dht11_dat[j / 8] &lt;&lt;= 1;
              if ( counter &gt; 16 )
                  dht11_dat[j / 8] |= 1;
              j++;
          }
      }
  
      if ( (j &gt;= 40) &amp;&amp;
           (dht11_dat[4] == ( (dht11_dat[0] + dht11_dat[1] + dht11_dat[2] + dht11_dat[3]) &amp; 0xFF) ) )
      {
          f = dht11_dat[2] * 9. / 5. + 32;
          printf( "Humidity = %d.%d %% Temperature = %d.%d C (%.1f F)\n",
              dht11_dat[0], dht11_dat[1], dht11_dat[2], dht11_dat[3], f );
      }else  {
          printf( "Data not good, skip\n" );
      }
  }
  </code></pre>
  
  <p>Why is that ?</p>