[英]Raspberry Pi A+ Bluetooth with DS18B20 not working when script runs on boot
我編寫了一個Python腳本,該腳本可在Raspberry Pi A +上運行,以通過藍牙與Raspberry Pi B +連接,並通過DS18B20傳感器發送溫度數據。 當我從A +手動運行腳本時,它運行得很好,但是當我嘗試將腳本設置為在Pi啟動時運行時,它無法通過藍牙連接。 這是腳本:
import socket
import time
import os
#time.sleep(10)
serverMACAddress = '00:15:83:12:1A:39'
port = 3
connected = False
while connected == False:
try:
s = socket.socket(socket.AF_BLUETOOTH, socket.SOCK_STREAM, socket.BTPROTO_RFCOMM)
s.connect((serverMACAddress,port))
connected=True
print("connected")
except Exception as e:
print('failed')
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
temp_sensor = '/sys/bus/w1/devices/28-000006773191/w1_slave'
current_time = time.time()
UPDATE_THRESHOLD = 5
newTemp = False
def temp_raw():
f = open(temp_sensor,'r')
lines = f.readlines()
f.close()
return lines
def read_temp():
lines = temp_raw()
temp_output = lines[1].find('t=')
if temp_output != -1:
temp_string = lines[1].strip()[temp_output+2:]
temp_c = float(temp_string)/1000.0
temp_f = temp_c*9.0/5.0+32.0
return temp_c
waterTemp = read_temp()
while True:
nextWaterTemp = read_temp()
current_time = time.time()
newTemp = True
if nextWaterTemp != waterTemp:
waterTemp = nextWaterTemp
try:
s.send(bytes(str(waterTemp),'UTF-8'))
time.sleep(30)
connected = True
print(waterTemp)
except Exception as e:
print("Fail")
connected = False
while connected == False:
try:
s = socket.socket(socket.AF_BLUETOOTH, socket.SOCK_STREAM, socket.s.connect((serverMACAddress,port)))
s.send(bytes(str(waterTemp),'UTF-8'))
connected = True
print("Connected")
except Exception as e:
print("Fail")
time.sleep(.5)
如果連接失敗,則此代碼也很難重新連接到B +。 由於某種原因,我必須重新啟動B +和A +上的代碼,才能使它們再次連接。 但這是一個不同的問題。 我只希望此代碼在啟動時運行。
我用來使腳本在引導時運行的方法是將此代碼放在/etc/rc.local文件中: /usr/bin/python /home/pi/Desktop/client.py >/tmp.client.out 2>/tmp/client.err
有任何想法嗎?
更新:
它將始終在client.out文件中打印“失敗”。
要么
什么都沒有登錄到client.err或client.out文件中,並且屏幕頂部的藍牙圖標顯示它已連接,但是我只收到應該發送的溫度值0。 但是,當我嘗試手動運行腳本而不在啟動時運行腳本時,溫度正常。
找到了。
/usr/bin/python /home/pi/Desktop/client.py >/tmp.client.out 2>/tmp/client.err
需要是
/usr/bin/python3 /home/pi/Desktop/client.py >/tmp.client.out 2>/tmp/client.err
與DS18B20一起使用
搜索了幾個小時后,我感覺像個白痴。 很抱歉浪費任何人的時間。 如果某些可憐的靈魂需要它作為參考,我將在此保留。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.