非商业、非专业用户,为业余爱好者项目寻求建议。
我有两个 Pi,如下所示: Pi 1 托管 Pimoroni Enviro Phat 并将其读数写入同样位于 Pi 1 上的数据库
Pi 2 托管 Pimoroni Scroll PhatHD。 我希望它从 Pi 1 上的数据库读取数据,并将其显示在 Pi 2 上的 Scroll Phat HD 上
我已经建立了所有必要的 Mysql 连接要求,它们都很好。
如何从 Pi1 上的数据库获取读数以在 Pi 2 上的 Scroll PhatHD 上滚动?
import signal
import time
import scrollphathd
from scrollphathd.fonts import font5x7
import mysql.connector
from mysql.connector import Error
con = mysql.connector.connect(host='192.168.#.##',database='test',user='#####',password='#######')
cur = con.cursor()
cur.execute('SELECT Yaxis FROM readings ORDER BY Added DESC LIMIT 1')
### Keep trailing comma from SELECT result away:
result = [row[0] for row in cur.fetchall()]
### Show result of SELECT query in terminal
print result
# temperature = int((float('result')))
temperature = ['result']
# Write the "Hello World!" string in the buffer and
# set a more eye-friendly default brightness
scrollphathd.write_string(" Hello World! %.1fC "%(temperature), brightness=0.5)
# Auto scroll using a while + time mechanism (no thread)
while True:
# Show the buffer
scrollphathd.show()
# Scroll the buffer content
scrollphathd.scroll()
# Wait for 0.1s
time.sleep(0.1)
错误内容为:“TypeError:需要浮点参数,而不是列表” 谢谢。
<小时/>好的,我的这个运行良好。 Pi2 正在从 Pi1 上的数据库中读取三个参数,并在某些 if/elif
中使用它们,以将它们适本地显示在 Scroll PhatHD 上。
我只是想知道。 Python 中有没有一种方法可以将数据库连接信息保存在单独的文件中,就像我在 PHP 中一样,使用 require
?
谢谢。
最佳答案
线路
温度=['结果']
将温度
变量设置为列表,而行
scrollphathd.write_string("Hello World!%.1fC"%(温度),亮度=0.5)
期望温度
变量是float
类型变量。
用 Temperature = result[0]
替换该行可以解决该问题,但请注意,它不能处理查询返回空响应的情况。
关于python TypeError : float argument required, 未列出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54513852/