Example #1
9
 def mqtt(self):
     if not hasattr(self, '_mqtt_client'):
         self.responses = {}
         self.ready = False
         client = MQTTClient()
         client.username_pw_set(self.apikey)
         client.on_connect = self._on_connect
         client.on_message = self._on_message
         client.on_subscribe = self._on_subscribe
         client.connect(self.client.endpoint.replace('mqtt://', ''))
         self._mqtt_client = client
         # Start the loop and wait for the connection to be ready
         self._mqtt_client.loop_start()
         while not self.ready:
             time.sleep(.1)
     return self._mqtt_client
Example #2
0
class MqttClient(TransportClient):
    def __init__(self, host_name: str, settings: MqttSettings):
        self.host_name = host_name
        self.settings = settings
        self.sub_topic_root = f"label_servers/print/{host_name}"
        self._callback: Optional[Callable[[PrintRequest], Any]] = None

        # Configure the client
        self.client = Client(self.host_name)

        # TLS settings if we have them
        if self.settings.tls_cafile is not None and self.settings.tls_cafile.strip(
        ):
            self.client.tls_set(self.settings.tls_cafile)

        self.client.connect(settings.mqtt_broker_host,
                            settings.mqtt_broker_port, 60)
        self.client.on_connect = self._on_connect
        self.client.on_message = self._on_message

    def _on_connect(self,
                    client: Client,
                    user_data,
                    flags,
                    reason_code,
                    properties=None):
        logger.info(f"Connected with reason code {reason_code}")

        sub_topic = f"{self.sub_topic_root}/#"
        logger.info(f"Subscribing to: {sub_topic}")
        client.subscribe(sub_topic)
        client.will_set(f"label_servers/status/{self.host_name}",
                        json.dumps({"online": False}))

    def _on_message(self, client: Client, user_data, message: MQTTMessage):
        logger.info(f"Received Message: {message.topic}")

        try:
            # Remove topic root
            stripped = message.topic.replace(self.sub_topic_root,
                                             "").strip("/")

            # Print requests should be of the form "<printer_serial>/<mode>"
            parts = stripped.split("/")
            if len(parts) >= 2:
                serial, mode, *_ = parts
                request = PrintRequest(serial, RequestMode.parse(mode),
                                       message.payload)
                if self._callback:
                    self._callback(request)
        except Exception as e:
            logger.error("Error processing received message", exc_info=e)

    def publish(self, status: HostStatus):
        self.client.publish(f"label_servers/status/{self.host_name}",
                            json.dumps(status.as_dict()))

    def start(self, on_message: Callable[[PrintRequest], None]):
        self._callback = on_message
        self.client.loop_start()
Example #3
0
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)
Example #4
0
        def setMqttClient(client: Client):
            client.on_message = on_message
            client.on_connect = on_connect

            client.connect(server, port)
            client.loop_start()
            self.client = client
Example #5
0
class MqttSubscriber(BaseSubscriber):
    def __init__(self, *args, **kwargs):
        self.client = None
        self.message = None
        self.f = open("time_taken_mqtt.txt", "a+")

    def connect(self, host="localhost", port=5000, topic="test"):
        print("Connected")
        self.client = Client()
        self.client.connect(host=host)
        self.client.on_message = self.recv_message
        self.client.on_subscribe = self.on_subscribe
        self.client.subscribe(topic)
        self.client.loop_forever()

    def on_subscribe(self, *args, **kwargs):
        print("Subscribed")

    def recv_message(self, client, userdata, message):
        print(message.payload)
        message = json.loads(message.payload)
        latency = time() - message["sentAt"]
        msg_size = len(message["message"].encode('utf-8'))
        self.f.write("{} : {}\n".format(msg_size, latency))
        print("Message received : {} size is {} in {}".format(
            message, msg_size, str(latency)))

    def close(self):
        pass
Example #6
0
class MqttController:
    def __init__(self):
        self.client = Client()
        self.client.on_connect = self.__on_connect
        self.client.on_message = self.__on_message
        self.evtCallback: Optional[EventCallback] = None

    def connect(self, connectCallback, host, port=1883, keepalive=60):
        log.info("Trying to connect MQTT client.")
        self.connectCallback = connectCallback
        self.client.connect(host, port, keepalive)
        self.client.loop_start()

    def disconnect(self):
        self.client.loop_stop()
        self.client.disconnect()

    def setCallback(self, callback: EventCallback):
        self.evtCallback = callback

    def delCallback(self):
        self.evtCallback = None

    def __on_connect(self, client, userdata, flags, rc):
        log.info("MQTT client connected, registering subscriptions.")
        self.client.subscribe("/baresip/event")
        self.connectCallback()

    def __on_message(self, client, userdata, msg: MQTTMessage):
        log.info("MQTT message received for path=%s.", msg.topic)
        log.debug("payload=%s", msg.payload)

        # parse message
        try:
            msgObj = json.loads(msg.payload)
            evtType = EventType[msgObj["type"]]

            # notify respective callback
            cb = self.evtCallback
            if cb is not None:
                log.debug("Calling event callback in a thread.")
                t = threading.Thread(target=lambda: cb(evtType, msgObj))
                t.start()
            else:
                log.debug("No callback registered.")
        except JSONDecodeError:
            log.error("Received invalid JSON message.")
        except KeyError:
            log.warn("Received unhandled type code or type code is missing.")

    def send_command(self, msg):
        log.debug("Trying to send message %s to phone.", msg)
        info = self.client.publish("/baresip/command/", msg)
        log.debug("Waiting for publish")
        log.debug(info.rc)
        info.wait_for_publish()
        if info.rc != MQTT_ERR_SUCCESS:
            log.error("Failed to publish message")
        else:
            log.debug("Message sent successfully.")
def test_mqtt_telemetry():

    # Create receiver
    sub = Client(clean_session=True)

    # def on_message(client, userdata, message):
    #     data = message.payload
    #     print(message)

    on_message_mock = mock.Mock()
    sub.on_message = on_message_mock
    sub.connect(cfg.TELEMETRY_MQTT_BROKER_HOST)
    sub.loop_start()
    name = "donkey/%s/telemetry" % cfg.TELEMETRY_DONKEY_NAME
    sub.subscribe(name)

    t = MqttTelemetry(cfg, default_inputs=['angle'], default_types=['float'])
    t.publish()

    timestamp = t.report({'speed': 16, 'voltage': 12})
    t.run(33.3)
    assert t.qsize == 2

    time.sleep(1.5)

    t.publish()
    assert t.qsize == 0

    time.sleep(0.5)

    res = str.encode(
        '[{"ts": %s, "values": {"speed": 16, "voltage": 12, "angle": 33.3}}]' %
        timestamp)
    assert on_message_mock.call_args_list[0][0][2].payload == res
Example #8
0
    def _run(self):
        '''
        The main function of this task.
        '''
        # join function input buffer, it's a shared memory
        join_in_buf = [None] * len(self.idf_confs)

        def on_connect(client, userdata, rc):
            log.info('ESM connect to mqtt broker with return code %s', rc)

            # create odf publish partial function
            for conf in self.odf_confs:
                conf['pub'] = partial(client.publish, conf['topic'])

            for idx, idf in enumerate(self.idf_confs):
                topic = idf['topic']
                client.subscribe(topic)
                client.message_callback_add(
                    topic,
                    self._msg_callback(
                        idf.get('func'), self.join_func.get('func'),
                        self.odf_confs, join_in_buf, idx))

        mqtt_conn = Client()
        mqtt_conn.on_connect = on_connect
        mqtt_conn.connect(config.mqtt_conf['host'],
                          port=config.mqtt_conf['port'])
        mqtt_conn.loop_forever()
Example #9
0
    def create_client(self,
                      host,
                      port,
                      username,
                      password,
                      clientid,
                      cafile=None):
        """Creating an MQTT Client Object"""
        client = MqttClient(clientid)

        if username and password:
            client.username_pw_set(username=username, password=password)
        else:
            self.logger.warn("Proceeding without username and password")

        if cafile:
            client.tls_set(ca_certs=cafile)
        else:
            self.logger.warn("Proceeding without certificate file")

        try:
            client.on_connect = self.on_connect
            client.on_message = self.on_message
            client.connect(host=host, port=port)
        except OSError as error:
            self.logger.error(error)

        return client
Example #10
0
File: paho.py Project: ebaggen/rise
class PahoClient(MQTTClientBase):
    def __init__(self, host: str, port: int, keepalive: int):
        self._client = Client()
        self._host = host
        self._port = port
        self._keepalive = keepalive

        self._on_message, self._on_connect, self._subscription_topics = None, None, []

    def connect(self, subscription_topics: List[str], on_connect=None, on_message=None):
        self._client.on_connect = self.__connected
        self._client.on_message = self.__message_received

        self._on_message, self._on_connect, self._subscription_topics = on_message, on_connect, subscription_topics

        self._client.connect(self._host, self._port, self._keepalive)

    def __connected(self):
        for topic in self._subscription_topics:
            self._client.subscribe(topic)

        if self._on_connect:
            self._on_connect()

    def __message_received(self, client: Client, userdata, msg):
        if self._on_message:
            self._on_message(msg)

    def loop_background(self):
        self._client.loop_start()
Example #11
0
def main(broker, port, interface):
    """Scan for all MiPlant devicesself.

    Send via MQTT if broker is provided. Print to console otherwise.
    """
    sensors = discover_and_scan(interface)
    if broker:
        client = Client()
        client.connect(broker, port)
        client.loop_start()
        for sensor in sensors:
            payload = json.dumps({
                'battery': sensor.battery,
                'temperature': sensor.temperature,
                'light': sensor.light,
                'moisture': sensor.moisture,
                'conductivity': sensor.conductivity
            })
            channel = "sensors/plantsensors/{addr}".format(addr=sensor.address)
            client.publish(channel, json.dumps(payload))
        client.loop_stop()
    else:
        for sensor in sensors:
            print('--------------------------')
            print('Address: {addr}'.format(addr=sensor.address))
            print('Battery level: {bat}'.format(bat=sensor.battery))
            print('Temperature: {temp} °C'.format(temp=sensor.temperature))
            print('Light: {light} lx'.format(light=sensor.light))
            print('Moisture: {moist}'.format(moist=sensor.moisture))
            print(
                'Conductivity: {cond} µS/cm'.format(cond=sensor.conductivity))
Example #12
0
class BugfixClient(PAHO_MQTT_Client):
    def connect(self):
        homie.mqtt.mqtt_base.MQTT_Base.connect(self)
        #self.mqtt_client = MQTTClientWrapper(client_id=self.mqtt_settings['MQTT_CLIENT_ID'])
        self.mqtt_client = Client(client_id=self.mqtt_settings['MQTT_CLIENT_ID'])
        self.mqtt_client.on_connect = self._on_connect
        self.mqtt_client.on_message = self._on_message
        #self.mqtt_client.on_publish = self._on_publish
        self.mqtt_client.on_disconnect = self._on_disconnect
        self.mqtt_client.enable_logger(homie.mqtt.paho_mqtt_client.mqtt_logger)
        self.mqtt_client.enable_logger()
        if self.mqtt_settings ['MQTT_USERNAME']:
            self.mqtt_client.username_pw_set(
                    self.mqtt_settings ['MQTT_USERNAME'],
                    password=self.mqtt_settings ['MQTT_PASSWORD']
            )
        try:
            self.mqtt_client.connect(
                self.mqtt_settings ['MQTT_BROKER'],
                port=self.mqtt_settings ['MQTT_PORT'],
                keepalive=self.mqtt_settings ['MQTT_KEEPALIVE'],
            )
            self.mqtt_client.loop_start()
        except Exception as e:
            homie.mqtt.paho_mqtt_client.logger.warning ('MQTT Unable to connect to Broker {}'.format(e))
Example #13
0
class Seller:
    '''
    Set objects for sale and publish them on 'Available-items' topic
    '''
    def __init__(self, number_items, broker='localhost', auth=None):
        self.number_of_items = number_items
        self.auth = auth
        self.client = Client()

        if auth != None:
            (usr, pwd) = auth
            self.client.username_pw_set(usr, pwd)

        self.client.connect(broker)

    def sell_process(self, update=False):
        available_itms = ''.join(
            [str(x) + ' ' for x in range(1, self.number_of_items + 1)])
        self.client.publish('Available-items', available_itms)
        while update:
            self.client.publish('Available-items', available_itms)
            time.sleep(3 * random.random())

    def sell(self):
        Process(target=self.sell_process, args=()).start()
Example #14
0
class MQTTValuePub(object):
    '''
    Use MQTT to send values on network
    pip install paho-mqtt
    '''
    def __init__(self, name, broker="iot.eclipse.org"):
        from paho.mqtt.client import Client

        self.name = name
        self.message = None
        self.client = Client()
        print("connecting to broker", broker)
        self.client.connect(broker)
        self.client.loop_start()
        print("connected.")

    def run(self, values):
        packet = {"name": self.name, "val": values}
        p = pickle.dumps(packet)
        z = zlib.compress(p)
        self.client.publish(self.name, z)

    def shutdown(self):
        self.client.disconnect()
        self.client.loop_stop()
Example #15
0
 def config(self, mqtt_client: MqttClient):
     if self.username and self.password:
         mqtt_client.username_pw_set(self.username, self.password)
     mqtt_client.connect(
         self.host,
         self.port,
     )
Example #16
0
def connect(client: mqtt.Client, args: argparse.Namespace):
    """Connect to an MQTT broker with supplied arguments."""
    if args.username:
        client.username_pw_set(args.username, args.password)

    # TLS
    if args.tls:
        # TLS is enabled
        if args.tls_version is None:
            # Use highest TLS version
            args.tls_version = ssl.PROTOCOL_TLS

        if args.tls_ca_certs is not None:
            args.tls_ca_certs = os.path.expandvars(args.tls_ca_certs)
        if args.tls_certfile is not None:
            args.tls_certfile = os.path.expandvars(args.tls_certfile)
        if args.tls_keyfile is not None:
            args.tls_keyfile = os.path.expandvars(args.tls_keyfile)

        client.tls_set(
            ca_certs=args.tls_ca_certs,
            certfile=args.tls_certfile,
            keyfile=args.tls_keyfile,
            cert_reqs=getattr(ssl, args.tls_cert_reqs),
            tls_version=args.tls_version,
            ciphers=(args.tls_ciphers or None),
        )

    client.connect(args.host, args.port)
Example #17
0
def init_client():
    #variabili di sistema settate 
    access_key = os.environ["AWS_ACCESS_KEY_ID"]
    secret_key = os.environ["AWS_SECRET_ACCESS_KEY"]
    port = 443

    region = "us-east-2"

    # This is specific to your AWS account
    host = "a3pwbt0axh6wnd-ats.iot.us-east-2.amazonaws.com".format(region)

    extra_headers = functools.partial(
        get_amazon_auth_headers,
        access_key,
        secret_key,
        region,
        host,
        port,
    )

    client = Client(transport="websockets")
  
    client.ws_set_options(headers=extra_headers)
    
    # Use client as normal from here
    client.on_connect = on_connect
    client.on_message = on_message
    client.on_publish = on_publish

    client.tls_set()
    client.connect(host, 443,60)
    
    return client
Example #18
0
class MqttBridge(BaseBridge):
    def __init__(self):
        super(MqttBridge, self).__init__()
        self.client = MqttClient()
        self.client.on_message = self.onMqttMessage
        self.client.on_connect = self.onConnect

    def publishApiMessage(self, heat_pump_id, base_topic, topic, value):
        self.client.publish(base_topic + topic, value)

    # noinspection PyUnusedLocal
    def onConnect(self, client, userdata, flags, rc):
        # type: (MqttBridge, MqttClient, object, dict, object) -> None
        topics = []
        for topic in self.binding.topics:
            topics.append((topic, 0))
        if len(topics) == 0:
            return
        print "mqtt subscribing to topics: ", topics
        client.subscribe(topics)

    # noinspection PyUnusedLocal
    def onMqttMessage(self, client, userdata, msg):
        # type: (MqttBridge, MqttClient, object, MQTTMessage) -> None
        topic = str(msg.topic)
        self.binding.onApiMessage(topic, msg.payload)

    def start(self):
        print "mqtt connect to:", config.MQTT['host']
        self.client.connect(config.MQTT['host'])
        self.client.loop_start()

    def stop(self):
        self.client.disconnect()
        self.client.loop_stop()
Example #19
0
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)
Example #20
0
def main():

    client = Client(protocol=MQTTv311)
    client.username_pw_set(USERNAME, password=PASSWORD)
    client.connect(HOST, port=PORT, keepalive=60)

    host1 = "98:07:2D:35:7B:00"
    host2 = "98:07:2D:40:95:83"

    print("Connecting to SensorTags")
    tag1 = SensorTag(host1)
    tag2 = SensorTag(host2)

    sensor_enabler(tag1)
    sensor_enabler(tag2)

    sleep(1.0)

    try:
        while True:
            data1 = read_sensor_data(tag1)
            data2 = read_sensor_data(tag2)
            sleep(1.0)
            pprint({"SensorTag1": data1})
            pprint({"SensorTag2": data2})
            client.publish(TOPIC1, payload=dumps(data1))
            client.publish(TOPIC2, payload=dumps(data2))
            sleep(30)
    except KeyboardInterrupt:
        print("Disconnected to SensorTags")
        tag1.disconnect()
        tag2.disconnect()
Example #21
0
def main():
    print("Hello! this is aggry! I send wonderful data :)")

    client = Client(client_id="aggry")
    client.on_connect = on_connect
    client.on_message = on_message

    if debug:
        client.on_log = on_log
    else:
        print("Debug mode disabled by configuration.")

    # Swarm does not handle dependencies
    sleep(2)

    try:
        rc = client.connect("broker", 1883, 60)
    except:
        # just try again
        print("connect: trying again...")
        sleep(4)
        client.connect("broker", 1883, 60)

    # main loop
    client.loop_forever()
Example #22
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
Example #23
0
def set_up(host, port, subscribe_to_topics_func):
    global _instance, _subscribe_to_topics_func
    _subscribe_to_topics_func = subscribe_to_topics_func
    _instance = Client()
    _instance.on_connect = _on_connect
    _instance.on_message = _on_message
    _instance.connect(host, port)
    _instance.loop_start()
Example #24
0
def ensure_mqtt(host):
    mqtt_client = MQTTClient()
    mqtt_client.on_connect = mqtt_on_connect
    mqtt_client.message_callback_add('embrace/sensors', mqtt_on_message_sensor)
    mqtt_client.message_callback_add('embrace/topology',
                                     mqtt_on_message_topology)
    mqtt_client.connect(host, 1883, 60)
    return mqtt_client
Example #25
0
def check_broker():
    from paho.mqtt.client import Client
    try:
        client = Client()
        client.connect('localhost', 1883)
        return True
    except Exception as e:
        return False
Example #26
0
class MQTTClient(object):
    """Manages Paho MQTT client lifecycle and callbacks"""

    def __init__(self, config: dict, message_processor=None):
        self.config = config

        self.client = Client(
            client_id=config.mqtt_client,
            clean_session=config.mqtt_clean_session,
            userdata={"client": config.mqtt_client},
        )

        self.client.username_pw_set(config.mqtt_username, config.mqtt_password)

        if self.config.mqtt_debug:
            self.client.on_log = self._on_log

        self.client.on_connect = self._on_connect
        self.client.on_subscribe = self._on_subscribe
        self.client.on_message = self._on_message
        self.client.on_publish = self._on_publish
        self.client.on_disconnect = self._on_disconnect

        self.client.connect(config.mqtt_host, config.mqtt_port, 60)

        if message_processor:
            self.message_processor = message_processor

    def _on_log(self, client, userdata, level, buf):
        click.echo(f"{buf}, origin: {userdata['client']}")

    def _on_connect(self, client, userdata, flags, rc):
        click.echo(f"Connected {userdata['client']}, result code: {str(rc)} {str(flags)}")
        click.echo(f"Subscribing to all topics...")
        self.client.subscribe(self.config.mqtt_topics)

    def _on_subscribe(self, client, userdata, mid, granted_qos):
        click.echo(f"Subscribed {userdata['client']}, mid: {mid}, granted qos: {granted_qos}")
        click.echo(f"Listening for {userdata['client']} messages...")

    def _on_disconnect(self, client, userdata, rc):
        click.echo(f"Disconnected {userdata['client']}, result code: {str(rc)}")

    def _on_message(self, client, userdata, msg):
        if hasattr(self, "message_processor"):
            self.message_processor(client, userdata, msg)
        else:
            click.echo(f"Topic: {msg.topic}, Mid: {msg.mid}, Payload: {msg.payload.decode('utf-8')}")

    def _on_publish(self, client, userdata, mid):
        click.echo(f"Published by {userdata['client']}, mid: {mid}")

    def listen(self):
        try:
            self.client.loop_forever()
        except KeyboardInterrupt:
            click.echo(f"Received KeyboardInterrupt, disconnecting {self.config.mqtt_client}")
            self.client.disconnect()
Example #27
0
def main():
    try:
        client = Client("Misol")  # create new instance
        client.connect("192.168.1.3")  # connect to broker

        client.publish("/devices/misol/meta/name",
                       "Misol Meteostation",
                       qos=0,
                       retain=True)

        for order, topic in enumerate(TOPICS, 1):
            publish(client, topic, order)

        log.debug("Connected to MQTT")

        s = Serial('/dev/ttyUSB0', 9600, timeout=60)

        log.debug("Connected to serial")

        while True:
            log.debug("Start Read")

            raw = s.read(21)

            checksum = sum(i for i in raw[:16]) & 0xFF
            assert checksum == raw[16], "Wrong checksum"

            wd = wdata.from_buffer_copy(raw)
            rwd: RawWeatherData = wd.rawdata

            wind = ((wd.rawdata.WSP8 << 8) + wd.rawdata.WIND) / 8 * 1.12
            uvi = next((i for i, v in enumerate(UVI) if rwd.UVI <= v), 13)

            payload = {
                'wind_direction': (wd.rawdata.DIR8 << 8) + wd.rawdata.DIR,
                'battery_low': rwd.BAT,
                'temperature': (rwd.TMP - 400) / 10.0,
                'humidity': rwd.HM,
                'wind_speed': round(wind),
                'wind_gust': round(rwd.GUST * 1.12),
                'rain': rwd.RAIN,
                'uvi': uvi,
                'light': round(rwd.LIGHT / 10.0),
                'bar': round(rwd.BAR / 100.0, 2),
                'last_update': int(time.time())
            }

            for k, v in payload.items():
                info = client.publish(f"{BASE}/{k}", v, retain=True)
                assert info.rc == MQTT_ERR_SUCCESS, f"MQTT Error: {info.rc}"

            log.debug("Updated MQTT")

    except AssertionError as e:
        log.error(e)

    except:
        log.exception("Exception")
Example #28
0
class mqtt_client_connect():
    def __init__(self,
                 broker="10.129.7.199",
                 port=1883,
                 username="******",
                 password="******",
                 client_id="12345"):
        self.broker = broker
        self.port = port
        self.username = username
        self.password = password
        self.payload = None
        self.client_id = client_id
        self.num = 1
        self.flag = 0
        try:
            # self.mqttc=Client(clean_session=False,client_id="12345")
            self.mqttc = Client(client_id=self.client_id)
            self.mqttc.on_connect = self.on_connect
            self.mqttc.on_publish = self.on_publish
            self.mqttc.on_subscribe = self.on_subscribe
            self.mqttc.username_pw_set(self.username, self.password)
            self.mqttc.connect(self.broker, port=self.port)
            self.mqttc.loop_start()
        except:
            print(
                "mqtt_client_connect error: mqttc connect failed Please check Broker and Port...."
            )
# ======================================================

    def on_connect(self, client, userdata, flags, rc):
        #rc为0 返回连接成功
        if rc == 0:
            self.flag = 1
            print("OnConnetc, rc: " + str(rc),
                  'successful  ' + str(client._username))
        else:
            self.flag = 0
            print("OnConnetc, rc: " + str(rc),
                  'unsuccessful  ' + str(client._username))

    def on_disconnect(self, client, userdata, rc):
        if rc != 0:
            self.flag = 0
            print("Unexpected MQTT disconnection. Will auto-reconnect")

    def on_publish(self, client, userdata, mid):
        print("OnPublish, mid: " + str(mid) + " " + str(client._username))

    def on_subscribe(self, client, userdata, mid, granted_qos):
        print("Subscribed: " + str(mid) + "   " + str(granted_qos) +
              "  订阅成功 " + str(client._username))
        self.mqttc.on_message = self.on_message

    def on_message(self, client, userdata, msg):
        strcurtime = time.strftime("%Y-%m-%d %H:%M:%S")
        print(strcurtime + ": " + msg.topic + " " + str(msg.qos) + " " +
              str(msg.payload) + str(client._username))
def create_client(host, port, username, password):
    """Creating an MQTT Client Object"""
    client = MqttClient()

    if username and password:
        client.username_pw_set(username=username, password=password)

    client.connect(host=host, port=port)
    return client
Example #30
0
 def __subscribe(self):
     ip_list = self.settings.get("slaves", ["127.0.0.1"])
     for ip in ip_list:
         client = Client()
         client.connect(ip, 1883, self.settings.get("mqtt_timeout", 60))
         client.on_connect = self.__on_connect
         client.on_message = self.__on_message
         client.loop_start()
         self.client_list.append(client)
Example #31
0
def main():
    influxdb_client = InfluxDBClient(INFLUXDB_HOST, INFLUXDB_PORT, INFLUXDB_USERNAME, INFLUXDB_PASSWORD, INFLUXDB_DATABASE) #First the database is initialized
    mqtt_client = MQTTClient( MQTT_CLIENT_ID, userdata=influxdb_client) #Then we create a client object
    mqtt_client.username_pw_set(MQTT_USERNAME, MQTT_PASSWORD) #and set the username and password for the MQTT client
    mqtt_client.tls_set()
    mqtt_client.on_connect = mqtt_connect_callback #We tell the client which functions are to be run on connecting
    mqtt_client.on_message = mqtt_message_callback #and on receiving a message
    mqtt_client.connect(MQTT_HOST, MQTT_PORT) #we can connect to the broker with the broker host and port
    mqtt_client.loop_forever()
def publish_job():
    """ 打卡任务 """
    client = Client()
    client.on_connect = on_connect
    with open('captured_packet.json', encoding='utf-8') as f:
        packet_info = json.load(f)
    # 连接代理服务器
    print('🙈 正在连接代理服务器...')
    client.connect(packet_info['dst host'], 1883)
    client.loop_forever()
Example #33
0
class MQTT(Component):

    def init(self, url):
        self.url = url

        host, port = parse_bind(self.url)

        self.client = Client()
        self.client.on_connect = self._on_connect
        self.client.on_message = self._on_message

        self.client.connect(host, port)
        self.client.loop_start()

    def _on_connect(self, client, userdata, flags, rc):
        print("Connected with result code {0}".format(rc))

    def _on_message(self, client, userdata, msg):
        print("{0} {1}".format(msg.topic, msg.payload))
Example #34
0
class MQTTEventSink(EventSink):
    def __init__(self, broker,
                topic="iot-1/d/%012x/evt/%s/json",
                hostname=None,
                hostport=1883,
                username=None,
                password=None,
                keepalive=60):
        EventSink.__init__(self, broker)
        self._client = Paho()
        self._client.on_connect = \
                lambda mosq, obj, rc: self._on_connect(mosq, obj, rc)
        self._client.on_disconnect = \
                lambda mosq, obj, rc: self._on_disconnect(mosq, obj, rc)
        self._client.on_publish = \
                lambda mosq, obj, mid: self._on_publish(mosq, obj, mid)
        self._topic_format = topic
        self._topic = self._topic_format % (0, "%s")

        self._hostname = hostname
        self._hostport = hostport
        self._username = username
        self._password = password
        self._keepalive = keepalive

        self._loopflag = False
        self._neta = None

    def _on_connect(self, mosq, obj, rc):
    	self._topic = self._topic_format % (get_mac(), "%s")
        log.debug("MQTT publisher connected: " + str(rc))

    def _on_disconnect(self, mosq, obj, rc):
        log.debug("MQTT publisher disconnected: " + str(rc))

    def _on_publish(self, mosq, obj, mid):
        #log.debug("MQTT publisher published: " + str(mid))
        pass

    def _try_connect(self):
        if self._username is not None and self._password is not None:
            self._client.username_pw_set(self._username, self._password)
        try:
            self._client.connect(self._hostname, self._hostport, self._keepalive)
        except socket.gaierror:
            return False
        self._client.loop_start()
        self._loopflag = True
        return True

    def on_start(self):
        return self._try_connect()

    def send(self, encoded_event):
        # Fill in the blank "%s" left in self._topic
        import json

        # extract the actual topic string
        event = json.loads(encoded_event)
        topic_event_type = event["d"]["event"]
        topic = self._topic % topic_event_type

        # Check to see if event is from neighbors 
        # and need to be published to Mqtt server
        if "published" in event["d"]:
            if event["d"]["published"] == 1:
                return True
            else:
                del event["d"]["published"]

        # Publish message
        res, mid = self._client.publish(topic, encoded_event)
        if res == paho.mqtt.client.MQTT_ERR_SUCCESS:
            log.info("MQTT message published to " + topic)
        elif res == paho.mqtt.client.MQTT_ERR_NO_CONN:
            log.error("MQTT publisher failure: No connection")
            return False
        else:
            log.error("MQTT publisher failure: Unknown error")
            return False
        return True

    def check_available(self, event):
        if self._neta is not None and not self._neta:
            return False
        if not self._loopflag:
            if not self._try_connect():
                log.error("MQTT publisher failure: Cannot connect")
                return False
        return True

    def on_event(self, event, topic):
        et = event.get_type()
        ed = event.get_raw_data()

        if et == "internet_access":
            self._neta = ed

    def encode_event(self, event):
        # return event.to_json()
        return json.dumps({"d": event.to_map()})
class JMSClient(object):
    """Class JMSClient
    """

    _mh = None
    _client = None
    _host = None
    _port = None
    _user = None
    _passw = None
    _verbose = None
    _is_connected = None
    _messages = []

    def __init__(self, verbose=False):
        """Class constructor

        Called when the object is initialized

        Args:                   
           verbose (bool): verbose mode

        """

        try:

            self._mh = MasterHead.get_head()

            self._client = Client()
            self._client.on_message = self._on_message

            self._verbose = verbose
            if (self._verbose):
                self._client.on_log = self._on_log

        except MQTTException as ex:
            self._mh.demsg('htk_on_error', ex, self._mh.fromhere())

    @property
    def client(self):
        """ MQTT client property getter """

        return self._client

    @property
    def host(self):
        """ server host property getter """

        return self._host

    @property
    def port(self):
        """ server port property getter """

        return self._port

    @property
    def user(self):
        """ username property getter """

        return self._user

    @property
    def passw(self):
        """ user password property getter """

        return self._passw

    @property
    def verbose(self):
        """ verbose property getter """

        return self._verbose

    @property
    def is_connected(self):
        """ is_connected property getter """

        return self._is_connected

    def _on_log(self, client, obj, level, string):
        """ Callback for on_log event """

        print(string)

    def _on_message(self, client, obj, msg):
        """ Callback for on_message event """

        self._messages.append(msg.payload.decode())

    def connect(self, host, port=1883, user=None, passw=None, timeout=10):
        """Method connects to server

        Args:
           host (str): hostname
           port (str): port
           user (str): username
           passw (str): password
           timeout (int): timeout

        Returns:
           bool: result

        Raises:
           event: jms_before_connect
           event: jms_after_connected            

        """

        try:

            msg = 'host:{0}, port:{1}, user:{2}, passw:{3}, timeout:{4}'.format(
                host, port, user, passw, timeout)
            self._mh.demsg('htk_on_debug_info', self._mh._trn.msg(
                'htk_jms_connecting', msg), self._mh.fromhere())

            ev = event.Event(
                'jms_before_connect', host, port, user, passw, timeout)
            if (self._mh.fire_event(ev) > 0):
                host = ev.argv(0)
                port = ev.argv(1)
                user = ev.argv(2)
                passw = ev.argv(3)
                timeout = ev.argv(4)

            self._host = host
            self._port = port
            self._user = user
            self._passw = passw

            if (ev.will_run_default()):
                if (self._user != None):
                    self._client.username_pw_set(self._user, self._passw)

                setdefaulttimeout(timeout)
                self._client.connect(self._host, self._port)
                self._is_connected = True

            self._mh.demsg('htk_on_debug_info', self._mh._trn.msg(
                'htk_jms_connected'), self._mh.fromhere())
            ev = event.Event('jms_after_connect')
            self._mh.fire_event(ev)

            return True

        except (MQTTException, error, ValueError) as ex:
            self._mh.demsg('htk_on_error', ex, self._mh.fromhere())
            return False

    def disconnect(self):
        """Method disconnects from server 

        Args:   
           none       

        Returns:
           bool: result

        """

        try:

            self._mh.demsg('htk_on_debug_info', self._mh._trn.msg(
                'htk_jms_disconnecting'), self._mh.fromhere())

            if (not self._is_connected):
                self._mh.demsg('htk_on_warning', self._mh._trn.msg(
                    'htk_jms_not_connected'), self._mh.fromhere())
                return False
            else:
                self._client.disconnect()
                self._is_connected = False
                self._mh.demsg('htk_on_debug_info', self._mh._trn.msg(
                    'htk_jms_disconnected'), self._mh.fromhere())
                return True

        except (MQTTException, error, ValueError) as ex:
            self._mh.demsg('htk_on_error', ex, self._mh.fromhere())
            return False

    def send(self, destination_name, message):
        """Method sends message

        Args:
           destination_name (str): topic name
           message (str): message

        Returns:
           bool: result

        Raises:
           event: jms_before_send
           event: jms_after_send             

        """

        try:

            msg = 'destination_name:{0}, message:{1}'.format(
                destination_name, message)
            self._mh.demsg('htk_on_debug_info', self._mh._trn.msg(
                'htk_jms_sending_msg', msg), self._mh.fromhere())

            if (not self._is_connected):
                self._mh.demsg('htk_on_warning', self._mh._trn.msg(
                    'htk_jms_not_connected'), self._mh.fromhere())
                return False

            ev = event.Event('jms_before_send', destination_name, message)
            if (self._mh.fire_event(ev) > 0):
                destination_name = ev.argv(0)
                message = ev.argv(1)

            if (ev.will_run_default()):
                res, id = self._client.publish(destination_name, message)

                if (res != 0):
                    self._mh.demsg('htk_on_error', self._mh._trn.msg(
                        'htk_jms_sending_error'), self._mh.fromhere())

            self._mh.demsg('htk_on_debug_info', self._mh._trn.msg(
                'htk_jms_msg_sent'), self._mh.fromhere())
            ev = event.Event('jms_after_send')
            self._mh.fire_event(ev)

            return True

        except (MQTTException, error, ValueError) as ex:
            self._mh.demsg('htk_on_error', ex, self._mh.fromhere())
            return False

    def receive(self, destination_name, cnt=1, timeout=10):
        """Method receives messages

        Args:
           destination_name (str): queue name
           cnt (int): count of messages
           timeout (int): timeout to receive message

        Returns:
           list:  messages

        Raises:
           event: jms_before_receive
           event: jms_after_receive             

        """

        try:

            msg = 'destination_name:{0}, count:{1}'.format(
                destination_name, cnt)
            self._mh.demsg('htk_on_debug_info', self._mh._trn.msg(
                'htk_jms_receiving_msg', msg), self._mh.fromhere())

            if (not self._is_connected):
                self._mh.demsg('htk_on_warning', self._mh._trn.msg(
                    'htk_jms_not_connected'), self._mh.fromhere())
                return None

            ev = event.Event('jms_before_receive', destination_name, cnt)
            if (self._mh.fire_event(ev) > 0):
                destination_name = ev.argv(0)
                cnt = ev.argv(1)

            if (ev.will_run_default()):
                res, id = self._client.subscribe(destination_name)
                if (res != 0):
                    self._mh.demsg('htk_on_error', self._mh._trn.msg(
                        'htk_jms_sending_error'), self._mh.fromhere())
                    return None

                res = 0
                cnt_before = 0
                start = time()
                while (res == 0):
                    res = self._client.loop()
                    cnt_after = len(self._messages)
                    if (cnt_after > cnt_before and cnt_after < cnt):
                        cnt_before = cnt_after
                    elif (cnt_after == cnt or time() > start + timeout):
                        res = -1

                messages = self._messages
                self._client.unsubscribe(destination_name)
                self._messages = []

            self._mh.demsg('htk_on_debug_info', self._mh._trn.msg(
                'htk_jms_msg_received', len(messages)), self._mh.fromhere())
            ev = event.Event('jms_after_receive')
            self._mh.fire_event(ev)

            return messages

        except (MQTTException, error, ValueError) as ex:
            self._mh.demsg('htk_on_error', ex, self._mh.fromhere())
            return None
Example #36
0
class Messenger(object):
    """
    MQTT client for Herald transport.
    """

    def __init__(self, peer):
        """
        Initialize client
        :param peer: The peer behind the MQTT client.
        :return:
        """
        self.__peer = peer
        self.__mqtt = MqttClient()
        self.__mqtt.on_connect = self._on_connect
        self.__mqtt.on_disconnect = self._on_disconnect
        self.__mqtt.on_message = self._on_message
        self.__callback_handler = None
        self.__WILL_TOPIC = "/".join(
            (TOPIC_PREFIX, peer.app_id, RIP_TOPIC))

    def __make_uid_topic(self, subtopic):
        """
        Constructs a complete UID topic.
        :param subtopic: The UID
        :return: Fully qualified topic
        :rtype : str
        """
        return "/".join(
            (TOPIC_PREFIX, self.__peer.app_id, UID_TOPIC, subtopic))

    def __make_group_topic(self, subtopic):
        """
        Constructs a complete group topic.
        :param subtopic: The group name
        :return: Fully qualified topic
        :rtype : str
        """
        return "/".join(
            (TOPIC_PREFIX, self.__peer.app_id, GROUP_TOPIC, subtopic))

    def __handle_will(self, message):
        if self.__callback_handler and self.__callback_handler.on_peer_down:
            self.__callback_handler.on_peer_down(
                message.payload.decode('utf-8'))
        else:
            _log.debug("Missing callback for on_peer_down.")

    def _on_connect(self, *args, **kwargs):
        """
        Handles a connection-established event.
        :param args: unnamed arguments
        :param kwargs: named arguments
        :return:
        """
        _log.info("Connection established.")
        _log.debug("Subscribing for topic %s.",
                   self.__make_uid_topic(self.__peer.uid))
        self.__mqtt.subscribe(self.__make_uid_topic(self.__peer.uid))
        self.__mqtt.subscribe(self.__make_group_topic("all"))
        self.__mqtt.subscribe(self.__WILL_TOPIC)
        for group in self.__peer.groups:
            _log.debug("Subscribing for topic %s.",
                       self.__make_group_topic(group))
            self.__mqtt.subscribe(self.__make_group_topic(group))
        if self.__callback_handler and self.__callback_handler.on_connected:
            self.__callback_handler.on_connected()
        else:
            _log.warning("Missing callback for on_connect.")

    def _on_disconnect(self, *args, **kwargs):
        """
        Handles a connection-lost event.
        :param args: unnamed arguments
        :param kwargs: named arguments
        :return:
        """
        _log.info("Connection lost.")
        if self.__callback_handler and self.__callback_handler.on_disconnected:
            self.__callback_handler.on_disconnected()

    def _on_message(self, client, data, message):
        """
        Handles an incoming message.
        :param client: the client instance for this callback
        :param data: the private user data
        :param message: an instance of MQTTMessage
        :type message: paho.mqtt.client.MQTTMessage
        :return:
        """
        _log.info("Message received.")
        if message.topic == self.__WILL_TOPIC:
            self.__handle_will(message)
            return
        if self.__callback_handler and self.__callback_handler.on_message:
            self.__callback_handler.on_message(message.payload.decode('utf-8'))
        else:
            _log.warning("Missing callback for on_message.")

    def fire(self, peer_uid, message):
        """
        Sends a message to another peer.
        :param peer_uid: Peer UID
        :param message: Message content
        :return:
        """
        self.__mqtt.publish(
            self.__make_uid_topic(peer_uid),
            message,
            1
        )

    def fire_group(self, group, message):
        """
        Sends a message to a group of peers.
        :param group: Group's name
        :param message: Message content
        :return:
        """
        self.__mqtt.publish(
            self.__make_group_topic(group),
            message,
            1
        )

    def set_callback_listener(self, listener):
        """
        Sets callback listener.
        :param listener: the listener
        :return:
        """
        self.__callback_handler = listener

    def login(self, username, password):
        """
        Set credentials for an MQTT broker.
        :param username: Username
        :param password: Password
        :return:
        """
        self.__mqtt.username_pw_set(username, password)

    def connect(self, host, port):
        """
        Connects to an MQTT broker.
        :param host: broker's host name
        :param port: broker's port number
        :return:
        """
        _log.info("Connecting to MQTT broker at %s:%s ...", host, port)
        self.__mqtt.will_set(self.__WILL_TOPIC, self.__peer.uid, 1)
        self.__mqtt.connect(host, port)
        self.__mqtt.loop_start()

    def disconnect(self):
        """
        Diconnects from an MQTT broker.
        :return:
        """
        _log.info("Disconnecting from MQTT broker...")
        self.__mqtt.publish(self.__WILL_TOPIC, self.__peer.uid, 1)
        self.__mqtt.loop_stop()
        self.__mqtt.disconnect()
Example #37
0
class PipaMQTTClient(object): 

    def __init__(self, client_id, host, port=1883, **kwargs):
        self._logger = logging.getLogger("gsensors.PipaMQTTClient")
        self._host = host
        self._port = port
        self._mqtt_client = Client(client_id=client_id, clean_session=False)
        self._mqtt_client.on_connect = self.on_connect
        self._mqtt_client.on_disconnect = self.on_disconnect
        self._mqtt_client.on_message = self.on_message

        self.worker = None
        self.worker_runner = None
        self.topics_sources = {}
        self.running = False
        self.connected = False

    def publish(self, topic, payload=None, qos=0, retain=False):
        # check connected
        if not self.connected:
            raise RuntimeError("MQTT client not connected ! ")
        if not self.running:
            raise RuntimeError("MQTT client not running ! ")
        self._mqtt_client.publish(topic, payload=payload, qos=qos, retain=retain)

    def PublishAction(self, topic, payload=None, retain=False):
        if payload is None:
            def _action(source, value):
                data = "%s" % value
                self._logger.debug("publish %s: %s" % (topic, data))
                self.publish(topic, payload=data, retain=retain)
        else:
            def _action(*args, **kwargs):
                self._logger.debug("publish %s: %s" % (topic, payload))
                self.publish(topic, payload=payload, retain=retain)
        return _action

    def on_disconnect(self, client, userdata, rc):
        self._logger.error("Disconnected with result code: "+str(rc))
        self.connected = False
        self.connect()

    def on_connect(self, client, userdata, flags, rc):
        if rc == 0:
            self._logger.info("Connected to MQTT broker")
            self.connected = True
            # Subscribing in on_connect() means that if we lose the connection and
            # reconnect then subscriptions will be renewed.
            #client.subscribe("$SYS/#")
            #client.subscribe("#")
            for topic in self.topics_sources.keys():
                client.subscribe(topic)
        else:
            self._logger.error("Connection fail with error: %s" % rc)

    def on_message(self, client, userdata, msg):
        self._logger.debug("get a msg %s: %s" % (msg.topic, msg.payload))
        if msg.topic in self.topics_sources:
            source = self.topics_sources[msg.topic]
            source.update(msg)

    def register_source(self, source, topic):
        # check that topic has no wildcard
        assert "#" not in topic
        if topic in self.topics_sources:
            raise ValueError("topic already monitored")
        self.topics_sources[topic] = source
        self._mqtt_client.subscribe(topic)
        return source

    def connect(self):
        self.worker_runner = gevent.spawn(self._connect)

    def _connect(self):
        while not self.connected:
            try:
                #self._mqtt_client.reinitialise()
                self._mqtt_client.connect(host=self._host, port=self._port, keepalive=60)
            except socket.error as err:
                self._logger.error("Imposible to connect to MQTT: %s" % err)
                self._logger.info("Will retry in 2 seconds")
            gevent.sleep(2)

    def wait_connected(self):
        while not self.connected:
            gevent.sleep(1)

    def start(self):
        if self.running:
            return
        self.running = True
        self.worker = gevent.spawn(self._mqtt_loop)
        self.connect()

    def _mqtt_loop(self):
        while self.running:
            self._mqtt_client.loop(timeout=0.02)
            gevent.sleep(0.01)
Example #38
0
from paho.mqtt.client import Client as MQTTClient

from client.models import ServerAPIModel
from client.core import make_device_client


# Init mqtt client
mqtt_client = MQTTClient('paho_lol')
mqtt_client.connect('mqtt.acandale.com', 1883, 6000)

# Create the model
model = ServerAPIModel('http://localhost:8000', 'test')

# Create the client
cl = make_device_client(model, 'my_client', mqtt_client)

# Call methods
cl.no_argument_handler('payload')
cl.one_argument_handler('payload', 'first_arg')
cl.two_argument_handler('payload', 'first_arg', 'second_arg')
class MqttApplication(Application):
    """
    An abstract base Application for running some type of MQTT client-based Application.
    """

    def __init__(self, broker,
                hostname=None,
                hostport=1883,
                username=None,
                password=None,
                keepalive=60,
                **kwargs):
        super(MqttApplication, self).__init__(broker=broker, **kwargs)
        self._client = MqttClient()
        self._client.on_connect = \
                lambda mqtt_client, obj, rc: self._on_connect(mqtt_client, obj, rc)
        self._client.on_disconnect = \
                lambda mqtt_client, obj, rc: self._on_disconnect(mqtt_client, obj, rc)
        self._client.on_publish = \
                lambda mqtt_client, obj, mid: self._on_publish(mqtt_client, obj, mid)
        self._client.on_subscribe = \
                lambda mqtt_client, userdata, mid, qos: self._on_subscribe(mqtt_client, mid, qos)
        self._client.on_message = \
                lambda mqtt_client, userdata, msg: self._on_message(mqtt_client, msg.payload, msg.topic, msg.qos, msg.retain)

        self._hostname = hostname
        self._hostport = hostport
        self._username = username
        self._password = password
        self._keepalive = keepalive

        self._is_connected = False

    @property
    def is_connected(self):
        return self._is_connected

    # Your API for pub-sub via MQTT:

    def mqtt_publish(self, raw_data, topic, **kwargs):
        """
        Publishes the given data to the specified topic.
        :param raw_data:
        :type raw_data: str
        :param topic:
        :param kwargs: e.g. qos, retain; passed to self._client.publish
        :return: err_code, msg_id
        """
        return self._client.publish(topic, raw_data, **kwargs)

    def mqtt_subscribe(self, topic, **kwargs):
        """
        Subscribes to the specified topic.
        :param topic:
        :param kwargs: e.g. qos; passed to self._client.publish
        :return: err_code, msg_id
        """
        return self._client.subscribe(topic, **kwargs)

    # To make use of either publish or subscribe, make sure to implement these!

    def _on_publish(self, mqtt_client, obj, mid):
        log.debug("MQTT app %s published msg %d: %s" % (self.name, mid, obj))

    def _on_subscribe(self, mqtt_client, mid, qos):
        log.debug("MQTT app %s subscribe response msg %d: qos=%s" % (self.name, mid, qos))

    def _on_message(self, mqtt_client, payload, topic, qos, retain):
        """Called when a message arrives on a topic we subscribed to."""
        log.debug("MQTT app %s received message on topic %s: %s" % (self.name, topic, payload))

    # You may want to override these functions in your concrete Application, but make sure to call super()!

    def _on_connect(self, mqtt_client, obj, rc):
        log.debug("MQTT app %s connected: %s" % (self.name, str(rc)))
        # need to start AFTER connecting, which is async!
        self._is_connected = True

    def _on_disconnect(self, mqtt_client, obj, rc):
        # sink will try reconnecting once EventReporter queries if it's available.
        self._is_connected = False
        log.debug("MQTT app %s disconnected: %s" % (self.name, str(rc)))

    def _try_connect(self):
        if self._username is not None and self._password is not None:
            self._client.username_pw_set(self._username, self._password)
        try:
            # NOTE: this is an async connection!
            self._client.connect(self._hostname, self._hostport, self._keepalive)
            self._client.loop_start()
        except socket.gaierror:
            return False
        return True

    def on_start(self):
        super(MqttApplication, self).on_start()
        return self._try_connect()
from paho.mqtt.client import Client

def on_connect(client, userdata, rc):
    client.subscribe("#")
def on_message(client, userdata, msg):
    print(msg.topic+" "+str(msg.payload))
client = Client()
client.on_message = on_message
client.on_connect = on_connect
client.connect("broker.mqttdashboard.com")
client.loop_forever()