def setup_platform(hass, config, add_devices, discovery_info=None): """Set up the available PM sensors.""" from pmsensor import serial_pm as pm try: coll = pm.PMDataCollector(config.get(CONF_SERIAL_DEVICE), pm.SUPPORTED_SENSORS[config.get(CONF_BRAND)]) except KeyError: _LOGGER.error("Brand %s not supported\n supported brands: %s", config.get(CONF_BRAND), list(pm.SUPPORTED_SENSORS.keys())) return except OSError as err: _LOGGER.error("Could not open serial connection to %s (%s)", config.get(CONF_SERIAL_DEVICE), err) return dev = [] for pmname in coll.supported_values(): if config.get(CONF_NAME) is None: name = '{} PM{}'.format(config.get(CONF_NAME), pmname) else: name = 'PM{}'.format(pmname) dev.append(ParticulateMatterSensor(coll, name, pmname)) add_devices(dev)
def setup_platform(opp, config, add_entities, discovery_info=None): """Set up the available PM sensors.""" try: coll = pm.PMDataCollector(config.get(CONF_SERIAL_DEVICE), pm.SUPPORTED_SENSORS[config.get(CONF_BRAND)]) except KeyError: _LOGGER.error( "Brand %s not supported\n supported brands: %s", config.get(CONF_BRAND), pm.SUPPORTED_SENSORS.keys(), ) return except OSError as err: _LOGGER.error( "Could not open serial connection to %s (%s)", config.get(CONF_SERIAL_DEVICE), err, ) return dev = [] for pmname in coll.supported_values(): if config.get(CONF_NAME) is not None: name = "{} PM{}".format(config.get(CONF_NAME), pmname) else: name = f"PM{pmname}" dev.append(ParticulateMatterSensor(coll, name, pmname)) add_entities(dev)
def setup_platform( hass: HomeAssistant, config: ConfigType, add_entities: AddEntitiesCallback, discovery_info: DiscoveryInfoType | None = None, ) -> None: """Set up the available PM sensors.""" try: coll = pm.PMDataCollector(config.get(CONF_SERIAL_DEVICE), pm.SUPPORTED_SENSORS[config.get(CONF_BRAND)]) except KeyError: _LOGGER.error( "Brand %s not supported\n supported brands: %s", config.get(CONF_BRAND), pm.SUPPORTED_SENSORS.keys(), ) return except OSError as err: _LOGGER.error( "Could not open serial connection to %s (%s)", config.get(CONF_SERIAL_DEVICE), err, ) return dev = [] for pmname in coll.supported_values(): if config.get(CONF_NAME) is not None: name = f"{config.get(CONF_NAME)} PM{pmname}" else: name = f"PM{pmname}" dev.append(ParticulateMatterSensor(coll, name, pmname)) add_entities(dev)
def main(): logging.basicConfig(level=logging.INFO) sensors = [] # sensors.append(pm.PMDataCollector("/dev/tty.wchusbserial144740", # pm.SUPPORTED_SENSORS["novafitness,sds011"])) sensors.append(pm.PMDataCollector("/dev/tty.SLAB_USBtoUART", pm.SUPPORTED_SENSORS["oneair,s3"])) # sensors.append(pm.PMDataCollector("/dev/tty.SLAB_USBtoUART4", # pm.SUPPORTED_SENSORS["plantower,pms7003"])) # sensors.append(pm.PMDataCollector("/dev/tty.SLAB_USBtoUART", # pm.SUPPORTED_SENSORS["winsen,zh03b"])) for s in sensors: print(s.supported_values()) while True: for s in sensors: print(s.read_data()) time.sleep(3)
def main(): """Doc string.""" logging.basicConfig(level=logging.INFO) sensors = [] # sensors.append(pm.PMDataCollector("/dev/tty.wchusbserial144740", # pm.SUPPORTED_SENSORS["novafitness,sds011"])) # sensors.append(pm.PMDataCollector("/dev/tty.SLAB_USBtoUART", # pm.SUPPORTED_SENSORS["oneair,s3"])) sensors.append( pm.PMDataCollector("/dev/ttyS1", pm.SUPPORTED_SENSORS["plantower,pms1003"])) client = InfluxDBClient(FLUXHOST, FLUXPORT, FLUXUSER, FLUXPASS, FLUXDBNM) for s in sensors: print(s.supported_values()) while True: for s in sensors: # print(s.read_data()) iso = int(time.time() * 1000000000) sdata = s.read_data() print(sdata) measurement = 'ug/m3' for i in sdata: print(i, sdata[i]) data = [{ "measurement": measurement, "tags": { "parameter": i, }, "time": iso, "fields": { "value": sdata[i] } }] print(data) # # Send the JSON data to InfluxDB client.write_points(data) time.sleep(15)
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)