作者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://www.ptt.cc/bbs/Python/M.1641286987.A.4B8.html
→ miku3920: 回傳格式錯誤 01/04 17:06
→ miku3920: 你仔細檢查看看哪裡打錯了 01/04 17:06
→ miku3920: 有可能是 key 沒打到 01/04 17:07
推 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
推 lycantrope: function.temperature()可以不用叫兩次 01/05 00:06