Skip to content

Arduino Library for Simulation of Changing Road Inclination for Indoor Cycling

Notifications You must be signed in to change notification settings

Berg0162/Simcline-V2

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SIMCLINE Icon   SIMCLINE-V2 for Smart Trainers

Simulation of Changing Road Inclination for Indoor Cycling

Mechanical SIMCLINE 2

The SIMCLINE physically adjusts the bike position to mimic hilly roads, climbing and descending. This allows the rider to naturally change position on the bike, engage climbing muscles, and improve pedaling technique to become a more efficient and powerful climber.
Without user intervention the SIMCLINE will replicate inclines and declines depicted in (online & offline) training programs (like Zwift, Rouvy, VeloReality, myWhoosh and many others) that adjust accordingly the resistance of the indoor trainer.
The SIMCLINE auto connects at power up with a Smart trainer and let's relive the ascents and descents from favorite rides or routes while training indoors. The physical reach is: 20% maximum incline and -10% maximum decline. However, the reach that the rider is comfortable with can be adjusted! The SIMCLINE pairs directly to the Smart trainer and with your PC/Laptop/Tablet with (Zwift) training App for a connection that notifies the SIMCLINE to simulate autonomous the (change in) physical grade of the road during an indoor ride. During operation an OLED display shows the road grade in digits and in graphics.

Notice that the description on how to build SIMCLINE consists of two parts:
  • Present Simcline-V2 Library
    The Simcline-V2 library comes with an Android companion app for configuring and controlling its features. See: Companion App

Simcline-V2 Library is optimised for ESP32 and NimBLE-Arduino 2.x!

The ESP32 family has a series of low-cost and low-power System on a Chip (SoC) microcontrollers developed by Espressif that include Wi-Fi and Bluetooth wireless capabilities and dual-core processor. See for an introduction: Random Nerds Tutorials. Particularly the multiprocessing capabilities of the dual-core processor make the ESP32 a very attractive choice for the project! See:  FAQ #5
To benefit of the same formfactor (fit with the Mechanical SIMCLINE 2.0 component box!), the Adafruit Feather ESP32 V2 plus Oled display is still the preferred board and display for the project. See: Adafruit Feather ESP32 V2 for settings, wiring scheme and more.
Notice that other members of the ESP32 (particularly ESP32S3) will do the job perfectly, when the development boards come with extra flash and psram memory! See:  FAQ #7.   For example SIMCLINE works successfully with the Lilygo esp32s3 T-Display board, however that Lilygo-board needs another size component box! See: Lilygo esp32s3 T-Display for settings, wiring scheme and more.
Simcline-V2 library builds on the experience, knowledge and code of Simcline projects since 2020. The original Simcline code has been revisited and redesigned completely for better stability, robustness and modularity. It was transformed to a C++ Object model that handles the many BLE services and hides most of the Simcline's internal operation. The Simcline community is very diverse when it comes to programming skills. The present Simcline-V2 library matches this much better! It also meets the urge for a greater variety in ESP32 boards and displays to work with.

The Simcline-V2 design separates completely:

  • presentation (display types/properties)
  • specific ESP32 board properties (pinout and initialization)
  • BLE operation

To achieve this the design has implemented Abstract Base Classes for ESP32 board and Display. As a consequence the novice programmer/user only has to change config settings to select between standard board- and display-types that are supplied with Simcline-V2. However, a proficient programmer/user can implement Concrete Classes for ESP32-board-type and Display-type of his/her choice easily without interfering with the Simcline BLE operational code.
Benefits of this Approach:

  • Scalability: Easily add new display types by creating new concrete classes implementing the IDisplay interface.
  • Maintainability: Changes to one display type do not affect others.
  • Flexibility: Change the display type used by the Presentation class without modifying its implementation.

As a result the Arduino ino-files that the user will access, in the Simcline-V2/examples folder, are very concise in comparison with the old code files.

Simcline-V2 has central configuration

All configuration settings have been gathered in one config directory ../documents/arduino/libaries/Simcline-V2/src/config for Board-, Debug-, Display-, NimBLE- and Simcline-configurations. Check this out: Simcline-V2 Configuration

NimBLE-Arduino 2.x

The Simcline project heavily leans on the NimBLE-Arduino library for Bluetooth handling, see: H2Zero/NimBE-Arduino. NimBLE-Arduino is structured for compilation with Arduino and for use with ESP32! Simcline-V2 works only (!) with the latest version: NimBLE-Arduino Version 2.x!

FiTness Machine Service a Bluetooth Service Specification

From 2015 to 2017 the Sports and Fitness Working Group (SIG) designed a Bluetooth Service specification. This service exposes training-related data in the sports and fitness environment, which allows a Server (e.g., a fitness machine) to send training-related data to a Client. In Februari 2017 the service specification reached a stable version: Fitness Machine Service 1.0 when it was adopted by the Bluetooth SIG Board of Directors. Have a look at the document to appreciate the effort of all the contributors and the companies they represented!
FTMS is an open (nonproprietary) protocol that is not owned by any particular company and not limited to a particular company's product. It can be compared in that respect with FE-C over ANT+, however FTMS is targeted to control fitness equipment over Bluetooth! Today Bluetooth Smart FTMS is the absolute industry standard for indoor trainers and Simcline-V2 is primarily targeted at FTMS!

Simcline-V2 is fully supporting:

Trainer Supported protocols
Elite Bluetooth Smart FTMS on all 2020 smart trainers.
Gravat Bluetooth Smart FTMS on all 2020 smart trainers
JetBlack Bluetooth Smart FTMS with HRM. FAQ #3
Kinetic Bluetooth Smart FTMS on all 2020 smart trainers.
Minoura Bluetooth Smart FTMS on all 2020 smart trainers.
Saris Bluetooth Smart FTMS on all 2020 smart trainers.
STAC Bluetooth Smart FTMS on all 2020 smart trainers.
Tacx Bluetooth Smart FTMS on all 2020 smart trainers.
Wahoo Bluetooth Smart FTMS on all 2020 smart trainers and legacy Wahoo Bluetooth Smart Control.
Zwift Hub Bluetooth Smart FTMS with HRM. FAQ #3

Man-In-The-Middle (MITM) software pattern

Man in the Middle
Man-In-The-Middle is a powerful software engineering pattern that is applied in many software designs. Unfortunately it is also known for a negative application in communication traffic: MITM is a common type of cybersecurity attack that allows attackers to eavesdrop on the communication between two targets. We have applied the very principle: the Simcline is strategicly positioned in between the BLE communication of the Smart Trainer and the training App (like Zwift) running on the PC/Laptop, all communication traffic can be inspected in that MITM position, when it is passed on from one to the other, in both directions. When Zwift sends resistance information (like the road inclination) to the Smart trainer, this information can be intercepted and applied to determine the up/down positioning of the Simcline.

How to start?

  • Install the Arduino IDE 2
  • Install your ESP32 board in the Arduino environment
  • Install Adafruit OLED and GFX Libraries
  • Install the ESP32 NimBLE-Arduino library (Version 2.#.#), in Arduino IDE go to Sketch menu -> Include Library -> Manage Libraries, search for NimBLE-Arduino and install. Ref
  • Install the Simcline-V2 library from this repository. Download as .zip and extract to Arduino/libraries folder, or
    in Arduino IDE from Sketch menu -> Include library -> Add .Zip library

How to make it work?

The requirements in this phase are simple:

  • running Zwift, Rouvy or myWhoosh app or alike,
  • working Feather ESP32-V2 (or other ESP32S3) development board and
  • working Smart Trainer (see above support list)
  • running Simcline-V2 library and Smart-MITM application.

Testing is Knowing!

I can understand and respect that you have some reserve: Is this really working in my situation? Better test if it is working, before buying all components and start building. In the Simcline-V2 Library Examples section (see ..documents/arduino/libraries/Simcline-V2/examples) you will find the appropriate applications for testing and running SIMCLINE that come with the library. The one you should start with in this stage is Smart-MITM It is developed with the only intention to allow you to check if the MITM solution is delivering in your specific situation. Within the Arduino IDE 2 select on the top bar menu: File > Examples > Simcline-V2 > Smart-MITM

What it does in short:
Simcline in the Middle
A working MITM implementation links a bike trainer (BLE Server FTMS) and a PC/Laptop (BLE Client running Zwift) with the Feather ESP32, like a bridge in between. The MITM bridge can pass on, control, filter and alter the interchanged trafic data! The MITM code is fully ignorant of mechanical or electronic components that drive the Simcline construction.

It simply estabishes a virtual BLE bridge and allows you to ride the bike on the Smart Trainer and 
feel the resistance that comes with the route you have choosen, thanks to Zwift.
The experience should not differ from a normal direct one-to-one connection, Zwift - Smart Trainer!

All Bluetooth Smart FTMS indoor trainers expose your efforts on the bike in 2 additional BLE services: Cyling Power (CPS) and Speed & Cadence (CSC). These services are detected and applied by many training app's and are therefore an integral part of the present design of the MITM bridge. Training app's simply expect, when they connect to the Bluetooth Smart FTMS trainer, that the CPS and CSC services are available in one go! The Zwift pairing screen is a good example: it expects Power Souce (CPS), Resistance (FTMS) and Cadence (CSC) to be connected...

  • The client-side (Feather ESP32) scans for (a trainer) and connects with FTMS, CPS and CSC and collects cyling power, speed and cadence data like Zwift would do! The Simcline Client is doing just that at the left side of the "bridge"!
  • The Server-side (Feather ESP32) advertises and enables connection with training/cycling/game apps like Zwift and collects relevant resistance data, it simulates as if an active FTMS enabled trainer is connected to Zwift or alike! Notice that the Server-side also exposes active CPS and CSC services. The Simcline Server is doing just that at the right side of the "bridge"!
  • The Simcline MITM code is connecting both sides at the same time: a full-blown working bridge

Load Smart-MITM application in Arduino IDE 2

Within the Arduino IDE 2 select on the top bar menu: File > Examples > Simcline-V2 > Smart-MITM
The default Simcline-V2 configuration settings for Smart-MITM are:

  • Display: NODISPLAY
  • ESP32 board: YOUR_ESP32_BOARD
  • NimBLE: FTMS and CSC
  • Debug: Defined

Optionally:

Mandatory:

Smart-MITM is default in Debug mode: using Serial Monitor (logging on the PC-screen) to show you what is happening!

When the settings are conforming your desired setup, it is time to run Smart-MITM!

Compile and Upload Smart-MITM to your ESP32 board

A recipe for success: follow ALWAYS the instructions and procedure at the top of the respective program codes!

/* -----------------------------------------------------------------------------------------------------
 *             This code should work with all indoor cycling trainers that fully support,
 *        Fitness Machine Service, Cycling Power Service and Cycling Speed & Cadence Service
 * ------------------------------------------------------------------------------------------------------
 * NOTICE: that you need to have set first all config file settings in accordance with your specific setup!
 *         see: ../Documents/Arduino/libraries/FTMS-Simcline/src/config
 *
 *  The code links a BLE Server (a Peripheral to Zwift) and a BLE Client (a Central to the Trainer) with a bridge 
 *  in between, the ESP32 being man-in-the-middle (MITM). The ESP32 is an integral part of the Simcline design,
 *  that interprets the exchanged road grade and moves the front wheel up and down with the change in inclination.
 *  The ESP32-bridge can control, filter and alter the bi-directional interchanged data!
 *  The client-side (central) scans and connects with the Trainer relevant services: CPS and FTMS. It collects 
 *  all cyling data of the services and passes these on to the server-side....  
 *  The client-side supplies the Indoor Trainer with target and resistance control data.
 *  The server-side (peripheral) advertises and enables connection with cycling apps like Zwift and collects the app's  
 *  control commands, target and resistance data. It passes these on to the client-side....  
 *  The server-side supplies the app with the generated cycling data in return. 
 *  
 *  The client plus server (MITM) are transparent to the Indoor Trainer as well as to the training app Zwift or alike!
 *  
 *  Requirements: Zwift app or alike, ESP32 board (NO display required) and a supported Indoor Trainer
 *  0) Upload and Run this code on your ESP32 board
 *  1) Start the Serial Monitor to catch debugging info
 *  2) The code will do basic testing of electronic parts and settings
 *  3) Start/Power On the Indoor Trainer  
 *  4) Your ESP32 and Trainer will pair as reported in the output
 *  5) Start Zwift on your computer or tablet and wait....
 *  6) Search on the Zwift pairing screens for your ESP32 a.k.a. <SIM32>
 *  7) Pair: Power Source, Resistance and Cadence one after another with <SIM32>
 *  8) Optionally one can pair as well devices for heartrate and/or steering (Sterzo)
 *  9) Start the default Zwift ride or any ride you wish
 * 10) Make Serial Monitor output window visible on top of the Zwift window 
 * 11) Hop on the bike: do the work and feel resistance change with the road
 * 12) Inspect the info presented by Serial Monitor.....
 *  
 *   This device is identified with the name <SIM32>. You will see this only when connecting to Zwift on the 
 *   pairing screens! Notice: Zwift extends device names with additional numbers for identification!
 *  
*/ 

Points of Interest

  • Be aware of undesirebly autoconnect of Zwift with your trainer using ANT+ or BLE Smart before Smart-MITM can establish a connection: always start Zwift AFTER Smart-MITM and trainer have connected successfully! The Smart-MITM code will than fail to connect, that does not help you getting representative results during the reconnaisance! Only one client can control at the same time: 2 captains on one ship is a recipe for disaster! See for more info FAQ #1 and FAQ #6
  • Please write down the presented MAC/Device Addresses of a) your Smart trainer and b) your Desktop/Laptop with Zwift. These showup in the Serial Monitor output when running the Smart-MITM test code. This is for your own convenience since it helps you to identify later both devices by MAC Addresses! The Smart-MITM detects the Mac addresses and stores these in ESP32 NVS (Non-Volatile-Storage) for later use to unmistakingly establish a BLE connection with the targeted devices. See FAQ #9
  • When you see different road grade values in the Zwift window compared with the Simcline display or in the Serial Monitor output, this can have different reasons! See: FAQ #8

What's next?

When you are pleased with the results sofar, you can consider to setup the complete SIMCLINE mechatronic system.