需要将变量设置为函数的一部分的结果

2024-04-20 11:42:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试创建一个脚本,该脚本使用远程数据库中的值来创建wol包。在

我需要把a的值传递给一个变量。我无法将其设置为变量,也无法确定如何从其他地方导入它。我需要在“def message”下打印并返回的“payload”保存到变量“data”

下面是我的代码,我将链接这依赖的MQTT代码。在

# Import standard python modules.
import sys

# Import Adafruit IO MQTT client.
from Adafruit_IO import MQTTClient
from Adafruit_IO import *


# Set to your Adafruit IO key & username below.
ADAFRUIT_IO_KEY      = 'where the api key goes'
ADAFRUIT_IO_USERNAME = 'my username'  # See https://accounts.adafruit.com
                                                    # to find your username.

# Set to the ID of the feed to subscribe to for updates.
FEED_ID = 'test1'


# Define callback functions which will be called when certain events happen.
def connected(client):
    # Connected function will be called when the client is connected to Adafruit IO.
    # This is a good place to subscribe to feed changes.  The client parameter
    # passed to this function is the Adafruit IO MQTT client so you can make
    # calls against it easily.
    print ('Connected to Adafruit IO!  Listening for {0} changes...').format(FEED_ID)
    # Subscribe to changes on a feed named DemoFeed.
    client.subscribe(FEED_ID)

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
    print ('Feed {0} received new value: {1}').format(FEED_ID, payload)
    return (payload == payload)

data = message(x, x, x) 

# 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()

# Start a message loop that blocks forever waiting for MQTT messages to be
# received.  Note there are other options for running the event loop like doing
# so in a background thread--see the mqtt_client.py example to learn more.

if data == '1':
    print('Latest value from Test: {0}'.format(data.value))
    wol.send_magic_packet('my mac addy')
    time.sleep(3)
    # Send a value to the feed 'Test'. 
    aio.send('test1', 0)
    print ("worked this time")
client.loop_blocking()

下面是它依赖的另一个代码的链接https://github.com/adafruit/io-client-python/blob/master/Adafruit_IO/mqtt_client.py


Tags: thetoioadafruitclientidmessagedata
1条回答
网友
1楼 · 发布于 2024-04-20 11:42:02

首先,不需要导入同一个模块两次

from Adafruit_IO import MQTTClient # this imports part MQTTClient
from Adafruit_IO import *          # this imports everything, including MQTTClient again

至于你的问题,你已经返回了(payload == payload)这将永远是真的,我不知道你在这里想做什么,但它应该看起来像这样:

^{pr2}$

相关问题 更多 >