raspberry pi_通过串行蓝牙从Raspberry Pi传感器单元发送数据
raspberry pi
介紹 (Introduction)
When building portable sensors we often want to calibrate and double-check their readings before allowing them to log data remotely. Whilst developing these we can easily SSH into them and write any results to screen. However what happens when we are in a very remote part of the world, with no laptop, wifi or signal?
在制造便攜式傳感器時,我們經常要校準并仔細檢查它們的讀數,然后再允許它們遠程記錄數據。 在開發這些程序時,我們可以輕松地將它們通過SSH SSH并將任何結果寫入屏幕。 但是,當我們身處世界的一個偏遠地區而沒有筆記本電腦,WiFi或信號時,會發生什么呢?
Within this tutorial, we look at exploiting the Bluetooth capabilities of a Raspberry Pi Zero (without WiFi) to transmit the initial set of results to a handheld device of our choosing. In our case, it will be through the use of a mobile phone or android tablet, such that we can compare the sensor and GPS readings.
在本教程中,我們將研究如何利用Raspberry Pi Zero(沒有WiFi)的藍牙功能將初始結果集傳輸到我們選擇的手持設備。 在我們的案例中,將通過使用手機或android平板電腦來進行,以便我們可以比較傳感器和GPS讀數。
安裝與設定 (Installation and Setup)
Before we start there are a couple of changes required for the Bluetooth to work. These are outlined below.
在我們開始之前,需要進行一些更改才能使藍牙工作。 這些概述如下。
配置設備藍牙 (Configuring the device Bluetooth)
We begin by changing the configuration of the installed Bluetooth library:
我們首先更改已安裝的藍牙庫的配置:
sudo nano /etc/systemd/system/dbus-org.bluez.serviceHere we locate the line starting ExecStart , and replace it with the following:
在這里,我們找到以ExecStart開頭的行,并將其替換為以下內容:
ExecStart=/usr/lib/bluetooth/bluetoothd --compat --noplugin=sapExecStartPost=/usr/bin/sdptool add SP
Having added the ‘compatibility’ flag, we now have to restart the Bluetooth service on the Pi:
添加了“ compatibility”標志后,我們現在必須在Pi上重新啟動藍牙服務:
sudo systemctl daemon-reload;sudo systemctl restart bluetooth.service;
配對我們的顯示器設備 (Pairing our monitor device)
To prevent issues with pairing Bluetooth devices whilst out in the field, it is always a good idea to pre-pair devices — saving their configuration.
為了防止在野外與藍牙設備配對時出現問題,對設備進行預配對始終是一個好主意-保存其配置。
To do this we use bluetoothctl following the process described in the link below:
為此,我們按照以下鏈接中所述的過程使用bluetoothctl :
This gives results in the format:
這樣得出的結果格式如下:
Scanning ...XX:XX:XX:XX:XX:XX device1XX:XX:XX:XX:XX:XX device22. select the device we want and copy its address.
2.選擇我們想要的設備并復制其地址。
3. execute the following:
3.執行以下操作:
sudo bluetoothctl4. In the Bluetooth console run the following 3 commands (substituting your copied address):
4.在藍牙控制臺中,運行以下3個命令(替換您的復制地址):
discoverable on# thenpair XX:XX:XX:XX:XX:XX# and trust XX:XX:XX:XX:XX:XX# where XX corresponds to the address copied from aboveWhen pairing you may be asked to confirm a pin on both devices. trust saves the device address to the trusted list.
配對時,可能會要求您確認兩個設備上的引腳。 trust將設備地址保存到可信列表中。
To make a PI discoverable on boot, you can have a look at the code here:
要使PI在啟動時可被發現,可以在此處查看代碼:
在啟動時啟用通訊 (Enabling communication on startup)
Finally, we wish to tell the device to watch for incoming Bluetooth connections when it boots. To do this we can add the following file to /etc/rc.local (before the exit command).
最后,我們希望告訴設備在啟動時要監視傳入的藍牙連接。 為此,我們可以將以下文件添加到/etc/rc.local (在exit命令之前)。
sudo rfcomm watch hci0 &Take care to include the ampersand at the end, as otherwise, it will stall the device bootup process. Also if you are reading another device over serial, e.g. a GPS receiver, you may want to use rfcomm1 instead of hci0 (rfcomm0).
請小心在末尾包含&符,否則,它將使設備啟動過程停頓。 同樣,如果您正在通過串行方式讀取其他設備(例如GPS接收器),則可能要使用rfcomm1而不是hci0 (rfcomm0) 。
從另一臺設備連接到藍牙串行 (Connecting to the Bluetooth serial from another device)
Depending on what device you are using, the method to read from a serial monitor varies. On an android device, you may take the node/javascript approach (this should work on all operating systems!). For the purpose of this demo, I will describe a method using python to check things are working on a MacBook Pro.
根據所用設備的不同,從串行監視器讀取數據的方法也有所不同。 在android設備上,您可以采用node / javascript方法(這在所有操作系統上均適用!)。 出于本演示的目的,我將描述一種使用python檢查MacBook Pro上是否工作正常的方法。
確定端口名稱 (Determining the port name)
If you have a terminal, the simplest way to do this is to type
如果您有終端,執行此操作的最簡單方法是鍵入
ls /dev/tty.and hit the tab (autocomplete) button.
并點擊標簽(自動填充)按鈕。
Presuming you have not changed this, this should be your device hostname followed by SerialPort. The default serial port path for a freshly installed raspberry pi should be
假設您尚未更改,則應為設備hostname后跟SerialPort。 新安裝的樹莓派的默認串行端口路徑應為
/dev/tty.raspberrypi-SerialPort讀取收到的數據 (Reading data received)
To read any data received, we can use the python serial library coupled with the following code snippet.
要讀取接收到的任何數據,我們可以將python serial庫與以下代碼段結合使用。
import serialser = serial.Serial('/dev/tty.raspberrypi-SerialPort', timeout=1, baudrate=115000)serial.flushInput();serial.flushOutput()while True:
out = serial.readline().decode()
if out!='' : print (out)
Note that this is an infinite loop that keeps printing anything it receives. To cancel it when the message ‘exit’ is received we can use:
請注意,這是一個無限循環,不斷打印收到的任何內容。 要在收到“退出”消息時取消它,我們可以使用:
if out == 'exit': break從傳感器發送數據 (Sending data from the sensor)
從外殼 (From the shell)
When testing the simplest way to send data is to echo it to /dev/rgcomm0 from the raspberry pi shell. This allows us to manually test communication over the port before writing anything more complicated.
測試發送數據的最簡單方法是從/dev/rgcomm0 pi shell將其回顯到/dev/rgcomm0 。 這使我們能夠在編寫更復雜的內容之前手動測試通過端口的通信。
echo "hello!" > /dev/rfcomm0從python腳本 (From a python script)
If reading data from the raspberry pi and pre-processing it, chances are we will be using python to do the heavy lifting. From here we can treat the rfcomm0 channel as a file and write to it as follows:
如果從樹莓派讀取數據并對其進行預處理,則很有可能我們將使用python進行繁重的工作。 在這里,我們可以將rfcomm0通道視為一個文件,并按如下所示對其進行寫入:
with open(‘/dev/rfcomm0’,’w’,1) as f:f.write(‘hello from python!’)
結論 (Conclusions)
If we want to quickly check that our sensors are behaving whilst out in the field, we can make use of the Bluetooth capabilities of the Raspberry Pi. This is done by creating a Bluetooth serial port and sending data over it. Such methods are particularly useful if we do not wish to carry bulky laptops, or where a WiFi network is occupied or unavailable.
如果我們想在野外快速檢查傳感器的運行狀況,可以使用Raspberry Pi的藍牙功能。 這是通過創建藍牙串行端口并通過其發送數據來完成的。 如果我們不希望攜帶笨重的筆記本電腦,或者在WiFi網絡被占用或不可用的情況下,這種方法特別有用。
More complex tasks such as sending commands to the Raspberry Pi, or even SSHing into it over Bluetooth are also possible but are beyond the scope of this tutorial.
也可以執行更復雜的任務,例如將命令發送到Raspberry Pi,甚至通過藍牙SSH到Raspberry Pi,但這不在本教程的范圍之內。
翻譯自: https://towardsdatascience.com/sending-data-from-a-raspberry-pi-sensor-unit-over-serial-bluetooth-f9063f3447af
raspberry pi
總結
以上是生活随笔為你收集整理的raspberry pi_通过串行蓝牙从Raspberry Pi传感器单元发送数据的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: win7的winsxs文件清理方法
- 下一篇: 问答机器人接口python_设计用于机器