def main(): global args parser = ArgumentParser(description=__doc__) parser.add_argument( "--host", default='localhost', help='hostname of mqtt broker' ) parser.add_argument( "--topic", default='velogen/raw', help='comma separated list of MQTT topics to subscribe to' ) args = parser.parse_args() c = Client() c.on_connect = on_connect c.on_disconnect = on_disconnect c.on_message = on_message c.connect(args.host) atexit.register(commit) while True: ts = datetime.now().timestamp() for tp, rts in rx_ts_dict.items(): if rts is not None: # dump to file after 30 s of silence if ts - rts > 30: clean_write(tp) rx_ts_dict[tp] = None c.loop(1.0)
def get_client(project_id: str, cloud_region: str, registry_id: str, device_id: str, password: str, mqtt_bridge_hostname: str, mqtt_bridge_port: str, ca_certs: str): client_id = 'projects/{}/locations/{}/registries/{}/devices/{}'.format( project_id, cloud_region, registry_id, device_id) secho('Client ID: ', fg='bright_green', nl=False) echo('\'{}\''.format(client_id)) client = Client(client_id=client_id) client.username_pw_set(username='******', password=password) client.tls_set(ca_certs=ca_certs, tls_version=ssl.PROTOCOL_TLSv1_2) # Assign callbacks client.on_connect = on_connect client.on_publish = on_publish client.on_disconnect = on_disconnect client.on_message = on_message # Connect to MQTT bridge client.connect(mqtt_bridge_hostname, mqtt_bridge_port) client.loop_start() return client
def main(): global args, write_api parser = ArgumentParser(description=__doc__) parser.add_argument("--host", default='localhost', help='hostname of mqtt broker') parser.add_argument( "-t", action='append', default=['velogen/raw'], help='MQTT topic to subscribe to. Can be put multiple times.') parser.add_argument("-d", action='store_true', help='enable debug output') parser.add_argument("--user", default=None, help="username") parser.add_argument("--pw", default=None, help="password") args = parser.parse_args() client = Client() client.on_connect = on_connect client.on_disconnect = on_disconnect client.on_message = on_message if args.user is not None and args.pw is not None: client.username_pw_set(args.user, args.pw) client.connect(args.host) # For influxdb2 only db = InfluxDBClient(url='http://localhost:8086', token=token, debug=args.d) write_api = db.write_api(write_options=SYNCHRONOUS) while True: client.loop(timeout=1.0)
def get_client(name, signals, *args, **kwargs): ''' Create a MQTT client and bind connect, disconnect, and message callbacks. ''' client = Client(*args, **kwargs) client.connected = threading.Event() client.on_connect = ft.partial(on_connect, name) client.on_disconnect = ft.partial( lambda *args, **kwargs: logging.debug('disconnected')) client.on_message = ft.partial(on_message, 'dropbot', signals) return client
def __init__(self, client_id, config, wait=True): """ initialize mqtt client :param client_id: client id :param config: keeper configuration :param wait: whether to wait for connection """ self.logger = Logger() user = config.get("mqtt.user") pwd = config.get("mqtt.pass") client = Client(client_id=client_id) client.on_connect = self._on_connect client.on_disconnect = self._on_disconnect client.on_message = self._on_message client.enable_logger(self.logger) if user and pwd: client.username_pw_set(user, pwd) client.connect_async(config["mqtt.broker"], config["mqtt.port"], 30) self.client = client self.connected = False self.manager = None self.wait = wait
print('The simulator will publish random datas evers 2 seconds on the ' + topic + 'topic') # DEFINE MQTT EVENTS def on_connect(client, userdata, flags, rc): print("Connected to broker") def on_disconnect(client, userdata, rc): print("Connexion with broker closed") # Create the mqtt client mqtt_c = Client(username) # Assign events mqtt_c.on_connect = on_connect mqtt_c.on_disconnect = on_disconnect # Connecting to the mqtt broker mqtt_c.username_pw_set(username, password) mqtt_c.connect(ip, int(port)) mqtt_c.loop_start() while True: payload = randint(0,10) # Send the mesured value to the Broker mqtt_c.publish(topic, payload) print ("Sended " + str(payload) + " to broker at : " + str(datetime.now())) sleep(2)
def start(client: mqtt.Client): client.on_connect = on_connect client.on_disconnect = on_disconnect print("about to connect on: " + BROKER) client.connect(BROKER, PORT) client.loop_forever()
mqtt_password = config['mqtt']['pwd'] TOPIC_HOME_MEALPLAN_LIST = "home/mealplan/list" TOPIC_HOME_MEALPLAN_CONSUMED = "home/mealplan/consumed" TOPIC_HOME_SHOPPINGLISTS_ADDED = "home/shoppinglists/added" TOPIC_HOME_PRODUCT_IN_STOCK = "home/stock/" TOPICS = (("grocy/mealplan/list", 2, on_message_grocy_mealplan_list), ("grocy/mealplan/consume", 2, on_message_grocy_mealplan_consume), ("grocy/shoppinglists/add", 2, on_message_grocy_shoppinglists_add), ("grocy/stock/get", 2, on_message_grocy_stock_get)) if __name__ == "__main__": client = Client(client_id="grocy") client.on_connect = on_connect client.on_disconnect = on_disconnect tops = [message_append(client, topic) for topic in TOPICS] if mqtt_user and mqtt_password: client.username_pw_set(mqtt_user, password=mqtt_password) print(mqtt_host) print(grocy_host) client.connect(mqtt_host) client.subscribe(tops) try: client.loop_forever() except (KeyboardInterrupt, SystemExit): client.disconnect() raise