I’m doing a similar thing, I use an esp32 to parse the sensor stream, then add multiple analog input data to it.
Here’s some code to read the stream:
#define START_BYTES 5
#define LENGTH 98
const byte startMarker[START_BYTES] = {'S', 'B', '1', '.', '0'};
byte receivedBytes[LENGTH];
uint8_t idx = 0;
boolean processData = false;
boolean started = false;
void SensorBoard() // SENSOR BOARD READ ROUTINE
{
while (Serial1.available() > 0) // Something to receive?
{
receivedBytes[idx++] = Serial1.read(); // Read next byte into array
if (! started) // Are we still trying to find the start marker?
{
if (receivedBytes[idx - 1] != startMarker[idx - 1]) // Check if the byte received matches expected.
idx = 0; // If not, start over.
if (idx == START_BYTES) // Have we got all 6 bytes?
started = true; // Then we're started, so will read reset of data.
}
else // We're started.
{
if (idx == LENGTH) // Read to the end?
processData = true; // Set the flag so the data gets processed.
}
}
if (processData) // Flag set?
{
for (uint8_t x = 0; x < LENGTH; x++) // Loop through the array.
{
freq[0] = receivedBytes[6]; // If you want to keep some of the sensor board data- i.e. audio, add it here
freq[1] = receivedBytes[7];
}
started = false; // Start looking for the start marker again.
processData = false; // Clear the processing flag.
idx = 0; // Reset the array index to start over.
}
}
Here’s how I write back to PB:
byte header[] = {0x53, 0x42, 0x31, 0x2E, 0x30, 0x00}; //HEADER SB1.0
byte freq[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // FREQUENCY 1/6
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // FREQUENCY 2/6
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // FREQUENCY 3/6
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // FREQUENCY 4/6
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // FREQUENCY 5/6
0x00, 0x00, 0x00, 0x00
}; // FREQUENCY 6/6
byte audio[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; // AUDIO ENERGY
byte acc[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; //ACCELEROMETER
byte light[] = {0x00, 0x00}; // LIGHT SENSOR
byte analog[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; //ANALOG INPUTS
byte end[] = {0x45, 0x4E, 0x44, 0x00}; // END
int SerialPeriod = 40; // Serial1 Write interval
unsigned long SerialTime_now = 0;
void loop() {
Colour1A = (analogRead(27)) * 16; // An example of sending analog data over to PB using some of the frequency bytes
freq[0] = (Colour1A & 0xFF);
freq[1] = ((Colour1A >> 8) & 0xFF);
Colour1B = (analogRead(14)) * 16;
freq[2] = (Colour1B & 0xFF);
freq[3] = ((Colour1B >> 8) & 0xFF);
if ((unsigned long)(millis() - SerialTime_now) > SerialPeriod) { //Send to PB every 40ms
SerialTime_now = millis();
Serial1.write(header, sizeof(header));
Serial1.write(freq, sizeof(freq));
Serial1.write(audio, sizeof(audio));
Serial1.write(acc, sizeof(acc));
Serial1.write(light, sizeof(light));
Serial1.write(analog, sizeof(analog));
Serial1.write(end, sizeof(end));
}