[英]Need to set a variable equal to the result of a part of a function
I am attempting to create a script that uses a value in a remote database to create a wol packet. 我正在尝试创建一个脚本,该脚本使用远程数据库中的值来创建wol数据包。
I need to pass the value of a (something that I honestly don't know what to call) to a variable. 我需要将a的值(老实说我不知道要调用什么)传递给变量。 I am unable to just set it to the variable, and I can't figure out how to import it from somewhere else.
我无法仅将其设置为变量,也无法弄清楚如何从其他位置导入它。 I need "payload" which is printed and returned under "def message" to be saved to the variable "data"
我需要打印并在“ def message”下返回的“ payload”以保存到变量“ data”
Below is my code, and I will link the MQTT code that this relies on. 下面是我的代码,我将链接它所依赖的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()
Here is the link to the other code it relies on https://github.com/adafruit/io-client-python/blob/master/Adafruit_IO/mqtt_client.py 这是它依赖于https://github.com/adafruit/io-client-python/blob/master/Adafruit_IO/mqtt_client.py的其他代码的链接
First of all, you don't need to import the same module twice 首先,您不需要两次导入相同的模块
from Adafruit_IO import MQTTClient # this imports part MQTTClient
from Adafruit_IO import * # this imports everything, including MQTTClient again
As for your problem, you've returned (payload == payload)
which will always be True, i'm not sure what you're trying to do here but it should look something like this: 至于您的问题,您已经返回了
(payload == payload)
,该值始终为True,我不确定您要在此处执行的操作,但它看起来应该像这样:
def message(client, feed_id, payload):
...
print ('Feed {0} received new value: {1}').format(FEED_ID, payload)
return payload == payload_from_database # what you return will be saved as the variable data
data = message("Steve the happy client", 85, "£100") # in this case, data will be payload
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.