def read(self): try: time.sleep(1) self.bus = smbus.SMBus(self.i2c_bus) adc = MCP342x(self.bus, self.i2c_address, channel=self.channel, gain=self.gain, resolution=self.resolution) self._voltage = adc.convert_and_read() except: return 1
def read(self): try: time.sleep(0.1) adc = MCP342x(self.bus, self.i2c_address, channel=self.channel - 1, gain=self.gain, resolution=self.resolution) response = adc.convert_and_read() return 1, response except Exception as msg: return 0, "Fail: {}".format(msg)
def read(self): """ Take a measurement """ try: time.sleep(1) adc = MCP342x(self.bus, self.i2c_address, channel=self.channel, gain=self.gain, resolution=self.resolution) self._voltage = adc.convert_and_read() except Exception as e: logger.error("{cls} raised exception during read(): " "{err}".format(cls=type(self).__name__, err=e)) return 1
def __init__(self, address, bus, channel, gain, resolution): self.logger = logging.getLogger('mycodo.mcp342x-{bus}-{add}'.format( bus=bus, add=address)) self._voltage = None self.i2c_address = address self.bus = smbus.SMBus(bus) self.channel = channel self.gain = gain self.resolution = resolution self.adc = MCP342x(self.bus, self.i2c_address, channel=self.channel, gain=self.gain, resolution=self.resolution)
def get_sample(): t = time.time() adc_list = [] for comp in ('x', 'y', 'z'): if comp in adc_devices: adc_list.append(adc_devices[comp]) if comp + '_ref' in adc_devices: adc_list.append(adc_devices[comp + '_ref']) mag_agg = get_aggregate_function(config, 'daemon', 'aggregate') md = MCP342x.convert_and_read_many(adc_list, samples=config.getint('daemon', 'oversampling')) #aggregate=mag_agg) r = {'sample_time': t} if 'sensor_temperature' in adc_devices: temp_adc = adc_devices['sensor_temperature'] temp_oversampling = config.getint('daemon', 'sensor_temperature_oversampling') temp_agg = get_aggregate_function(config, 'daemon', 'sensor_temperature_aggregate') r['sensor_temperature'] = \ temp_data = temp_adc.convert_and_read(samples=temp_oversampling, aggregate=temp_agg) n = 0 for comp in ('x', 'y', 'z'): if comp in adc_devices: r[comp + '_all_samples'] = md[n] r[comp] = mag_agg(md[n]) n += 1 if comp + '_ref' in adc_devices: for m in range(len(md[n])): r[comp + '_all_samples'][m] -= md[n][m] r[comp] -= mag_agg(md[n]) n += 1 # Take CPU temperature as system temperature r['system_temperature'] = np.NaN with open('/sys/class/thermal/thermal_zone0/temp') as f: r['system_temperature'] = float(f.read().strip())/1000 return r
def __init__(self, input_dev, testing=False): self.logger = logging.getLogger('mycodo.mcp342x') self._voltage = None self.i2c_address = int(str(input_dev.location), 16) self.i2c_bus = input_dev.i2c_bus self.adc_channel = input_dev.adc_channel self.adc_gain = input_dev.adc_gain self.adc_resolution = input_dev.adc_resolution if not testing: self.logger = logging.getLogger( 'mycodo.mcp342x_{id}'.format(id=input_dev.id)) self.bus = smbus.SMBus(self.i2c_bus) self.adc = MCP342x(self.bus, self.i2c_address, channel=self.adc_channel, gain=self.adc_gain, resolution=self.adc_resolution)
def __init__(self, input_dev, testing=False): self.logger = logging.getLogger('mycodo.mcp342x') self.acquiring_measurement = False self._voltage = None self.i2c_address = int(str(input_dev.location), 16) self.i2c_bus = input_dev.i2c_bus self.adc_channel = input_dev.adc_channel self.adc_gain = input_dev.adc_gain self.adc_resolution = input_dev.adc_resolution if not testing: from smbus2 import SMBus from MCP342x import MCP342x self.logger = logging.getLogger( 'mycodo.mcp342x_{id}'.format(id=input_dev.unique_id.split('-')[0])) self.bus = SMBus(self.i2c_bus) self.adc = MCP342x(self.bus, self.i2c_address, channel=self.adc_channel, gain=self.adc_gain, resolution=self.adc_resolution)
for bus in glob.glob(prefix + '*'): try: n = int(bus.replace(prefix, '')) candidates.append(n) except: pass if len(candidates) == 1: return smbus.SMBus(candidates[0]) elif len(candidates) == 0: raise Exception("Could not find an I2C bus") else: raise Exception("Multiple I2C busses found") bus = get_smbus() # Create objects for each signal to be sampled # get i2c addr by `sudo i2cdetect -y 1` addr68_ch0 = MCP342x(bus, addr, channel=0, resolution=18) addr68_ch1 = MCP342x(bus, 0x68, channel=1, resolution=16) addr68_ch2 = MCP342x(bus, 0x68, channel=2, resolution=14) addr68_ch3 = MCP342x(bus, 0x68, channel=3, resolution=12) # Create a list of all the objects. They will be sampled in this # order, unless any later objects can be sampled can be moved earlier # for simultaneous sampling. adcs = [addr68_ch0, addr68_ch1, addr68_ch2, addr68_ch3] r = MCP342x.convert_and_read_many(adcs, samples=3) print('return values: ') print(r)
from MCP342x import MCP342x import smbus import logging bus = smbus.SMBus(1) addr68_ch0 = MCP342x(bus, 0x6e, channel=0, resolution=12) addr68_ch1 = MCP342x(bus, 0x6e, channel=1, resolution=12) addr68_ch2 = MCP342x(bus, 0x6e, channel=2, resolution=12) addr68_ch3 = MCP342x(bus, 0x6e, channel=3, resolution=12) adcs = [addr68_ch0, addr68_ch1, addr68_ch2, addr68_ch3] r = MCP342x.convert_and_read_many(adcs, samples=1) print('return values: ') print(r)
except: pass if len(candidates) == 1: return smbus.SMBus(candidates[0]) elif len(candidates) == 0: raise Exception("Could not find an I2C bus") else: raise Exception("Multiple I2C busses found") #logging.basicConfig(level='DEBUG') logger = logging.getLogger(__name__) bus = get_smbus() # Create objects for each signal to be sampled addr68_ch0 = MCP342x(bus, 0x68, channel=0, resolution=12) addr68_ch1 = MCP342x(bus, 0x68, channel=1, resolution=12) # Create a list of all the objects. They will be sampled in this # order, unless any later objects can be sampled can be moved earlier # for simultaneous sampling. adcs = [addr68_ch0, addr68_ch1] r = MCP342x.convert_and_read_many(adcs, samples=2) print('return values: ') print(r) print(addr68_ch1.convert_and_read(scale_factor=3.01942319011))
from machine import Pin, I2C from MCP342x import MCP342x import time sensor_type = "None" try: i2c = I2C(0, I2C.MASTER, pins=('P22', 'P21')) addr68_ch0 = MCP342x(i2c, 0x68, channel=0, resolution=18, gain=8, scale_factor=1000.0) addr68_ch1 = MCP342x(i2c, 0x68, channel=1, resolution=18, gain=8, scale_factor=1000.0) addr68_ch2 = MCP342x(i2c, 0x68, channel=2, resolution=18, gain=8, scale_factor=1000.0) addr68_ch3 = MCP342x(i2c, 0x68, channel=3, resolution=18, gain=8, scale_factor=1000.0) time.sleep(1) print('Ready to read ADC') except Exception as error: print(error) pass while True: adc_values = [-1.0, -1.0, -1.0, -1.0] try: adc_values[0] = addr68_ch0.convert_and_read() adc_values[1] = addr68_ch1.convert_and_read() adc_values[2] = addr68_ch2.convert_and_read() adc_values[3] = addr68_ch3.convert_and_read() except Exception as error:
def do_measurements(): if my_config_dict['node_version'] == 0x02: try: addr68_ch0 = MCP342x(i2c_irt, 0x68, channel=0, resolution=18, gain=8, scale_factor=1000.0) addr68_ch1 = MCP342x(i2c_irt, 0x68, channel=1, resolution=18, gain=8, scale_factor=1000.0) addr68_ch2 = MCP342x(i2c_irt, 0x68, channel=2, resolution=18, gain=8, scale_factor=1000.0) addr68_ch3 = MCP342x(i2c_irt, 0x68, channel=3, resolution=18, gain=8, scale_factor=1000.0) float_values[0] = addr68_ch0.convert_and_read() float_values[1] = addr68_ch1.convert_and_read() float_values[2] = addr68_ch2.convert_and_read() float_values[3] = addr68_ch3.convert_and_read() float_values[4] = float_values[0] * 5.0 float_values[5] = 0.0 except Exception as ex: rgb_blink(100, 0x13f2ab) print("Couldn't find ADC") print(ex) float_values[0] = -1.0 float_values[1] = -1.0 float_values[2] = -1.0 float_values[3] = -1.0 float_values[4] = -1.0 float_values[5] = 0.0 else: print("Waking up I2C sensors...") ### IRT Sensor irt = None try: irt = MLX90614(i2c_irt, 90) print("Waking IRT...OK") time.sleep(1) float_values[0] = irt.read_ambient_temp() float_values[1] = irt.read_object_temp() rgb_blink(100, 0xa013f2) except Exception as error: red_blink(1000) irt = None print("Couldn't find IRT") float_values[0] = -1.0 float_values[1] = -1.0 wdt.feed() ### Air sensor if my_config_dict["air_sensor"] == NONE: print("Ignoring air sensor...") float_values[2] = 0.0 float_values[3] = 0.0 float_values[4] = 0.0 elif my_config_dict["air_sensor"] == BME280: bme = None try: bme = BME280(address=BME280_I2CADDR, i2c=i2c_air) print("Waking BME280...OK") time.sleep(1) float_values[2] = bme.read_temperature() / 100.0 float_values[3] = bme.read_humidity() / 1024.0 float_values[4] = bme.read_pressure() / 256.0 / 100.0 rgb_blink(100, 0x0000ff) except Exception as error: red_blink(1000) print("Couldn't find BME") float_values[2] = -1.0 float_values[3] = -1.0 float_values[4] = -1.0 elif my_config_dict["air_sensor"] == SHT3x: sht30 = None try: sht30 = SHT30(i2c_air) print("Waking SHT3x...OK") time.sleep(1) float_values[2], float_values[3] = sht30.measure() float_values[4] = 0.0 green_blink(100) except Exception as error: red_blink(1000) print("Couldn't find SHT30") float_values[2] = -1.0 float_values[3] = -1.0 float_values[4] = 0.0 elif my_config_dict["air_sensor"] == SHT3x_single: sht30 = None try: sht30 = SHT30(i2c_air, i2c_address=0x44) print("Waking SHT3x...OK") time.sleep(1) float_values[2], float_values[3] = sht30.measure() float_values[4] = 0.0 green_blink(100) except Exception as error: rgb_blink(100, 0x13f2ab) print("Couldn't find SHT30") float_values[2] = -1.0 float_values[3] = -1.0 float_values[4] = 0.0 else: print("Sensor not supported") wdt.feed() ### Soil sensor temp = None try: ow_devices = ow.scan() if len(ow_devices) > 0: print("Waking OWD...OK") if len(ow_devices) > 0: ow_id = ubinascii.hexlify(ow_devices[0]).decode('ascii') else: ow_id = "None" print("OW ID: {}".format(ow_id)) temp = DS18X20(ow) time.sleep(1) temp.start_convertion() time.sleep(1) float_values[5] = temp.read_temp_async() if float_values[5] is None: float_values[5] = -100 rgb_blink(100, 0xf2b313) else: print("Soil temp not found") red_blink(1000) float_values[5] = -1.0 #time.sleep(1) except Exception as error: print(error) red_blink(1000) print("Couldn't find OWD") float_values[5] = -100.0 wdt.feed() # Battery sensing adc = machine.ADC(0) batt = adc.channel(pin='P16', attn=3) float_values[6] = (batt.value() / 4096.0) * 354.8 / 31.6 print("Battery: {}V".format(float_values[6]))