SimHub without the wires
Published: 2021-02-20 08:17:45

This is a guide to creating sim racing displays that work over Bluetooth, using SimHub's Custom Serial Device plugin and an ESP32. Note that to follow this you'll need to have some experience with coding on Arduino-like boards. And to use it as a base to create your own device, you'll need to have some experience of electronics.

You'll also need:

- An ESP32 device, ideally a Lolin D32
- Windows 
- Bluetooth support in your PC
- SimHub
- Arduino IDE

SimHub is an awesome piece of software for Sim Racing on PC - kudos to [SHWotever](https://github.com/SHWotever) for creating and updating it. 

I made these displays for the Thrustmaster Open Wheel, named Tow, that use the telemetry exposed by SimHub:

![W7N Racing Tow](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e8ggga4b5wul0j0ecfx0.gif)

SimHub does not directly support Bluetooth, but using the Custom Serial Device plugin you can connect to bluetooth serial ports just like you do a standard COM port. You then make your ESP32 read the data coming over the COM port from SimHub, and control your displays, LEDs etc.

Here we'll walk through making an ESP32 (Lolin D32 in this case) connect to SimHub over bluetooth and receive telemetry data - Speed and Rpm.

First enable the **Custom serial devices** plugin as described in the SimHub Wiki - https://github.com/SHWotever/SimHub/wiki/Custom-serial-devices#enabling-the-plugin, stop at the **Configuration** step for this guide.

Upload this code to your ESP32 - I use Arduino IDE - so that it reads data from the BT COM port, the data will come from SimHub and we'll configure that in a bit:

```c
#include "BluetoothSerial.h"

#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif

#define BUF_SIZE 64

char simHubMessageBuf[BUF_SIZE];
BluetoothSerial btSerial;

int spd;
int revs;

void setup() {
  Serial.begin(115200);
  Serial.println("setup");

  memset(simHubMessageBuf, 0x0, BUF_SIZE);
  btSerial.begin("BT-DASH");
}

void loop() {
  if (btSerial.available() > 0) {
    btSerial.readBytesUntil('{', simHubMessageBuf, BUF_SIZE);
    int readCount = btSerial.readBytesUntil('}', simHubMessageBuf, BUF_SIZE);
    simHubMessageBuf[min(readCount, BUF_SIZE - 1)] = 0x0;
    processMessage();
    memset(simHubMessageBuf, 0x0, BUF_SIZE);
  }
}

void processMessage() {
  char msgType = simHubMessageBuf[0];

  switch (msgType) {
    case 'R':
      sscanf(&simHubMessageBuf[1], "%d", &revs);
      break;
    case 'S':
      sscanf(&simHubMessageBuf[1], "%d", &spd);
      break;
  }

  Serial.print("Revs: ");
  Serial.print(revs);
  Serial.print(", Speed: ");
  Serial.println(spd);
}
```

The code looks for two pieces of data, the Revs percent and the Speed in KMH. It reads both as integers, so we'll end up with whole numbers.

Once this code is uploaded to your ESP32 you can pair it over Bluetooth with Windows. But first, open Windows **Device Manager** and expand the *Ports (COM & LPT)* section. Make a quick note of the port numbers of any existing entries named *Standard Serial over Bluetooth link* - there may not be any, that's fine. 

Now pair the device in Windows' Bluetooth settings, it'll appear as *BT-DASH*, you can change this in the above code if you want a different name.

Once paired go back to **Device Manager** and you should have two new *Standard Serial over Bluetooth link* entries in the *Ports (COM & LPT)* section. Make a note of the port number of the higher numbered one.

In **SimHub**, go to the *Custom serial devices* section, click *Add new serial device*, and then expand the entry that appears.

In the *Serial port* drop down, choose the serial port you noted above. Tick the *Automatic reconnect* box.

Now we need to add the Speed and Revs messages in the *Update messages* section, click *Add new message* so that you have two empty Update messages and then edit them with these:

`'{R'+isnull([CarSettings_CurrentDisplayedRPMPercent], '0')+'}'`

`'{S'+isnull([SpeedKmh], '0')+'}'`

Change the frequency to *Changes only* for both - using the dropdown next to the *EDIT* button. This optimizes the data that's sent over the port a little.

You should end up with this:

![Revs and Speed messages](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sadjghhecd2yonvd36d3.png)

Connect your ESP32 to your PC so that is has power, and click the toggle button to enable the custom serial device entry, and it should connect to your ESP32.

![SimHub Connected](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nk9ynf1ijryz83j1v4ux.png)

Now should be able to monitor the ESP32's serial port (the USB one, not the Bluetooth one) in the Arduino IDE and see the messages coming from SimHub when racing or playing a replay - there's buttons at the top to record and playback SimHub telemetry:

![Telemetry record & playback](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4vm9ej7qxit4acs8ujpv.png)

Ensure you have the correct baud rate selected - 115200 - in the serial monitor, and you should see data being logged to the USB serial port when the revs and speed change:

![SimHub data read over bluetooth](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d3ygaiisndtv0bbb7d2k.png)

You can now use the code above as a base to control displays, LEDs etc. To see what other data you can read in SimHub, click *EDIT* on an Update message and click *Insert Property*. You'll see a list that can be searched:

![SimHub telemetry](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yaf229qr61g3rtbyjp5s.png) 

Please leave any questions in the comments below. I'll either answer them or update the article to include the answer or clarification.

Thanks for reading.