2016-12-10 13 views
0

リモートデータベースの値を使用してwolパケットを作成するスクリプトを作成しようとしています。関数の一部の結果と同じ変数を設定する必要があります

私は(私は正直なところ、何を呼び出すのかわからない)値を変数に渡す必要があります。私はそれを変数に設定することができず、どこからインポートするのかわかりません。変数 "data"に保存される "def message"の下に "payload"が出力されます。

以下は私のコードです。これに依存する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

+0

'message'関数が実行されたら、あなたの変数' data'はどのようになるべきですか? –

+0

Tom Fuller – user3715459

+0

から読み取っているオンラインデータベースの値に応じて0または1を返すはずです。 'payload == payload'は常に1になります。' return payload == payload_from_database'のようにする必要があります。 –

答えて

0

まずに依存している他のコードへのリンクである、あなたは、二回

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

問題については同一のモジュールをインポートする必要はありません。あなたは(payload == payload)を返しました。これは常に真ですが、ここで何をしようとしているのか分かりませんが、次のようになります。

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 
+0

これは、 "name x is not defined"を返しました。origional関数のパラメータを使用すると、 "name feed_id is not defined"と表示されます。 – user3715459

+0

コードで使用したので 'x'を使用しました。 xはあなたが望むものであれば何でも構いません、同じことが 'feed_id'のためにあります私は答えにいくつかのデータの例を追加しました –

関連する問題