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


Python MQTTClient.loop_background方法代码示例

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


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

示例1: dataAdafruitHandler

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

示例2: AdafruitNotifier

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

示例3: main

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

示例4: MQTTClient

# 需要导入模块: from Adafruit_IO import MQTTClient [as 别名]
# 或者: from Adafruit_IO.MQTTClient import loop_background [as 别名]
    # the new value.
    print 'Feed {0} received new value: {1}'.format(feed_id, 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()

# Now the program needs to use a client loop function to ensure messages are
# sent and received.  There are a few options for driving the message loop,
# depending on what your program needs to do.  

# The first option is to run a thread in the background so you can continue
# doing things in your program.
client.loop_background()
# Now send new values every 4 seconds.
print 'Publishing a new message every 4 seconds (press Ctrl-C to quit)...'
while True:
    value = random.randint(0, 100)
    print 'Publishing {0} to DemoFeed.'.format(value)
    client.publish('DemoFeed', value)
    time.sleep(4)

开发者ID:cdavalox,项目名称:TheIoTLearningInitiative,代码行数:31,代码来源:1.py


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