日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

raspberry pi_通过串行蓝牙从Raspberry Pi传感器单元发送数据

發(fā)布時(shí)間:2023/12/15 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 raspberry pi_通过串行蓝牙从Raspberry Pi传感器单元发送数据 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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?

在制造便攜式傳感器時(shí),我們經(jīng)常要校準(zhǔn)并仔細(xì)檢查它們的讀數(shù),然后再允許它們遠(yuǎn)程記錄數(shù)據(jù)。 在開發(fā)這些程序時(shí),我們可以輕松地將它們通過SSH SSH并將任何結(jié)果寫入屏幕。 但是,當(dāng)我們身處世界的一個(gè)偏遠(yuǎn)地區(qū)而沒有筆記本電腦,WiFi或信號時(shí),會發(fā)生什么呢?

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)的藍(lán)牙功能將初始結(jié)果集傳輸?shù)轿覀冞x擇的手持設(shè)備。 在我們的案例中,將通過使用手機(jī)或android平板電腦來進(jìn)行,以便我們可以比較傳感器和GPS讀數(shù)。

安裝與設(shè)定 (Installation and Setup)

Before we start there are a couple of changes required for the Bluetooth to work. These are outlined below.

在我們開始之前,需要進(jìn)行一些更改才能使藍(lán)牙工作。 這些概述如下。

配置設(shè)備藍(lán)牙 (Configuring the device Bluetooth)

We begin by changing the configuration of the installed Bluetooth library:

我們首先更改已安裝的藍(lán)牙庫的配置:

sudo nano /etc/systemd/system/dbus-org.bluez.service

Here we locate the line starting ExecStart , and replace it with the following:

在這里,我們找到以ExecStart開頭的行,并將其替換為以下內(nèi)容:

ExecStart=/usr/lib/bluetooth/bluetoothd --compat --noplugin=sap
ExecStartPost=/usr/bin/sdptool add SP

Having added the ‘compatibility’ flag, we now have to restart the Bluetooth service on the Pi:

添加了“ compatibility”標(biāo)志后,我們現(xiàn)在必須在Pi上重新啟動藍(lán)牙服務(wù):

sudo systemctl daemon-reload;
sudo systemctl restart bluetooth.service;

配對我們的顯示器設(shè)備 (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.

為了防止在野外與藍(lán)牙設(shè)備配對時(shí)出現(xiàn)問題,對設(shè)備進(jìn)行預(yù)配對始終是一個(gè)好主意-保存其配置。

To do this we use bluetoothctl following the process described in the link below:

為此,我們按照以下鏈接中所述的過程使用bluetoothctl :

  • Locate our host MAC address

    找到我們的主機(jī)MAC地址
  • hcitool scan

    This gives results in the format:

    這樣得出的結(jié)果格式如下:

    Scanning ...XX:XX:XX:XX:XX:XX device1XX:XX:XX:XX:XX:XX device2

    2. select the device we want and copy its address.

    2.選擇我們想要的設(shè)備并復(fù)制其地址。

    3. execute the following:

    3.執(zhí)行以下操作:

    sudo bluetoothctl

    4. In the Bluetooth console run the following 3 commands (substituting your copied address):

    4.在藍(lán)牙控制臺中,運(yùn)行以下3個(gè)命令(替換您的復(fù)制地址):

    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 above

    When pairing you may be asked to confirm a pin on both devices. trust saves the device address to the trusted list.

    配對時(shí),可能會要求您確認(rèn)兩個(gè)設(shè)備上的引腳。 trust將設(shè)備地址保存到可信列表中。

    To make a PI discoverable on boot, you can have a look at the code here:

    要使PI在啟動時(shí)可被發(fā)現(xiàn),可以在此處查看代碼:

    在啟動時(shí)啟用通訊 (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).

    最后,我們希望告訴設(shè)備在啟動時(shí)要監(jiān)視傳入的藍(lán)牙連接。 為此,我們可以將以下文件添加到/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).

    請小心在末尾包含&符,否則,它將使設(shè)備啟動過程停頓。 同樣,如果您正在通過串行方式讀取其他設(shè)備(例如GPS接收器),則可能要使用rfcomm1而不是hci0 (rfcomm0) 。

    從另一臺設(shè)備連接到藍(lán)牙串行 (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.

    根據(jù)所用設(shè)備的不同,從串行監(jiān)視器讀取數(shù)據(jù)的方法也有所不同。 在android設(shè)備上,您可以采用node / javascript方法(這在所有操作系統(tǒng)上均適用!)。 出于本演示的目的,我將描述一種使用python檢查MacBook Pro上是否工作正常的方法。

    確定端口名稱 (Determining the port name)

    If you have a terminal, the simplest way to do this is to type

    如果您有終端,執(zhí)行此操作的最簡單方法是鍵入

    ls /dev/tty.

    and hit the tab (autocomplete) button.

    并點(diǎn)擊標(biāo)簽(自動填充)按鈕。

    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

    假設(shè)您尚未更改,則應(yīng)為設(shè)備hostname后跟SerialPort。 新安裝的樹莓派的默認(rèn)串行端口路徑應(yīng)為

    /dev/tty.raspberrypi-SerialPort

    讀取收到的數(shù)據(jù) (Reading data received)

    To read any data received, we can use the python serial library coupled with the following code snippet.

    要讀取接收到的任何數(shù)據(jù),我們可以將python serial庫與以下代碼段結(jié)合使用。

    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:

    請注意,這是一個(gè)無限循環(huán),不斷打印收到的任何內(nèi)容。 要在收到“退出”消息時(shí)取消它,我們可以使用:

    if out == 'exit': break

    從傳感器發(fā)送數(shù)據(jù) (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.

    測試發(fā)送數(shù)據(jù)的最簡單方法是從/dev/rgcomm0 pi shell將其回顯到/dev/rgcomm0 。 這使我們能夠在編寫更復(fù)雜的內(nèi)容之前手動測試通過端口的通信。

    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:

    如果從樹莓派讀取數(shù)據(jù)并對其進(jìn)行預(yù)處理,則很有可能我們將使用python進(jìn)行繁重的工作。 在這里,我們可以將rfcomm0通道視為一個(gè)文件,并按如下所示對其進(jìn)行寫入:

    with open(‘/dev/rfcomm0’,’w’,1) as f:
    f.write(‘hello from python!’)

    結(jié)論 (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.

    如果我們想在野外快速檢查傳感器的運(yùn)行狀況,可以使用Raspberry Pi的藍(lán)牙功能。 這是通過創(chuàng)建藍(lán)牙串行端口并通過其發(fā)送數(shù)據(jù)來完成的。 如果我們不希望攜帶笨重的筆記本電腦,或者在WiFi網(wǎng)絡(luò)被占用或不可用的情況下,這種方法特別有用。

    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.

    也可以執(zhí)行更復(fù)雜的任務(wù),例如將命令發(fā)送到Raspberry Pi,甚至通過藍(lán)牙SSH到Raspberry Pi,但這不在本教程的范圍之內(nèi)。

    翻譯自: https://towardsdatascience.com/sending-data-from-a-raspberry-pi-sensor-unit-over-serial-bluetooth-f9063f3447af

    raspberry pi

    總結(jié)

    以上是生活随笔為你收集整理的raspberry pi_通过串行蓝牙从Raspberry Pi传感器单元发送数据的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。