第1步:您需要什么
Rapberry Pi 3-可從RS Components獲得。
RPi3內置了藍牙無線電。您也許可以將其與RPi的早期版本和藍牙加密狗配合使用,但我沒有嘗試過。
用于RPi的microSD卡
監視器,鍵盤和鼠標(僅用于RPi的初始設置)
一個Google帳戶。
像這樣的小型顯示器。 (可選)
10芯帶狀電纜,40針和10針接頭連接器插座等,用于將顯示器連接到RPi GPIO接頭(可選)
第2步:設置Raspberry Pi操作系統
由于在https://www上已經有很多howtos,因此我將不涉及設置raspberry pi SD卡的細節。 raspberrypi.org。下載最新版本的Raspbian,并使用Win32 Disk Imager(Windows)或dd(linux)等軟件包對SD卡進行映像。插入電源,將顯示器,鍵盤和鼠標連接到RPi并打開電源。將其啟動到桌面后,使用WiFi或直接將其插入網絡集線器將其連接到本地網絡。
打開終端外殼,并使用pi更新pi上的軟件存儲庫信息。以下命令:
sudo apt-get update -y
然后使用
sudo apt-get upgrade -y
和
sudo apt-get dist-upgrade -y將pi上的所有軟件升級到最新版本
最后,確保已安裝所有必需的藍牙和python軟件
sudo apt-get install bluez python-bluez python-requests python-pygame python-rpi.gpio -y
步驟3:檢查Pi是否可以看到傾斜的IBeacon
將傾斜式比重計放在一杯水中或傾斜一定角度以將其打開并在終端中發出以下命令。
sudo hcitool lescan
當您看到傾斜的地址和名稱彈出時,請按ctrl + c停止掃描。 12位十六進制數字是傾斜的藍牙地址。它就像是用于網絡設備的MAC地址,但用于藍牙。
如果未出現,則表示不正確。檢查Tilt的電池狀態,并且它實際上已經打開(當傾斜度從垂直位置移動到傾斜位置時,LED應當閃爍)。傾斜到垂直位置時,需要20到30秒才能進入睡眠模式。嘗試使用Android或IOS設備連接到Tilt。如果Tilt正常,則RPi一定有問題。仔細檢查是否已安裝所有必需的藍牙軟件。
步驟4:設置Python代碼
這是我第一次完成任何操作Python除了運行別人開發的奇怪腳本外,還可以運行。這是一種很棒的高級解釋器語言,看起來非常直觀。并不是說我立即成為超級黑客,但是我可以讓它做我想讓它相對容易地做的事情。希望新手能夠輕松瀏覽我的代碼,知道自己在做什么的家伙不會太笑了。
要使此代碼正常工作,您需要安裝Google表格并如Tilt Hydrometer的人員在此博客文章中所述,作為Web應用程序進行了部署。將blescan.py和tiltV1.py復制到RPi上的同一目錄(我與/home/pi/tilt一起使用),導航到該目錄,然后使用文本編輯器(如文本編輯器)將表格Web部署應用程序的URL復制到納米或gedit。使用
sudo python tiltV1.py
代碼執行代碼:
import blescan
import sys
import requests
import datetime
import time
import bluetooth._bluetooth as bluez
import pygame
import os
#Assign uuid‘s of various colour tilt hydrometers. BLE devices like the tilt work primarily using advertisements.
#The first section of any advertisement is the universally unique identifier. Tilt uses a particular identifier based on the colour of the device
red = ’a495bb10c5b14b44b5121370f02d74de‘
green = ’a495bb20c5b14b44b5121370f02d74de‘
black = ’a495bb30c5b14b44b5121370f02d74de‘
purple = ’a495bb40c5b14b44b5121370f02d74de‘
orange = ’a495bb50c5b14b44b5121370f02d74de‘
blue = ’a495bb60c5b14b44b5121370f02d74de‘
yellow = ’a495bb70c5b14b44b5121370f02d74de‘
pink = ’a495bb80c5b14b44b5121370f02d74de‘
#The default device for bluetooth scan. If you’re using a bluetooth dongle you may have to change this.
dev_id = 0
#function to calculate the number of days since epoch (used by google sheets)
#In python time.time() gives number of seconds since epoch (Jan 1 1970)。
#Google Sheets datetime as a number is the number of days since the epoch except their epoch date is Jan 1 1900
def sheetsDate(date1):
temp = datetime.datetime(1899, 12, 30)
delta=date1-temp
return float(delta.days) + (float(delta.seconds) / 86400)#scan BLE advertisements until we see one matching our tilt uuid
def getdata():
try:
sock = bluez.hci_open_dev(dev_id)
except:
print “error accessing bluetooth device.。。”
sys.exit(1)
blescan.hci_le_set_scan_parameters(sock)
blescan.hci_enable_le_scan(sock)
gotData = 0
while (gotData == 0):
returnedList = blescan.parse_events(sock, 10)
for beacon in returnedList: #returnedList is a list datatype of string datatypes seperated by commas (,)
output = beacon.split(‘,’) #split the list into individual strings in an array
if output[1] == black: #Change this to the colour of you tilt
tempf = float(output[2]) #convert the string for the temperature to a float type
gotData = 1
tiltTime = sheetsDate(datetime.datetime.now())
tiltSG = float(output[3])/1000
tiltTemp = tempf
tiltColour = ‘BLACK’
tiltBeer = ‘test’ #Change to an identifier of a particular brew
#assign values to a dictionary variable for the http POST to google sheet
data= {
‘Time’: tiltTime,
‘SG’: tiltSG,
‘Temp’: tiltTemp,
‘Color’: tiltColour,
‘Beer’: tiltBeer,
‘Comment’: “”
}
blescan.hci_disable_le_scan(sock)
return data
def main():
global screen
updateSecs = 600 #time in seconds between updating the google sheet
timestamp = time.time() #Set time for beginning of loop
updateTime = timestamp + updateSecs #Set the time for the next update to google sheets
while True:
data = getdata()
if time.time() 》 updateTime: #if we‘ve reached the update time then do a POST to the google sheet and reset the updateTime
r = requests.post(’https://the.address.of.your.google.sheets.script/exec‘, data) #Change this to the address of your google sheet script
#print r.text
updateTime = updateTime + updateSecs
if __name__ == “__main__”: #dont run this as a module
main()
我建議您在測試時減少’updateSecs‘變量的時間,無需等待片刻的更新。嘗試將奇數打印(變量)也放入代碼中,這樣您就可以看到發生了什么事
步驟5:添加本地顯示
“太好了”,我聽你說,“但是如果我沒有網絡可以連接,那就太好了!”。是的,是的。我在介紹中提到的LAMP服務器安排將是一個很好的理由。另一種可能性是增加一個小的TFT顯示屏,讓您知道發生了什么。我使用了從我當地的Jaycar購買的Freetronics 128x128像素OLED顯示器,價格約為20澳元。我按照這里的說明將其連接到Raspberry Pi上的GPIO接頭,然后按照說明在此處安裝由Notro開發的fbtft模塊。
就我而言,我必須在目錄/etc/modules-load.d/中添加一個名為fbtft.conf的文件:
sudo nano /etc/modules-load.d/fbtft.conf
,并添加以下幾行
spi-bcm2835
fbtft_device
(在nano中按CTRL + x保存并退出)
然后將一個名為fbtft.conf的文件添加到/etc/modprobe.d/:
sudo nano /etc/modprobe.d/fbtft.conf
,并在其中添加以下幾行
options fbtft_device name=freetronicsoled128
最后,最重要的是在“主界面”-》“首選項”-》“ Raspberry Pi配置”中’標簽,啟用SPI接口。重新啟動,如果一切正常,將有另一個幀緩沖區fb1。通過發出以下命令進行檢查:
ls /dev | grep fb
響應應為:
fb0
fb1
fb0是pi的HDMI輸出。
《這些小屏幕很容易燒壞,所以我想建立一個函數使屏幕進入睡眠狀態并有一個按鈕將其喚醒。該顯示器在顯示器的“機翼”上帶有兩個按鈕。我將它們以下拉配置連接到GPIO17和GPIO27。上面的電路圖。
步驟6:使用Pygame開發顯示器
一旦硬件就位并且操作系統能夠識別顯示器,我們就可以修改Python代碼以使用Pygame合并屏幕。在這種情況下,我將修改SDL Environment變量以直接寫入Freetronics顯示器的幀緩沖區。希望代碼中的行注釋可以說明發生了什么。
與上次相同,將tiltV2.py模塊復制到與blescan.py相同的目錄中,并使用以下命令執行程序:
sudo python tiltV2.py
需要注意的幾件事。如果您沒有連接幾個按鈕,則沒有辦法退出該程序,除非將RPi上的插頭拔出或將其推入一個單獨的控制臺并終止進程或重新啟動。同樣,一旦進入睡眠狀態,將無法喚醒屏幕。您可以使用Pygame來監視鍵盤事件。我確定代碼在那里。
Anywho,就是這樣。也許這不是一個超級優雅的解決方案,我還沒有考慮到校準點的編碼,但是它可以工作,并且是新的android設備的廉價替代品。
-
樹莓派
+關注
關注
121文章
2009瀏覽量
107472
發布評論請先 登錄
樹莓派6 傳聞:發布日期與規格!

樹莓派小技巧:無需鍵盤或顯示器,如何通過WiFi設置樹莓派?

用樹莓派“揪出”家里的耗電怪獸!

樹莓派“吉尼斯世界記錄”:將樹莓派的性能發揮到極致的項目!

評論