def read_hdc1008(addr=DEVICE): bus = smbus.SMBus(get_smbus()) # set config register bus.write_byte_data(addr, 0x02, 0x00) time.sleep(0.015) # From the data sheet # read temperature bus.write_byte(addr, 0x00) time.sleep(0.0625) # From the data sheet # read temp data data0 = bus.read_byte(addr) data1 = bus.read_byte(addr) temp = ((((data0 << 8) + data1) / 65536.0) * 165.0) - 40.0 time.sleep(0.015) # From the data sheet # read humidity bus.write_byte(addr, 0x01) time.sleep(0.0625) # From the data sheet data0 = bus.read_byte(addr) data1 = bus.read_byte(addr) humid = (((data0 << 8) + data1) / 65536.0) * 100.0 return (temp, humid)
def isSMBusConnected(): try: bus = smbus.SMBus(get_smbus()) return 1 except Exception as ex: pass #print("No BME680 connected: " + str(ex)) return 0
def measure_voltage(ts_sensor): fields = {} # I2C-address of YL-40 PCF8591 address = 0x48 try: # Create I2C instance and open the bus PCF8591 = smbus.SMBus(get_smbus()) # read pin from ts_sensor settings if 'pin' in ts_sensor and ts_sensor['pin'] is not None: pin = int(ts_sensor['pin']) else: pin = 2 # AIN0 => Pin 0 # AIN1 => Pin 1 # AIN2 => Pin 2 (default) # AIN3 => Pin 3 #factor = 0.09765625 # 25V/256 (=5V analog output signal) factor = 16.5 / 256 # 16.5V/256 | 16.5V max voltage for 0xff (=3.3V analog output signal) if 'I2CVoltage' in ts_sensor and ts_sensor['I2CVoltage'] is not None: # convert 8 bit number to voltage factor = float(ts_sensor['I2CVoltage']) / 256 PCF8591.write_byte(address, 0x40 + pin) # set channel to AIN0, AIN1, AIN2 or AIN3 # measure once just for fun _v = PCF8591.read_byte(address) Voltage_8bit = PCF8591.read_byte(address) # = i2cget -y 1 0x48 voltage = Voltage_8bit * factor # convert 8 bit number to voltage if 'ts_field' in ts_sensor and isinstance(voltage, (int, float)): fields[ts_sensor["ts_field"]] = round(voltage, 4) return fields except Exception as e: error_log( e, 'Error while reading PCF8591 (Voltage). Is the Sensor connected?') return None
def read_aht10(addr=DEVICE): bus = smbus.SMBus(get_smbus()) # init & read config = [0x08, 0x00] MeasureCmd = [0x33, 0x00] bus.write_i2c_block_data(DEVICE, 0xE1, config) time.sleep(0.5) byt = bus.read_byte(addr) bus.write_i2c_block_data(addr, 0xAC, MeasureCmd) time.sleep(0.5) data = bus.read_i2c_block_data(addr,0x00) # Data temp = ((data[3] & 0x0F) << 16) | (data[4] << 8) | data[5] ctemp = ((temp*200) / 1048576) - 50 tmp = ((data[1] << 16) | (data[2] << 8) | data[3]) >> 4 ctmp = int(tmp * 100 / 1048576) return (ctemp, ctmp)
def read_sht31(addr=DEVICE): # Get I2C bus bus = smbus.SMBus(get_smbus()) # Send measurement command, 0x2C(44) # 0x06(06) High repeatability measurement bus.write_i2c_block_data(addr, 0x2C, [0x06]) time.sleep(0.5) # Read data back from 0x00(00), 6 bytes # Temp MSB, Temp LSB, Temp CRC, Humididty MSB, Humidity LSB, Humidity CRC data = bus.read_i2c_block_data(addr, 0x00, 6) # Convert the data temp = data[0] * 256 + data[1] cTemp = -45 + (175 * temp / 65535.0) fTemp = -49 + (315 * temp / 65535.0) humidity = 100 * (data[3] * 256 + data[4]) / 65535.0 return (cTemp, humidity)
def get_raw_voltage(ts_sensor): # I2C-address of YL-40 PCF8591 address = 0x48 try: # Create I2C instance and open the bus PCF8591 = smbus.SMBus(get_smbus()) # read pin from ts_sensor settings if 'pin' in ts_sensor: pin = int(ts_sensor['pin']) else: pin = 2 # AIN0 => Pin 0 # AIN1 => Pin 1 # AIN2 => Pin 2 (default) # AIN3 => Pin 3 #factor = 0.09765625 # 25V/256 (=5V analog output signal) factor = 16.5 / 256 # 16.5V/256 | 16.5V max voltage for 0xff (=3.3V analog output signal) if 'I2CVoltage' in ts_sensor and ts_sensor['I2CVoltage'] is not None: # convert 8 bit number to voltage factor = float(ts_sensor['I2CVoltage']) / 256 PCF8591.write_byte(address, 0x40 + pin) # set channel to AIN0, AIN1, AIN2 or AIN3 Voltage_8bit = PCF8591.read_byte(address) # = i2cget -y 1 0x48 voltage = Voltage_8bit * factor # convert 8 bit number to voltage if isinstance(voltage, (int, float)): voltage = round(voltage, 4) return voltage except Exception as e: print("Exception in get_raw_voltage: " + str(e)) return None
def _measure_all(): address = 0x5e #I2C-address of EE-895 try: # Create I2C instance and open the bus EE895 = smbus.SMBus(get_smbus()) _c = EE895.read_word_data(address, 0x0) # co2 _t = EE895.read_word_data(address, 0x2) # temp _p = EE895.read_word_data(address, 0x6) # pressure co = _switchBit(_c) temp = _switchBit(_t) / 100 pressure = _switchBit(_p) / 10 return co, temp, pressure except Exception as e: error_log(e, 'Error: Error while reading EE895 Sensor. Is it connected?') return None, None, None
def measure(pin): # I2C-addr of YL-40 PCF8591 i2c_addr = 0x48 try: # Create I2C instance and open the bus PCF8591 = smbus.SMBus(get_smbus()) # set channel to AIN0, AIN1, AIN2 or AIN3 PCF8591.write_byte(i2c_addr, 0x40+pin) # i2cget -y 1 0x48 data_8bit = PCF8591.read_byte(i2c_addr) logger.debug("PCF8591 PIN: AIN" + str(pin) + " measureed raw data: " + str(data_8bit)) if isinstance(data_8bit, (int, float)): return data_8bit except IOError as ex: if str(ex) == "[Errno 121] Remote I/O error": logger.error("Reading PCF8591 failed: Most likely I2C Bus needs a reboot.") except Exception as ex: logger.exception("Unhaldled exception measure") return None
# https://www.raspberrypi-spy.co.uk/ # #-------------------------------------- import smbus import time from ctypes import c_short from ctypes import c_byte from ctypes import c_ubyte from sensors.sensor_utilities import get_smbus import logging logger = logging.getLogger('HoneyPi.bme280') DEVICE = 0x76 # Default device I2C address bus = smbus.SMBus(get_smbus()) def getShort(data, index): # return two bytes from data as a signed 16-bit value return c_short((data[index + 1] << 8) + data[index]).value def getUShort(data, index): # return two bytes from data as an unsigned 16-bit value return (data[index + 1] << 8) + data[index] def getChar(data, index): # return one byte from data as a signed char result = data[index]