当前位置: 首页>>代码示例>>Python>>正文


Python MQTTClient.loop方法代码示例

本文整理汇总了Python中Adafruit_IO.MQTTClient.loop方法的典型用法代码示例。如果您正苦于以下问题:Python MQTTClient.loop方法的具体用法?Python MQTTClient.loop怎么用?Python MQTTClient.loop使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Adafruit_IO.MQTTClient的用法示例。


在下文中一共展示了MQTTClient.loop方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: on_message

# 需要导入模块: from Adafruit_IO import MQTTClient [as 别名]
# 或者: from Adafruit_IO.MQTTClient import loop [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

示例2: test_subscribe_and_publish

# 需要导入模块: from Adafruit_IO import MQTTClient [as 别名]
# 或者: from Adafruit_IO.MQTTClient import loop [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

示例3: print

# 需要导入模块: from Adafruit_IO import MQTTClient [as 别名]
# 或者: from Adafruit_IO.MQTTClient import loop [as 别名]
# 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()

print('Ctrl-C to exit')

# The first option is to run a thread in the background so you can continue
# doing things in your program.
#client.loop_background()
#client.loop_blocking()


pinNum = 26
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM) #numbering scheme that corresponds to breakout board and pin layout
GPIO.setup(pinNum,GPIO.OUT) #replace pinNum with whatever pin you used, this sets up that pin as$

while True:
    client.loop()
    if var1 == 1:
        print('Received <- ON\n')
    elif var1 == 0:
        print('Received <- OFF\n')
    time.sleep(2)
#GPIO.output(pinNum,GPIO.HIGH) #set GPIO High
#GPIO.cleanup()
开发者ID:robingreig,项目名称:raspi-git,代码行数:32,代码来源:subscribe05.py


注:本文中的Adafruit_IO.MQTTClient.loop方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。