當前位置: 首頁>>代碼示例>>Python>>正文


Python MQTTClient.connect方法代碼示例

本文整理匯總了Python中Adafruit_IO.MQTTClient.connect方法的典型用法代碼示例。如果您正苦於以下問題:Python MQTTClient.connect方法的具體用法?Python MQTTClient.connect怎麽用?Python MQTTClient.connect使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在Adafruit_IO.MQTTClient的用法示例。


在下文中一共展示了MQTTClient.connect方法的11個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: test_connect

# 需要導入模塊: from Adafruit_IO import MQTTClient [as 別名]
# 或者: from Adafruit_IO.MQTTClient import connect [as 別名]
 def test_connect(self):
     # Create MQTT test client.
     client = MQTTClient(self.get_test_username(), self.get_test_key())
     # Verify on_connect handler is called and expected client is provided.
     def on_connect(mqtt_client):
         self.assertEqual(mqtt_client, client)
     client.on_connect = on_connect
     # Connect and wait until on_connect event is fired.
     client.connect()
     self.wait_until_connected(client)
     # Verify connected.
     self.assertTrue(client.is_connected())
開發者ID:Mecabot,項目名稱:io-client-python,代碼行數:14,代碼來源:test_mqtt_client.py

示例2: on_message

# 需要導入模塊: from Adafruit_IO import MQTTClient [as 別名]
# 或者: from Adafruit_IO.MQTTClient import connect [as 別名]
def on_message(client, userdata, msg):
    print(str(datetime.datetime.now()) + ": " + msg.topic + " " + str(msg.payload))    
    #
    # Forward the data to Adafruit IO. Replace topic with a valid feed name
    #
    feedname=msg.topic.replace("/","_")
    print("Publish to Adafruit feedname: " + feedname)

    # Initialize the client that should connect to io.adafruit.com
    adafruitClient = MQTTClient(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY,service_port=1883)
    adafruitClient.on_connect = adafruit_connected
    adafruitClient.connect()
    adafruitClient.loop()
    adafruitClient.publish(feedname,msg.payload)
開發者ID:abacuspix,項目名稱:MQTT_IoT,代碼行數:16,代碼來源:subscriber_adafruit_proxy.py

示例3: dataAdafruitHandler

# 需要導入模塊: from Adafruit_IO import MQTTClient [as 別名]
# 或者: from Adafruit_IO.MQTTClient import connect [as 別名]
def dataAdafruitHandler():
    client = MQTTClient(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)

    client.on_connect    = connected
    client.on_disconnect = disconnected
    client.on_message    = message

    client.connect()
    client.loop_background()
    while True:
        value = random.randint(0, 100)
        print 'Publishing {0} to my-data.'.format(value)
        client.publish('my-data', value)
        time.sleep(5)
開發者ID:brahamcosoX3,項目名稱:TheIoTLearningInitiative,代碼行數:16,代碼來源:main.py

示例4: test_disconnect

# 需要導入模塊: from Adafruit_IO import MQTTClient [as 別名]
# 或者: from Adafruit_IO.MQTTClient import connect [as 別名]
 def test_disconnect(self):
     # Create MQTT test client.
     client = MQTTClient(self.get_test_username(), self.get_test_key())
     # Verify on_connect handler is called and expected client is provided.
     def on_disconnect(mqtt_client):
         self.assertEqual(mqtt_client, client)
     client.on_disconnect = on_disconnect
     # Connect and wait until on_connect event is fired.
     client.connect()
     self.wait_until_connected(client)
     # Now disconnect and wait until disconnection event occurs.
     client.disconnect()
     self.wait_until_connected(client, connect_value=False)
     # Verify diconnected.
     self.assertFalse(client.is_connected())
開發者ID:adafruit,項目名稱:io-client-python,代碼行數:17,代碼來源:test_mqtt_client.py

示例5: test_secure_connect

# 需要導入模塊: from Adafruit_IO import MQTTClient [as 別名]
# 或者: from Adafruit_IO.MQTTClient import connect [as 別名]
 def test_secure_connect(self):
     """Test a secure (port 8883, TLS enabled) AIO connection
     """
     # Create MQTT-Secure test client.
     client = MQTTClient(self.get_test_username(), self.get_test_key())
     # Verify on_connect handler is called and expected client is provided.
     def on_connect(mqtt_client):
         self.assertEqual(mqtt_client, client)
     client.on_connect = on_connect
     # Connect and wait until on_connect event is fired.
     client.connect()
     self.wait_until_connected(client)
     # Verify connected.
     self.assertTrue(client.is_connected())
     self.assertTrue(client._secure)
開發者ID:adafruit,項目名稱:io-client-python,代碼行數:17,代碼來源:test_mqtt_client.py

示例6: test_insecure_connect

# 需要導入模塊: from Adafruit_IO import MQTTClient [as 別名]
# 或者: from Adafruit_IO.MQTTClient import connect [as 別名]
 def test_insecure_connect(self):
     """Test an insecure (port 1883, TLS disabled) AIO connection
     """
     # Create MQTT-Insecure (non-SSL) test client.
     client = MQTTClient(self.get_test_username(), self.get_test_key(), secure=False)
     # Verify on_connect handler is called and expected client is provided.
     def on_connect(mqtt_client):
         self.assertEqual(mqtt_client, client)
     client.on_connect = on_connect
     # Connect and wait until on_connect event is fired.
     client.connect()
     self.wait_until_connected(client)
     # Verify connected.
     self.assertTrue(client.is_connected())
     # Verify insecure connection established
     self.assertFalse(client._secure)
開發者ID:adafruit,項目名稱:io-client-python,代碼行數:18,代碼來源:test_mqtt_client.py

示例7: AdafruitNotifier

# 需要導入模塊: from Adafruit_IO import MQTTClient [as 別名]
# 或者: from Adafruit_IO.MQTTClient import connect [as 別名]
class AdafruitNotifier(Notifier):
    # TODO: consider supporting this directly in thingamon and not using
    # the adafruit package. both have the same paho mqtt connection logic.
    # adafruit does not lock the connected state variable, thingamon does
    # not sure which is right yet
    def __init__(self, username=None, api_key=None, host='io.adafruit.com',
                 port=1883):
        """
        Create an Adafruit MQTT notifier

        Args:
            host (str): host name of Adafruit MQTT broker
            port (int): port of Adafruit MQTT broker
            username (str): Adafruit IO username
            api_key (str): Adafruit IO API key
        """
        self.log = logging.getLogger('thingpin')
        self.username = username
        self.api_key = api_key
        self.host = host
        self.port = port
        self.client = None

    def initialize(self):
        self.client = MQTTClient(self.username, self.api_key,
                                 service_host=self.host,
                                 service_port=self.port)

        def on_disconnect(client):
            if client.disconnect_reason != 0:
                self.log.info('client disconnected, exiting')
                os._exit(1)

        self.client.on_disconnect = on_disconnect

        self.client.connect()
        self.log.info('connected to Adafruit')
        self.client.loop_background()

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

    def notify(self, name, value):
        self.log.info('Adafruit IO: publish({}={})'.format(name, value))
        self.client.publish(name, value['state'])
開發者ID:dmreiland,項目名稱:thingpin,代碼行數:47,代碼來源:notifiers.py

示例8: main

# 需要導入模塊: from Adafruit_IO import MQTTClient [as 別名]
# 或者: from Adafruit_IO.MQTTClient import connect [as 別名]
def main():

    # Create a mqttclient
    client = MQTTClient(IO_USERNAME, IO_KEY)

    # Assign handlers for events
    client.on_connect    = connected
    client.on_disconnect = disconnected
    client.on_message    = message

    logger.debug('Connect to Adafruit IO server')
    client.connect()

    logger.debug('Start background thread for messaging')
    client.loop_background()

    temp_buffer = []
    hum_buffer = []
    while True:
        humidity, temperature = measure()
        if humidity is not None and temperature is not None:
            logger.info('Temperature={0:0.1f}, Humidity={1:0.1f}'.format(temperature, humidity))
            temp_buffer.append(temperature)
            hum_buffer.append(humidity)

        # Send median of last three records
        if len(temp_buffer) == BUFFER_SIZE:
            temp_buffer = sorted(temp_buffer)
            temp_median = temp_buffer[BUFFER_SIZE // 2]
            temp_median = round(temp_median, 1)
            logger.debug('Rounded median of temp_buffer({}) is {}'.format(temp_buffer, temp_median))
            publish(client, 'temperature', temp_median)
            temp_buffer = []

        # Send median of last three records
        if len(hum_buffer) == BUFFER_SIZE:
            hum_buffer = sorted(hum_buffer)
            hum_median = hum_buffer[BUFFER_SIZE // 2]
            hum_median = round(hum_median, 1)
            logger.debug('Rounded median of hum_buffer({}) is {}'.format(hum_buffer, hum_median))
            publish(client, 'humidity', hum_median)
            hum_buffer = []

        sleep(TIME_INTERVAL)
開發者ID:FlorianDr,項目名稱:bee-observation,代碼行數:46,代碼來源:main.py

示例9: dataNetworkHandler

# 需要導入模塊: from Adafruit_IO import MQTTClient [as 別名]
# 或者: from Adafruit_IO.MQTTClient import connect [as 別名]
def dataNetworkHandler():
    global idDevice 
    idDevice = GetMACAddress() # Make this a global variable
    mqttclient = paho.Client()
    mqttclient.on_publish = on_publish
    adaclient = MQTTClient(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
    mqttclient.connect("test.mosquitto.org", 1883, 60)
    adaclient.connect()
    #adaclient.loop_background()
    while True:
        packets = dataNetwork()    
    	global message 
        message = idDevice + " " + str(packets)
    	#pdb.set_trace()
        print "dataNetworkHandler " + message
    	mqttclient.publish("IoT101/"+idDevice+"/Network", message)
    	adaclient.publish("IoT101/"+idDevice+"/Network", message)
	json = {'id':idDevice,'packets':int(packets)}
        dweepy.dweet_for('DataReportingSystem',json)
        time.sleep(3)
開發者ID:marcelarosalesj,項目名稱:TheIoTLearningInitiative,代碼行數:22,代碼來源:DataReportingSystem.py

示例10: test_subscribe_and_publish

# 需要導入模塊: from Adafruit_IO import MQTTClient [as 別名]
# 或者: from Adafruit_IO.MQTTClient import connect [as 別名]
 def test_subscribe_and_publish(self):
     # Create MQTT test client.
     client = MQTTClient(self.get_test_username(), self.get_test_key())
     # Save all on_message handler responses.
     messages = []
     def on_message(mqtt_client, feed, payload):
         self.assertEqual(mqtt_client, client)
         messages.append((feed, payload))
     client.on_message = on_message
     # Connect and wait until on_connect event is fired.
     client.connect()
     self.wait_until_connected(client)
     # Subscribe to changes on a feed.
     client.subscribe('testfeed')
     # Publish a message on the feed.
     client.publish('testfeed', 42)
     # Wait for message to be received or timeout.
     start = time.time()
     while len(messages) == 0 and (time.time() - start) < TIMEOUT_SEC:
         client.loop()
         time.sleep(0)
     # Verify one update message with payload is received.
     self.assertListEqual(messages, [('testfeed', '42')])
開發者ID:adafruit,項目名稱:io-client-python,代碼行數:25,代碼來源:test_mqtt_client.py

示例11: disconnected

# 需要導入模塊: from Adafruit_IO import MQTTClient [as 別名]
# 或者: from Adafruit_IO.MQTTClient import connect [as 別名]
def disconnected(client):
    # Disconnected function will be called when the client disconnects.
    print 'Disconnected from Adafruit IO!'
    sys.exit(1)

def message(client, feed_id, payload):
    # Message function will be called when a subscribed feed has a new value.
    # The feed_id parameter identifies the feed, and the payload parameter has
    # the new value.
    print 'Feed {0} received new value: {1}'.format(feed_id, payload)
    # Set a global var with the payload then check for it above
    global msg
    msg = payload

# Create an MQTT client instance
client = MQTTClient(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)

# Setup the callback functions defined above
client.on_connect = connected
client.on_disconnect = disconnected
client.on_message = message

# Connect to the Adafruit IO server
client.connect()

ble.initialize()

ble.run_mainloop_with(ble_main)

開發者ID:jbdamask,項目名稱:Adafruit,代碼行數:30,代碼來源:jbd_uart_mqtt_service.py


注:本文中的Adafruit_IO.MQTTClient.connect方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。