Python3 output buffer content to msg.payload

I've got a raspberry pi 4 and a pimoroni automation hat which has an I2C device ads1015. I'm trying to format a command to the device using the python3 node.

I'm trying to pass buffer data back to the ms.payload, but byes aren't JSON serializable, so I'm running into issues.

I get this error:

"Object of type bytes is not JSON serializable↵"

I've tried pre-converting it to base64 and then as a string, as shown below. But this doesn't feel quite right. Can you recommend how to do this correctly? In java, it would be something like Buffer.from(x, 'utf8')

import base64

channel = 0
samples_per_second = 1600
programmable_gain = 4096


SAMPLES_PER_SECOND_MAP = {128: 0x0000, 250: 0x0020, 490: 0x0040, 920: 0x0060, 1600: 0x0080, 2400: 0x00A0, 3300: 0x00C0}
CHANNEL_MAP = {0: 0x4000, 1: 0x5000, 2: 0x6000, 3: 0x7000}
PROGRAMMABLE_GAIN_MAP = {6144: 0x0000, 4096: 0x0200, 2048: 0x0400, 1024: 0x0600, 512: 0x0800, 256: 0x0A00}

PGA_6_144V = 6144
PGA_4_096V = 4096
PGA_2_048V = 2048
PGA_1_024V = 1024
PGA_0_512V = 512
PGA_0_256V = 256

# sane defaults
config = 0x0003 | 0x0100

config |= SAMPLES_PER_SECOND_MAP[samples_per_second]
config |= CHANNEL_MAP[channel]
config |= PROGRAMMABLE_GAIN_MAP[programmable_gain]

# set "single shot" mode
config |= 0x8000

data = str(base64.b64encode(bytes(config)))

msg['payload'] = data
return msg

Here's my flow:
[{"id":"bc9d780c.66a1d8","type":"inject","z":"b7fbdb4d.ce3888","name":"Read Ch0","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"bool","x":100,"y":700,"wires":[["fd6e9124.83928"]]},{"id":"fd6e9124.83928","type":"python3-function","z":"b7fbdb4d.ce3888","name":"Ch0 Read Req","func":"import base64\n\nchannel = 0\nsamples_per_second = 1600\nprogrammable_gain = 4096\n\n\nSAMPLES_PER_SECOND_MAP = {128: 0x0000, 250: 0x0020, 490: 0x0040, 920: 0x0060, 1600: 0x0080, 2400: 0x00A0, 3300: 0x00C0}\nCHANNEL_MAP = {0: 0x4000, 1: 0x5000, 2: 0x6000, 3: 0x7000}\nPROGRAMMABLE_GAIN_MAP = {6144: 0x0000, 4096: 0x0200, 2048: 0x0400, 1024: 0x0600, 512: 0x0800, 256: 0x0A00}\n\nPGA_6_144V = 6144\nPGA_4_096V = 4096\nPGA_2_048V = 2048\nPGA_1_024V = 1024\nPGA_0_512V = 512\nPGA_0_256V = 256\n\n# sane defaults\nconfig = 0x0003 | 0x0100\n\nconfig |= SAMPLES_PER_SECOND_MAP[samples_per_second]\nconfig |= CHANNEL_MAP[channel]\nconfig |= PROGRAMMABLE_GAIN_MAP[programmable_gain]\n\n# set \"single shot\" mode\nconfig |= 0x8000\n\ndata = base64.b64encode(bytes(config))\n\nmsg['payload'] = data\nreturn msg\n","outputs":1,"x":280,"y":700,"wires":[["98b9f764.373bc"]]},{"id":"98b9f764.373bc","type":"change","z":"b7fbdb4d.ce3888","name":"Cmd: Config","rules":[{"t":"set","p":"command","pt":"msg","to":"\"0x01\"","tot":"bin"}],"action":"","property":"","from":"","to":"","reg":false,"x":510,"y":700,"wires":[["7865037d.c50ffc"]]},{"id":"7865037d.c50ffc","type":"change","z":"b7fbdb4d.ce3888","name":"Addr: 0x48","rules":[{"t":"set","p":"address","pt":"msg","to":"\"0x48\"","tot":"bin"}],"action":"","property":"","from":"","to":"","reg":false,"x":710,"y":700,"wires":[["632c84e3.0921b4"]]},{"id":"632c84e3.0921b4","type":"i2c out","z":"b7fbdb4d.ce3888","name":"","busno":"1","address":"","command":"","payload":"payload","payloadType":"msg","count":"","x":870,"y":700,"wires":[["2b532c72.181e74"]]},{"id":"2b532c72.181e74","type":"change","z":"b7fbdb4d.ce3888","name":"Ch0 Read Result","rules":[{"t":"set","p":"payload","pt":"msg","to":"","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":290,"y":740,"wires":[["88fbd249.34c3b"]]},{"id":"88fbd249.34c3b","type":"change","z":"b7fbdb4d.ce3888","name":"Cmd: Read Analog","rules":[{"t":"set","p":"command","pt":"msg","to":"\"0x00\"","tot":"bin"}],"action":"","property":"","from":"","to":"","reg":false,"x":530,"y":740,"wires":[["e1455c8e.74a678"]]},{"id":"e1455c8e.74a678","type":"change","z":"b7fbdb4d.ce3888","name":"Addr: 0x48","rules":[{"t":"set","p":"address","pt":"msg","to":"\"0x48\"","tot":"bin"}],"action":"","property":"","from":"","to":"","reg":false,"x":710,"y":740,"wires":[["bfb2d3d5.0df438"]]},{"id":"bfb2d3d5.0df438","type":"i2c in","z":"b7fbdb4d.ce3888","name":"","busno":"1","address":"","command":"","count":"2","x":870,"y":740,"wires":[["d88c5eaa.5a7da8"]]},{"id":"d88c5eaa.5a7da8","type":"python3-function","z":"b7fbdb4d.ce3888","name":"Parse Data","func":"programmable_gain = 4096\n\ndata = msg['payload']\nvalue = ((data[0] << 4) | (data[1] >> 4))\n\nif value & 0x800:\n value -= 1 << 12\n\nvalue /= 2047.0 # Divide down to percentage of FS\nvalue *= float(programmable_gain)\nvalue /= 3300.0 # Divide by VCC\n\nmsg['payload'] = value\nreturn msg","outputs":1,"x":270,"y":780,"wires":[["b5caf480.7bd0c"]]},{"id":"b5caf480.7bd0c","type":"debug","z":"b7fbdb4d.ce3888","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":490,"y":780,"wires":[]}]

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.