Пример #1
0
def connect():
    global CLIENT_ID, SERVER, PORT, USERNAME, PASSWORD, TOPIC_PUB
    client = MQTTClient(CLIENT_ID, SERVER, PORT, USERNAME, PASSWORD)
    client.set_callback(sub_cb)
    client.connect()
    client.subscribe(TOPIC_SUB)
    return client
Пример #2
0
def connect_and_subscribe():
  global CLIENT_ID, BROKER_ADDRESS, ROOT_TOPIC, client, SWITCH_PIN
  try:
    SWITCH_PIN.value(0)
    client = MQTTClient(CLIENT_ID, BROKER_ADDRESS)
    client.set_callback(received)
    client.connect()
    time.sleep(2)
    print("connecting")
    client.subscribe(ROOT_TOPIC + b'#')
    print('Connected to %s MQTT broker, subscribed to %s topic' % (BROKER_ADDRESS, ROOT_TOPIC))
    SWITCH_PIN.value(1)
    client.publish(ROOT_TOPIC + b'version', 'Version 2021.091')
    return client
  except:
    SWITCH_PIN.value(1)
    time.sleep(1)
    SWITCH_PIN.value(0)
    time.sleep(1)
    SWITCH_PIN.value(1)
    time.sleep(1)
    SWITCH_PIN.value(0)
    time.sleep(1)
    SWITCH_PIN.value(1)
    time.sleep(1)
    SWITCH_PIN.value(0)
    return None
Пример #3
0
def connect_and_subscribe():
    global client_id, topic_led
    client = MQTTClient(client_id, Config.mqtt_server, user=Config.mqtt_user, password=Config.mqtt_pass)
    client.set_callback(sub_cb)
    client.connect(clean_session=False)
    client.subscribe(topic_led)
    return client
Пример #4
0
def connect_and_subscribe():
  global CLIENT_ID, SERVER, PORT, USERNAME, PASSWORD, TOPIC_PUB
  client=MQTTClient(CLIENT_ID, SERVER,PORT , USERNAME, PASSWORD)
  client.set_callback(sub_cb)
  client.connect()
  client.subscribe(TOPIC_SUB)
  print('Connected to %s MQTT broker, subscribed to %s topic' % (SERVER, TOPIC_SUB))
  return client
Пример #5
0
def connect_and_subscribe():
  global client, client_id, mqtt_server, topic_sub,  topic_subLed
  client = MQTTClient(client_id, mqtt_server)
  client.set_callback(sub_CheckTopics)
  client.connect()
  client.subscribe(topic_subLed)
  print('Connected to %s MQTT broker, subscribed to %s topic' % (mqtt_server, topic_subLed))
  return client
Пример #6
0
 def connect_and_subscribe():
   global client_id, mqtt_server, topic_sub
   client = MQTTClient(client_id, mqtt_server)
   client.set_callback(sub_cb)
   client.connect()
   client.subscribe(topic_sub)
   print('Connected1 to %s MQTT broker, subscribed to %s topic' % (mqtt_server, topic_sub))
   return client
Пример #7
0
 def connect_and_subscribe(self):
     client = MQTTClient(self.client_id, self.mqtt_server)
     client.set_callback(self.sub_cb)
     client.connect()
     client.subscribe(self.topic_sub)
     print('MQTT: Connected to %s MQTT broker, subscribed to %s topic' %
           (self.mqtt_server, self.topic_sub))
     return client
Пример #8
0
def connect_and_subscribe():
  global client_id, mqtt_server, topic_sub, topic_pub
  client = MQTTClient(client_id, mqtt_server, 1883, 'janus', 'Janus506')
  client.set_last_will(topic_pub, '{\"msg\":\"offline\"}')
  client.set_callback(sub_cb)
  client.connect()
  client.subscribe(topic_sub)
  print('Connected to %s MQTT broker, subscribed to %s topic' % (mqtt_server, topic_sub))
  return client
Пример #9
0
def connect_and_subscribe():
    global client_id, mqtt_server, topic_sub
    client = MQTTClient(client_id, mqtt_server)
    client.set_callback(sub_cb)
    client.connect()
    for i in subscribe_list:
        client.subscribe(i)
        print('Connected to %s MQTT broker, subscribed to %s topic\n' % (mqtt_server, i))
    return client
Пример #10
0
def connect_and_subscribe(CLIENT_ID, MQTT_SERVER):
    client = MQTTClient(CLIENT_ID, MQTT_SERVER)
    client.set_callback(subscrition_callback)

    client.connect()
    client.subscribe("client/{}/check".format(CLIENT_ID))

    print("Connected to MQTT broker {}".format(MQTT_SERVER))
    return client
Пример #11
0
def connect_and_subscribe():
    client = MQTTClient(client_id, MQTT_SERVER)
    client.set_callback(sub_cb)
    client.connect()
    client.subscribe('servo')

    print('Connected to MQTT')
    client.publish('esp32', 'connected!')

    return client
Пример #12
0
def connect_and_subscribe():
    global client_id, mqtt_server, topic_sub
    client = MQTTClient(client_id,
                        mqtt_server,
                        user='******',
                        password='******')
    client.set_callback(sub_cb)
    client.connect()
    client.subscribe(topic_sub)
    return client
Пример #13
0
def connect_and_subscribe():
    global SERVER, TOPIC
    server = SERVER
    # c = MQTTClient(CLIENT_ID, server,0,username,password)     #create a mqtt client
    c = MQTTClient(CLIENT_ID, server, 0)  #create a mqtt client
    c.set_callback(sub_cb)  #set callback
    c.connect()  #connect mqtt
    c.subscribe(TOPIC)  #client subscribes to a topic
    print("Connected to %s, subscribed to %s topic" % (server, TOPIC))
    return c
Пример #14
0
def connect_and_subscribe(subtopic):
    global client_id, mqtt_server, mqtt_port, mqtt_user, mqtt_password, topic_sub
    client = MQTTClient(client_id, mqtt_server, mqtt_port, mqtt_user,
                        mqtt_password, keepalive)
    client.set_callback(sub_cb)
    client.connect()
    subscribeTotopic = topic_sub + subtopic
    client.subscribe(subscribeTotopic)
    print('Connected to %s MQTT broker' % (mqtt_server))
    print('subscribed to %s topic' % (subscribeTotopic))
    return client
Пример #15
0
def connect_and_subscribe():
    client = MQTTClient(client_id=client_id,
                        server=mqtt_server,
                        port=mqtt_port,
                        user=mqtt_user,
                        password=mqtt_password)
    client.set_callback(sub_cb)
    client.connect()
    client.subscribe(topic_sub)
    print('Connected to %s MQTT broker, subscribed to %s topic' %
          (mqtt_server, topic_sub))
    return client
Пример #16
0
def connect_and_subscribe():
    client_id = ubinascii.hexlify(machine.unique_id())
    client = MQTTClient(client_id,
                        config['server'],
                        port=config['port'],
                        user=config['user'],
                        password=config['pswd'])
    client.set_callback(sub_cb)
    client.connect()
    client.subscribe(config['pub_topic'])
    print('Connected to %s MQTT broker, subscribed to %s topic' %
          (config['server'], config['pub_topic']))
    return client
Пример #17
0
    def connect_to_mqtt(self, publish_discovery):
        client = MQTTClient(self.MQTT_CLIENT_ID, self.MQTT_SERVER)
        client.connect()

        client.set_callback(self.on_mqtt_msg)
        client.subscribe(self.MQTT_TOPIC_CMD)

        if publish_discovery:
            discovery = {
                "name": "standingdesk",
                "state_topic": self.MQTT_TOPIC_STATE,
                "unit_of_measurement": "cm"
            }
            client.publish(self.MQTT_TOPIC_DISCOVERY, json.dumps(discovery))
        return client
Пример #18
0
def connect_mqtt():
    global client_id, mqtt_server
    print("Cliente ID: %s" % (client_id))
    #client = MQTTClient(client_id, mqtt_server)
    client = MQTTClient(client_id,
                        mqtt_server,
                        1883,
                        user="******",
                        password="******")
    # Esta funcao sera chamada quando uma mensagem for recebida.
    client.set_callback(sub_cb)
    client.connect()
    print("Conectado ao broker MQTT %s" % (mqtt_server))
    client.subscribe(topic_sub_led)
    return client
Пример #19
0
def connect_and_subscribe():
    global client, client_id, mqtt_server, topic_sub, topic_subLedRGB, topic_subLed
    try:
        client = MQTTClient(client_id, mqtt_server)
        client.set_callback(sub_CheckTopics)
        client.connect()
        #        client.subscribe(topic_subFree)
        #        client.subscribe(topic_subLed)
        client.subscribe(topic_subLedRGB)
        #        print('Connected to %s MQTT broker, subscribed to %s topic' % (mqtt_server, topic_subLedRGB))
        return client
    except KeyboardInterrupt:
        pass
    except Exception as e:
        myLog('Connect&Subscribe>' + str(e))
        restart_and_reconnect()
Пример #20
0
def connect_and_subscribe():
    global client, client_id, topic_sub, topic_subLedRGB, topic_subLed
    try:
        client = MQTTClient(client_id, config.MQTT_SERVER)
        client.set_callback(sub_CheckTopics)
        client.connect()
        client.subscribe(topic_subFree)
        client.subscribe(topic_subCaldera)
        myLog('Connected to %s MQTT broker, subscribed to %s topic' %
              (config.MQTT_SERVER, topic_subCaldera))
        return client
    except KeyboardInterrupt:
        pass
    except Exception as e:
        myLog('Connect&Subscribe>' + str(e))
        restart_and_reconnect()
Пример #21
0
def connect_and_subscribe():

    print("[-] Connecting to MQTT client ...")
    # set the MQTT broker object
    client = MQTTClient()
    # set a callback for incoming messages (subscribed topics)
    client.set_callback(on_message_callback)
    # connect to the broker
    client.connect()

    # subscribe to the topics
    for topic in topics:
        client.subscribe("%s/%s" % (UUID, topic))
        print("[-] Subscribed to %s successfully" % topic)
        print("[-] Connected to %s MQTT broker successfully" % client.server)

    return client
Пример #22
0
def main(server=SERVER):
    c = MQTTClient(CLIENT_ID,
                   server,
                   user='******',
                   password='******',
                   keepalive=alive_time)
    c.set_callback(sub_cb)
    c.connect()
    c.subscribe(TOPIC1)
    c.subscribe(TOPIC2)
    c.subscribe(TOPIC3)
    c.subscribe(TOPIC4)
    # retain the last published value by MQTT broker
    c.publish(b"icreate/feeds/wecreate.relay1/get", b"hello")
    c.publish(b"icreate/feeds/wecreate.relay2/get", b"hello")
    c.publish(b"icreate/feeds/wecreate.relay3/get", b"hello")
    c.publish(b"icreate/feeds/wecreate.relay4/get", b"hello")
    print("Connected to %s, subscribed to %s, %s, %s and %s topics" %
          (server, TOPIC1, TOPIC2, TOPIC3, TOPIC4))

    # function to continuously ping the MQTT broker in order to keep the connection alive
    def ping_wait():
        while True:
            for i in range(alive_time):
                time.sleep(1)
            c.ping()
            print("Pinging...")
            #for i in range(4):
            #pingLed.value(not pingLed.value())
            #time.sleep(0.1)

    # start a thread to ping the broker
    th.start_new_thread(ping_wait, ())

    try:
        while True:
            c.wait_msg()

    except OSError as e:
        print("Reconnecting...")
        machine.reset()

    finally:
        c.disconnect()
Пример #23
0
def ensure_mqtt(config):
    from umqttsimple import MQTTClient

    global mqtt_client
    if 'mqtt' not in config:
        return False
    mqtt_config = config['mqtt']

    mqtt_client = MQTTClient(get_device_id(), mqtt_config.get('server'),
                             **mqtt_config.get('kwargs', {}))
    mqtt_client.set_callback(sub_cb)

    mqtt_client.connect()
    for (topic, f, prefix) in subscribes:
        print('MQTT sub to', topic)

        topic = _prefix_topic(topic, prefix)

        mqtt_client.subscribe(topic)
Пример #24
0
def connect_and_subscribe(topics2Subscribe,callbackFunc):
    global client, client_id
    myLog('create client')
    client = MQTTClient(client_id, config.mqtt_server)
    myLog('set callback')
    client.set_callback(callbackFunc)
    try:
        myLog('Connecting to %s MQTT broker ' % (config.mqtt_server), saveToFile = True)
        client.connect()
        myLog('connected')
        for topic in topics2Subscribe:
            myLog('subscribe 2 ' + str(topic))
            client.subscribe(topic)
            myLog('Subscribed to ' + str(topic))
        return client
    except MQTTException as e:
        myLog('MQTT exception: ' + str(e), saveToFile = True)
        #restart_and_reconnect()
        exit()
Пример #25
0
class Mqtt(object):
    pwm = PWM(led_board)

    wifi = Wifi()
    wifi.connect()
    client_id = ubinascii.hexlify(machine.unique_id())
    server = "192.168.43.165"
    topic = b"ruang_tamu"

    def __init__(self, *args, **kwargs):
        super(Mqtt, self).__init__(*args, **kwargs)
        self.client = MQTTClient(self.client_id, self.server)
        # self.client = MQTTClient(self.client_id,"m16.cloudmqtt.com",port=10490 ,user='******', password='******')
        try:
            led_green.value(1)
            print(
                "mqtt sedang berjalan dan menunggu message dengan topic= {} ".
                format(self.topic))
            self.client.connect()
            self.client.set_callback(self.sub_cb)
            self.client.subscribe(self.topic)
            while True:
                self.client.wait_msg()
        except:
            print("koneksi wifi atau server terputus")
            led_green.value(0)
            time.sleep(5)
            machine.reset()

    def sub_cb(self, topic, msg):
        print("topic   ={}".format(topic.decode()))
        print("message = {}".format(msg.decode()))

        if msg == b"led_on":
            led_blue.value(1)
        if msg == b"led_off":
            led_blue.value(0)

        if msg.isdigit():
            self.pwm.freq(1024)
            self.pwm.duty(1024 - int(msg))
Пример #26
0
def connect_and_subscribe():
    with open(CLIENT_CERT_DER) as fo:
        cert_data = fo.read()

    with open(CLIENT_KEY_DER) as fo:
        key_data = fo.read()

    ssl_params = {'cert': cert_data, 'key': key_data, 'server_side': False}

    client = MQTTClient(ubinascii.hexlify(machine.unique_id()),
                        MQTT_SERVER_IP,
                        user=MQTT_USERNAME,
                        password=MQTT_PASSWORD,
                        ssl=True,
                        ssl_params=ssl_params)
    client.set_callback(sub_cb)
    print('Connecting to', MQTT_SERVER_IP)
    print('Sit tight, the handshake is slow...  Seriously, be patient...')
    client.connect()
    client.subscribe('$SYS/broker/uptime')
    print('CONNECTED!')
    return client
Пример #27
0
def connect_and_subscribe():
    global client, client_id, mqtt_server, topic_sub, topic_subLedRGB, topic_subLed
    client = MQTTClient(client_id, mqtt_server)
    client.set_callback(sub_CheckTopics)
    try:
        client.connect()
        client.subscribe(topic_subFree)
        client.subscribe(topic_subLed)
        client.subscribe(topic_subLedRGB)
        print('Connected to %s MQTT broker, subscribed to %s topic' % (mqtt_server, topic_subFree))
        return client
    except:
        restart_and_reconnect()
Пример #28
0
def ler():
    Ok.on()
    try:
        mqtt = MQTTClient(client_id,
                          config.aws_ip,
                          user=config.mqtt_user,
                          password=config.mqtt_pass)
        mqtt.set_callback(sub_cb)
        mqtt.connect()
        mqtt.subscribe("esp32/pin1")
        mqtt.subscribe("esp32/red")
        mqtt.subscribe("esp32/green")
        mqtt.subscribe("esp32/blue")
        while True:
            mqtt.wait_msg()
            time.sleep(1)

        mqtt.disconnect()
    except Exception as e:
        print("Falha!", e)
        Ok.off()
        time.sleep(10)
        machine.reset()
Пример #29
0
def connect():
  global device_id, mqtt_server, connected, client
  client = MQTTClient(client_id, mqtt_server, 1883, device_id, b'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzb21lIjoicGF5bG9hZCJ9.UJxr678lFNPfhg9vBVw2AUstoxtzfPmcygBHPLu65Z8')
  client.set_callback(listenToSystemCommands)
  try:
      client.connect()
  except OSError as e:
      print("cannot connect to IOTINE.\nRebooting in 15s.")
      time.sleep(15)
      restart_and_reconnect()    


  client.subscribe(device_id+'/$SYS/COMMANDS/NON')
  client.subscribe(device_id+'/$SYS/COMMANDS/IO_STATE/NON')
  client.subscribe(device_id+'/$SYS/COMMANDS/UPDATE/NON')
  client.subscribe(device_id+'/$SYS/COMMANDS/NEWFILE/NON')


  client.publish(device_id+'/$__VERSION/'+user_id, str(__VERSION))
  client.publish(device_id+'/FSYS/'+user_id, str(os.listdir()))

  print('Connected to IOTINE')
  return client
Пример #30
0
def connect_and_subscribe():
    global client_id, mqtt_server, topic_sub
    client = MQTTClient(client_id, mqtt_server)
    client.set_callback(sub_cb)
    client.connect()

    client.subscribe(topic_sub)
    client.subscribe("inTopic")
    client.subscribe("servoDown")
    client.subscribe("servoUp")
    client.subscribe("servoDistance")
    client.subscribe("inTopic/Router/Time")
    client.subscribe("inTopic/MovementTimer")

    print('Connected to %s MQTT broker, subscribed to %s topic' %
          (mqtt_server, topic_sub))
    return client