我想在 Raspberry 上的 kivy 中从我的 Arduino 中“提取”一些值,该值通过无线 NRF24 模块连接。我正在将此库与python 包装器一起使用
在纯 Python 中,代码运行良好,现在我想将它集成到 Kivy 中。
为此,我在里面做了两个函数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 函数应该每隔 x 秒从 kivy 应用程序调用一次,部分函数按照 kivy 时钟模块中的建议使用
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'
我想知道为什么会返回一个浮点对象,当我使用 help(radio) 打印无线电对象时,它会返回class RF24(Boost.Python.instance)
并且函数 stoplistening() 存在。