Ejemplo n.º 1
0
    def get_measurement(self):
        """ Gets the humidity and temperature """
        self._humidity = 0.0
        self._temperature = 0.0

        # Ensure if the power pin turns off, it is turned back on
        if (self.power_relay_id and not db_retrieve_table_daemon(
                Relay, device_id=self.power_relay_id).is_on()):
            self.logger.error(
                'Sensor power relay {rel} detected as being off. '
                'Turning on.'.format(rel=self.power_relay_id))
            self.start_sensor()
            time.sleep(2)

        try:
            try:
                self.setup()
            except Exception as except_msg:
                self.logger.error(
                    'Could not initialize sensor. Check if gpiod is running. '
                    'Error: {msg}'.format(msg=except_msg))
            self.pi.write(self.gpio, pigpio.LOW)
            time.sleep(0.017)  # 17 ms
            self.pi.set_mode(self.gpio, pigpio.INPUT)
            self.pi.set_watchdog(self.gpio, 200)
            time.sleep(0.2)
            self._dew_point = dewpoint(self._temperature, self._humidity)
        except Exception as e:
            self.logger.error(
                "Exception raised when taking a reading: {err}".format(err=e))
        finally:
            self.close()
Ejemplo n.º 2
0
 def measure_sensor(self):
     self.temp_temperature = None
     self.temp_humidity = None
     self.temp_dew_point = None
     try:
         try:
             self.setup()
         except Exception as except_msg:
             self.logger.error(
                 'Could not initialize sensor. Check if gpiod is running. '
                 'Error: {msg}'.format(msg=except_msg))
         self.pi.write(self.gpio, self.pigpio.LOW)
         time.sleep(0.017)  # 17 ms
         self.pi.set_mode(self.gpio, self.pigpio.INPUT)
         self.pi.set_watchdog(self.gpio, 200)
         time.sleep(0.2)
         if (self.temp_humidity is not None and
                 self.temp_temperature is not None):
             self.temp_dew_point = dewpoint(
                 self.temp_temperature, self.temp_humidity)
     except Exception as e:
         self.logger.exception(
             "Exception when taking a reading: {err}".format(
                 err=e))
     finally:
         self.close()
         return (self.temp_dew_point,
                 self.temp_humidity,
                 self.temp_temperature)
Ejemplo n.º 3
0
 def get_measurement(self):
     """ Gets the measurement in units by reading the """
     time.sleep(2)
     temperature, humidity, pressure = self.read_bme280_all()
     alt = altitude(pressure)
     dew_pt = dewpoint(temperature, humidity)
     return alt, dew_pt, humidity, pressure, temperature
Ejemplo n.º 4
0
    def read(self):
        try:
            bus = smbus.SMBus(self.I2C_bus_number)
            # SHT25 address, 0x40(64)
            # Send temperature measurement command
            #       0xF3(243)   NO HOLD master
            bus.write_byte(self.i2c_address, 0xF3)
            time.sleep(0.5)
            # SHT25 address, 0x40(64)
            # Read data back, 2 bytes
            # Temp MSB, Temp LSB
            data0 = bus.read_byte(self.i2c_address)
            data1 = bus.read_byte(self.i2c_address)
            self._temperature = -46.85 + (((data0 * 256 + data1) * 175.72) / 65536.0)

            # SHT25 address, 0x40(64)
            # Send humidity measurement command
            #       0xF5(245)   NO HOLD master
            bus.write_byte(self.i2c_address, 0xF5)
            time.sleep(0.5)
            # SHT25 address, 0x40(64)
            # Read data back, 2 bytes
            # Humidity MSB, Humidity LSB
            data0 = bus.read_byte(self.i2c_address)
            data1 = bus.read_byte(self.i2c_address)
            self._humidity = -6 + (((data0 * 256 + data1) * 125.0) / 65536.0)

            self._dewpoint = dewpoint(self.temperature, self.humidity)
        except:
            return 1
Ejemplo n.º 5
0
    def get_measurement(self):
        """ Gets the humidity and temperature """
        self._humidity = None
        self._temperature = None

        try:
            # Send temperature measurement command
            # 0xF3(243) NO HOLD master
            self.sht2x.write_byte(self.i2c_address, 0xF3)
            time.sleep(0.5)
            # Read data back, 2 bytes
            # Temp MSB, Temp LSB
            data0 = self.sht2x.read_byte(self.i2c_address)
            data1 = self.sht2x.read_byte(self.i2c_address)
            temperature = -46.85 + (((data0 * 256 + data1) * 175.72) / 65536.0)
            # Send humidity measurement command
            # 0xF5(245) NO HOLD master
            self.sht2x.write_byte(self.i2c_address, 0xF5)
            time.sleep(0.5)
            # Read data back, 2 bytes
            # Humidity MSB, Humidity LSB
            data0 = self.sht2x.read_byte(self.i2c_address)
            data1 = self.sht2x.read_byte(self.i2c_address)
            humidity = -6 + (((data0 * 256 + data1) * 125.0) / 65536.0)
            dew_point = dewpoint(temperature, humidity)
            self._dew_point = dew_point
            self._humidity = humidity
            self._temperature = temperature
        except Exception as e:
            logger.exception(
                "Exception when taking a reading: {err}".format(err=e))
Ejemplo n.º 6
0
    def read(self):
        try:
            bus = smbus.SMBus(self.i2c_bus)
            # SHT25 address, 0x40(64)
            # Send temperature measurement command
            #       0xF3(243)   NO HOLD master
            bus.write_byte(self.i2c_address, 0xF3)
            time.sleep(0.5)
            # SHT25 address, 0x40(64)
            # Read data back, 2 bytes
            # Temp MSB, Temp LSB
            data0 = bus.read_byte(self.i2c_address)
            data1 = bus.read_byte(self.i2c_address)
            self._temperature = -46.85 + ((
                (data0 * 256 + data1) * 175.72) / 65536.0)

            # SHT25 address, 0x40(64)
            # Send humidity measurement command
            #       0xF5(245)   NO HOLD master
            bus.write_byte(self.i2c_address, 0xF5)
            time.sleep(0.5)
            # SHT25 address, 0x40(64)
            # Read data back, 2 bytes
            # Humidity MSB, Humidity LSB
            data0 = bus.read_byte(self.i2c_address)
            data1 = bus.read_byte(self.i2c_address)
            self._humidity = -6 + (((data0 * 256 + data1) * 125.0) / 65536.0)

            self._dewpoint = dewpoint(self.temperature, self.humidity)
        except:
            return 1
Ejemplo n.º 7
0
 def get_measurement(self):
     """ Gets the measurement in units by reading the """
     time.sleep(2)
     temperature, humidity, pressure = self.read_bme280_all()
     pressure_pa = pressure * 100  # Correct units, Pa = hPa * 100
     alt = altitude(pressure_pa)
     dew_pt = dewpoint(temperature, humidity)
     return alt, dew_pt, humidity, pressure_pa, temperature
 def get_measurement(self):
     """ Gets the humidity and temperature """
     self.am = AM2315.AM2315(0x5c, "/dev/i2c-" + self.I2C_bus_number)
     temperature, humidity, crc_check = self.am.sense()
     if crc_check != 1:
         return 1
     else:
         dew_pt = dewpoint(temperature, humidity)
         return dew_pt, humidity, temperature
Ejemplo n.º 9
0
 def next(self):
     """
     Call the read method and return temperature and humidity information.
     """
     if self.read():
         return None
     response = {
         'humidity': float("{0:.2f}".format(self.humidity)),
         'temperature': float("{0:.2f}".format(self.temperature)),
         'dewpoint': float("{0:.2f}".format(dewpoint(self.temperature, self.humidity)))
     }
     return response
Ejemplo n.º 10
0
    def get_measurement(self):
        """ Gets the measurement in units by reading the """
        self._altitude = None
        self._dew_point = None
        self._humidity = None
        self._pressure = None
        self._temperature = None

        temperature = self.sensor.read_temperature()
        pressure = self.sensor.read_pressure()
        humidity = self.sensor.read_humidity()
        alt = altitude(pressure)
        dew_pt = dewpoint(temperature, humidity)
        return alt, dew_pt, humidity, pressure, temperature
Ejemplo n.º 11
0
 def next(self):
     """
     Call the read method and return temperature, humidity, and pressure information.
     """
     if self.read():
         return None
     response = {
         'temperature': float("{0:.2f}".format(self.temperature)),
         'humidity': float("{0:.2f}".format(self.humidity)),
         'dewpoint': float("{0:.2f}".format(dewpoint(self.temperature, self.humidity))),
         'pressure': int(self.pressure),
         'altitude': float("{0:.2f}".format(altitude(self.pressure)))
     }
     return response
Ejemplo n.º 12
0
    def return_measurements(self):
        # Retry measurement if CRC fails
        for num_measure in range(3):
            temperature, humidity, crc_check = self.am.sense()
            if crc_check != 1:
                self.logger.debug(
                    "Measurement {num} returned failed CRC".format(
                        num=num_measure))
                pass
            else:
                dew_pt = dewpoint(temperature, humidity)
                return dew_pt, humidity, temperature
            time.sleep(2)

        self.logger.error("All measurements returned failed CRC")
        return None, None, None
Ejemplo n.º 13
0
 def get_measurement(self):
     """ Gets the humidity and temperature """
     self._humidity = 0.0
     self._temperature = 0.0
     try:
         try:
             self.setup()
         except Exception as except_msg:
             self.logger.error(
                 'Could not initialize sensor. Check if gpiod is running. '
                 'Error: {msg}'.format(msg=except_msg))
         self._humidity, self._temperature = Adafruit_DHT.read_retry(device, gpio)
         self._dew_point = dewpoint(self._temperature, self._humidity)
     except Exception as e:
         self.logger.error(
             "Exception raised when taking a reading: {err}".format(
                 err=e))
     finally:
         self.close()
Ejemplo n.º 14
0
 def next(self):
     """
     Call the read method and return temperature, humidity, and pressure information.
     """
     if self.read():
         return None
     response = {
         'temperature':
         float("{0:.2f}".format(self.temperature)),
         'humidity':
         float("{0:.2f}".format(self.humidity)),
         'dewpoint':
         float("{0:.2f}".format(dewpoint(self.temperature, self.humidity))),
         'pressure':
         int(self.pressure),
         'altitude':
         float("{0:.2f}".format(altitude(self.pressure)))
     }
     return response
Ejemplo n.º 15
0
    def get_measurement(self):
        """ Gets the humidity and temperature """
        # wtreg = 0xE6
        # rdreg = 0xE7
        rdtemp = 0xE3
        rdhumi = 0xE5

        handle = self.pi.i2c_open(self.I2C_bus_number,
                                  self.address)  # open i2c bus
        self.pi.i2c_write_byte(handle, rdtemp)  # send read temp command
        time.sleep(0.055)  # readings take up to 50ms, lets give it some time
        (_, byte_array) = self.pi.i2c_read_device(handle,
                                                  3)  # vacuum up those bytes
        self.pi.i2c_close(handle)  # close the i2c bus
        t1 = byte_array[0]  # most significant byte msb
        t2 = byte_array[1]  # least significant byte lsb
        temp_reading = (t1 *
                        256) + t2  # combine both bytes into one big integer
        temp_reading = float(temp_reading)
        temperature = (
            (temp_reading / 65536) * 175.72) - 46.85  # formula from datasheet

        handle = self.pi.i2c_open(self.I2C_bus_number,
                                  self.address)  # open i2c bus
        self.pi.i2c_write_byte(handle, rdhumi)  # send read humi command
        time.sleep(0.055)  # readings take up to 50ms, lets give it some time
        (_, byte_array) = self.pi.i2c_read_device(handle,
                                                  3)  # vacuum up those bytes
        self.pi.i2c_close(handle)  # close the i2c bus
        h1 = byte_array[0]  # most significant byte msb
        h2 = byte_array[1]  # least significant byte lsb
        humi_reading = (h1 *
                        256) + h2  # combine both bytes into one big integer
        humi_reading = float(humi_reading)
        uncomp_humidity = (
            (humi_reading / 65536) * 125) - 6  # formula from datasheet
        humidity = ((25 - temperature) * -0.15) + uncomp_humidity
        dew_pt = dewpoint(temperature, humidity)
        return dew_pt, humidity, temperature
 def get_measurement(self):
     """ Gets the humidity and temperature """
     bus = smbus.SMBus(self.i2c_bus)
     # Send temperature measurement command
     #       0xF3(243)   NO HOLD master
     bus.write_byte(self.i2c_address, 0xF3)
     time.sleep(0.5)
     # Read data back, 2 bytes
     # Temp MSB, Temp LSB
     data0 = bus.read_byte(self.i2c_address)
     data1 = bus.read_byte(self.i2c_address)
     temperature = -46.85 + (((data0 * 256 + data1) * 175.72) / 65536.0)
     # Send humidity measurement command
     #       0xF5(245)   NO HOLD master
     bus.write_byte(self.i2c_address, 0xF5)
     time.sleep(0.5)
     # Read data back, 2 bytes
     # Humidity MSB, Humidity LSB
     data0 = bus.read_byte(self.i2c_address)
     data1 = bus.read_byte(self.i2c_address)
     humidity = -6 + (((data0 * 256 + data1) * 125.0) / 65536.0)
     dew_point = dewpoint(temperature, humidity)
     return dew_point, humidity, temperature
Ejemplo n.º 17
0
    count = 0
    total_diff = 0
    none_count = 0
    max_diff = 0
    for measurements in dht:
        if measurements is not None:
            count += 1
            diff = time.time() - time_diff
            diff_success = time.time() - time_success
            total_diff += diff
            if diff > max_diff:
                max_diff = diff
            print(
                "Temperature: {temp}".format(temp=measurements['temperature']))
            print("Humidity: {hum}".format(hum=measurements['humidity']))
            print("Dew Point: {dp}".format(dp=dewpoint(
                measurements['temperature'], measurements['humidity'])))
            print("No Resp. = {cnt}, Avg Read Time: {avg_read:.2f}, "
                  "Max Read Time: {max_read:.2f}, Read Time: {read:.2f}, "
                  "Time Success: {success:.2f}".format(cnt=none_count,
                                                       avg_read=total_diff /
                                                       count,
                                                       max_read=max_diff,
                                                       read=diff,
                                                       success=diff_success))
            time_diff = time.time()
            time_success = time.time()
        else:
            none_count += 1
            time_diff = time.time()
Ejemplo n.º 18
0
        response = {
            'humidity':
            float("{0:.2f}".format(self.humidity)),
            'temperature':
            float("{0:.2f}".format(self.temperature)),
            'dewpoint':
            float("{0:.2f}".format(dewpoint(self.temperature, self.humidity))),
            'crc_check':
            self.crc_check
        }
        return response

    def stopSensor(self):
        self.running = False


if __name__ == "__main__":
    if GPIO.RPI_INFO['P1_REVISION'] in [2, 3]:
        I2C_bus_number = 1
    else:
        I2C_bus_number = 0
    am2315 = AM2315_read(I2C_bus_number)

    for measurements in am2315:
        print("Temperature: {}".format(measurements['temperature']))
        print("Humidity: {}".format(measurements['humidity']))
        print("Dew Point: {}".format(
            dewpoint(measurements['temperature'], measurements['humidity'])))
        print("CRC Check: {}".format(measurements['crc_check']))
        time.sleep(4)
Ejemplo n.º 19
0
    def next(self):
        """
        Call the read method and return temperature and humidity information.
        """
        if self.read():
            return None
        response = {
            'humidity': float("{0:.2f}".format(self.humidity)),
            'temperature': float("{0:.2f}".format(self.temperature)),
            'dewpoint': float("{0:.2f}".format(dewpoint(self.temperature, self.humidity)))
        }
        return response

    def stopSensor(self):
        self.running = False


if __name__ == "__main__":
    if GPIO.RPI_INFO['P1_REVISION'] in [2, 3]:
        I2C_bus_number = 1
    else:
        I2C_bus_number = 0
    htu21d = HTU21D_read(I2C_bus_number)

    for measurements in htu21d:
        print("Temperature: {}".format(measurements['temperature']))
        print("Humidity: {}".format(measurements['humidity']))
        print("Dew Point: {}".format(dewpoint(measurements['temperature'], measurements['humidity'])))
        time.sleep(1)