API reference: Difference between revisions

From bcmeter.org
Jump to navigation Jump to search
Restructure: platform separation (ESP32 commercial prioritised, Raspberry Pi DIY in own section); verified against firmware/Pi source; complete serial command list
Document local API access, measurement, file export and firmware updates by device version.
Line 1: Line 1:
== API Reference ==
== Local HTTP API ==


The bcMeter exposes a REST API on port 80. All endpoints are accessible without authentication when connected to the device network. CORS is enabled for all responses (<code>Access-Control-Allow-Origin: *</code>).
bcMeter Kit and Complete instruments serve the API on the device’s own network over HTTP, port 80. The examples below use ESP32 firmware 1.5.361 and 1.5.681. Read <code>GET /api/status</code> first and use the returned <code>version</code>. Firmware 1.5.681 also returns <code>"platform":"esp32"</code>. On 1.5.361 the platform field is absent. On both versions <code>env</code> identifies the build environment.


The device identifies its platform via the <code>"env"</code> field in <code>/api/status</code>: <code>"env": "esp32"</code> for bcMeter (commercial) and <code>"env": "pi"</code> for bcMeter (DIY / Raspberry Pi).
=== Connecting ===


The software is source-available — freely available for private, educational and research use (CC BY-NC 4.0): [https://github.com/dahljo/bcmeter-pi github.com/dahljo/bcmeter-pi] (Pi / DIY version).
Replace the example hostname with the hostname or LAN IP shown by your device. This read-only example works with both versions.


=== Status & Diagnostics ===
<pre>
device='http://bcmeter-XXXX.local'
curl --fail --show-error -H 'X-bcMeter-Client: bcmctl' "$device/api/status"
</pre>
 
On the ESP32 market build at 1.5.681, status, CSV and routine controls accept local requests carrying the client header without a device password. Browser clients must use the device's own origin. Owner operations, including Wi-Fi credential changes, require a separate device-password proof for each request. On 1.5.361 the local API uses the earlier access model without these proofs.


{| class="wikitable"
For an owner request on 1.5.681:
! Method !! Endpoint !! Description !! Platforms
|-
| GET || <code>/api/status</code> || Current measurement state, last sensor readings (BC, ATN, sensor/reference voltages, flow, filter loading), error codes, sample count, session active, WiFi mode/SSID/RSSI, internet flag, firmware version, OTA result flag, time-sync state. || Both
|-
| GET || <code>/api/system</code> || Hardware info: IP, MAC address, heap and PSRAM usage, flash usage, modem status and signal, GPS detail, barometric altitude, geolocation source, current time, cycle timing statistics. || Both
|-
| GET || <code>/api/logs</code> || Structured health report as JSON with sections: <code>hardware</code>, <code>measurement</code>, <code>system</code>, <code>network</code>, <code>incidents</code>. Each entry has <code>{k, v, s}</code> (key, value, status: ok/warn/error/info). || Both
|-
| GET || <code>/api/debug_mobile/status</code> || Returns modem debug email send progress (from email handler). || Both
|}


=== Measurement Control ===
# Fetch <code>GET /api/auth/challenge</code> with the client header. The returned <code>nonce</code> is single-use, bound to the requesting client and valid for 30 seconds.
# Compute the lowercase hexadecimal SHA-256 digest of the exact request body, using the empty byte string for a bodyless request. Join the nonce, uppercase HTTP method, exact path including its query string, and body digest with one newline between fields. Compute HMAC-SHA256 over this string with the device password as the key and encode the result in lowercase hexadecimal.
# Send the client header plus <code>X-bcMeter-Nonce</code>, <code>X-bcMeter-Body-SHA256</code> and <code>X-bcMeter-Proof</code> on the intended request. To check a proof without changing settings, sign <code>GET /api/auth/verify</code> and look for <code>"authorized":true</code>. Obtain a fresh nonce for the next owner request.


All control is dispatched through a single endpoint with an <code>action</code> query parameter:
<div class="bcmeter-pi-note" role="note" style="border-left:4px solid #356481;background:#eef5f8;color:#162c39;padding:0.7em 1em;margin:1em 0;">
<strong>heads up for raspberry pi bcmeters</strong>


{| class="wikitable"
The Pi notes on this page apply to runtime 1.6.15. Check <code>version</code> on your device. Local control requests use the same client header. <code>GET /api/maintenance-logs</code> downloads a protected ZIP of operating-system and device diagnostics, which can include recent measurement logs.
! Method !! Endpoint !! Description !! Platforms
</div>
|-
| GET || <code>/api/control?action=start</code> || Begin measurement session. Returns 409 if time not synced, 423 if never calibrated, 424 if calibration stale (>28 days). Add <code>&force=1</code> to override preflight checks. Optional <code>&indoor=0|1</code> for one-shot indoor/outdoor override. || Both
|-
| GET || <code>/api/control?action=stop</code> || Stop measurement and finalise session file. || Both
|-
| GET || <code>/api/control?action=calibrate</code> || Start optical calibration routine (returns 202 immediately — poll <code>/api/calibration</code> for progress). Requires sampling to be stopped. || Both
|-
| GET || <code>/api/calibration</code> || Calibration status: <code>{running, done, ok, elapsed_ms, log}</code>. || Both
|-
| GET || <code>/api/control?action=clear_error</code> || Reset error state to ERR_NONE. || Both
|-
| GET || <code>/api/control?action=identify</code> || Triggers LED identify blink pattern (Morse code "bcmeter", ~30 seconds). Useful for locating a specific device when multiple units are deployed. || Both
|-
| GET || <code>/api/control?action=debug_mobile</code> || Triggers modem debug email via Lambda. Requires modem present. Returns 409 if modem unavailable or already running. || Both
|-
| GET || <code>/api/control?action=synctime&ts=EPOCH&tz=IANA</code> || Sync device clock. <code>ts</code> = Unix timestamp, <code>tz</code> = IANA timezone string (e.g. <code>Europe/Berlin</code>). || Both
|-
| GET || <code>/api/control?action=cleardata</code> || Delete all stored log files. || Both
|-
| GET || <code>/api/control?action=reboot</code> || End session and reboot the device. || Both
|-
| GET || <code>/api/control?action=shutdown</code> || Safely shut down the device. || Both
|-
| GET || <code>/api/control?action=factory_reset</code> || Permanently erase all logs, WiFi credentials, calibration data, and settings. Reboots into factory state. || Both
|}


=== Configuration ===
=== Measurement and configuration ===


{| class="wikitable"
{| class="wikitable"
! Method !! Endpoint !! Description !! Platforms
! Request !! Result
|-
|-
| GET || <code>/api/config</code> || All settings as JSON. Each parameter includes type, value, label, tab, and description. || Both
| <code>GET /api/status</code> || JSON including <code>status</code> (0 idle, 1 starting, 2 sampling, 3 stopped with an error), <code>session</code> (whether a log session is open), <code>error</code>, <code>error_msg</code>, <code>bc</code>, <code>atn</code>, <code>flow</code> (L/min), <code>time_synced</code> and <code>version</code>. On 1.5.681, <code>stop_pending</code> means finalization is still running.
|-
|-
| POST || <code>/api/config</code> || Update settings. JSON body with key-value pairs. Accepts <code>{key: value}</code> or <code>{key: {value: ...}}</code> format. Only changed values need to be sent. || Both
| <code>GET /api/system</code> · <code>GET /api/logs</code> || System reports device, storage and network information. On 1.5.681 some fields such as the MAC address are blank in ordinary reads. Use an owner-signed <code>GET /api/system?private=1</code> for private system details. The logs endpoint returns a structured health report including hardware, measurement, system and network sections.
|-
|-
| POST || <code>/api/device/rename</code> || Set device name. Body: <code>{"name": "bcmeter-A3AB"}</code> (1–32 chars). Updates mDNS/hostname accordingly. || Both
| <code>GET /api/control?action=synctime&amp;ts=EPOCH&amp;tz=Europe/Berlin</code> || Set the clock using Unix time in seconds. Use the timezone for the measurement site.
|-
|-
| GET || <code>/api/ap-security</code> || Hotspot security settings: <code>{secured: bool, password: str}</code>. || Both
| <code>GET /api/control?action=start</code> · <code>GET /api/control?action=stop</code> || Start a session or request a stop. For a one-session indoor/outdoor selection append <code>&amp;indoor=1</code> or <code>&amp;indoor=0</code> to start. Start can return 409 for an unsynchronized clock, 423 for missing calibration or 424 for calibration older than 28 days. On 1.5.681, a 202 stop response means cleanup continues. Wait for <code>stop_pending=false</code> and <code>session=false</code> before treating the file as final.
|-
|-
| POST || <code>/api/ap-security</code> || Update hotspot password. Body: <code>{"secured": true, "password": "..."}</code> (min 8 chars). || Both
| <code>GET /api/control?action=calibrate</code> · <code>GET /api/calibration</code> || Install a clean filter and stop sampling before starting calibration. The start call returns 202 when accepted. Poll the second endpoint for <code>running</code>, <code>done</code>, <code>ok</code>, <code>elapsed_ms</code> and <code>log</code>. A finished, successful run has <code>done=true</code> and <code>ok=true</code>. Use the setup wizard for first calibration.
|-
|-
| POST || <code>/api/email/validate</code> || Validate email API key against the Lambda endpoint. Body: <code>{"api_key": "..."}</code>. Returns <code>{valid: bool, error: str}</code>. || Both
| <code>GET /api/config</code> · <code>POST /api/config</code> || Read a JSON object keyed by setting. On 1.5.681 entries have <code>value</code>, <code>type</code>, <code>description</code> and <code>parameter</code> (settings group). Send only edited fields as JSON, for example <code>{"sample_time":300}</code> for a 300-second sampling interval. Apply acquisition changes while idle. Owner settings require a proof. A redacted value is unsuitable for copying into a write.
|}
|}


=== WiFi Management ===
<code>GET /api/control?action=identify</code> flashes the device indicator. <code>GET /api/control?action=clear_error</code> clears the recorded measurement error. <code>GET /api/crash</code> reports the last reset. On 1.5.681 its ordinary response contains a summary.


{| class="wikitable"
On 1.5.681, reboot and shutdown require a <code>confirm</code> query parameter matching the action. Erasing stored logs and factory reset require both matching confirmation and an owner proof. A missing confirmation returns 428. Use the device interface for these actions.
! Method !! Endpoint !! Description !! Platforms
|-
| GET || <code>/api/wifi/scan</code> || List nearby networks: <code>{scanning: bool, networks: [{ssid, rssi, secure}]}</code>. Add <code>?refresh=1</code> to trigger a new background scan. || Both
|-
| GET || <code>/api/wifi/status</code> || Current connection: <code>{mode, status, ssid, ip, rssi, quality (0-4), internet, timeSynced}</code>. || Both
|-
| POST || <code>/api/wifi</code> || Save WiFi credentials. Body: <code>{"ssid": "...", "pass": "..."}</code> (password min 8 chars). Initiates reconnect. || Both
|-
| POST || <code>/api/wifi/connect</code> || Save credentials and initiate background connection attempt (returns 202). Password must be 8–63 chars. Body: <code>{"ssid": "...", "pass": "..."}</code>. || Both
|-
| GET || <code>/api/wifi/connect/status</code> || Connection progress: <code>{state: 0-3, elapsed, log, mode, ip, ssid, device_name, hostname}</code>. States: 0=idle, 1=connecting, 2=success, 3=failed. Poll after POST to <code>/api/wifi/connect</code>. || Both
|-
| POST || <code>/api/wifi/delete</code> || Delete credentials and switch to AP mode (deferred 2s background switch). || Both
|-
| POST || <code>/api/wifi/scan/refresh</code> || Explicitly trigger a new WiFi scan (returns 202, or 409 if already scanning). || Both
|}


=== Data Management ===
<div class="bcmeter-pi-note" role="note" style="border-left:4px solid #356481;background:#eef5f8;color:#162c39;padding:0.7em 1em;margin:1em 0;">
<strong>heads up for raspberry pi bcmeters</strong>


{| class="wikitable"
In Pi runtime 1.6.15, <code>factory_reset</code> stops sampling and resets device settings, including the device password and last calibration date. Stored measurement files, saved Wi-Fi connections and the device-specific correction factor remain. The Pi stays running. Use this action only to restore settings. Unsupported control actions return HTTP 400.
! Method !! Endpoint !! Description !! Platforms
</div>
|-
| GET || <code>/api/csv</code> || Current session CSV data. Returns header-only if no active session. Add <code>?file=FILENAME</code> for archived files. || Both
|-
| GET || <code>/api/files</code> || List stored CSV logs: <code>[{name, size, lines, date}]</code> sorted newest first. Add <code>?name=FILENAME</code> to download a specific file. || Both
|}


=== Firmware Updates ===
=== Downloading data ===


{| class="wikitable"
<pre>
! Method !! Endpoint !! Description !! Platforms
curl --fail --show-error -H 'X-bcMeter-Client: bcmctl' "$device/api/files"
|-
</pre>
| GET || <code>/api/ota/status</code> || Check if update available: <code>{available, version, notes, apply_state, apply_progress, apply_error}</code>. || Both
|-
| POST || <code>/api/ota/check</code> || Force check for new firmware. || Both
|-
| POST || <code>/api/ota/skip</code> || Skip current available update. || Both
|-
| POST || <code>/api/ota/apply</code> || Download and apply firmware update. Returns 409 if none pending. || Both
|-
| POST || <code>/api/update</code> || Direct firmware upload. ESP32: accepts a raw <code>.bin</code> file, flashes to the OTA partition, reboots. Pi: accepts a <code>.tar.gz</code> or <code>.zip</code> archive, extracts over the code directory, restarts the <code>bcMeter.service</code>. || Both
|}


=== Captive Portal ===
<code>GET /api/files</code> lists session files as a JSON array. Select a returned <code>name</code>. <code>GET /api/csv?file=FILENAME</code> and <code>GET /api/files?name=FILENAME</code> retrieve the selected CSV. For example, set <code>file</code> to a name from the list and run:


These endpoints handle captive portal detection on mobile devices and redirect to the main interface in AP mode:
<pre>
curl --fail --show-error -H 'X-bcMeter-Client: bcmctl' --get \
  --data-urlencode "file=$file" "$device/api/csv" -o measurement.csv
</pre>


{| class="wikitable"
<code>GET /api/csv</code> without a filename uses the session selected on the device. On 1.5.681 it falls back to the newest stored log if the selected session file is unavailable. An idle instrument can therefore return a previous session. If the device has no log to return, the response contains only the CSV header. On 1.5.681 <code>GET /api/files?name=FILENAME&amp;manifest=1</code> retrieves the session's JSON manifest when available. The list reports <code>manifest_available</code> and <code>active</code>. For SD-card logs use <code>GET /api/sdfiles</code>.
! Endpoint !! Purpose
|-
| <code>/generate_204</code> || Android captive portal check
|-
| <code>/hotspot-detect.html</code> || Apple captive portal check
|-
| <code>/canonical.html</code> || Firefox captive portal check
|-
| <code>/ncsi.txt</code> || Windows captive portal check
|-
| <code>/success.txt</code> || Generic connectivity probe (returns <code>"success"</code>)
|}


=== Static Routes ===
On 1.5.681, <code>POST /api/files/upload</code> with <code>{"name":"FILENAME"}</code> asks the device to upload the named, completed session. A 202 response means queued. A 422 response can indicate disabled sharing, missing location or provisional timestamps. <code>DELETE /api/files</code> takes the <code>name</code> query parameter, owner proof and <code>confirm=delete_log</code>. The device rejects deletion of its active session with 409.


{| class="wikitable"
<div class="bcmeter-pi-note" role="note" style="border-left:4px solid #356481;background:#eef5f8;color:#162c39;padding:0.7em 1em;margin:1em 0;">
! Path !! Description
<strong>heads up for raspberry pi bcmeters</strong>
|-
| <code>/</code> || Serves the SPA frontend
|-
| <code>/discover</code> || Standalone device discovery page
|-
| <code>/current_log.csv</code> || Legacy redirect to <code>/api/csv</code>
|}


=== bcMeter (DIY) — Raspberry Pi OS-level extras ===
Pi 1.6.15 provides <code>GET /api/csv?view=chart</code> for chart data with precise coordinates removed. A raw CSV download uses the local client header. Without a filename, the Pi serves the active session, the newest stored session, or a header if it has no logs.
</div>


The following endpoints exist '''only on the Raspberry Pi''' version. They use Linux system commands and are not applicable to the ESP32.
=== Wi-Fi and firmware updates ===


{| class="wikitable"
{| class="wikitable"
! Method !! Endpoint !! Description
! Request !! Result
|-
|-
| GET || <code>/api/maintenance-logs</code> || Downloads a zip bundle containing: rotating bcmeter.log files, syslog, 10 newest CSV sessions, dmesg, journalctl output for bcmeter and pigpiod services, config JSON, incident log. Uses <code>journalctl</code> and <code>dmesg</code> — Linux only.
| <code>GET /api/wifi/scan?refresh=1</code> · <code>GET /api/wifi/status</code> || Request a scan and read cached results in <code>networks</code> (SSID, RSSI and security flag). Poll <code>/api/wifi/scan</code> while <code>scanning</code> is true. The status endpoint reports the current connection.
|}
 
Additionally, on the Pi:
 
* <code>/api/control?action=reboot</code> runs <code>sudo reboot</code> (Pi: <code>routes_control.py:151</code>)
* <code>/api/control?action=shutdown</code> runs <code>sudo shutdown -h now</code> (Pi: <code>routes_control.py:161</code>)
* <code>/api/control?action=synctime</code> sets the timezone via <code>sudo timedatectl set-timezone</code> (Pi: <code>routes_control.py:276</code>)
* <code>/api/device/rename</code> calls <code>sudo hostnamectl set-hostname</code> and restarts avahi (Pi: <code>bcmeter/identity.py:41</code>)
* <code>/api/update</code> runs <code>sudo systemctl restart bcMeter.service</code> after extracting the archive (Pi: <code>routes_update.py:134</code>)
 
The following endpoints exist only on the Pi and have no ESP32 equivalent:
 
{| class="wikitable"
! Method !! Endpoint !! Description
|-
|-
| GET || <code>/api/qc/pi/report</code> || Pi QC test report as JSON.
| <code>POST /api/wifi/connect</code> · <code>GET /api/wifi/connect/status</code> || Submit JSON with <code>ssid</code> and <code>pass</code> (8–63 characters). After a 202 response, poll connection status for <code>state</code>, <code>log</code> and <code>ip</code>. On 1.5.681 this operation requires owner access after setup.
|-
|-
| GET || <code>/api/qc/pi/report.html</code> || Pi QC test report as HTML.
| <code>POST /api/wifi</code> · <code>POST /api/wifi/delete</code> || Save Wi-Fi credentials and request reconnection, or forget credentials and return to hotspot mode. The save accepts JSON with <code>ssid</code> and <code>pass</code>. On 1.5.681 deletion requires owner proof and <code>?confirm=wifi-delete</code>.
|-
|-
| POST || <code>/api/qc/pi/start</code> || Start the Pi QC test sequence.
| <code>GET /api/ap-security</code> · <code>POST /api/device/password</code> || On 1.5.681 the first call reports <code>secured</code> and <code>password_set</code>. Its <code>password</code> field is empty. After onboarding, use the second call with the current owner's proof to change the device password. Use the setup wizard to establish the first password. The wizard uses <code>GET</code> and <code>POST /api/onboarding/setup</code> and <code>POST /api/onboarding/claim</code>.
|-
|-
| GET || <code>/api/qc/pi/status</code> || Pi QC test status.
| <code>POST /api/device/rename</code> || Change the device name and local hostname. Send JSON with a <code>name</code> field containing 1–32 characters. On 1.5.681 this requires owner proof.
|-
|-
| GET || <code>/api/lab/run</code> || Lab/engineering raw ADC capture (Pi equivalent of ESP32 serial LAB mode).
| <code>GET /api/ota/status</code> · <code>POST /api/ota/check</code> · <code>POST /api/ota/skip</code> || Read update availability, advertised version and progress, request a new check, or skip the offered version. After requesting a check, read status again.
|-
|-
| GET || <code>/api/lab/info</code> || Lab session status.
| <code>POST /api/ota/apply</code> · <code>POST /api/update</code> || Apply the update offered by the device or supply a compatible firmware image as a raw binary body. On 1.5.681 apply requires <code>?confirm=ota_apply</code>. Direct binary upload requires the <code>X-Firmware-SHA256</code> header containing the image digest. A successful update reboots the device.
|}
|}


The following endpoints exist only on the '''ESP32''' and have no Pi equivalent:
<div class="bcmeter-pi-note" role="note" style="border-left:4px solid #356481;background:#eef5f8;color:#162c39;padding:0.7em 1em;margin:1em 0;">
<strong>heads up for raspberry pi bcmeters</strong>
 
The Pi 1.6.15 setup hotspot stays open. <code>GET /api/ap-security</code> reports that the setting is locked. An offered Pi software update uses <code>POST /api/ota/apply?confirm=ota-apply</code>. A local upload to <code>POST /api/update</code> uses a multipart <code>.tar.gz</code>, <code>.tgz</code> or <code>.zip</code> software archive, an owner proof and the <code>X-Firmware-SHA256</code> header. After installation, the Pi restarts its software service.
</div>
 
=== Location, cloud sharing and optical diagnostics (1.5.681) ===


{| class="wikitable"
{| class="wikitable"
! Method !! Endpoint !! Description
! Request !! Result
|-
| GET || <code>/api/sdfiles</code> || List CSV files on the SD card.
|-
| GET || <code>/api/crash</code> || Last crash/reset report from CrashReport module.
|-
| GET || <code>/api/coredump</code> || Stream the raw ELF coredump from the coredump flash partition. Add <code>?erase=1</code> to erase after download.
|-
| GET || <code>/api/modem/test</code> || Run modem AT diagnostic and return results as JSON.
|-
| POST || <code>/api/lab/start</code> || Start a lab ADC capture session (params: <code>led_ch</code>, <code>led_duty</code>, <code>pump_duty</code>, <code>pump_hz</code>).
|-
|-
| POST || <code>/api/lab/stop</code> || Stop the lab capture session.
| <code>GET /api/location</code> · <code>POST /api/location</code> || Read <code>available</code>, <code>timezone</code> and the coordinates/source when available. Supply <code>lat</code> and <code>lon</code> as JSON numbers to set a location, with optional IANA <code>timezone</code>. Set <code>next_session_only</code> to true to stage a position for the next session. An active session blocks changes to its current position.
|-
|-
| GET || <code>/api/lab/status</code> || Lab session status: active, pairs captured, LED/pump settings.
| <code>POST /api/location/discover</code> || Request location discovery. Availability depends on location settings, network access and available positioning hardware.
|-
|-
| POST || <code>/api/raw_capture/enable</code> || Toggle raw ADC sample capture ring buffer (param: <code>on=0|1</code>).
| <code>GET /api/cloud/status</code> · <code>POST /api/cloud/consent</code> || Check local sharing, server consent and any pending change. Use <code>?local=1</code> for local status. The operator must explicitly accept the current privacy notice to enable sharing, or confirm pausing it. Changes can return 202 while synchronization is pending. Pausing stops uploads locally and preserves stored data.
|-
|-
| GET || <code>/api/raw_capture/read</code> || Drain the raw capture ring buffer as binary float32 pairs (8 bytes each). <code>X-Dropped</code> header reports overflow count.
| <code>POST /api/cloud/upload</code> · <code>GET /api/cloud/upload/status</code> || Request a cellular upload with owner proof and JSON <code>{"transport":"cellular"}</code>. A 202 response includes a <code>request_id</code>. Read the owner-protected status endpoint for the outcome. Cloud sharing and an available cellular data path are required.
|-
|-
| GET || <code>/api/qc/sensors</code> || Live SPS30 + env-sensor read for QC (409 while sampling).
| <code>POST /api/optical_diagnostic/start</code> · <code>GET /api/optical_diagnostic/status</code> · <code>POST /api/optical_diagnostic/cancel</code> || Owner proof is required for all three calls. Start takes an empty body and requires <code>?resume_after=0</code> or <code>?resume_after=1</code> to specify whether to resume an eligible measurement. A 202 response contains <code>run_id</code>. Poll for <code>state</code>, <code>stage</code> and <code>active</code>.
|-
| GET || <code>/api/qc/diag</code> || Passive diagnostic snapshot: heap, PSRAM, uptime, reset reason, WiFi, modem state. No AT calls.
|-
| GET || <code>/api/qc/pump_ramp/status</code> || QC pump ramp test status.
|-
| GET || <code>/api/qc/led_880/status</code> || QC LED stability test status.
|}
|}


=== Example: Start a Measurement ===
For column names, units and file versions, see [[CSV data format]]. For filter changes and calibration, see [[Maintenance]].
 
<pre>
# Sync time first
curl "http://bcmeter-XXXX.local/api/control?action=synctime&ts=$(date +%s)&tz=Europe/Berlin"
 
# Start measurement
curl "http://bcmeter-XXXX.local/api/control?action=start"
 
# Poll status
curl "http://bcmeter-XXXX.local/api/status"
 
# Download current data
curl "http://bcmeter-XXXX.local/api/csv" -o measurement.csv
</pre>

Revision as of 12:11, 27 November 2026

Local HTTP API

bcMeter Kit and Complete instruments serve the API on the device’s own network over HTTP, port 80. The examples below use ESP32 firmware 1.5.361 and 1.5.681. Read GET /api/status first and use the returned version. Firmware 1.5.681 also returns "platform":"esp32". On 1.5.361 the platform field is absent. On both versions env identifies the build environment.

Connecting

Replace the example hostname with the hostname or LAN IP shown by your device. This read-only example works with both versions.

device='http://bcmeter-XXXX.local'
curl --fail --show-error -H 'X-bcMeter-Client: bcmctl' "$device/api/status"

On the ESP32 market build at 1.5.681, status, CSV and routine controls accept local requests carrying the client header without a device password. Browser clients must use the device's own origin. Owner operations, including Wi-Fi credential changes, require a separate device-password proof for each request. On 1.5.361 the local API uses the earlier access model without these proofs.

For an owner request on 1.5.681:

  1. Fetch GET /api/auth/challenge with the client header. The returned nonce is single-use, bound to the requesting client and valid for 30 seconds.
  2. Compute the lowercase hexadecimal SHA-256 digest of the exact request body, using the empty byte string for a bodyless request. Join the nonce, uppercase HTTP method, exact path including its query string, and body digest with one newline between fields. Compute HMAC-SHA256 over this string with the device password as the key and encode the result in lowercase hexadecimal.
  3. Send the client header plus X-bcMeter-Nonce, X-bcMeter-Body-SHA256 and X-bcMeter-Proof on the intended request. To check a proof without changing settings, sign GET /api/auth/verify and look for "authorized":true. Obtain a fresh nonce for the next owner request.

heads up for raspberry pi bcmeters

The Pi notes on this page apply to runtime 1.6.15. Check version on your device. Local control requests use the same client header. GET /api/maintenance-logs downloads a protected ZIP of operating-system and device diagnostics, which can include recent measurement logs.

Measurement and configuration

Request Result
GET /api/status JSON including status (0 idle, 1 starting, 2 sampling, 3 stopped with an error), session (whether a log session is open), error, error_msg, bc, atn, flow (L/min), time_synced and version. On 1.5.681, stop_pending means finalization is still running.
GET /api/system · GET /api/logs System reports device, storage and network information. On 1.5.681 some fields such as the MAC address are blank in ordinary reads. Use an owner-signed GET /api/system?private=1 for private system details. The logs endpoint returns a structured health report including hardware, measurement, system and network sections.
GET /api/control?action=synctime&ts=EPOCH&tz=Europe/Berlin Set the clock using Unix time in seconds. Use the timezone for the measurement site.
GET /api/control?action=start · GET /api/control?action=stop Start a session or request a stop. For a one-session indoor/outdoor selection append &indoor=1 or &indoor=0 to start. Start can return 409 for an unsynchronized clock, 423 for missing calibration or 424 for calibration older than 28 days. On 1.5.681, a 202 stop response means cleanup continues. Wait for stop_pending=false and session=false before treating the file as final.
GET /api/control?action=calibrate · GET /api/calibration Install a clean filter and stop sampling before starting calibration. The start call returns 202 when accepted. Poll the second endpoint for running, done, ok, elapsed_ms and log. A finished, successful run has done=true and ok=true. Use the setup wizard for first calibration.
GET /api/config · POST /api/config Read a JSON object keyed by setting. On 1.5.681 entries have value, type, description and parameter (settings group). Send only edited fields as JSON, for example {"sample_time":300} for a 300-second sampling interval. Apply acquisition changes while idle. Owner settings require a proof. A redacted value is unsuitable for copying into a write.

GET /api/control?action=identify flashes the device indicator. GET /api/control?action=clear_error clears the recorded measurement error. GET /api/crash reports the last reset. On 1.5.681 its ordinary response contains a summary.

On 1.5.681, reboot and shutdown require a confirm query parameter matching the action. Erasing stored logs and factory reset require both matching confirmation and an owner proof. A missing confirmation returns 428. Use the device interface for these actions.

heads up for raspberry pi bcmeters

In Pi runtime 1.6.15, factory_reset stops sampling and resets device settings, including the device password and last calibration date. Stored measurement files, saved Wi-Fi connections and the device-specific correction factor remain. The Pi stays running. Use this action only to restore settings. Unsupported control actions return HTTP 400.

Downloading data

curl --fail --show-error -H 'X-bcMeter-Client: bcmctl' "$device/api/files"

GET /api/files lists session files as a JSON array. Select a returned name. GET /api/csv?file=FILENAME and GET /api/files?name=FILENAME retrieve the selected CSV. For example, set file to a name from the list and run:

curl --fail --show-error -H 'X-bcMeter-Client: bcmctl' --get \
  --data-urlencode "file=$file" "$device/api/csv" -o measurement.csv

GET /api/csv without a filename uses the session selected on the device. On 1.5.681 it falls back to the newest stored log if the selected session file is unavailable. An idle instrument can therefore return a previous session. If the device has no log to return, the response contains only the CSV header. On 1.5.681 GET /api/files?name=FILENAME&manifest=1 retrieves the session's JSON manifest when available. The list reports manifest_available and active. For SD-card logs use GET /api/sdfiles.

On 1.5.681, POST /api/files/upload with {"name":"FILENAME"} asks the device to upload the named, completed session. A 202 response means queued. A 422 response can indicate disabled sharing, missing location or provisional timestamps. DELETE /api/files takes the name query parameter, owner proof and confirm=delete_log. The device rejects deletion of its active session with 409.

heads up for raspberry pi bcmeters

Pi 1.6.15 provides GET /api/csv?view=chart for chart data with precise coordinates removed. A raw CSV download uses the local client header. Without a filename, the Pi serves the active session, the newest stored session, or a header if it has no logs.

Wi-Fi and firmware updates

Request Result
GET /api/wifi/scan?refresh=1 · GET /api/wifi/status Request a scan and read cached results in networks (SSID, RSSI and security flag). Poll /api/wifi/scan while scanning is true. The status endpoint reports the current connection.
POST /api/wifi/connect · GET /api/wifi/connect/status Submit JSON with ssid and pass (8–63 characters). After a 202 response, poll connection status for state, log and ip. On 1.5.681 this operation requires owner access after setup.
POST /api/wifi · POST /api/wifi/delete Save Wi-Fi credentials and request reconnection, or forget credentials and return to hotspot mode. The save accepts JSON with ssid and pass. On 1.5.681 deletion requires owner proof and ?confirm=wifi-delete.
GET /api/ap-security · POST /api/device/password On 1.5.681 the first call reports secured and password_set. Its password field is empty. After onboarding, use the second call with the current owner's proof to change the device password. Use the setup wizard to establish the first password. The wizard uses GET and POST /api/onboarding/setup and POST /api/onboarding/claim.
POST /api/device/rename Change the device name and local hostname. Send JSON with a name field containing 1–32 characters. On 1.5.681 this requires owner proof.
GET /api/ota/status · POST /api/ota/check · POST /api/ota/skip Read update availability, advertised version and progress, request a new check, or skip the offered version. After requesting a check, read status again.
POST /api/ota/apply · POST /api/update Apply the update offered by the device or supply a compatible firmware image as a raw binary body. On 1.5.681 apply requires ?confirm=ota_apply. Direct binary upload requires the X-Firmware-SHA256 header containing the image digest. A successful update reboots the device.

heads up for raspberry pi bcmeters

The Pi 1.6.15 setup hotspot stays open. GET /api/ap-security reports that the setting is locked. An offered Pi software update uses POST /api/ota/apply?confirm=ota-apply. A local upload to POST /api/update uses a multipart .tar.gz, .tgz or .zip software archive, an owner proof and the X-Firmware-SHA256 header. After installation, the Pi restarts its software service.

Location, cloud sharing and optical diagnostics (1.5.681)

Request Result
GET /api/location · POST /api/location Read available, timezone and the coordinates/source when available. Supply lat and lon as JSON numbers to set a location, with optional IANA timezone. Set next_session_only to true to stage a position for the next session. An active session blocks changes to its current position.
POST /api/location/discover Request location discovery. Availability depends on location settings, network access and available positioning hardware.
GET /api/cloud/status · POST /api/cloud/consent Check local sharing, server consent and any pending change. Use ?local=1 for local status. The operator must explicitly accept the current privacy notice to enable sharing, or confirm pausing it. Changes can return 202 while synchronization is pending. Pausing stops uploads locally and preserves stored data.
POST /api/cloud/upload · GET /api/cloud/upload/status Request a cellular upload with owner proof and JSON {"transport":"cellular"}. A 202 response includes a request_id. Read the owner-protected status endpoint for the outcome. Cloud sharing and an available cellular data path are required.
POST /api/optical_diagnostic/start · GET /api/optical_diagnostic/status · POST /api/optical_diagnostic/cancel Owner proof is required for all three calls. Start takes an empty body and requires ?resume_after=0 or ?resume_after=1 to specify whether to resume an eligible measurement. A 202 response contains run_id. Poll for state, stage and active.

For column names, units and file versions, see CSV data format. For filter changes and calibration, see Maintenance.