作者TheSims4 (模拟市民4)
看板Python
标题[问题] LINE bot回传数值的问题
时间Tue Jan 4 17:03:05 2022
不好意思我是Python新手
目前在做LINE bot回传raspberry pi温度监测数值的作业
网路上的资源大多都教直接回传原讯息
我的是收到user传送来的特定字元时会侦测sensor的温度并回传
因为不太熟 app跟functino一直想不出来 想请各位大神指点
app.py
reply = event.message.text
if message.count('temp'):
function.temperature()
temperature = function.temperature
reply = temperature
function.py
dht = adafruit_dht.DHT(board.D17)
def temperature():
temperature = dht.temperature
print (temperature)
return temperature
======
function.py的print是为了验证能显示出来结果真的可以在设备上看到温度
但是执行时有这个错误不知道怎麽排除
Object of type function is not JSON serializable
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 1.160.231.139 (台湾)
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Python/M.1641286987.A.4B8.html
1F:→ miku3920: 回传格式错误 01/04 17:06
2F:→ miku3920: 你仔细检查看看哪里打错了 01/04 17:06
3F:→ miku3920: 有可能是 key 没打到 01/04 17:07
4F:推 lycantrope: temperature = function.temperature() # call func 01/04 18:31
感谢你们热心提供解答及方向
後来app.py改写如下 已经可以正常运作
留个纪录
if message.count('temp'):
function.temperature()
temperature = function.temperature()
line_bot_api.reply_message(event.reply_token,
TextSendMessage(text="Temperature is" + str(temperature)))
※ 编辑: TheSims4 (1.160.231.44 台湾), 01/04/2022 23:53:00
5F:推 lycantrope: function.temperature()可以不用叫两次 01/05 00:06