月份彙整: 2021 年 12 月

將 Windows 10 PC 變成 iBeacon:使用 windows-rs 實作

這幾天在練習使用 windows-rs,寫了一個讓 Windows 10 廣播 iBeacon 訊號的小程式。

完整的程式碼:https://github.com/riddleling/beacon-publisher-on-windows

這個程式很簡單, 使用 UWP API 的 BluetoothLEAdvertisementPublisher 發布 iBeacon 廣告:

let manufacturer_data = BluetoothLEManufacturerData::new()?;

// 設定 iBeacon 的 Manufacturer Data:
manufacturer_data.SetCompanyId(0x004C)?;  // Company ID
let data:[u8; 23] = [
    // Type:
    0x02,
    // Data Length:
    0x15,
    // Proximity UUID:
    0xE3, 0x0A, 0xC8, 0xFE,
    0x75, 0xB8, 0x47, 0x21,
    0x4B, 0x5D, 0x56, 0xB7, 
    0x07, 0x64, 0x25, 0xA9,
    // Major:
    0x00, 0x02,
    // Minor:
    0x00, 0x03,
    // TX Power:
    0xC8
];

let writer = DataWriter::new()?;
writer.WriteBytes(&data)?;

let buffer = writer.DetachBuffer()?;
manufacturer_data.SetData(buffer)?;

let publisher = BluetoothLEAdvertisementPublisher::new()?;

// 把 Manufacturer Data 加入 publisher:
publisher.Advertisement()?.ManufacturerData()?.Append(manufacturer_data)?;

// 開始廣播:
publisher.Start()?;

停止廣播:

publisher.Stop()?;

寫了一個 Serial Port Tool

最近用 Rust 寫了一個 serial port tool 給自己用,GUI 的部分使用 gtk-rs, serial port 通訊的部分使用 tokio-serial (serialport-rs 的 tokio 運行時版本),USB hotplug 的部分使用 rusb (libusb 的 Rust 包裝)。

不過,因為 libusb 不支持 Windows 上的 hotplug,所以在 Windows 上執行時,我使用一個 loop 每隔一秒列舉一次 USB 裝置,來判斷是否有 USB 裝置插入或拔出。

Serial Port Tool 的原始碼:https://github.com/riddleling/serial-tool

然後可以用 Arduino 寫一個 serial echo 程式來測試,把寫入 serial port 的字元回傳回來:

// Arduino serial echo

void setup() {
    Serial.begin(115200);
}

void loop() {    
    if (Serial.available() > 0) {
        Serial.print((char)Serial.read());
    }
}

在 Linux (Raspberry Pi OS) 上執行 serial port tool 的畫面:

在 Windows 上執行的 demo 影片 (透過 serial port 傳輸指令,打開或關閉 LED):

用 Raspberry Pi 打造一台 Spotify 播放器 – Part 2

延續上一篇的內容,要接著說明控制端的 GUI app 如何編譯執行。

在開始之前,先用 ssh 連到 Raspberry Pi,然後安裝 mDNS 的套件包與修改主機名稱。

$ ssh pi@<Raspberry Pi's IP address>
$ sudo apt -y install avahi-daemon
$ sudo raspi-config

執行 sudo raspi-config 後,會看到一個文字介面的選單,選擇 System Options => Hostname,然後把 Hostname 改成 spotifypi (或是你喜歡的名字)。改完後請重新開幾。


Raspberry Pi 開啟 mDNS 的服務後,我們就可以透過 <hostname>.local 這個網址來取代 IP 位址,例如:假設 Raspberry Pi 的 IP 位址是 192.168.1.110,那我們透過 WebSocket 去控制時的 URL 是 ws://192.168.1.110:9487,現在有了 mDNS,可以把 URL 改成 ws://spotifypi.local:9487。

閱讀全文

用 Raspberry Pi 打造一台 Spotify 播放器 – Part 1

最近把 Raspberry Pi 4B 改成 Spotify 播放器,想把製作方式記錄下來。

以前要在 Raspberry Pi OS 的 Chromium 上執行 Spotify Web Player,會因為缺少 Widevine DRM 支持的關係而無法播放。約在今年三月時,Raspberry Pi OS 正式提供了 Widevine DRM 的支持,所以現在只要在 Raspberry Pi OS 上安裝 Widevine DRM 的套件包即可用 Chromium 播放 Spotify!

在 Raspberry Pi OS 上開啟終端機,執行:

$ sudo apt update
$ sudo apt upgrade
$ sudo apt install libwidevinecdm0
$ sudo reboot

然後用 Chromium 打開 https://open.spotify.com,就可以開始使用 Spotify 了。


不過我想把 Raspberry Pi 改成 Spotify 專屬播放機,所以接下來就來說明如何設定吧。

閱讀全文