本文整理汇总了Python中Adafruit_IO.MQTTClient.publish方法的典型用法代码示例。如果您正苦于以下问题:Python MQTTClient.publish方法的具体用法?Python MQTTClient.publish怎么用?Python MQTTClient.publish使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Adafruit_IO.MQTTClient
的用法示例。
在下文中一共展示了MQTTClient.publish方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: dataAdafruitHandler
# 需要导入模块: from Adafruit_IO import MQTTClient [as 别名]
# 或者: from Adafruit_IO.MQTTClient import publish [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: on_message
# 需要导入模块: from Adafruit_IO import MQTTClient [as 别名]
# 或者: from Adafruit_IO.MQTTClient import publish [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)
示例3: AdafruitNotifier
# 需要导入模块: from Adafruit_IO import MQTTClient [as 别名]
# 或者: from Adafruit_IO.MQTTClient import publish [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'])
示例4: dataNetworkHandler
# 需要导入模块: from Adafruit_IO import MQTTClient [as 别名]
# 或者: from Adafruit_IO.MQTTClient import publish [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)
示例5: test_subscribe_and_publish
# 需要导入模块: from Adafruit_IO import MQTTClient [as 别名]
# 或者: from Adafruit_IO.MQTTClient import publish [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')])
示例6: listdir
# 需要导入模块: from Adafruit_IO import MQTTClient [as 别名]
# 或者: from Adafruit_IO.MQTTClient import publish [as 别名]
#print '\n'.join(str(p) for p in nodelist)
FILES_LIST = [f for f in listdir(DIR_BASE) if isfile(join(DIR_BASE, f))]
print FILES_LIST
print 'Publishing a new message every 15 seconds (press Ctrl-C to quit)...'
while True:
#sourcefile is instant temperature log file
for sourcefile in FILES_LIST:
sourcefile = str(DIR_BASE) + sourcefile
print "Sourcefile = ", sourcefile
fh = open(sourcefile)
for fileread in fh:
print fileread
fileread2 = fileread.split("=")
value = fileread2[1]
nodemac = fileread2[2]
print "Node MAC from temperature log file = ", nodemac
for nodecheck in nodelist:
nodemaclist = nodecheck[0]
nodefeed = nodecheck[1]
if nodemac == nodemaclist:
feedname = nodefeed
print "Node MAC from list(config file) = ", nodemaclist
print "Node feed name = ", nodefeed
print value
print 'Publishing {0} to {1} feed.'.format(value, feedname)
client.publish(feedname, value)
fh.close()
time.sleep(15)
示例7: str
# 需要导入模块: from Adafruit_IO import MQTTClient [as 别名]
# 或者: from Adafruit_IO.MQTTClient import publish [as 别名]
message = "unknown_loc " + str(loops)
#message = str(loops)
if Loc == ROCCAMORICE:
#message = "Roccamorice_heartbeat " + str(loops)
message = "Roccamorice_heartbeat " + str(IP)
if Loc == COLDIPOZZO:
#message = "Coldipozzo_heartbeat " + str(loops)
message = "Coldipozzo_heartbeat " + str(IP)
if Loc == STARLIGHT:
#message = "Starlight_heartbeat " + str(loops)
message = "Starlight_heartbeat " + str(IP)
if Loc == WEEHAWKEN:
#message = "Weehawken_heartbeat " + str(loops)
message = "Weehawken_heartbeat " + str(IP)
print("Published: ",message)
client.publish('heartbeat', message)
time.sleep(300) #every 5 minutes
loops = loops + 1
if loops > 288: # 5min X 288 = 24 hrs
loops = 0
os.system("/home/pi/src/mqtt/trim_file_500 /home/pi/src/mqtt/mqtt_data.log")
# Another option is to pump the message loop yourself by periodically calling
# the client loop function. Notice how the loop below changes to call loop
# continuously while still sending a new message every 10 seconds. This is a
# good option if you don't want to or can't have a thread pumping the message
# loop in the background.
#last = 0
#print 'Publishing a new message every 10 seconds (press Ctrl-C to quit)...'
#while True:
# # Explicitly pump the message loop.
示例8: MQTTClient
# 需要导入模块: from Adafruit_IO import MQTTClient [as 别名]
# 或者: from Adafruit_IO.MQTTClient import publish [as 别名]
# 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)
# Create an MQTT client instance.
client = MQTTClient(cdavalox, 55177304338e4b578dca347c357d3402)
# 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 10 seconds.
print 'Publishing a new message every 10 seconds (press Ctrl-C to quit)...'
while True:
value = random.randint(0, 100)
print 'Publishing {0} to Welcome feed.'.format(value)
client.publish('Welcome feed', value)
time.sleep(10)
示例9: sleep
# 需要导入模块: from Adafruit_IO import MQTTClient [as 别名]
# 或者: from Adafruit_IO.MQTTClient import publish [as 别名]
try:
# Connect to the Adafruit IO server.
client.connect()
#Run a thread in the background so you can continue doing things in your program.
client.loop_background()
# client.loop_blocking()
sleep(2)
# Now send new values every X seconds. This is done simply to keep the connection alive
print 'Publishing a new message periodically to keep the connection alive (press Ctrl-C to quit)...'
while True:
value = random.randint(0, 100)
print 'Publishing {0} to outbound feed.'.format(value)
client.publish(conf["adafruit"]["outboundFeed"], value)
time.sleep(conf["adafruit"]["MQQTPublishingDelay"])
except KeyboardInterrupt:
pass
except Exception, e:
print e
logging.error('Exception raise', exc_info=True)
emailServer.setMailList(conf["log"]["developerEmail"])
emailServer.sendErrorMessage(e)
finally:
dispenser.cleanupTreatDispenser()
logging.info('Program terminated at ' + str(datetime.datetime.now()))
示例10: MQTTClient
# 需要导入模块: from Adafruit_IO import MQTTClient [as 别名]
# 或者: from Adafruit_IO.MQTTClient import publish [as 别名]
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.loop_background()
client.connect()
signal.signal(signal.SIGTERM, sigterm_handler)
signal.signal(signal.SIGINT, sigterm_handler)
time.sleep(2)
try:
while True:
temperature = tempSensor.readTemperature()
#print("temperature "+str(temperature))
(result,mid)=client.publish("bedroom",temperature)
if (result != 0):
client.disconnect()
time.sleep(2)
client.connect()
else:
time.sleep(30)
finally:
client.disconnect()
print("disconnect from server")
示例11: seconds
# 需要导入模块: from Adafruit_IO import MQTTClient [as 别名]
# 或者: from Adafruit_IO.MQTTClient import publish [as 别名]
# 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 10 seconds.
print 'Publishing a new message every 10 seconds (press Ctrl-C to quit)...'
while True:
value = random.randint(0, 1)
print 'Publishing {0} to Door State.'.format(value)
client.publish('doorstate', value)
time.sleep(10)
# Another option is to pump the message loop yourself by periodically calling
# the client loop function. Notice how the loop below changes to call loop
# continuously while still sending a new message every 10 seconds. This is a
# good option if you don't want to or can't have a thread pumping the message
# loop in the background.
#last = 0
#print 'Publishing a new message every 10 seconds (press Ctrl-C to quit)...'
#while True:
# # Explicitly pump the message loop.
# client.loop()
# # Send a new message every 10 seconds.
# if (time.time() - last) >= 10.0:
# value = random.randint(0, 100)
示例12: print
# 需要导入模块: from Adafruit_IO import MQTTClient [as 别名]
# 或者: from Adafruit_IO.MQTTClient import publish [as 别名]
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)
client.disconnect()
示例13: print
# 需要导入模块: from Adafruit_IO import MQTTClient [as 别名]
# 或者: from Adafruit_IO.MQTTClient import publish [as 别名]
client.tls_set("/home/vorasilp/adafruitio-temperature/certs/geotrust.pem")
print("connecting to adafruit")
client.connect()
client.loop_background()
print("init signal handler")
signal.signal(signal.SIGTERM, sigterm_handler)
signal.signal(signal.SIGINT, sigterm_handler)
time.sleep(2)
print("start sending")
# 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))
try:
while True:
if (running == False):
break
client.publish("photocell",12)
print("data sent")
time.sleep(30)
finally:
#client.disconnect()
print("exiting")
示例14: print
# 需要导入模块: from Adafruit_IO import MQTTClient [as 别名]
# 或者: from Adafruit_IO.MQTTClient import publish [as 别名]
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))
# 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()
client.loop_background()
print('Publishing a new message every 10 seconds (press Ctrl-C to quit)...')
while True:
value = random.randint(0, 100)
print('Publishing {0} to {1}.'.format(value, IO_FEED))
client.publish(IO_FEED, value, IO_FEED_USERNAME)
time.sleep(10)
示例15: MQTTClient
# 需要导入模块: from Adafruit_IO import MQTTClient [as 别名]
# 或者: from Adafruit_IO.MQTTClient import publish [as 别名]
client = MQTTClient(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY,service_host='io.adafruit.com',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/vorasilp/adafruitio-temperature/certs/geotrust.pem")
client.connect()
client.loop_background()
signal.signal(signal.SIGTERM, sigterm_handler)
signal.signal(signal.SIGINT, sigterm_handler)
#time.sleep(4)
try:
while True:
temperature = 12
#print("temperature "+str(temperature))
(result,mid)=client.publish("photocell",temperature)
if (result != 0):
#client.disconnect()
time.sleep(30)
#client.connect()
else:
print("data sent OK")
time.sleep(30)
finally:
#client.disconnect()
print("disconnect from server")