No description
  • Python 75.4%
  • HTML 24%
  • Shell 0.6%
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
Lutz Freitag ffefa9f29a MCP server: connect on the first tool call, release the link when idle
dm40ctl mcp no longer connects at start: the first tool call opens the
link, and after --idle seconds (default 120) without a call it is
released again, together with the per-link lock. A Claude Code session
with the server configured no longer keeps Bluetooth connecting while
nobody measures. Running calls hold the link; a failed connect is
reported at once instead of retrying in the background. --idle 0 keeps
the old behaviour; the HTTP server inside tui/web is unchanged.

Meter.start() can run again after stop().

Fix: a transport whose open failed before its fd / socket existed
crashed the poller with AttributeError in release().

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 20:45:21 +01:00
src/dm40ctl MCP server: connect on the first tool call, release the link when idle 2026-11-30 20:45:21 +01:00
udev dm40ctl: TUI and web interface for the DM40 (opendm40) over USB and Bluetooth 2026-11-16 21:10:24 +01:00
.gitignore dm40ctl: TUI and web interface for the DM40 (opendm40) over USB and Bluetooth 2026-11-16 21:10:24 +01:00
install.sh Add install.sh for a per-user install (uv tool, system Python) 2026-11-16 21:27:19 +01:00
pyproject.toml Bluetooth LE transport and per-link locks 2026-11-16 22:32:59 +01:00
README.md MCP server: connect on the first tool call, release the link when idle 2026-11-30 20:45:21 +01:00
uv.lock Bluetooth LE transport and per-link locks 2026-11-16 22:32:59 +01:00

dm40ctl

TUI and web interface for the Alientek DM40 multimeter running the opendm40 firmware, over USB (USBTMC) or Bluetooth (SPP/RFCOMM). Uses the SCPI interface of opendm40.

  • Multimeter: all functions (V/A DC/AC/AC+DC/auto, Ω, continuity, diode, C, f, temperature, power meter), range (auto/manual, step), rate, AutoHold, smoothing, statistics on the meter, limits (pass/fail), temperature sensor, power-meter coupling and energy, wrong-connection alerts
  • Trend chart of the readings, CSV export, recording of every reading to CSV on the PC
  • Generator: waveform, frequency, amplitude, offset, duty cycle, output on/off
  • Scope (web): live waveform, timebase, V/div, coupling, offset, trigger, measurements
  • Wave view (web, MCP; USB): the meter's slow scope on the multimeter's own V / A jacks
  • Files (web): screenshots and recordings from the meter's disk, recordings also as CSV
  • SCPI console in both interfaces
  • TUI and web can run at the same time in one process (tui --web); this matters for Bluetooth, because the meter accepts only one Bluetooth connection

Installation

uv sync            # uses the system Python (needed for Bluetooth sockets)
uv run dm40ctl --help

Install for your user

./install.sh            # installs dm40ctl to ~/.local/bin (update: run it again)
./install.sh --udev     # additionally installs the udev rule for USB (asks for sudo)
./install.sh --remove   # uninstall

The script runs uv tool install with the system Python (/usr/bin/python3), because uv's own Python builds lack Bluetooth sockets. Plain uv tool install . also works, but then possibly only over USB.

USB access without root

The kernel driver usbtmc creates /dev/usbtmcN; by default only root may open it.

sudo install -m 644 udev/70-dm40.rules /etc/udev/rules.d/
sudo udevadm control --reload && sudo udevadm trigger

(or re-plug the meter). dm40ctl list shows whether access works.

Bluetooth

On the meter: Settings > Other > Bluetooth on, and close the phone app (the module takes only one connection). The meter's Bluetooth module is dual-mode and appears twice; dm40ctl can use both:

Variant Name in the scan --conn Notes
Bluetooth LE DM40A / DM40B / DM40C ble or ble:MAC no pairing needed; ble scans by itself if BlueZ does not know the meter
Classic serial port (SPP) C-1-ATK-DM40-CALI bt:MAC address = BLE address with the first byte + 1; needs the system Python (Bluetooth sockets)
dm40ctl list                      # shows the known meters and the matching --conn
dm40ctl --conn ble tui            # Bluetooth LE
dm40ctl --conn bt:60:82:BA:76:59:3B tui   # classic SPP
dm40ctl --conn bt tui             # whatever BlueZ knows (SPP before BLE)

auto (the default) tries USB first, then a meter BlueZ knows. USB and Bluetooth are separate sessions in the meter: one dm40ctl can use USB while another one uses Bluetooth at the same time (the lock is per link). A bound /dev/rfcomm0 works too (--conn /dev/rfcomm0).

Over Bluetooth the firmware blocks debug commands (so also the screenshot via MCP and the Wave view), deleting files and recording to the meter's disk; the link carries about 11 kB/s, and a query takes about 30 ms (BLE connect ~4 s).

Usage

dm40ctl                         # TUI, connection auto (USB first, then Bluetooth)
dm40ctl tui --web               # TUI plus web interface on http://localhost:8040/
dm40ctl web                     # web only
dm40ctl web --host 0.0.0.0      # reachable on the network (no authentication!)
dm40ctl --conn bt tui           # force Bluetooth
dm40ctl --conn sim tui --web    # simulator, no meter needed
dm40ctl scpi '*IDN?' 'MEAS:VOLT:DC?'
dm40ctl list                    # meters found on USB / Bluetooth
dm40ctl mcp                     # MCP server on stdio (see below)

Generator from the command line

dm40ctl gen                                   # show the current settings
dm40ctl gen -s sine -f 1k -a 2 --on           # 1 kHz sine, 2 Vpp, output on
dm40ctl gen -s square -f 100k -d 25 -o auto   # 100 kHz square, 25 % duty, offset auto
dm40ctl gen --off                             # output off

-s/--shape sine|square|triangle|ramp, -f/--freq Hz, -a/--amplitude Vpp (0…3), -o/--offset V or auto, -d/--duty %, --on/--off. Numbers take SI suffixes (2.5k, 500m). Options left out stay as they are; afterwards the meter's actual settings are printed. If the meter limits a value (sine/triangle/ramp only up to 50 kHz, at most 3 Vpp), dm40ctl warns about it.

--conn accepts auto, usb, usb:/dev/usbtmc1, bt, bt:MAC, ble, ble:MAC, /dev/rfcommN, sim (default: environment variable DM40_CONN, otherwise auto). --record-dir sets where recordings go. If the link drops, dm40ctl reconnects by itself.

TUI keys

Key Action
1…9, 0, p V DC, V AC, A DC, A AC, Ω, continuity, diode, C, f, temperature, power
+ / - / a range up / down / auto
r / h change the rate / AutoHold
s / c statistics on/off / reset
w / x chart window (30 s … all) / clear the chart
l record CSV on/off
o generator output on/off
d g t or F1 F2 F3 Multimeter / Generator / Console tab
Esc leave an input field
q quit

The other functions (AC+DC, auto coupling) can be picked in the list on the left.

MCP server (AI assistants)

dm40ctl includes an MCP server, so an assistant such as Claude Code can operate the meter.

Tool What it does
get_status everything the meter is set to and shows (reading, range, alerts, statistics, limits, power, generator)
measure fresh reading(s), optionally selects function and range first; with count > 1 min/max/mean/stdev
log_readings records for a duration (≤ 1 h): statistics, drift, up to 200 points, optional CSV
configure function, range, rate, AutoHold, smoothing, temperature sensor/unit, power-meter coupling, energy reset
statistics the meter's statistics: read / on / off / reset
set_limits pass/fail limits
generator read/set the generator (warns when the meter limits values)
scope_capture scope settings, single shot, measurements and waveform; input="dmm": the Wave view
list_files, download_file files on the meter's disk
screenshot picture of the display (USB only; reads the display memory, changes nothing)
scpi raw SCPI for everything else

Standalone (stdio): the MCP server owns the meter itself.

claude mcp add --scope user dm40 -- dm40ctl mcp

It connects only when the assistant calls a tool and releases the link again after 2 minutes without a call, so it can stay configured for every session without keeping the meter (or its Bluetooth) busy; the TUI can use the link in between. --idle SECONDS changes the time, --idle 0 connects at start and stays connected. A failed connect is reported at once; the next call tries again.

Alongside the TUI / web interface (HTTP): one process owns the meter, you watch along in the TUI or the browser while the assistant measures.

dm40ctl tui --web --mcp          # MCP at http://localhost:8041/mcp
claude mcp add --scope user --transport http dm40-live http://localhost:8041/mcp

Only one dm40ctl process can use a link at a time (lock file per link in $XDG_RUNTIME_DIR): two processes on one link would mix up each other's replies. While the TUI holds USB, the stdio server takes Bluetooth with auto, or you use the HTTP variant.

Notes

  • The scope tab of the web interface switches the meter to its scope page; while it is open, multimeter readings pause. Leaving the tab selects the previous multimeter function again.
  • The Wave view (scope tab, input DMM jacks) shows the signal on the multimeter's V / A jacks in DC/AC voltage or current: opendm40's slow scope, 2 ms/div to 50 s/div, values in A on current. There is no SCPI command for it: dm40ctl finds it with DBG:VIEW? and opens and closes it with taps on the meter's screen (DBG:TAP / DBG:DRAG), so it needs the USB link. Opened on the meter itself, dm40ctl follows it to the scope (the multimeter has no readings meanwhile; in the TUI m goes back). V/div and coupling follow the multimeter's range and function and are not set from here (the firmware would close the view). 1 ms/div is avoided: on opendm40 0.4.1 its 76.8 kS/s stream never locks and the meter drops the view after ~5 s ("Wave stopped: DMM changed"); dm40ctl moves it to 2 ms/div. Back in the multimeter, dm40ctl leaves it with MENU and shows the meter's multimeter page (on the scope page the multimeter has no readings while the scope's CH2, the DMM trace, is on).
  • The CSV recording (Record / l) writes time, t_s, function, value, unit, one row per reading, overload as OL.
  • The web interface binds to 127.0.0.1 by default. With --host 0.0.0.0, anyone on the network can control the meter, including all SCPI commands (over USB also the debug commands of the firmware).

Structure

src/dm40ctl/
  transport.py   USBTMC (/dev/usbtmcN), Bluetooth SPP (RFCOMM) and LE (bleak), serial, discovery
  meter.py       core: polling, state, history, commands, recording
  model.py       functions, ranges, units, formatting
  sim.py         simulated meter (--conn sim)
  tui.py         Textual interface
  web.py         aiohttp server + WebSocket
  mcp_server.py  MCP tools (stdio and streamable HTTP)
  static/        web page (no external dependencies, works offline)