def sample(self): try: I2C.start_tx(self.__addr) temp_msb, temp_lsb, _, humid_msb, humid_lsb, _ = I2C.read_cmd16( SHT31.__CMD_READ_SINGLE_HIGH, 6) raw_humid = (humid_msb << 8) | humid_lsb raw_temp = (temp_msb << 8) | temp_lsb return SHTDatum(SHT31.humid(raw_humid), SHT31.temp(raw_temp)) finally: I2C.end_tx()
def sample(self): if self.__addr == 0: return None try: I2C.start_tx(self.__addr) temp_msb, temp_lsb, _, humid_msb, humid_lsb, _ = I2C.read_cmd16(SHT31.__CMD_READ_SINGLE_HIGH, 6, wait=self.MEASUREMENT_DURATION) raw_humid = (humid_msb << 8) | humid_lsb raw_temp = (temp_msb << 8) | temp_lsb return SHTDatum(SHT31.humid(raw_humid), SHT31.temp(raw_temp)) finally: I2C.end_tx()
def construct_from_jdict(cls, jdict): if not jdict: return None source = jdict.get('src') rec = LocalizedDatetime.construct_from_jdict(jdict.get('rec')) period = jdict.get('per') pm1 = jdict.get('pm1') pm2p5 = jdict.get('pm2p5') pm10 = jdict.get('pm10') bins = jdict.get('bin') if bins is None: print("OPCDatum incomplete: %s" % jdict, file=sys.stderr) sys.stderr.flush() return None bin_1_mtof = jdict.get('mtf1') bin_3_mtof = jdict.get('mtf3') bin_5_mtof = jdict.get('mtf5') bin_7_mtof = jdict.get('mtf7') sfr = jdict.get('sfr') sht = SHTDatum.construct_from_jdict(jdict.get('sht')) return OPCDatum(source, rec, pm1, pm2p5, pm10, period, bins, bin_1_mtof, bin_3_mtof, bin_5_mtof, bin_7_mtof, sfr=sfr, sht=sht)
def null_datum(cls): return SHTDatum(None, None)
#!/usr/bin/env python3 """ Created on 18 Sep 2016 @author: Bruno Beloff ([email protected]) """ from scs_core.climate.sht_datum import SHTDatum from scs_core.data.json import JSONify # -------------------------------------------------------------------------------------------------------------------- sht = SHTDatum(54.3, 23.4) print(sht) print("-") jstr = JSONify.dumps(sht) print(jstr) print("-")
def sample(self): try: self.obtain_lock() self._spi.open() # command... self.__cmd(self.__CMD_READ_HISTOGRAM) chars = self.__read_bytes(86) # checksum... required = Decode.unsigned_int(chars[84:86], '<') actual = ModbusCRC.compute(chars[:84]) if required != actual: raise ValueError( "bad checksum: required: 0x%04x actual: 0x%04x" % (required, actual)) # time... rec = LocalizedDatetime.now() # bins... bins = [ Decode.unsigned_int(chars[i:i + 2], '<') for i in range(0, 48, 2) ] # bin MToFs... bin_1_mtof = chars[48] bin_3_mtof = chars[49] bin_5_mtof = chars[50] bin_7_mtof = chars[51] # period... raw_period = Decode.unsigned_int(chars[52:54], '<') period = round(float(raw_period) / 100.0, 3) # temperature & humidity raw_temp = Decode.unsigned_int(chars[56:58], '<') raw_humid = Decode.unsigned_int(chars[58:60], '<') sht = SHTDatum(SHT31.humid(raw_humid), SHT31.temp(raw_temp)) # PMx... try: pm1 = Decode.float(chars[60:64], '<') except TypeError: pm1 = None try: pm2p5 = Decode.float(chars[64:68], '<') except TypeError: pm2p5 = None try: pm10 = Decode.float(chars[68:72], '<') except TypeError: pm10 = None return OPCDatum(self.SOURCE, rec, pm1, pm2p5, pm10, period, bins, bin_1_mtof, bin_3_mtof, bin_5_mtof, bin_7_mtof, sht) finally: self._spi.close() self.release_lock()