ワイヤレスNRF24モジュールを介して接続されているラズベリーのKivyのArduinoからいくつかの値を 'プル'したいと思います。私はthisライブラリを使用していますpython wrapperKivy Python - 部分的なコールバック関数
純粋なPythonではコードはうまくいきましたが、今はKivyに統合したいと思います。そのため
Iは、zimmerwetter.py
内部に二つの機能を作った:
(アプリケーションが開始されると実行されなければならない)、無線デバイスを設定し、無線オブジェクトを返すための一つ:
def radiosetup():
radio = RF24(RPI_BPLUS_GPIO_J8_22, RPI_BPLUS_GPIO_J8_24, BCM2835_SPI_SPEED_8MHZ)
# doing setup stuff...
return radio
及び他の機能いくつかの環境日時(温度、湿度など)を提供するArduinoにリクエストを送信します。提案されているよう
def getenviroment(self,radio):
millis = lambda: int(round(time.time() * 1000))
# send command
send_payload = 'getdata'
# First, stop listening so we can talk.
radio.stopListening()
# Take the time, and send it. This will block until complete
print 'Now sending length ', len(send_payload), ' ... ',
radio.write(send_payload[:len(send_payload)])
a = datetime.datetime.now()
# Now, continue listening
radio.startListening()
# Wait here until we get a response, or timeout
started_waiting_at = millis()
timeout = False
while (not radio.available()) and (not timeout):
if (millis() - started_waiting_at) > 1000:
timeout = True
# Describe the results
if timeout:
b = datetime.datetime.now()
# print(b - a)
print 'failed, response timed out.'
else:
# Grab the response, compare, and send to debugging spew
length = radio.getDynamicPayloadSize()
receive_payload = []
receive_payload = radio.read(length)
print 'got response size=', length
print struct.unpack("bbbbhbbbb", ''.join(chr(c) for c in receive_payload))
b = datetime.datetime.now()
print(b - a)
return receive_payload
getenviroment機能がkivyアプリからすべてのX秒後に呼び出されなければなりませんが、一部の機能がthe kivy clock module
from zimmerwetter import *
class PyowmApp(App):
def build(self):
radio = radiosetup()
Clock.schedule_interval(partial(getenviroment,radio), 10)
で使用されるエラーは次のとおりです。
File "/home/pi/pyscripts/pyowm/zimmerwetter.py", line 83, in getenviroment
radio.stopListening()
AttributeError: 'float' object has no attribute 'stopListening'
I何故floatオブジェクトが返されているのか疑問に思っていますが、ラジオオブジェクトをヘルプ(ラジオ)で印刷すると、class RF24(Boost.Python.instance)
とstplistening()という関数が返されます。 ts。
文を 'Clock.schedule_once(lambda dt:getenviroment(radio)、10)'に変更し、functonからself引数を取り除いても機能しています(関数istはApp) – Triscus
@Triscus 'getenviroment'がどこに定義されているかという質問はないので、私は' PyownApp'と仮定しました。 'self'を' getenvironment'を持つクラスの正しいインスタンスに置き換えます。あるいは、パラメータ 'self'を変更することを検討してください。 – zeeMonkeez
関数はクラス内では定義されておらず、 'def'ステートメントでのみ定義されていました。私はOOPに慣れるべきだと思う、あなたのインスピレーションのためにありがとう – Triscus