def setup_platform(hass, config, add_entities, discovery_info=None): """Set up the available CO2 sensors.""" from pmsensor import co2sensor try: co2sensor.read_mh_z19(config.get(CONF_SERIAL_DEVICE)) except OSError as err: _LOGGER.error( "Could not open serial connection to %s (%s)", config.get(CONF_SERIAL_DEVICE), err, ) return False SENSOR_TYPES[SENSOR_TEMPERATURE][1] = hass.config.units.temperature_unit data = MHZClient(co2sensor, config.get(CONF_SERIAL_DEVICE)) dev = [] name = config.get(CONF_NAME) for variable in config[CONF_MONITORED_CONDITIONS]: dev.append(MHZ19Sensor(data, variable, SENSOR_TYPES[variable][1], name)) add_entities(dev, True) return True
def setup_platform( hass: HomeAssistant, config: ConfigType, add_entities: AddEntitiesCallback, discovery_info: DiscoveryInfoType | None = None, ) -> None: """Set up the available CO2 sensors.""" _LOGGER.warning( "The MH-Z19 CO2 Sensor integration is deprecated and will be removed " "in Home Assistant Core 2022.4; this integration is removed under " "Architectural Decision Record 0019, more information can be found here: " "https://github.com/home-assistant/architecture/blob/master/adr/0019-GPIO.md" ) try: co2sensor.read_mh_z19(config.get(CONF_SERIAL_DEVICE)) except OSError as err: _LOGGER.error( "Could not open serial connection to %s (%s)", config.get(CONF_SERIAL_DEVICE), err, ) return data = MHZClient(co2sensor, config.get(CONF_SERIAL_DEVICE)) name = config[CONF_NAME] monitored_conditions = config[CONF_MONITORED_CONDITIONS] entities = [ MHZ19Sensor(data, name, description) for description in SENSOR_TYPES if description.key in monitored_conditions ] add_entities(entities, True)
def setup_platform( hass: HomeAssistant, config: ConfigType, add_entities: AddEntitiesCallback, discovery_info: DiscoveryInfoType | None = None, ) -> None: """Set up the available CO2 sensors.""" try: co2sensor.read_mh_z19(config.get(CONF_SERIAL_DEVICE)) except OSError as err: _LOGGER.error( "Could not open serial connection to %s (%s)", config.get(CONF_SERIAL_DEVICE), err, ) return data = MHZClient(co2sensor, config.get(CONF_SERIAL_DEVICE)) name = config[CONF_NAME] monitored_conditions = config[CONF_MONITORED_CONDITIONS] entities = [ MHZ19Sensor(data, name, description) for description in SENSOR_TYPES if description.key in monitored_conditions ] add_entities(entities, True)
def setup_platform(hass, config, add_devices, discovery_info=None): """Setup the available CO2 sensors.""" from pmsensor import co2sensor try: co2sensor.read_mh_z19(config.get(CONF_SERIAL_DEVICE)) except OSError as err: _LOGGER.error("Could not open serial connection to %s (%s)", config.get(CONF_SERIAL_DEVICE), err) return False dev = MHZ19Sensor(config.get(CONF_SERIAL_DEVICE), config.get(CONF_NAME)) add_devices([dev])
def setup_platform(hass, config, add_devices, discovery_info=None): """Set up the available CO2 sensors.""" from pmsensor import co2sensor try: co2sensor.read_mh_z19(config.get(CONF_SERIAL_DEVICE)) except OSError as err: _LOGGER.error("Could not open serial connection to %s (%s)", config.get(CONF_SERIAL_DEVICE), err) return False SENSOR_TYPES[SENSOR_TEMPERATURE][1] = hass.config.units.temperature_unit data = MHZClient(co2sensor, config.get(CONF_SERIAL_DEVICE)) dev = [] name = config.get(CONF_NAME) for variable in config[CONF_MONITORED_CONDITIONS]: dev.append( MHZ19Sensor(data, variable, SENSOR_TYPES[variable][1], name)) add_devices(dev, True) return True
def update(self): """Read from sensor and update the state.""" from pmsensor import co2sensor _LOGGER.debug("Reading data from CO2 sensor") try: ppm = co2sensor.read_mh_z19(self._serial) # values from sensor can only between 0 and 5000 if (ppm >= 0) & (ppm <= 5000): self._state = ppm except OSError as err: _LOGGER.error("Could not open serial connection to %s (%s)", self._serial, err) return
def main(): """Doc string.""" logging.basicConfig(level=logging.INFO) # ppm = co2sensor.read_mh_z19("/dev/ttyS2") # print("CO2 concentration is {} ppm".format(ppm)) # print(co2sensor.read_mh_z19_with_temperature("/dev/ttyS2")) # print("CO2 concentration is {} ppm".format(ppm)) # client = InfluxDBClient(FLUXHOST, FLUXPORT, FLUXUSER, FLUXPASS, FLUXDBNM) mqttc = mqtt.Client() mqttc.username_pw_set(MQTTUSER, MQTTPASS) # mqttc.on_message = on_message # mqttc.on_connect = on_connect # mqttc.on_publish = on_publish # mqttc.on_subscribe = on_subscribe # Uncomment to enable debug messages if DEBUG: mqttc.on_log = on_log # mqttc.connect("localhost", 1883, 60) # mqttc.connect(MQTTHOST) # mqttc.subscribe(inp_topics) mqttc.connect(MQTTHOST) while True: try: mqttc.connect(MQTTHOST) except Exception as e: # pass # raise e print(e) # print(s.read_data()) iso = int(time.time() * 1000000000) ppm = co2sensor.read_mh_z19("/dev/ttyS2") print("CO2 concentration is {} ppm".format(ppm)) measurement = 'ppm' if ppm is not None: data = """{"MH-Z19B": {"measurement": "%s", "tags": {"location": "%s", "parameter": "CO2"}, "time": %s, "fields": {"value": %s}}} """ % (measurement, hostname, iso, ppm) print(data) # # # Send the JSON data to InfluxDB # client.write_points(data) try: mqttc.publish(MQTTPUBT, payload=data, qos=0, retain=False) except Exception as e: pass # raise e print(e) # time.sleep(60) pms = pm.PMDataCollector("/dev/ttyS1", pm.SUPPORTED_SENSORS["plantower,pms1003"]) sdata = pms.read_data() print(sdata) measurement = 'ug/m3' for i in sdata: print(i, sdata[i]) data = """{"PMS1003": {"measurement": "%s", "tags": {"location": "%s", "parameter": "%s"}, "time": %s, "fields": {"value": %s}}} """ % (measurement, hostname, i, iso, sdata[i]) print(data) try: mqttc.publish(MQTTPUBT, payload=data, qos=0, retain=False) except Exception as e: pass # raise e print(e) time.sleep(1) # mqttc.disconnect() time.sleep(57)
''' Created on Aug 19, 2016 @author: matuschd ''' from pmsensor import co2sensor if __name__ == '__main__': ppm = co2sensor.read_mh_z19("/dev/tty.SLAB_USBtoUART") print("CO2 concentration is {} ppm".format(ppm))
# -*- coding: utf-8 -*- """ Created on Aug 19, 2016. @author: matuschd """ from pmsensor import co2sensor if __name__ == '__main__': ppm = co2sensor.read_mh_z19("/dev/ttyS2") print("CO2 concentration is {} ppm".format(ppm)) print(co2sensor.read_mh_z19_with_temperature("/dev/ttyS2")) # print("CO2 concentration is {} ppm".format(ppm))