title: Adafruit SHT31-D and Raspberry Pi2 -- Unable to read data from sensor
body: <p>hopefully one of you out there can help me!</p>
  
  <p>I am trying to use the <a href="https://www.adafruit.com/products/2857" rel="nofollow">Adafruit SHT31-D</a> (an i2c device) board with my Pi2.  I am going off of <a href="https://www.sensirion.com/fileadmin/user_upload/customers/sensirion/Dokumente/Humidity_and_Temperature_Sensors/Sensirion_Humidity_and_Temperature_Sensors_SHT3x_Datasheet_digital.pdf" rel="nofollow">this datasheet</a> to guide my coding efforts.  I am using Wiring Pi (wiringpi.com) to facilitate things.</p>
  
  <p>I am able to successfully open a connection to the device, and sending commands seems to work fine, but I am unable to read data back!  Here is the little mini library I have put together.  I am hoping that one of you might have some experience with this sort of thing and be able to help me see where I've gone wrong.</p>
  
  <p>To rule out any possible issues with the sensor hardware, I have tested it with my Arduino UNO and it works without issues.</p>
  
  <p>Here is my C++ code:</p>
  
  <p><strong>SHT3x.h</strong></p>
  
  <pre><code>#pragma once
  
  /* Sensor Commands */
  #define DEFAULT_SHT_ADDR 0x44
  #define MEAS_HREP_STRETCH 0x2C06
  #define MEAS_MREP_STRETCH 0x2C0D
  #define MEAS_LREP_STRETCH 0x2C10
  #define MEAS_HREP 0x2400
  #define MEAS_MREP 0x240B
  #define MEAS_LREP 0x2416
  
  #include &lt;cstdint&gt;
  
  
  class SHT3x {
  
    public:
      SHT3x(const uint8_t&amp; i2cAddr);
      float readHumidity(const uint16_t&amp; command) const;
      float readTempC(const uint16_t&amp; command) const;
      float readTempF(const uint16_t&amp; command) const;
  
    private:
      int8_t _fd;
      uint8_t _header;
      uint32_t getMeasurement(const uint16_t&amp; command) const;
      void sendCommand(const uint16_t&amp; command) const;
      uint32_t receiveData(void) const;
  };
  </code></pre>
  
  <p><strong>SHT3x.cpp</strong></p>
  
  <pre><code>#include &lt;stdexcept&gt;
  #include &lt;wiringPi.h&gt;
  #include &lt;wiringPiI2C.h&gt;
  #include "SHT3x.h"
  
  
  SHT3x::SHT3x(const uint8_t&amp; i2cAddr) {
      _fd = wiringPiI2CSetup(i2cAddr);
      _header = i2cAddr &lt;&lt; 1;
      if (_fd &lt; 0) {
          throw std::runtime_error("Unable to connect");
      }
  }
  
  float SHT3x::readHumidity(const uint16_t&amp; command) const {
      uint32_t raw_data = getMeasurement(command);
      if (!raw_data) {
          throw std::runtime_error("Bad Reading.");
      }
      uint16_t raw_humidity = raw_data &amp; 0xFFFF;
      float humidity = 100.0 * ((float) raw_humidity / (float) 0xFFFF);
      return humidity;
  }
  
  float SHT3x::readTempC(const uint16_t&amp; command) const {
      uint32_t raw_data = getMeasurement(command);
      if (!raw_data) {
          throw std::runtime_error("Bad Reading.");
      }
      uint16_t raw_temp = raw_data &gt;&gt; 16;
      float tempC = -45.0 + (175.0 * ((float) raw_temp / (float) 0xFFFF));
      return tempC;
  }
  
  float SHT3x::readTempF(const uint16_t&amp; command) const {
      uint32_t raw_data = getMeasurement(command);
      if (!raw_data) {
          throw std::runtime_error("Bad Reading.");
      }
      uint16_t raw_temp = raw_data &gt;&gt; 16;
      float tempF = -49.0 + (315.0 * ((float) raw_temp / (float) 0xFFFF));
      return tempF;
  }
  
  uint32_t SHT3x::getMeasurement(const uint16_t&amp; command) const {
      try {
          sendCommand(command);
      } catch (std::runtime_error&amp; e) {
          throw;
      }
      return receiveData();
  }
  
  void SHT3x::sendCommand(const uint16_t&amp; command) const {
      // break command into bytes
      uint8_t MSB = command &gt;&gt; 8;
      uint8_t LSB = command &amp; 0xFF;
  
      // send header
      int8_t ack = wiringPiI2CWrite(_fd, _header);
  
      // send command
      ack &amp;= wiringPiI2CWrite(_fd, MSB);
      ack &amp;= wiringPiI2CWrite(_fd, LSB);
  
      // handle errors
      if (ack) {
          throw std::runtime_error("Sending command failed.");
      }
  }
  
  uint32_t SHT3x::receiveData(void) const {
      uint32_t data;
  
      // send header
      uint8_t read_header = _header | 0x01;
      int8_t ack = wiringPiI2CWrite(_fd, read_header);
  
      // handle errors
      if (ack) throw std::runtime_error("Unable to read data.");
  
      // read data
      data = wiringPiI2CRead(_fd);
      for (size_t i = 0; i &lt; 4; i++) {
          printf("Data: %d\n", data);
          data &lt;&lt;= 8;
          if (i != 1) {
              data |= wiringPiI2CRead(_fd);
          } else {
              wiringPiI2CRead(_fd);   // skip checksum
          }
      }
      wiringPiI2CRead(_fd);   // second checksum
      return data;
  }
  </code></pre>