Parse json string from UART to Object

Hi all,
I though, this would be trivial to do, but somehow, I don’t get it !
All sample code I’ve found, including what I get from AI, is based on “platform: custom”, which has been removed !! (for example Convert JSON String from UART to object).
How can I perform such “easy” task ?
Thx
Fred

You can bring it back with this approach:
GitHub - robertklep/esphome-custom-component: Brings back support for custom ESPHome components

1 Like

That might be a “emergency” workaround… but is it really “safe”?
I read that this “platform: custom” has been removed, because of some missing validity checks, …

As safe as it was before…

You didn’t really state WHY you are using the thing you asked for help with. I would guess that is because you have something that is generating some data and it has multiple values that interest you.

The idiomatic way in esphome is to create a custom component to do this and make it easy for everyone else to use it. Unfortunately, the barrier to entry for external component is quite high and the documentation is quite lacking, which makes it incredibly difficult for most people.

Luckily, there is this: Easy UART Parsing ESPHome Component

I have not tried it, since my use case is even more problematic (it involves 9-bit serial with different packet types in a binary format) so I am doing it with an external component.

The easy part of the easy-uart-parsing is likely in comparison to creating your own external component (which is most definitely in the difficult area).

Avoid the custom component.

Yes, I forget to mention what I wanted to do.
I’ve a solar diverter that is running on a Atmega328p. This stuff produces some output like the power at the grid, the amount of diverted energy, …
I’ve formatted this output in a json string which is readable on serial.
My goal was to program an ESP32 to read this serial output and upload it to HomeAssistant.

So why don’t you post what you get on your log with uart debug?
Also, wouldn’t it be easier to replace the atmega with esp32 in the first place?

I cannot (and don’t want to) replace the Atmega with an ESP32. I’m using all 6 ADCs from the Arduino, which are less noisy than these from the ESP32 !

Agree that.

plus, I’ve already searched for some “external” ADCs… but they’re all sooooo slow… it’s simply not usable !

So stay with atmega. You just pick if you prefer to go with custom like before or trying to get your data with uart debug with dummy_receiver option. I don’t see why it wouln’t work.

# Example configuration entry
uart:
  baud_rate: 115200
  debug:
    direction: RX
    dummy_receiver: true
    after:
      delimiter: "\n"
    sequence:
      - lambda: UARTDebug::log_string(direction, bytes);

Or study how this component could help you:
espcomponents/components/uartex at master · eigger/espcomponents · GitHub

That’s exactly what I started to implement… I’ll write again about the result of my try.

You have the data available byte by byte starting from
bytes[0]

look here: https://www.microchip.com/en-us/product/mcp3208

there are MANY very fast ADCs, but they are a little harder to find than the very common but slow ones that are generally good enough for most tasks.

You can even find breakout boards: PCBs for ADC Chips (MCP3208 and ADC124S021) — Parallax Forums

If you stick with your current solution you really should look at the link I posted above for easy UART parsing, since it seems to be able to do exactly what you want.

I did a project to do just this (parse JSON and create sensors). Not used for 3 years so don’t know if it still works.

esphome_components/components/emontx4 at main · borpin/esphome_components

That’s very similar to what I need… I think, I’ll “copy-paste” this emontx4 component and adapt it to my own serial output !
Great… thx for the link…

1 Like

Just beware it is a few years old so some of the sensor declarations might need revised as the standards have changed.

1 Like