Skip to content

Custom Arduino Hardware Support

Wotever edited this page Nov 12, 2022 · 3 revisions

Introduction

Despite the out of the box supported components, you may want to implement your own hardware and/or logic, it's now possible to add custom message which will be received by the arduino

This feature will be available from 5.4.1 version

Warning

Please understand that mods made using custom protocol are under your own responsibility and I can't give support for each specific case.

Basics

Computer side

The message is built around NCalc, you can specify a message formula, the arduino will receive it in text format, the message is sent when the value changes and when it's not empty (the empty messages are not sent).

The custom protocol is only available in multiple usb mode. It's a measure to ensure the message is sent to the correct arduino. (Multiple usb can be used even on a single arduino)

Arduino side

On the arduino side, the custom message handler can be found in the SHCustomProtocol.h class

Don't forget to save your resulting sketch before any update or install of Simhub, the sketch will be overwritten with a SimHub update!

Message format

  • The message is sent as a string and ends with a new line character (\n, added by simhub automatically). it can contain anything except '\n'
  • Empty messages are never sent
  • The message is sent only when it changes

Custom message example

Building the message :

Let's say we want to retrieve speed and current gear on arduino.

We build a message splited by semicolons (ex : 100;4) :

Formula used :

format([DataCorePlugin.GameData.NewData.SpeedKmh],'0') + ';' + isnull([DataCorePlugin.GameData.NewData.Gear],'N')

for simplicity in this example we use "format" to remove the decimal part of the speed.

We use isnull to ensure a default value of the gear

Reading on the arduino (SHCustomProtocol.h)

	// Called when starting the arduino (setup method in main sketch)
	void setup() 
	{
	}

	// Called when new data is coming from computer
	void read() 
	{
         // Reads until the semicolon and converts it to integer
        int speed = FlowSerialReadStringUntil(';').toInt();
        // Read until the ends of the message
         String gear = FlowSerialReadStringUntil('\n');
         
        // Sends back the values to simhub, it will show into the log file/panel
          FlowSerialDebugPrintLn("Speed : " + String(speed));
          FlowSerialDebugPrintLn("Gear : " + gear);
	}

	// Called once per arduino loop, timing can't be predicted, 
	// but it's called between each command sent to the arduino
	void loop() {
	}

	// Called once between each byte read on arduino,
	// THIS IS A CRITICAL PATH :
	// AVOID ANY TIME CONSUMING ROUTINES !!!
	// PREFER READ OR LOOP METHOS AS MUCH AS POSSIBLE
	// AVOID ANY INTERRUPTS DISABLE (serial data would be lost!!!)
	void idle() {
	}
};
Clone this wiki locally