I’m working on a project where I’m sending sensor data from an ESP32 via ESP-NOW to TouchDesigner over Serial. I have three sensors, and each one sends a distance value with an ID. The ESP32 outputs data in this format:
ID_1:50, ID_2:62, ID_3:45
In TouchDesigner, I’m using a Serial DAT to read the incoming data, but I’m struggling to separate the three IDs and map each one to a separate CHOP channel.
Does anyone have advice on how to process this correctly using a Script CHOP? I assume I need to split the data string, extract the values, and assign them to channels, but I’m not sure how to implement this in Python.
you can make use of the onMessage callback of the Serial DAT and parse the message string in there writing out the result to either a CHOP or a Table DAT. Without specifically good reason, i prefer using a Table DAT for value storage and convert this to a CHOP later on.
For the onMessage callback you would have to split the message first by , and then each resulting item by : giving you a name, value pair that you can add to a Table DAT.
I have not tried this but it should work:
# me - this DAT
#
# dat - the DAT that received the data
# rowIndex - the row number the data was placed into
# message - an ascii representation of the data
# Unprintable characters and unicode characters will
# not be preserved. Use the 'byteData' parameter to get
# the raw bytes that were sent.
# byteData - byte array of the data received
# reference to the table where the values will be stored
valueTbl = op('table_values')
def onReceive(dat, rowIndex, message, byteData):
# split the message by comma
pairs = message.split(',')
# for each pair, split by colon
for p in pairs:
name, value = p.split(':')
# if the name is not in the table, add it
# otherwise, update the value
if name not in valueTbl.col('name'):
valueTbl.appendRow([name, value])
else:
valueTbl[name, 'value'] = value
return
Append a DAT to CHOP to the Table DAT to convert this to channels after.