本文整理汇总了Python中Adafruit_IO.MQTTClient.on_disconnect方法的典型用法代码示例。如果您正苦于以下问题:Python MQTTClient.on_disconnect方法的具体用法?Python MQTTClient.on_disconnect怎么用?Python MQTTClient.on_disconnect使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Adafruit_IO.MQTTClient
的用法示例。
在下文中一共展示了MQTTClient.on_disconnect方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: dataAdafruitHandler
# 需要导入模块: from Adafruit_IO import MQTTClient [as 别名]
# 或者: from Adafruit_IO.MQTTClient import on_disconnect [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)
示例2: test_disconnect
# 需要导入模块: from Adafruit_IO import MQTTClient [as 别名]
# 或者: from Adafruit_IO.MQTTClient import on_disconnect [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())
示例3: main
# 需要导入模块: from Adafruit_IO import MQTTClient [as 别名]
# 或者: from Adafruit_IO.MQTTClient import on_disconnect [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)
示例4: instance_start
# 需要导入模块: from Adafruit_IO import MQTTClient [as 别名]
# 或者: from Adafruit_IO.MQTTClient import on_disconnect [as 别名]
def instance_start(c, name):
# Using 'c' instead of 'client' for easier reading/writing
#Last field 'name' of client will be used to set '_instance_name' so we can read a unique name inside callback functions
logger.info('Setting up instance and connecting: %s', name)
c['instance'] = MQTTClient(c['USERNAME'], c['PASSWORD'], c['SERVER'], int(c['PORT']), c['CLIENTID'], c['TOPIC_FMT'], name) # Create MQTTClient instance
logger.debug("New instance: %s *PASSWORD* %s %s %s %s %s ",c['USERNAME'], c['SERVER'], int(c['PORT']), c['CLIENTID'], c['TOPIC_FMT'], name)
# Setup the callback functions defined below.
c['instance'].on_connect = connected
c['instance'].on_disconnect = disconnected
c['instance'].on_message = message
#Setup TLS for basic connection encryption (like a web browser using HTTPS)
# - You can probably do other TLS connection types like pre-shared key authentication if you modify this code and add setting variables
# For Adafruit, just use normal OS CA lookup bundle file in /etc/ssl/certs/: (Probably OS-dependent location)
if c['TLS_SET'] == "1":
logger.debug("Set TLS: %s", c['CACERT'])
c['instance'].tls_set(c['CACERT']) # If you need more of the TLS settings, feel free to add to config file and here.
# Connect to servers
# Need to convert these from strings to integers to use for counting
c['RETRY_COUNTER'] = int(c['RETRY_COUNTER'])
c['MAX_RETRIES'] = int(c['MAX_RETRIES'])
while True: #Loop until complete or retry limit hit
try:
c['instance'].connect() # Try to connect. Some errors might be fatal.
except: # Have only seen 'socket.errors', but could be other kinds
if c['RETRY_COUNTER'] > c['MAX_RETRIES']:
logger.critical('Giving up retries to %s (%s). Terminating process.', c['instance']._service_host, c['instance']._instance_name)
for name in settings_dict:
if 'instance' in settings_dict[name]: #For each existing instance besides this one
settings_dict[name]['instance'].disconnect() # Terminate instance
sys.exit(1)
logger.error('%s connect error for %s. Retry # %s', str(sys.exc_info()[0]), c['instance']._instance_name, str(c['RETRY_COUNTER']))
c['RETRY_COUNTER'] += 1; # Iterate try
time.sleep(5) # Delay before retry
else:
# Connection Worked. Reset counter and break loop
c['RETRY_COUNTER'] = 1;
break
示例5: disconnected
# 需要导入模块: from Adafruit_IO import MQTTClient [as 别名]
# 或者: from Adafruit_IO.MQTTClient import on_disconnect [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)
示例6: on_message
# 需要导入模块: from Adafruit_IO import MQTTClient [as 别名]
# 或者: from Adafruit_IO.MQTTClient import on_disconnect [as 别名]
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
print(msg.topic+" "+str(msg.payload))
def on_publish(client, userdata, mid):
print("data sent")
def on_disconnect(client, userdata, rc):
print("disconnect")
client = MQTTClient(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY,service_port=8883)
client.on_connect = on_connect
client.on_message = on_message
client.on_publish = on_publish
client.on_disconnect = on_disconnect
client.tls_set("/home/pi/adafruitio-temperature/certs/geotrust.pem")
client.connect()
#client.loop_background()
# Get temperature from sensor
#byte1 = 29
#byte2 = 0x00
#temperature = ((byte1 << 8) + byte2) >> 4
#if (temperature & 0x800):
# temperature = (temperature & 0x7FF) - 0x800
#temperature = temperature * 0.0625
#temperature = 30
temperature = tempSensor.readTemperature()
print("temperature "+str(temperature))
client.publish("bedroom",temperature)