Bluetooth: laptop and Pico
The Pico 2 W has a radio chip, the CYW43439, that speaks Wi-Fi and Bluetooth. For a connection, Bluetooth Low Energy (BLE) has two roles: a peripheral advertises – it sends out its name again and again – and a central, here your laptop, finds it and connects. What the two say to each other is up to them. The simplest choice is the Nordic UART Service: a serial line over BLE, with one characteristic the laptop writes to and one the Pico notifies on.
PicoRuby has it as BLE::UART, written by HASUMI Hitoshi. It needs a Pico 2 W with R2P2-PICORUBY: the R2P2 for the Pico W leaves BLE out, because its 2 MB of flash cannot hold both networking and BLE. On the bench the laptop is simulated, and so is the radio – the methods of BLE::UART are the real ones, but the simulation passes the lines along without a Bluetooth stack. Start the echo server, then type a line into the laptop panel:
require "ble"
uart = BLE::UART.new(name: "PicoRuby")
uart.on_connect { puts "a laptop connected" }
# start calls the block about every 20 ms, here for 30 seconds
uart.start(30_000) do
if uart.available?
data = uart.read_nonblock(256)
uart.puts("echo: #{data}") if data
end
end
On the laptop (CRuby)
The next cell is different: it does not run on the Pico but on CRuby, the course's other Ruby (lesson 89 lines up the Rubies) – it is the laptop's side of the radio. It uses the API of the ble gem, which talks to BlueZ, the Bluetooth stack of Linux: look for the Pico by its name, connect, subscribe to what it notifies, write a line. In the course a small stand-in for the gem hands the lines to the simulated radio.
Run the echo server above first. It advertises for 30 seconds, and a laptop only finds a Pico that advertises – otherwise adapter.devices is [] and the cell says "not found".
require "ble"
nus = "6e400001-b5a3-f393-e0a9-e50e24dcca9e" # Nordic UART Service
rx = "6e400002-b5a3-f393-e0a9-e50e24dcca9e" # the laptop writes here
tx = "6e400003-b5a3-f393-e0a9-e50e24dcca9e" # the Pico notifies here
adapter = BLE::Adapter.new("hci0")
adapter.start_discovery
sleep 2
adapter.stop_discovery
mac = adapter.devices.find { |m| adapter[m].name == "PicoRuby" }
if mac
pico = adapter[mac]
pico.connect
pico.subscribe(nus, tx, raw: true) { |bytes| print bytes }
# pico[nus, rx, raw: true] = ... is no longer Ruby (3.4): no keywords
# in an index assignment - so the method is called by its name
pico.[]=(nus, rx, "hello from CRuby #{RUBY_VERSION}\n", raw: true)
main = DBus::Main.new
main << DBus::SystemBus.instance
main.run # on Linux: the loop that delivers the notifications
else
puts "not found - is the Pico above advertising?"
end
PicoPaper: both directions
A real program does more than echo. PicoPaper takes commands from the laptop and reports by itself: the line SHOW Chunky bacon! puts the text on the e-paper (the driver from lesson 99 is in the tabs), and every 5 seconds, while a laptop is connected, the Pico sends T and the chip's temperature. gets_nonblock never waits: it returns a whole line, or nil while none has arrived – so the block is done at once when there is nothing to do, and start calls it again 20 ms later. The bench script sends one SHOW; type more into the laptop panel.
require "ble"
require "adc"
require "spi"
require "epd2in13"
spi = SPI.new(unit: :RP2040_SPI1, frequency: 4_000_000,
sck_pin: 10, copi_pin: 11)
paper = EPD2in13.new(spi: spi, cs_pin: 9, dc_pin: 8,
rst_pin: 12, busy_pin: 13)
sensor = ADC.new(:temperature)
uart = BLE::UART.new(name: "PicoPaper")
last = Machine.board_millis
uart.start(30_000) do
line = uart.gets_nonblock # a whole line, or nil
if line
command, text = line.chomp.split(" ", 2)
if command == "SHOW"
paper.clear
paper.text(4, 4, text.to_s, scale: 2)
paper.refresh
uart.puts "OK"
end
end
if Machine.board_millis - last > 5000 && uart.connected?
celsius = 27 - (sensor.read - 0.706) / 0.001721
uart.puts "T #{(celsius * 10).to_i / 10.0}"
last = Machine.board_millis
end
end
The Pico's code runs as it is on a Pico 2 W with R2P2-PICORUBY – lesson 103 shows how it gets there. For the laptop's side there are several ways:
- CRuby with the
blegem on Linux with BlueZ:gem install ble, then the cell above, wheremain.runreally is the loop. The gem's last release is from 2018. Its block forms useProc.newwithout a block, which Ruby 3 removed, so stick to the calls without a block, as above – and be ready to patch it, or to talk to BlueZ over D-Bus yourself withruby-dbus. - Web Bluetooth from a web page: PicoRuby.wasm has
JS::BLE::UART, the Nordic UART Service from the browser's side. It needs Chrome or Edge, a page on HTTPS or localhost, and a click that opens the device chooser. It does not work in a Web Worker – and the cells here run in one. So this page does it itself: under a bench with the laptop, 📶 A real Pico 2 W (Bluetooth) connects the bench's laptop to a real Pico 2 W running one of the programs above; the laptop's lines then go to it, and what it sends shows on the laptop and reaches the CRuby cell. Lesson 103 says which browsers have it; it has not been tried with real hardware yet. - A Pico without radio: Waveshare's Pico-BLE board sits on UART0 (GP0, GP1). On the Pico it is a plain
UART; a Linux computer pairs with it and opens/dev/rfcomm0like a serial port. - No Bluetooth at all, over Wi-Fi: R2P2-PICORUBY has
picoruby-drb, which speaks CRuby's dRuby.DRb.start_service("druby://0.0.0.0:8787", paper)on the Pico serves an object, and irb on the laptop calls its methods throughDRbObject.new_with_uri("druby://<address>:8787"). Its interop script is written for exactly that; whether it has been tried on a board is not documented.
PING with PONG, and TEMP with the chip's temperature in °C, worked out with the formula from PicoPaper. The bench script connects the laptop, sends PING after 0.3 seconds and TEMP after 0.8, and sets the room to 23 °C.require "ble"
require "adc"
link = BLE::UART.new(name: "ChunkyPico")
chip = ADC.new(:temperature)
link.start(1500) do
message = link.gets_nonblock
# "PING" -> answer "PONG", "TEMP" -> the temperature in °C
end