def get_probe_temp(): """ Get the temperature from the ds18b20 probe """ pin = machine.Pin(4) sensor = ds18x20.DS18X20(onewire.OneWire(pin)) rom = sensor.scan().pop() # We only have one ds18b20 probe, just pop the first one off of the list sensor.convert_temp() return sensor.read_temp(rom) * 1.8 + 32
def __init__(self, *args, **kwargs): print("Initializing DS18x20 temperature sensor") super().__init__(*args, **kwargs) self.dallas = ds18x20.DS18X20(onewire.OneWire(machine.Pin(self.settings["pin"]))) self.sensors = self.dallas.scan() print('Found temperature sensors: ', self.sensors)
def read_temp(gpio): print('read all temp sensors on gpio ', gpio) try: dallas_power_pin.on() # power sensor sleep_ms(300) import onewire, ds18x20 # the device is on GPIOx dat = Pin(gpio) # create the onewire object ds = ds18x20.DS18X20(onewire.OneWire(dat)) # scan for devices on the bus roms = ds.scan() print('ds18b20 scan, found devices:', roms) temp = [] ds.convert_temp() sleep_ms(750) for rom in roms: # read all sensors on this gpio bus t = ds.read_temp(rom) t = round(t, 1) print('dallas: ', t) temp.append(t) dallas_power_pin.off() return (temp) except Exception as e: print('exception reading dallas ', str(e)) return ([])
def __init__(self, gpio_n): self.ow = onewire.OneWire(Pin(gpio_n)) # create a OneWire bus on pin self.ow.scan() # return a list of devices on the bus self.ow.reset() # reset the bus self.ds = ds18x20.DS18X20(self.ow) self.t = 0 self.m = True
def __init__(self, pin, rom): self.pin = machine.Pin(pin) self.ow = onewire.OneWire(self.pin) self.rom = rom self.buf = bytearray(9) self.set_resolution(12)
def init(self, plugin, device, queue, scriptqueue): self._log.debug("Plugin: ds18 init") # generic section self._utils.plugin_initdata(self, plugin, device, queue, scriptqueue) self.pincnt = pincnt self.valuecnt = valuecnt self.stype = stype self.content = plugin.get('content',content) self.dxpin = device.get('dxpin',dxpin) self._log.debug("Plugin: ds18 init dxpin"+self.dxpin) self.valuenames['devicename'] = device['name'] # gets device/plugin name, added AJ plugin['dtype'] = dtype plugin['stype'] = stype plugin['template'] = template datastore = self._plugins.readstore(device["name"]) # plugin specific section self._log.debug("Plugin: ds18 init, pin used: "+str(self.dxpin)) # the device is on Dx self.mPin = self._hal.pin(self.dxpin) if self.mPin: # create the onewire object self.ds18 = ds18x20.DS18X20(onewire.OneWire(self.mPin)) # scan for devices on the bus roms = self.ds18.scan() # scan for devices on the bus self.roms = self.ds18.scan() return True
def serve(): bme = bme280.BME280(i2c=i2c) # create the onewire object ds = ds18x20.DS18X20(onewire.OneWire(Pin(2))) # the ds18b20 is on GPIO4 # mqtt client client_id = CONFIG['MQTT_CLIENT_ID_PREFIX'] + ubinascii.hexlify( machine.unique_id()).decode('utf-8') password = ubinascii.a2b_base64(CONFIG['MQTT_PASSWORD']) client = MQTTClient(client_id, CONFIG['MQTT_BROKER'], user=CONFIG['MQTT_USER'], password=password, port=CONFIG['MQTT_PORT']) client.connect() print("'%s' is connected to '%s'" % (client_id, CONFIG['MQTT_BROKER'])) while True: temp = bme.temperature hum = bme.humidity pres = bme.pressure temps = ds18b20.read_temperatures(ds) # publish the values client.publish("%s.temp_c" % client_id, temp) client.publish("%s.humidity_rh" % client_id, hum) client.publish("%s.pressure_hpa" % client_id, pres) for serial_nbr, temp in temps.items(): client.publish("%s.%s.temp_c" % (client_id, serial_nbr), '%0.5f' % temp) sleep(300)
def __init__(self, onewire): self.counter = 0 dat = machine.Pin(14) self.ow = onewire.OneWire(dat) self.ow.pin.init(self.ow.pin.OPEN_DRAIN, None) self.crc16 = bytearray(2) self.roms = []
def plugin_init(self, enableplugin=None): plugin.PluginProto.plugin_init(self, enableplugin) self.initialized = False try: self._ds = ds18x20.DS18X20( onewire.OneWire(Pin(int(self.taskdevicepin[0])))) misc.addLog(pglobals.LOG_LEVEL_DEBUG, "Dallas bus initialized!") self.initialized = True except Exception as e: self._ds = None misc.addLog(pglobals.LOG_LEVEL_ERROR, "Dallas device can not be initialized!") if str(self.taskdevicepluginconfig[0]) == "0" or str( self.taskdevicepluginconfig[0]).strip() == "": self.initialized = False if self.enabled and enableplugin: misc.addLog(pglobals.LOG_LEVEL_INFO, "Dallas device not selected!") if self.initialized: self.ports = self.taskdevicepluginconfig[0] self._roms = self.find_dsb_devices() for rom in self._roms: if self.addresstostr(rom) == self.taskdevicepluginconfig[0]: self._arom = rom break else: self.ports = "" self.readinprogress = 0
def __init__( self, pin, precision_temp=2, precision_humid=1, # extend or shrink according to your sensor offset_temp=0, offset_humid=0, # also here interval=None, mqtt_topic=None): interval = interval or config.INTERVAL_SEND_SENSOR self.topic = mqtt_topic or mqtt.getDeviceTopic(component_name) ############################## # adapt to your sensor by extending/removing unneeded values like in the constructor arguments self._prec_temp = int(precision_temp) self._prec_humid = int(precision_humid) ### self._offs_temp = float(offset_temp) self._offs_humid = float(offset_humid) ############################## # create sensor object if type(pin) == str: pin = config.pins[pin] super().__init__(onewire.OneWire(machine.Pin(pin))) ############################## # choose a background loop that periodically reads the values and publishes it # (function is created below) background_loop = self.temperature ############################## gc.collect() asyncio.get_event_loop().create_task( self._loop(background_loop, interval))
def saveform(self,plugindata): self._log.debug("Plugin: ds18 saveform") # generic section self._utils.plugin_saveform(self, plugindata) # plugin specific section self.romid = plugindata.get('deviceid','') self.resolution = plugindata['resolution'] # store values data = {} data["romid"] = self.romid data["dxpin"] = self.dxpin data["resolution"] = self.resolution data["valueN1"] = self.valuenames["valueN1"] data["valueF1"] = self.valuenames["valueF1"] data["valueD1"] = self.valuenames["valueD1"] self._plugins.writestore(self.devicename, data) # the device is on Dx self.mPin = self._hal.pin(self.dxpin) self._log.debug("Plugin: ds18 saveform, pin used: "+str(self.dxpin)) # create the onewire object self.ds18 = ds18x20.DS18X20(onewire.OneWire(self.mPin)) # scan for devices on the bus roms = self.ds18.scan() # scan for devices on the bus self.roms = self.ds18.scan()
def etemp(self): if not self._etemp: _logger.debug("Initializing external temp sensor") import onewire onewire_bus = onewire.OneWire(Pin(self._onewire_pin_name)) self._etemp = onewire.DS18X20(onewire_bus) return self._etemp
def read_temp(): ds = ds18x20.DS18X20(onewire.OneWire(Pin(5))) roms = ds.scan() ds.convert_temp() sleep_ms(100) # assume there is only 1 probe return ds.read_temp(roms[0])
def __init__(self,sensorPin = 22,hBrigde1=5,hBridge2=18): #define pins for the H-Bridge (which will be controlling a peltier) self.HBPin1 = machine.Pin(hBrigde1,machine.Pin.OUT) self.HBPin2 = machine.Pin(hBridge2,machine.Pin.OUT) #make both pins go low self.HBPin1.value(0) self.HBPin2.value(0) #define pin for the temperature sensors (DS18B20+) # the device is on GPIO22 # create the onewire object self.ow = onewire.OneWire(machine.Pin(sensorPin)) self.ds = ds18x20.DS18X20(self.ow) #scan devices on bus self.roms = self.ds.scan() self.temps = [0]*len(self.roms) print('found devices:', self.roms) #temperature sensor1 self.ts1 = self.rom[0] #temperature sensor2 self.ts2 = self.rom[1]
def call(self, chronos): import time import machine import onewire, ds18x20 # the device is on GPIO14 dat = machine.Pin(14) temperature = -1 # create the onewire object ds = ds18x20.DS18X20(onewire.OneWire(dat)) # scan for devices on the bus roms = ds.scan() # loop 10 times and print all temperatures #for i in range(10): #print('temperatures:', end=' ') ds.convert_temp() time.sleep_ms(750) for rom in roms: #print(ds.read_temp(rom), end=' ') temperature = ds.read_temp(rom) #print() data = {'temperature_C': temperature} return data
def __init__(self): self.data = {} self.config = { "ssid": "OpenWRT", "password": "******", "ds_pin": 12, "mqtt": { "topic": "esp", "qos": 1, "retain": False } } try: with open('config.json', 'rt') as fp: self.config.update(json.load(fp)) except Exception as e: print(e) print("Unable to read config.json, use default parameters") pass self.ds_sensor = ds18x20.DS18X20( onewire.OneWire(Pin(self.config['ds_pin'])) ) mqtt_con = mqtt_as.config.copy() mqtt_con.update(self.config['mqtt']) n = network.WLAN(network.STA_IF) mac = n.config('mac') mqtt_con['client_id'] = 'ESP_'+bytes_to_hex(mac) mqtt_con['connect_coro'] = self.on_mqtt_connect self.mqtt = mqtt_as.MQTTClient(mqtt_con) pass
def measure_temp(): ow = onewire.OneWire(Pin(DS18B20_DATA_PIN)) ds = ds18x20.DS18X20(ow) roms = ds.scan() ds.convert_temp() time.sleep_ms(750) return ds.read_temp(roms[0])
def __init__(self, pin, rom: str = None, auto_detect=False, precision_temp: int = 2, offset_temp: float = 0, friendly_name=None, **kwargs): """ Class for a single ds18 unit to provide an interface to a single unit. Alternatively it can be used to automatically detect all connected units and create objects for those units. :param pin: pin number/name/object :param rom: optional, ROM of the specific DS18 unit, can be string or bytearray (in json bytearray not possible). If not given then the first found ds18 unit will be used, no matter the ROM. Makes it possible to have a generic ds18 unit. :param auto_detect: optional, if true and ROM is None then all connected ds18 units will automatically generate a sensor object with the given options. :param precision_temp: the precision to for returning/publishing values :param offset_temp: temperature offset to adjust bad sensor readings :param friendly_name: friendly name in homeassistant. Has no effect if rom is None and auto_detect True """ if rom is None and auto_detect: # only a dummy sensor for detecting connected sensors interval_reading = kwargs[ "interval_reading"] if "interval_reading" in kwargs else None interval_publish = kwargs[ "interval_publish"] if "interval_publish" in kwargs else None self._instances = {} # rom:object self._auto_detect = True self._kwargs = kwargs # store kwargs for initialization of detected sensors kwargs[ "interval_reading"] = 60 # scan every 60 seconds for new units kwargs["interval_publish"] = -1 global _unit_index _unit_index += 1 super().__init__(COMPONENT_NAME, __version__, _unit_index, logger=_log, **kwargs) self.rom: str = rom self._generic = True if rom is None and not auto_detect else False if type(pin) == ds18x20.DS18X20: self.sensor: ds18x20.DS18X20 = pin else: self._pins[pin] = ds18x20.DS18X20(onewire.OneWire(Pin(pin))) self.sensor: ds18x20.DS18X20 = self._pins[pin] self._last_conv[self.sensor] = None if rom or not auto_detect: # sensor with rom or generic sensor self._addSensorType(SENSOR_TEMPERATURE, precision_temp, offset_temp, VALUE_TEMPLATE_FLOAT, "°C", friendly_name) self._auto_detect = False elif self._auto_detect: self._kwargs["interval_reading"] = interval_reading self._kwargs["interval_publish"] = interval_publish self._kwargs["precision_temp"] = precision_temp self._kwargs["offset_temp"] = offset_temp gc.collect()
def __init__(self, blynk, ow_pin, i2c_pins, period): self.blynk = blynk self.ds18b20 = onewire.DS18X20(onewire.OneWire(Pin(ow_pin))) i2c = I2C(baudrate=100000, pins=i2c_pins) self.bh1750fvi = BH1750FVI(i2c, BH1750FVI_ADDR, period) self.ds_state = 'CONV' self.lux = 0 self.enabled = True
def setup_DS(): roms_list = [] while len(roms_list) == 0: ds_object = ds18x20.DS18X20( onewire.OneWire(machine.Pin(CONFIG.get('THERMO_SENSOR_PIN')))) roms_list = ds_object.scan() print('found devices: ', roms_list) return ds_object, roms_list
def init(config): therm_config = config['onewire'] pin = therm_config['pin'] ow = onewire.OneWire(Pin(pin)) ds = ds18x20.DS18X20(ow) rom = ds.scan() # only handles 1 thermometer right now return ds, rom[0]
def __init__(self): self._s_main = None self._s_temp = None self._s_main_connected = False self._s_temp_connected = False self._state = {} self._s_main = dht.DHT22(PIN_S_MAIN) self._s_temp = ds18x20.DS18X20(onewire.OneWire(PIN_S_TEMP))
def set_sensor(): dat = machine.Pin(2) ow = onewire.OneWire(dat) global ds ds = ds18x20.DS18X20(ow) global roms roms = ds.scan() print('found devices:', roms)
def setup(): ds_pin = Pin(ds18_pin) ds_sensor = ds18x20.DS18X20(onewire.OneWire(ds_pin)) roms = ds_sensor.scan() print('Found DS devices: ', roms) show_sensors(roms) return ds_sensor, roms
def __init__(self, pin): """ Initialize the DS18 temperature sensor :param pin: int; GPIO for OneWire """ self.ow = onewire.OneWire(machine.Pin(pin)) self.ds = ds18x20.DS18X20(self.ow) self.device_list = None self.last_reading_available = False
def read_temps(): """Read DS18B20's.""" dat = machine.Pin(PIN_1W) ds = ds18x20.DS18X20(onewire.OneWire(dat)) ds.convert_temp() time.sleep_ms(750) return {rom_to_hex(rom): ds.read_temp(rom) for rom in ds.scan()}
def showTempDS(tm): ds = ds18x20.DS18X20(onewire.OneWire(machine.Pin(Wemos.D2))) roms = ds.scan() if len(roms) > 0: ds.convert_temp() tempDS = ds.read_temp(roms[0]) tm.write_int(0x8000000000000000) number_helper.show2FiguresNumberX3(tm, tempDS) return tempDS
def init(): global __dht22__, __ds18x20__ if config.exists('sensors.dht22'): pin = Pin(int(config.get('sensors.dht22'))) __dht22__ = dht.DHT22(pin) if config.exists('sensors.ds18x20'): pin = Pin(int(config.get('sensors.ds18x20'))) ow = onewire.OneWire(pin) __ds18x20__ = ds18x20.DS18X20(ow)
def __init__(self, name, pin, on_change=None): import onewire, ds18x20 gc.collect() Device.__init__(self, name, pin,on_change=on_change) self.ds = ds18x20.DS18X20(onewire.OneWire(pin)) self.roms = self.ds.scan() self.lasttime = time.ticks_ms() self.ds.convert_temp() self.temp_list = None
def temperature(): dat = Pin(15) ds = ds18x20.DS18X20(onewire.OneWire(dat)) sensors = ds.scan() ds.convert_temp() time.sleep_ms(750) reading = str(ds.read_temp(sensors[0])) print("onewire: %s" % reading) return reading