async def serviceMqtt(): config['wifi_coro'] = handleBrokerState config['connect_coro'] = handleConnection config['subs_cb'] = handleMessage publishPeriod = 60 client = MQTTClient(config) try: try: await client.connect() while True: await publishLiveness(client) printReport() await async_sleep_ms(publishPeriod) except OSError: print('Connection failed.') raise finally: # Prevent LmacRxBlk:1 hang? client.close() except KeyboardInterrupt: raise except Exception as e: print("Exception in serviceMqtt()") sys.print_exception(e) hardreset()
def __init__(self, event_mqtt_publish): log.info('MQTT:init') self.event_mqtt_publish = event_mqtt_publish self.feedname_pm25 = bytes('{:s}/feeds/{:s}'.format(b'MikeTeachman', b'pm25'), 'utf-8') self.feedname_o3 = bytes('{:s}/feeds/{:s}'.format(b'MikeTeachman', b'o3'), 'utf-8') self.feedname_no2 = bytes('{:s}/feeds/{:s}'.format(b'MikeTeachman', b'no2'), 'utf-8') self.feedname_temp = bytes('{:s}/feeds/{:s}'.format(b'MikeTeachman', b'tdegc'), 'utf-8') self.feedname_humidity = bytes('{:s}/feeds/{:s}'.format(b'MikeTeachman', b'humidity'), 'utf-8') self.feedname_dba_avg = bytes('{:s}/feeds/{:s}'.format(b'MikeTeachman', b'dba_avg'), 'utf-8') self.feedname_dba_max = bytes('{:s}/feeds/{:s}'.format(b'MikeTeachman', b'dba_max'), 'utf-8') self.feedname_vbat_avg = bytes('{:s}/feeds/{:s}'.format(b'MikeTeachman', b'vbat_avg'), 'utf-8') self.feedname_vbat_min = bytes('{:s}/feeds/{:s}'.format(b'MikeTeachman', b'vbat_min'), 'utf-8') self.wifi_status = 'unknown' self.client = MQTTClient(server='io.adafruit.com', ssid=mqtt_config['ssid'], wifi_pw=mqtt_config['wifi_pw'], user=mqtt_config['user'], password=mqtt_config['password']) loop = asyncio.get_event_loop() try: loop.create_task(self.run_mqtt()) finally: self.client.close() # Prevent LmacRxBlk:1 errors
def setupmqttas(self, wifiname, wifipassword, mqttbroker): config['ssid'] = wifiname config['wifi_pw'] = wifipassword config['server'] = mqttbroker config['subs_cb'] = self.callback config['connect_coro'] = self.conn_han MQTTClient.DEBUG = True self.client = MQTTClient(config)
def __init__(self): MQTTClient.DEBUG = True # Optional: print diagnostic messages config['server'] = env.MQTT_BROKER config['ssid'] = env.WIFI_SSID config['wifi_pw'] = env.WIFI_PASSWORD config['connect_coro'] = self.conn_han self.client = MQTTClient(config) self.pending_messages = [] print('MqttService: Client created')
def __init__(self, config: dict): config["subs_cb"] = self.sub_cb config["connect_coro"] = self.subscribe_topics config["wifi_coro"] = self.on_wifi self.config = config self.client = MQTTClient(config) self.stopped = False self.ir_handler = IRHandler(config) self.last_lifesign = None self.iscp_handler = ISCPHandler()
def __init__(self, server, topic, config, update_ms): self._config = config self._config['subs_cb'] = self.callback self._config['connect_coro'] = self.conn_han self._config['server'] = server self._topic = topic self._update_ms = update_ms self._adj_update_ms = update_ms MQTTClient.DEBUG = True # Optional: print diagnostic messages self._client = MQTTClient(self._config) loop = asyncio.get_event_loop() loop.create_task(self.run())
def __init__(self, settings): self.debug = getattr(settings, "DEBUG", False) self._state = STATE_INIT self._version = __version__ self._fw_name = "Microhomie" self._extensions = getattr(settings, "EXTENSIONS", []) self._bc_enabled = getattr(settings, "BROADCAST", False) self._wifi = getattr(settings, "WIFI_CREDENTIALS", False) self._wifi_rescan_delay = getattr(settings, "WIFI_RESCAN_DELAY", MAIN_DELAY) self.first_start = True self.stats_interval = getattr(settings, "DEVICE_STATS_INTERVAL", 60) self.device_name = getattr(settings, "DEVICE_NAME", "") self.callback_topics = {} # Registered homie nodes self.nodes = [] # Generate unique id if settings has no DEVICE_ID self.device_id = getattr(settings, "DEVICE_ID", get_unique_id()) # Base topic self.btopic = getattr(settings, "MQTT_BASE_TOPIC", "homie") # Device base topic self.dtopic = "{}/{}".format(self.btopic, self.device_id) # mqtt_as client self.mqtt = MQTTClient( client_id=self.device_id, server=settings.MQTT_BROKER, port=getattr(settings, "MQTT_PORT", 1883), user=getattr(settings, "MQTT_USERNAME", None), password=getattr(settings, "MQTT_PASSWORD", None), keepalive=getattr(settings, "MQTT_KEEPALIVE", 30), ping_interval=getattr(settings, "MQTT_PING_INTERVAL", 0), ssl=getattr(settings, "MQTT_SSL", False), ssl_params=getattr(settings, "MQTT_SSL_PARAMS", {}), response_time=getattr(settings, "MQTT_RESPONSE_TIME", 10), clean_init=getattr(settings, "MQTT_CLEAN_INIT", True), clean=getattr(settings, "MQTT_CLEAN", True), max_repubs=getattr(settings, "MQTT_MAX_REPUBS", 4), will=("{}/{}".format(self.dtopic, DEVICE_STATE), "lost", True, QOS), subs_cb=self.subs_cb, wifi_coro=None, connect_coro=self.connection_handler, ssid=getattr(settings, "WIFI_SSID", None), wifi_pw=getattr(settings, "WIFI_PASSWORD", None), )
def __init__(self, settings): self.debug = getattr(settings, "DEBUG", False) self._state = STATE_INIT self._extensions = getattr(settings, "EXTENSIONS", []) self._extensions.append("org.microhomie.mpy:0.1.0:[4.x]") self._first_start = True self.stats_interval = getattr(settings, "DEVICE_STATS_INTERVAL", 60) self.nodes = [] self.callback_topics = {} self.device_name = getattr(settings, "DEVICE_NAME", b"mydevice") # Generate unique id if settings has no DEVICE_ID try: device_id = settings.DEVICE_ID except AttributeError: device_id = utils.get_unique_id() # Base topic self.btopic = getattr(settings, "MQTT_BASE_TOPIC", "homie") # Device base topic self.dtopic = "{}/{}".format(self.btopic, device_id) self.mqtt = MQTTClient( client_id=device_id, server=settings.MQTT_BROKER, port=getattr(settings, "MQTT_PORT", 1883), user=getattr(settings, "MQTT_USERNAME", None), password=getattr(settings, "MQTT_PASSWORD", None), keepalive=getattr(settings, "MQTT_KEEPALIVE", 30), ping_interval=getattr(settings, "MQTT_PING_INTERVAL", 0), ssl=getattr(settings, "MQTT_SSL", False), ssl_params=getattr(settings, "MQTT_SSL_PARAMS", {}), response_time=getattr(settings, "MQTT_RESPONSE_TIME", 10), clean_init=getattr(settings, "MQTT_CLEAN_INIT", True), clean=getattr(settings, "MQTT_CLEAN", True), max_repubs=getattr(settings, "MQTT_MAX_REPUBS", 4), will=("{}/{}".format(self.dtopic, DEVICE_STATE), "lost", True, QOS), subs_cb=self.sub_cb, wifi_coro=None, connect_coro=self.connection_handler, ssid=getattr(settings, "WIFI_SSID", None), wifi_pw=getattr(settings, "WIFI_PASSWORD", None), )
def __init__(self, settings): self._state = STATE_INIT self._extensions = getattr(settings, "EXTENSIONS", []) self._first_start = True self.async_tasks = [] self.stats_interval = getattr(settings, "DEVICE_STATS_INTERVAL", 60) self.nodes = [] self.callback_topics = {} self.device_name = getattr(settings, "DEVICE_NAME", b"mydevice") device_id = getattr(settings, "DEVICE_ID", get_unique_id()) self.btopic = getattr(settings, "MQTT_BASE_TOPIC", b"homie") self.dtopic = SLASH.join((self.btopic, device_id)) self.mqtt = MQTTClient( client_id=device_id, server=settings.MQTT_BROKER, port=getattr(settings, "MQTT_PORT", 1883), user=getattr(settings, "MQTT_USERNAME", None), password=getattr(settings, "MQTT_PASSWORD", None), keepalive=getattr(settings, "MQTT_KEEPALIVE", 30), ping_interval=getattr(settings, "MQTT_PING_INTERVAL", 0), ssl=getattr(settings, "MQTT_SSL", False), ssl_params=getattr(settings, "MQTT_SSL_PARAMS", {}), response_time=getattr(settings, "MQTT_RESPONSE_TIME", 10), clean_init=getattr(settings, "MQTT_CLEAN_INIT", True), clean=getattr(settings, "MQTT_CLEAN", True), max_repubs=getattr(settings, "MQTT_MAX_REPUBS", 4), will=(SLASH.join((self.dtopic, DEVICE_STATE)), b"lost", True, QOS), subs_cb=self.sub_cb, wifi_coro=None, connect_coro=self.connection_handler, ssid=getattr(settings, "WIFI_SSID", None), wifi_pw=getattr(settings, "WIFI_PASSWORD", None), )
def __init__(self, settings): self._state = "init" self._stime = time() self.stats_interval = settings.DEVICE_STATS_INTERVAL self.nodes = [] self.callback_topics = {} self.device_name = settings.DEVICE_NAME self.btopic = settings.MQTT_BASE_TOPIC self.dtopic = SLASH.join( (settings.MQTT_BASE_TOPIC, settings.DEVICE_ID) ) # setup networking utils.setup_network(settings.WIFI_PASSWORD) utils.wifi_connect(settings.WIFI_SSID) self.mqtt = MQTTClient( client_id=settings.DEVICE_ID, server=settings.MQTT_BROKER, port=settings.MQTT_PORT, user=settings.MQTT_USERNAME, password=settings.MQTT_PASSWORD, keepalive=settings.MQTT_KEEPALIVE, ssl=settings.MQTT_SSL, ssl_params=settings.MQTT_SSL_PARAMS, subs_cb=self.sub_cb, connect_coro=self.connection_handler, will=(SLASH.join((self.dtopic, DEVICE_STATE)), b"lost", True, QOS), ) # Start coros loop = get_event_loop() loop.create_task(self.publish_stats())
try: await client.connect() except OSError: print('Connection failed.') return n = 0 while True: await asyncio.sleep(5) print('publish', n) # If WiFi is down the following will pause for the duration. await client.publish('result', '{} {}'.format(n, client.REPUB_COUNT), qos = 1) n += 1 # Define configuration config['subs_cb'] = sub_cb config['wifi_coro'] = wifi_han config['connect_coro'] = conn_han config['clean'] = True config['server'] = SERVER # Set up client MQTTClient.DEBUG = True # Optional client = MQTTClient(config) loop = asyncio.get_event_loop() loop.create_task(heartbeat()) try: loop.run_until_complete(main(client)) finally: client.close() # Prevent LmacRxBlk:1 errors
gui.set_value(sensor.max_value, "TEMP_MAX") loop = asyncio.get_event_loop() loop.create_task(pub_mqtt("TEMP/IN/CURRENT", sensor.current_value)) elif sensor.serial == external_temp_sensor.serial: gui.set_value(sensor.current_value, "TEMP_OUT") loop = asyncio.get_event_loop() loop.create_task(pub_mqtt("TEMP/OUT/CURRENT", sensor.current_value)) elif sensor.serial == internal_humidity_sensor.serial: gui.set_value(sensor.current_value, "HUMID_IN") gui.set_value(sensor.min_value, "HUMID_MIN") gui.set_value(sensor.max_value, "HUMID_MAX") loop = asyncio.get_event_loop() loop.create_task(pub_mqtt("HUMIDITY/IN/CURRENT", sensor.current_value)) mc = MQTTClient(config) gui = guicontroller.GUI(128, 64, I2C(-1, scl=Pin(25), sda=Pin(33))) s = sensorcontroller.SensorController(on_sensor_change) internal_temp_sensor = s.add_sensor(sensorcontroller.DallasTempSensor, Pin(14), serial=b'(yd\x84\x07\x00\x00\xb3') external_temp_sensor = s.add_sensor(sensorcontroller.DallasTempSensor, Pin(14), serial=b'(\x84tV\xb5\x01<\xdb') internal_humidity_sensor = s.add_sensor(sensorcontroller.DHT22Sensor, Pin(27), serial=b'INTERNAL_HUMIDITY') loop = asyncio.get_event_loop() try: loop.run_until_complete(connect_mqtt(mc))