def connect(mqtt_user="******", mqtt_pass="******"): global client client = MQTTClient(client_id, mqtt_server, 1883) client.connect() print('Connected to %s MQTT broker' % (mqtt_server)) return client
def connect_mqtt(): global client_id, mqtt_server client = MQTTClient(client_id, mqtt_server) #client = MQTTClient(client_id, mqtt_server, user=your_username, password=your_password) client.connect() print('Connected to %s MQTT broker' % (mqtt_server)) return client
class MQTT(object): def __init__(self,config="mqtt.txt"): self.user = None self.password = None self.root = b"" self.config = config self.enable = True self.load_config() self.mqtt = MQTTClient("pylinky",self.server,user=self.user,password=self.password,keepalive=60) def connect(self): if self.enable: self.mqtt.connect() def load_config(self): try: with open(self.config,"r") as f: conf = f.readlines() if len(conf) > 0: self.server = conf[0].strip() if len(conf) > 1: self.user = conf[1].strip() if len(conf) > 2: self.password = conf[2].strip() if len(conf) > 3: self.root = conf[3].strip().encode() except OSError: self.enable = False def publish(self,tag,value): if self.enable: self.mqtt.publish(self.root+b"/"+tag,value)
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
def start_mqtt(self): try: self.c = MQTTClient(self.clients, net.server) self.c.connect() except: self.c = MQTTClient(self.clients, self.server) self.c.connect() try: print("mulai mqtt") self.c.set_callback(self.sub_cb) self.c.subscribe(self.topic) self.c.subscribe(b"waktu") self.c.subscribe(b"nyala_pada_1") self.c.subscribe(b"nyala_pada_2") self.c.subscribe(b"nyala_pada_3") self.c.subscribe(b"nyala_pada_4") self.c.subscribe(b"mati_pada_1") self.c.subscribe(b"mati_pada_2") self.c.subscribe(b"mati_pada_3") self.c.subscribe(b"mati_pada_4") print("Connected to %s, subscribed to %s topic" % (self.server, self.topic)) while 1: self.timer.deinit() self.state = "terhubung" self.c.wait_msg() except: print("pedot") self.state = "terputus" self.timer.init(period=1000, callback=self.tim)
def connect(): global client_id, mqtt_server client = MQTTClient(client_id, mqtt_server) client.connect() print('Connected to %s MQTT broker ' % (mqtt_server)) return client
def __init__(self,config="mqtt.txt"): self.user = None self.password = None self.root = b"" self.config = config self.enable = True self.load_config() self.mqtt = MQTTClient("pylinky",self.server,user=self.user,password=self.password,keepalive=60)
def connect_and_subscribe(i): try: client = MQTTClient(CLIENT_ID, MQTT_SERVER) client.connect() print('Connected to %s MQTT broker, published to "%s" topic' % (MQTT_SERVER, TOPIC_PUB)) return client except: err_msg = 'MQTT broker connection error' restart_and_reconnect(i, err_msg)
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
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(topic_sub1) print('Connected to %s MQTT broker, subscribed to %s topic' % (mqtt_server, topic_sub)) return client
def mqtt_connect_and_subscribe(): global client_id, mqtt_server, adafruit_user, adafruit_iokey, topic_sub client = MQTTClient(client_id=client_id, server=mqtt_server, user=adafruit_user, password=adafruit_iokey) client.connect() print('Verbonden met %s mqtt-broker.' % mqtt_server) return client
def connect(self): try: self.client = MQTTClient(self.CLIENT_ID, self.__server) self.client.set_callback(self.__sub_cb) self.client.connect() self.client.subscribe(self.__sub_topic) self.__connection = True return self.client except Exception: return None
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
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) client.connect() client.subscribe(topic_subFree) client.subscribe(topic_subLed) print('Connected to %s MQTT broker, subscribed to %s topic' % (mqtt_server, topic_subFree)) return client
def mqtt_connect_and_subscribe(): global client_id, mqtt_server, topic_sub client = MQTTClient(client_id, mqtt_server) client.connect() print('Verbonden met %s mqtt-broker.' % mqtt_server) return client
def connect_mqtt(mqtt_server_addr: str, mqtt_login: str, mqtt_password: str) -> MQTTClient: # import usocket # ip = usocket.getaddrinfo('blackpearl.local', 9000)[0][-1][0] #ip = usocket.getaddrinfo('www.micropython.org', 80)[0][-1][0] client_id = ubinascii.hexlify(machine.unique_id()) print('MQTTBROKER: %s' %mqtt_server_addr) client = MQTTClient(client_id, mqtt_server_addr, port=1883, user=mqtt_login, password=mqtt_password) client.connect() print('Connected to %s MQTT broker' % (mqtt_server_addr)) return client
def mqtt_connect(self, client_id, server, port, ssl, user, password, sub_cb): self.screen("Status", "MQTT Connecting") self._mqttClient = MQTTClient(client_id=client_id, server=server, port=port, ssl=ssl, user=user, password=password) self._mqttClient.set_callback(sub_cb) self._mqttClient.connect() self.screen("Status", "MQTT Connected")
def connect_and_subscribe(): global client_id, mqtt_server, topic_sub client = MQTTClient(client_id, mqtt_server) #client.set_callback(sub_cb) client.connect() print('Connected to %s MQTT broker' % (mqtt_server)) oled.fill(0) oled.text('Conn. MQTT broker', 0, 0) oled.text(mqtt_server, 0, 12) oled.show() sleep(5) return client
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="******") client.connect() print("Conectado ao broker MQTT %s" % (mqtt_server)) return client
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
def start_process(): mac = ubinascii.hexlify(network.WLAN().config('mac'), ':').decode() try: set_wifi() client = MQTTClient("umqtt_client", "10.0.0.233") connect_server(client) print("-------press ctrl-c to stop the polling") # create and publish control message control_message = {"mac": mac} client.publish(MQTT_CONTROL_CHANNEL, json.dumps(control_message)) print("Publish control message : " + str(control_message)) print() time.sleep(5) client.check_msg() # process configuration sensors = process_configuration() while True: for sensor in sensors: publish_data_message(client, sensor) # catches the ctrl-c command, which breaks the loop above except KeyboardInterrupt: print("Continuous polling stopped") finally: client.disconnect()
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
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
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
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
def _check_mqtt(self): current_state, _ = self._get_conn_state() conf = self.conf if 'mqtt_server' not in conf: # Bad config. Cannot do anything return if self._current_state < self._conn_state_of('wifi_up'): # No WiFi. Byebye return if self._conn_state_of('mqtt_up') <= self._current_state: # MQTT allegedly connected. Just make sure we're still in... if self.mqtt_connected(): return print("MQTT connection temporarily lost...") # Here it means WiFi is up, and MQTT not... Let's give it a try # Note: This can somewhat hang the python interpeter, when it's waiting for the socket probably self._set_conn_state('mqtt_trying') conf = self.conf server = conf.get('mqtt_server') port = conf.get('mqtt_port') user = conf.get('mqtt_user') password = conf.get('mqtt_pass') Print( "Mqtt-connecting device %s to server %s, port %s, user=%s, pass of %d chars" % (api.uid(), server, port, user, 0 if password is None else len(password))) if self._mqtt_client is None: self._mqtt_client = MQTTClient(client_id=api.uid(), server=server, port=port, user=user, password=password) if self._mqtt_client.connect(): self._mqtt_client.set_callback(self.on_mqtt) conf_keys_listen = conf.get('mqtt_listen', '').split() auto_topics = self.autos_mqtt_topics.keys() conf_topics = [conf.get(k) for k in conf_keys_listen] for topic in set(conf_topics).union(set(auto_topics)): if topic is None: continue print("Subscribing to %s" % topic) self._mqtt_client.subscribe(topic) self.on_mqtt_connect() self._set_conn_state('mqtt_up') self._mqtt_recently_ok = True return
def __init__(self): # Instantiate MQTT client self.mqtt_client = MQTTClient( client_id=config.DEVICE_ID, server=config.MQTT_ENDPOINT, port=config.MQTT_PORT, user=config.MQTT_USERNAME, password=config.MQTT_PASSWORD, keepalive=10000, ) # MQTT messages handler self.mqtt_client.set_callback(self.on_mqtt_message) # Button press handler self.pusher.irq(trigger=Pin.IRQ_RISING, handler=self.on_button_press)
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()
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
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