text: import RPi.GPIO as GPIO
  import time
  import numpy  # sudo apt-get python-numpy
  
  class HX711:
      def __init__(self, dout, pd_sck, gain=128):
          self.PD_SCK = pd_sck
          self.DOUT = dout
  
          GPIO.setmode(GPIO.BCM)
          GPIO.setup(self.PD_SCK, GPIO.OUT)
          GPIO.setup(self.DOUT, GPIO.IN)
  
          self.GAIN = 0
          self.REFERENCE_UNIT = 1  # The value returned by the hx711 that corresponds to your reference unit AFTER dividing by the SCALE.
          
          self.OFFSET = 1
          self.lastVal = long(0)
  
          self.LSByte = [2, -1, -1]
          self.MSByte = [0, 3, 1]
          
          self.MSBit = [0, 8, 1]
          self.LSBit = [7, -1, -1]
  
          self.byte_format = 'LSB'
          self.bit_format = 'MSB'
  
          self.byte_range_values = self.LSByte
          self.bit_range_values = self.MSBit
  
          self.set_gain(gain)
  
          time.sleep(1)
  
      def is_ready(self):
          return GPIO.input(self.DOUT) == 0
  
      def set_gain(self, gain):
          if gain is 128:
              self.GAIN = 1
          elif gain is 64:
              self.GAIN = 3
          elif gain is 32:
              self.GAIN = 2
  
          GPIO.output(self.PD_SCK, False)
          self.read()
      
      def createBoolList(self, size=8):
          ret = []
          for i in range(size):
              ret.append(False)
          return ret
  
      def read(self):
          while not self.is_ready():
              #print("WAITING")
              pass
  
          dataBits = [self.createBoolList(), self.createBoolList(), self.createBoolList()]
          dataBytes = [0x0] * 4
  
          for j in range(self.byte_range_values[0], self.byte_range_values[1], self.byte_range_values[2]):
              for i in range(self.bit_range_values[0], self.bit_range_values[1], self.bit_range_values[2]):
                  GPIO.output(self.PD_SCK, True)
                  dataBits[j][i] = GPIO.input(self.DOUT)
                  GPIO.output(self.PD_SCK, False)
              dataBytes[j] = numpy.packbits(numpy.uint8(dataBits[j]))
  
          #set channel and gain factor for next reading
          for i in range(self.GAIN):
              GPIO.output(self.PD_SCK, True)
              GPIO.output(self.PD_SCK, False)
  
          #check for all 1
          #if all(item is True for item in dataBits[0]):
          #    return long(self.lastVal)
  
          dataBytes[2] ^= 0x80
  
          return dataBytes
  
      def get_binary_string(self):
          binary_format = "{0:b}"
          np_arr8 = self.read_np_arr8()
          binary_string = ""
          for i in range(4):
              # binary_segment = binary_format.format(np_arr8[i])
              binary_segment = format(np_arr8[i], '#010b')
              binary_string += binary_segment + " "
          return binary_string
  
      def get_np_arr8_string(self):
          np_arr8 = self.read_np_arr8()
          np_arr8_string = "[";
          comma = ", "
          for i in range(4):
              if i is 3:
                  comma = ""
              np_arr8_string += str(np_arr8[i]) + comma
          np_arr8_string += "]";
          
          return np_arr8_string
  
      def read_np_arr8(self):
          dataBytes = self.read()
          np_arr8 = numpy.uint8(dataBytes)
  
          return np_arr8
  
      def read_long(self):
          np_arr8 = self.read_np_arr8()
          np_arr32 = np_arr8.view('uint32')
          self.lastVal = np_arr32
  
          return long(self.lastVal)
  
      def read_average(self, times=15, output = False):
  
          # Take [times] samples and remove outliers until the standard deviation
          # is less than [desired_deviation]. If the set reduces to less than
          # [min_sample_size] consider this a bad reading and return None.
  
          values = [float(self.read_long()) for i in range(times)]
          desired_deviation = 30
          min_sample_size = 5
          indent = 0
  
          while True:
              mean = numpy.mean(values)
              std_dev = numpy.std(values)
              if output: print '%s[%s] std: %s | avg: %s' % ('  ' * indent, len(values), std_dev, mean)
              indent += 1
              if len(values) >= min_sample_size and std_dev > desired_deviation:
                  outlier = mean
                  for x in values:
                      if abs(mean - x) > abs(mean - outlier):
                          outlier = x
                  values.remove(outlier)
              else:
                  break
  
          if len(values) < min_sample_size:
              return None
  
          return mean
  
  
      def get_value(self, times=15):
          return self.read_average(times) - self.OFFSET
  
      def get_weight(self, times=15):
          value = self.get_value(times)
          value = value / self.REFERENCE_UNIT
          return value
  
      def tare(self, times=15):
         
          # Backup REFERENCE_UNIT value
          reference_unit = self.REFERENCE_UNIT
          self.set_reference_unit(1)
  
          value = self.read_average(times)
          self.set_offset(value)
  
          self.set_reference_unit(reference_unit)
          return value;
  
      def set_reading_format(self, byte_format="LSB", bit_format="MSB"):
  
          self.byte_format = byte_format
          self.bit_format = bit_format
  
          if byte_format == "LSB":
              self.byte_range_values = self.LSByte
          elif byte_format == "MSB":
              self.byte_range_values = self.MSByte
  
          if bit_format == "LSB":
              self.bit_range_values = self.LSBit
          elif bit_format == "MSB":
              self.bit_range_values = self.MSBit
  
      def set_offset(self, offset):
          self.OFFSET = offset
  
      def set_reference_unit(self, reference_unit):
          self.REFERENCE_UNIT = reference_unit
  
      # HX711 datasheet states that setting the PDA_CLOCK pin on high for >60 microseconds would power off the chip.
      # I used 100 microseconds, just in case.
      # I've found it is good practice to reset the hx711 if it wasn't used for more than a few seconds.
      def power_down(self):
          GPIO.output(self.PD_SCK, False)
          GPIO.output(self.PD_SCK, True)
          time.sleep(0.0001)
  
      def power_up(self):
          GPIO.output(self.PD_SCK, False)
          time.sleep(0.0001)
  
      def reset(self):
          self.power_down()
          self.power_up()
  