🇮🇹

Luca Cavallini

← Blog
Case study · Bluetooth LE · physical modelling · ESP32

Measuring the Real Power of a Magnetic Exercise Bike

Reverse engineering the Bluetooth protocol of the Merach S26S, calibrating its eddy-current brake with spin-down tests, and rebroadcasting the calibrated data as an FTMS smart trainer through an ESP32.

Abstract

The head unit of the Merach S26S computes power as the product of cadence and a tabulated coefficient indexed by a sensor on the resistance knob; the table is ordered backwards, so the reported power falls as resistance rises. Without a reference power meter, I estimated the real power with spin-down tests, using a crank revolution counter found in the bike's proprietary protocol. A model with constant friction and a speed-proportional brake describes every coast-down, and the brake coefficient grows exponentially with knob position. The calibrated data are rebroadcast in real time to a Garmin Edge 540 by an ESP32 acting as an FTMS smart trainer.

1Introduction

The Merach S26S is an exercise bike with a fixed-gear flywheel and an eddy-current magnetic brake, adjusted by a manual knob that moves the magnets towards the flywheel rim. Its head unit transmits cadence, speed, power and calories over Bluetooth Low Energy. In use I had noticed two inconsistencies: the reported power seemed barely sensitive to the resistance setting, and in the manufacturer's app the displayed resistance level appeared to decrease as the knob was tightened.

I set myself three goals:

  1. capture and decode all data transmitted by the head unit, and reconstruct how it is computed;
  2. estimate the real mechanical power without a reference power meter;
  3. make the corrected data available, in real time, to a Garmin Edge 540 and to training applications such as Zwift.

The work comprises the software (Bluetooth connector, dashboard, analysis tools and firmware) and the experiments on the bike. All data reported here come from sessions recorded on 19 November 2026.

2Data acquisition

The connector, written in Python with the bleak library, connects to the head unit, stores every notification in a raw session log and feeds a real-time web dashboard. On macOS, Bluetooth access requires a permission granted to the application hosting the process; the connector is therefore launched from Terminal, and the analysis runs on the recorded logs and on a local endpoint.

The bike advertises as MRK-S26S-XXXX and contains a ThinkFIT module exposing two data streams:

The proprietary protocol is undocumented; I decoded it by comparing it with the FTMS stream. Each frame is delimited by two fixed bytes and closed by a checksum equal to the XOR of the bytes in between:

02 | command | payload … | XOR | 03
FrameBytesContent
0x42 live data2state: standby, running, paused
3–4speed, 0.01 km/h
5resistance level 1–8
6–7cadence, whole rpm
9–10power, 0.1 W
0x43 totals3–4elapsed time, s
5–6distance, m
7–8energy, 0.1 kcal
9–10cumulative crank revolution counter (see section 5)

A recorded frame and its decoding (two-byte fields are little-endian):

02 42 02 70 06 06 34 00 00 dd 08 00 00 d1 03
→ running · 16.48 km/h · level 6 · 52 rpm · 226.9 W

Every packet was logged in full, including fields not yet understood. This turned out to be decisive: one of the initially unknown fields is the crank revolution counter on which the calibration relies.

3The head unit's power model

The first minutes of logging already reveal two exact relations. Speed is always cadence multiplied by 0.317, whatever the resistance. The ratio of power to cadence is constant for a given level: in the example above 226.9 W / 52 rpm = 4.363 W/rpm, and the same value appears at level 6 at any cadence.

Model implemented by the head unit
$$\begin{gathered}P_{\text{unit}} = k(n)\cdot c\\[2pt] v_{\text{unit}} = 0.317\ \tfrac{\text{km/h}}{\text{rpm}}\cdot c\\[2pt] E_{\text{unit}} = 15\ \tfrac{\text{kcal}}{\text{km}}\cdot d\end{gathered}$$

where \(c\) is the cadence in revolutions per minute, \(k(n)\) a tabulated coefficient indexed by the knob sensor reading, and \(d\) the virtual distance. There is no force or torque sensor.

Knob sweep

To determine the relation between knob position and the coefficient \(k\), I ran two sweeps at approximately constant cadence, the first around 38 rpm and the second around 60 rpm. The knob was moved from minimum to maximum one step at a time, about 20 s per position, and each position was logged as a marker in the session. At position 15 the emergency brake locks the flywheel, as described in the manual, so the usable range ends at position 14.

Power reported by the head unit at about 60 rpm, by knob position
Reported power decreases as resistance increases. Below each column, the displayed level.
Sweep at about 60 rpm (21:00 session): median value at each position, cadence between 55 and 62 rpm.
Show data

The two sweeps agree and fully characterise the behaviour of the head unit:

  1. Inverted level: the displayed level is 8 at the knob minimum and 1 at the maximum.
  2. Inverted power: at equal cadence the reported power goes from 302 W at position 0 to 84 W at position 14.
  3. Sensor saturation: positions 0 to 4 produce the same reading and are therefore indistinguishable.
  4. Instability at level boundaries: 64 level changes in 12 minutes, caused by noise in the reading.
  5. Tabulated power: \(k\) takes 11 discrete values between positions 4 and 14, from 5.028 to 1.527 W/rpm, in decreasing order.
  6. Nominal speed: 0.317 km/h per rpm corresponds to 0.88 m per flywheel revolution, i.e. a 28 cm diameter, whereas the flywheel measures 38 cm.
  7. Nominal energy: 15 kcal per virtual kilometre, hence a function of cadence alone.
  8. Averaged cadence: frames arrive at 5 Hz, but the cadence value updates about every 3 s and is always an integer.

4A first empirical correction

The most immediate correction is to invert the table, assigning the highest coefficient to the hardest position:

$$\begin{gathered}\tilde P = \big(k_{\max} + k_{\min} - k_{\text{unit}}\big)\cdot c\\[2pt] k_{\max} = 5.03 \qquad k_{\min} = 1.527\end{gathered}$$

The resulting trend is correct (from 92 W at position 0 to 277 W at position 14, at about 60 rpm). Since the table has 11 fixed steps between positions 4 and 14, the knob position can also be recovered from \(k\) without entering it manually; below position 4 this is not possible, because the sensor is saturated.

However, the correction is still built on the manufacturer's coefficients, with an estimated uncertainty of 30–40 %, and it is physically inadequate: the torque of an eddy-current brake depends on flywheel speed, whereas the model \(P = k\,c\) assumes constant torque. Estimating the real power required a measurement independent of the head unit's coefficients.

5Requirements for an independent measurement

The reference instrument would be a pedal-based power meter, which I do not own. The available Garmin sensors were not usable: the speed sensor must be mounted on a hub, which a solid disc flywheel does not have, and the frame leaves no room on the rim; the cadence sensor transmits only over ANT+, which the Mac cannot receive without a dedicated receiver.

The chosen method is the spin-down test, used to calibrate magnetic trainers: the flywheel is brought to high speed and left to coast freely, at each knob position. The deceleration profile determines the braking torque; with the inertia known, torque converts to power. Two features of the bike simplify the measurement: the drivetrain is fixed (pedals and flywheel are linked by a belt with no freewheel), and the ratio is exactly 6 flywheel revolutions per crank revolution, verified by direct counting.

The method requires the time of each revolution during the coast-down. The head unit's cadence does not meet this requirement:

Frame rate
5 Hz
but with the same cadence value repeated
Cadence update
≥ 2.82 s
median 3.05 s, integer values
On stopping
45 → 0 rpm
with no intermediate values

A coast-down lasting a few seconds would yield one or two values, already averaged between motion and standstill.

The crank revolution counter

Bytes 9–10 of frame 0x43, initially uninterpreted, turned out to be a cumulative crank revolution counter. The hypothesis was checked on the 21:00 sweep: the counter went from 0 to 317, while the time integral of the reported cadence gives 321 revolutions over the same interval, a 1.2 % difference consistent with the rounding of cadence.

Revolutions from the counter
317
increments by 1 per crank revolution
Revolutions from integrated cadence
321
same time interval
Time resolution
0.2 s
counter refreshed at 5 Hz

The counter keeps incrementing during the coast-down, up to the last complete revolution before standstill. The spin-down test can therefore be performed with the bike's own instrumentation, without external sensors.

6Physical model of the coast-down

Braking torques

With no input on the pedals, two braking torques act on the flywheel–crank system:

$$F_{\text{brake}} \;\propto\; \sigma\,\delta\,B^2\,v$$

with \(\sigma\) the conductivity of the rim material, \(\delta\) its thickness, \(B\) the magnetic field in the air gap and \(v\) the rim velocity.

Equation of motion

With \(\omega\) the crank angular velocity, \(I\) the system inertia referred to the crank and \(n\) the knob position, the rotational equation of motion is

$$I\,\frac{d\omega}{dt} \;=\; -\,\tau_{\text{friction}} \;-\; \beta(n)\,\omega$$

Dividing by \(I\) gives an equation with only two parameters, both determinable from timing measurements:

$$\begin{gathered}\frac{d\omega}{dt} = -\big(a + b\,\omega\big)\\[4pt] a = \frac{\tau_{\text{friction}}}{I} \qquad b = \frac{\beta(n)}{I}\end{gathered}$$

Solution

The equation is linear and first-order. With \(\omega(0)=\omega_0\) at the moment of release:

$$\omega(t) = \Big(\omega_0 + \frac{a}{b}\Big)\,e^{-bt} \;-\; \frac{a}{b}$$

The counter measures angles travelled (one increment every \(2\pi\) radians); integrating:

$$\theta(t) = \Big(\omega_0 + \frac{a}{b}\Big)\,\frac{1 - e^{-bt}}{b} \;-\; \frac{a}{b}\,t$$

The crank stops after a time

$$t_{\text{stop}} = \frac{1}{b}\,\ln\!\Big(1 + \frac{b\,\omega_0}{a}\Big)$$

The exponential term, associated with the magnetic brake, dominates at high speed; the linear term, associated with friction, dominates over the last revolutions. The two components thus leave distinct signatures on the same curve \(\theta(t)\), which makes it possible to separate them even from a single spin.

Power

At constant speed the torque applied by the rider equals the braking torque, so the power delivered is

$$\begin{aligned}P(\omega) &= \tau\,\omega = I\,\big(a + b\,\omega\big)\,\omega\\[4pt] &= \underbrace{I\,a\,\omega}_{\text{friction}} \;+\; \underbrace{I\,b\,\omega^2}_{\text{magnetic brake}}\end{aligned}$$

The magnetic term grows with the square of angular velocity. The head unit instead assumes power proportional to cadence, and therefore underestimates the increase in power at high cadence.

Inertia

Spin-down tests determine \(a\) and \(b\), i.e. torques divided by inertia; absolute power requires \(I\). The product sheet states a 6 kg flywheel; the measured outer diameter is 38 cm and the thickness 3 cm, with a plastic shell. A solid steel disc of the same size would weigh about 27 kg:

$$\begin{aligned}m_{\text{steel}} &= \rho\,\pi R^2 s\\ &= 7850 \cdot \pi \cdot 0.19^2 \cdot 0.03 \;\approx\; 27\ \text{kg}\end{aligned}$$

The stated mass is therefore concentrated in a metal ring under the shell, a typical arrangement for eddy-current flywheels. The two limiting cases, mass distributed uniformly and mass concentrated at the rim, give:

$$\begin{gathered}I_{\text{disc}} = \tfrac12\,m R^2 \approx 0.11\ \text{kg}\cdot\text{m}^2\\[2pt] I_{\text{ring}} \approx m\,r^2 \approx 0.17\ \text{kg}\cdot\text{m}^2\end{gathered}$$

I adopted the central value of 0.15 kg·m². Referred to the crank, the flywheel inertia must be multiplied by the square of the gear ratio \(G\), as follows from equating kinetic energies with \(\omega_{\text{flywheel}} = G\,\omega\):

$$\begin{gathered}\tfrac12\,I_{\text{flywheel}}\,\omega_{\text{flywheel}}^2 = \tfrac12\,\big(G^2 I_{\text{flywheel}}\big)\,\omega^2\\[4pt] I = G^2\,I_{\text{flywheel}} = 6^2 \cdot 0.15 = 5.4\ \text{kg}\cdot\text{m}^2\end{gathered}$$

The uncertainty on \(I\), about ±25 %, carries over directly to the absolute scale of every power estimate, since \(P \propto I\).

7Preliminary tests

A first set of three spins, at positions 0–4, 7 and 14, was meant to verify that the coast-down lasts enough revolutions for a fit. The figure shows the duration of each revolution as logged by the counter.

Crank revolution times in the three preliminary tests
Start-up and driven phase (short revolutions), followed by the free coast-down. Same scale for all three panels.
Knob 0–4
Knob 7
Knob 14
The alternation between 0.21 and 0.42 s reflects the resolution of the counter, refreshed at 5 Hz; the error averages out over the full set of revolutions.
Show data

At position 0 the crank, released at about 180 rpm, completes some fifteen free revolutions in 10.6 s before slowing to 30 rpm; at position 7, nine revolutions in 5.2 s; at position 14 it stops almost immediately, because the brake is already in contact with the flywheel.

A first fit, expressed per unit inertia, shows that at 60 rpm the braking at position 7 is 2.5 times that at position 0. The head unit's table implies the opposite ratio; the inverted table of section 4 gives 1.7, so it too underestimates the growth of resistance. With \(I = 5.4\ \text{kg}\cdot\text{m}^2\) the first absolute powers are:

KnobHead unit, 60 rpmInverted tableSpin-down, 60 rpmSpin-down, 90 rpm
0–4302 W92 W42 W (30–49)75 W
7233 W157 W86 W (62–123)195 W

The ranges correspond to the two limiting cases for inertia. The tests also allow a consistency check of the reported values: dissipating 302 W at position 0 and 60 rpm (\(\omega \approx 6.28\) rad/s), with the measured friction and brake, would require an inertia of

$$\begin{aligned}I_{\text{req}} &= \frac{P}{\big(a + b\,\omega\big)\,\omega}\\[4pt] &= \frac{302}{\big(0.8 + 0.064 \cdot 6.28\big)\cdot 6.28}\\[4pt] &\approx 40\ \text{kg}\cdot\text{m}^2\end{aligned}$$

corresponding to a flywheel of more than 30 kg, against the stated 6 kg. The head unit's values are therefore incompatible with the observed flywheel dynamics.

Note on procedure

In these tests the crank was brought up to speed by pedalling and then released by lifting the feet off the pedals; the coast-down starts at that instant. With a fixed drivetrain the pedals keep turning during the coast-down: anyone repeating the test should consider the safer alternative of spinning the crank by hand from the side of the bike.

8Calibration series

The calibration series comprises one spin at each even knob position from 0 to 14, with positions 0 to 4 entered manually in the dashboard. To avoid relabelling after the fact, the series was recorded in a dedicated session, discarding an earlier attempt that contained a marker error. Release speeds range from about 150 to 200 rpm.

At positions 10, 12 and 14 the coast-down has 4, 3 and 3 revolutions respectively: too few to estimate the initial speed, friction and brake coefficient independently for each spin.

Estimation procedure

The estimation introduces two physically motivated constraints:

  1. Friction shared by all spins. Belt and bearings do not depend on knob position, so \(a\) is a single parameter, chosen to minimise the total error over all spins; only \(b\) remains to be estimated for each spin.
  2. Estimated release time. The instant \(t_r\) at which driving ends is included among the parameters, under the constraint that after the last logged revolution the crank cannot complete another one.

For each spin, given the times \(t_i\) of the logged revolutions, the objective function is

$$\min_{b,\,t_r,\,\theta_0}\ \sum_i \Big[\,2\pi i - \theta_0 - \theta_{\text{mod}}(t_i)\Big]^2$$

where \(\theta_{\text{mod}}\) is linear at constant speed \(\omega_0\) before \(t_r\) and follows the solution of section 6, with the shared friction \(a\), afterwards. The result is \(a = 0.80\ \text{s}^{-2}\), i.e. a friction torque of 4.3 N·m at the crank and about 27 W at 60 rpm. The RMS residuals of the individual spins range from 0.02 to 0.23 revolutions.

The figure below shows the spins of the series, revolution by revolution, together with the fitted model. The axes are fixed so that knob positions can be compared.

Spins of the calibration series
Revolution times logged by the counter and the \(a + b\,\omega\) model with shared friction.
Knob
measured (bike counter)modelestimated release time
Completed revolutions
Crank speed
In the lower panel the points are mean speeds over 3-revolution windows; the line is the model speed, constant until release and then decreasing according to \(\omega(t)\).
Show data

9Brake dependence on knob position

The values of \(b\) estimated at the different positions lie on a straight line on a logarithmic scale:

Magnetic brake coefficient by knob position
Logarithmic scale: a straight line corresponds to an exponential law.
spins included in the regressionknob 14, excluded (coast-down too short)\(b = 0.064 \cdot 1.258^{\,n}\)
Values of \(b\) estimated with shared friction \(a = 0.80\ \text{s}^{-2}\). The regression includes spins with at least 2 free revolutions.
Show data

Linear regression of \(\ln b\) on position \(n\), over 7 spins, gives:

$$\begin{gathered}b(n) = b_0\,r^{\,n} = 0.0640 \cdot 1.258^{\,n}\ \ \text{s}^{-1}\\[2pt] \ln b = \ln b_0 + n\,\ln r\end{gathered}$$

with coefficient of determination \(R^2 = 0.997\), a scatter of 6 % about the line (1σ, residuals within ±8.4 %) and a 95 % confidence interval for \(r\) of 1.239–1.277. The brake therefore strengthens by a factor of 1.26 per step, about 25 times between positions 0 and 14.

The exponential trend has a physical justification. The magnetic field in the air gap decreases approximately exponentially with the distance \(g\) between magnets and flywheel, the braking torque is proportional to \(B^2\), and each knob step reduces the distance by a constant increment \(\Delta g\):

$$\begin{gathered}B(g) \propto e^{-g/\lambda} \;\Rightarrow\; \beta \propto B^2 \propto e^{-2g/\lambda}\\[2pt] g = g_0 - n\,\Delta g \;\Rightarrow\; \beta(n) \propto \big(e^{2\Delta g/\lambda}\big)^{n}\end{gathered}$$

The complete model is defined by four parameters, stored in the file calibration.json:

$$\boxed{\;P = I\,\big(a + b_0\,r^{\,n}\,\omega\big)\,\omega\;}$$
$$\begin{gathered}I = 5.4\ \text{kg}\cdot\text{m}^2 \qquad a = 0.80\ \text{s}^{-2}\\[2pt] b_0 = 0.0640\ \text{s}^{-1} \qquad r = 1.258\end{gathered}$$

with \(\omega = \pi c/30\) the crank angular velocity in rad/s and \(n\) the knob position.

10Results

I applied the three power estimates (head unit, inverted table and calibrated model) to the 60 rpm sweep of section 3.

Three power estimates on the same sweep
Power at about 60 rpm by knob position.
calibrated modelinverted tablehead unit
Median values at each position, cadence between 55 and 62 rpm. For positions 0–4, which the sensor does not distinguish, the model uses the position entered manually.
Show data

The calibrated model rises slowly up to position 10 and steeply beyond it, consistent with the effort perceived during the test; the head unit shows the opposite trend. The two curves cross around position 10.

Energy was recomputed as well. The head unit derives it from the virtual distance, and hence from cadence alone; the model instead allows the mechanical work to be integrated and converted to metabolic energy, assuming a typical gross efficiency of 24 %:

$$\begin{gathered}W = \int P\,dt\\[2pt] E_{\text{kcal}} = \frac{W_{\text{kJ}}}{4.184\cdot\eta} \;\approx\; W_{\text{kJ}} \qquad (\eta \approx 0.24)\end{gathered}$$

Over the 60 rpm sweep (5 min 25 s of pedalling) the work is 43.5 kJ, about 43 kcal, against the 67 kcal reported by the head unit. Average power is 134 W and the best minute 292 W.

Calculator

The calculator below applies the calibrated model and compares it with the head unit's table for the selected position and cadence.

Power estimated by the model and reported power
Model \(P = I(a + b_0 r^n \omega)\,\omega\) and head unit table \(P = k(n)\,c\).
Estimated power (model)
Reported power
Virtual speed on the flat
Energy per hour
calibrated modelhead unitfriction component
Power as a function of cadence for the selected position. For positions 0–4 the head unit uses the same coefficient, because the sensor is saturated. The model's absolute scale has an uncertainty of ±25 %.

11Uncertainty analysis

SourceAffected quantityMagnitude
Flywheel inertia (0.11–0.17 kg·m²)absolute scale of all powers, \(P \propto I\)±25 %
Spin repeatability (knob 0: 43, 49 and 42 W at 60 rpm)single estimate of \(b\)±8 % (1σ)
Scatter about the exponential law\(b\) at a single position±6 % (1σ)
Extrapolation to positions 12–14 (0–2 free revolutions)power at position 14, 60 rpm302–447 W, central 367 W (95 % CI on \(r\))
Positions 0–4 not detected by the sensorpower at 60 rpm between 41 and 65 Wdepends on manual entry (default: 2)
Cadence averaged over about 3 spower during rapid changessmoothing; no effect at steady state
Counter resolution (0.2 s)revolution timesabsorbed by the fit over 10–25 revolutions

The ratio of two powers measured under the same conditions does not depend on \(I\). For comparisons between sessions, for instance to assess an improvement in sustainable power, the relevant uncertainty is therefore the relative one, of the order of 6–8 %, rather than the scale uncertainty.

The model assumes constant friction and a magnetic torque linear in \(\omega\). Over the range explored, from 30 to about 200 rpm at the crank (up to about 20 m/s at the flywheel rim), these assumptions reproduce the spins with the residuals reported in section 8; validity beyond this range has not been verified.

12Alternative methods considered

To reduce the scale uncertainty, several methods for measuring inertia or friction directly were evaluated. None was carried out; for each, I give the principle and the reason it was excluded.

Physical pendulum

A known mass attached to the pedal, released from about 45° and filmed to measure its period, would give the inertia directly:

$$\begin{gathered}T = 2\pi\sqrt{\frac{I + m L^2}{m\,g\,L}}\\[4pt] \Longrightarrow\quad I = \frac{m\,g\,L\,T^2}{4\pi^2} - m L^2\end{gathered}$$

with \(L\) the crank length. The method is not applicable because of friction: in an oscillator with Coulomb friction the amplitude decreases, at each half-oscillation, by

$$\Delta\theta = \frac{2\,\tau_{\text{friction}}}{m\,g\,L}$$

With 10 kg, \(L = 0.17\) m and the measured friction torque of 4.3 N·m, this gives \(\Delta\theta \approx 30°\): starting from 45°, the pedal would stop before completing one oscillation.

Equal masses on both pedals

Two identical masses on opposite pedals balance each other statically and increase the inertia by a known amount, \(\Delta I = 2mL^2\); comparing coast-downs with and without the masses would yield \(I\). The sensitivity, however, is insufficient:

$$\frac{\Delta I}{I} = \frac{2\,m\,L^2}{G^2\,I_{\text{flywheel}}} = \frac{2 \cdot 5 \cdot 0.17^2}{5.4} \;\approx\; 5\,\%$$

With 5 kg per pedal the change is of the same order as the spin repeatability (section 11).

Static friction measurement

A mass hanging from the pedal measures the friction torque in N·m; combined with \(a = \tau/I\) from the coast-downs, it yields the inertia with no assumption about the flywheel's construction:

$$I = \frac{\tau_{\text{friction}}}{a}$$

Two variants were considered. In the first, one looks for the minimum mass for which the pedal, starting horizontal, keeps descending after a slight push: \(m g L = \tau\), about 2.6 kg predicted at position 0. In the second, more objective, one looks for the minimum mass that takes the pedal from horizontal to vertical; the potential energy lost over the quarter turn is entirely dissipated by friction:

$$\begin{gathered}m\,g\,L = \tau_{\text{friction}}\cdot\frac{\pi}{2}\\[4pt] \Longrightarrow\quad m = \frac{\pi\,\tau_{\text{friction}}}{2\,g\,L} \;\approx\; 4\ \text{kg}\end{gathered}$$

The method was excluded for practical reasons: it requires masses of several kilograms already at position 0, to be determined by repeated weighings at several knob positions.

Other references

13Implementation: dashboard and virtual trainer

Dashboard

The dashboard gives prominence to the quantities computed by the model (power, energy, cadence, knob position) and to a session summary. The head unit's power is shown only as a reference in the charts, and the remaining head unit data are grouped in a separate section marked as unverified. Recorded sessions are recomputed with the current calibration.

Dashboard on the 60 rpm sweep: energy 43 kcal, work 43.5 kJ, average power 134 W, best minute 292 W
Summary of the 60 rpm sweep recomputed with the calibration: 43.5 kJ, 134 W average, best minute at 292 W.
Dashboard charts: calibrated power in purple rising with the knob, head unit power in grey falling; below, cadence steady at about 60 rpm
The same sweep in the dashboard charts: calibrated power in purple, head unit power in grey, knob position markers as vertical lines.

Virtual trainer “Better_Merach”

The third goal required a Bluetooth device rebroadcasting the calibrated data in real time, recognised by the Garmin Edge 540 and by Zwift. The virtual device was named Better_Merach. The first implementation used the Mac as a Bluetooth peripheral and ran into three successive limitations.

  1. Advertising packet length. A Bluetooth LE advertisement holds at most 31 bytes. The first library used encoded service identifiers as 128-bit UUIDs (16 bytes each): two services already take 34 bytes, and macOS truncated the name and dropped the services. A GATT server written directly on CoreBluetooth, with standard 16-bit identifiers (0x1818 for power, 0x1816 for speed and cadence), solved the problem; the Edge, however, paired the device as a plain power sensor.
  2. FTMS service data. To carry cadence, speed and distance as well, the device must present itself as an FTMS smart trainer. Advertised alone, the FTMS service was not listed by the Edge. Comparison with the bike's advertisement, which the Edge recognises as a trainer, revealed a service data block, 1826 → 01 20 00 (machine available, indoor bike type), defined by the FTMS specification. When acting as a peripheral, macOS advertises only the local name and the list of services: the block is accepted by the API but not transmitted.
  3. One sensor type per address. As an alternative, two separate standard sensors were advertised, speed/cadence and power. The Edge paired the first but not the second: it associates a single sensor type with each Bluetooth address, and the Mac exposes only one. The Edge also reports the device name as the computer's name, which macOS provides in the GAP service.

ESP32 bridge

The adopted solution is an ESP32 board connected to the Mac over USB, on which the advertisement can be composed byte by byte. The board is a classic ESP32 (ESP32-D0WDQ6, Bluetooth and Wi-Fi, 4 MB flash), running MicroPython 1.29. Because opening the serial port resets the board, the standard mpremote tool could not establish a connection; a client that waits for boot to complete before taking control was written to upload the firmware.

Advertising packet of Better_Merach on the ESP32
Each structure consists of length, type and value; the highlighted one cannot be transmitted by macOS.
020106
Flags: discoverable, Bluetooth LE only
03032618
16-bit services: 0x1826, FTMS
06162618012000
FTMS service data: machine available, indoor bike
0E094265747465725F4D6572616368
Complete local name: “Better_Merach”
29 of 31 available bytes. The first byte of each structure is its length, the second its type (01 flags, 03 services, 16 service data, 09 name). 16-bit values are little-endian: 26 18 is 0x1826.

The MicroPython firmware, about 150 lines long, exposes the FTMS, Cycling Power and Device Information services and accepts up to three simultaneous connections, for example the Edge and Zwift. All processing (decoding, calibration, packet encoding) runs on the Mac, which once per second sends the board a line of text over USB serial with the packets already encoded; the board retransmits them and reports its status every two seconds.

System architecture
Data path from the head unit to the receiving devices.
Merach S26SThinkFIT head unit: cadence, revolution counter, tabulated coefficient
→Bluetooth
5 frames/s
Mac: connectordecoding, calibrated model, dashboard, logging
→USB serial
1 line/s
ESP32FTMS smart trainer “Better_Merach”, MicroPython
→Bluetooth
FTMS
Edge 540 · Zwiftpower, cadence, speed, distance, calories
Mac → ESP32   N <indoor_bike_data_hex> <cycling_power_hex>
ESP32 → Mac   S {"fw": "better_merach-1", "conn": 1, "adv": 1, "age": 0}

FTMS control point commands, such as resistance changes requested by Zwift, are acknowledged but not executed: resistance remains set manually by the knob, so ERG mode is not available.

Conformance of the advertisement was verified in two ways. The connector's own scanner, which selects the bike by FTMS service and service data, classified the board as equivalent to the bike; a rule excluding Better_Merach devices from the search was therefore added. Subsequently the Garmin Edge 540 paired Better_Merach as a smart trainer, receiving power, cadence, speed, distance and calories; the first values rebroadcast were 32 W, 45 rpm and 16.1 km/h at position 2.

Virtual speed

On an indoor bike speed is necessarily a derived quantity. The virtual trainer transmits the speed that would be reached on flat ground, with no wind, at the estimated power, by numerically solving the balance

$$P = \Big(C_{rr}\,m\,g + \tfrac12\,\rho\,C_dA\,v^2\Big)\,v$$

with \(C_{rr} = 0.004\), \(C_dA = 0.32\ \text{m}^2\), \(\rho = 1.225\ \text{kg/m}^3\) and \(m\) the combined mass of rider and road bike, set in the dashboard. At 85 kg, 100 W corresponds to about 26 km/h, 150 W to about 31 km/h and 250 W to about 37 km/h.

Source code

The connector, dashboard, calibrated model, ESP32 firmware and calibration tools are published under the MIT licence, together with documentation of the protocol, the calibration method and the virtual trainer:

github.com/lucacava/merach_deviceBluetooth connector, dashboard, calibrated model, ESP32 firmware and calibration tools.

The calibration can be reproduced on other units with the same head unit: after a series of coast-downs, one per knob position, the analysis script performs the fit and writes the calibration.json file.

.venv/bin/python tools/spindown.py <session> --save

14Conclusions

The main figures obtained in this work are summarised below.

Knob 0, 60 rpm
302 → 41 W
power reported by the head unit → power estimated by the model
Knob 14, 55 rpm
84 → 310 W
reported power decreases as resistance increases
Magnetic brake
×1.258
per knob step, exponential regression with R² = 0.997
Scale uncertainty
±25 %
due to flywheel inertia; relative comparisons are more precise

The main future development is a comparison session against a pedal power meter, which would fix the absolute scale and allow the model to be verified at positions 12–14, which are currently extrapolated.

References