Sending of JSON data using the serial port

Hi Zerynth Team

I’m trying to send the JSON data from ESP32 to Raspberry Pi4 using the Serial Port2(IO16, IO17).
I’d like to know three things.

  1. Is it possible to send JSON data directly from the ESP32 to the RPI4 using the serial port?
  2. Is it necessary to use write() method to send the data? Or can I use only print() to send the data?
  3. The JSON data I want to send includes many bytes. From that point of view, is it reasonable to use serial port? Can it work reliably?

Thanks.
Anatoli.

  1. you can send JSON objects over UART by using the JSON library and sending it as a string, then parse the received string using JSON library.
  2. you can send them over print() as a normal string.
  3. how many bytes do you want to send.

Hi @karimhamdy1
Thanks for your help.

  1. As to the JSON library, you mean json.dumps() and json.loads() function?

  2. In the end, we don’t have to use write() method to send the data over UART. We can send data with just print() function. Right?

  3. The JSON data format I want to send over UART is as follows.
    json_data = {
    “uuid”: Constants.MAC_ADDR,
    “channel”: channel,
    “data_id”: i,
    “vrms”: Constants.RAW_DATA_LIST[“VRMS”][i],
    “irms”: Constants.RAW_DATA_LIST[“IRMS”][i],
    “snapshot_time”: Constants.SNAPSHOT_TIME,
    “snapshot_date”: current_time,
    “sample_rate”: Constants.SAMPLE_RATE,
    “snapshot_flag”: Constants.SNAPSHOT_STATUS
    }
    Here Constants.RAW_DATA_LIST[“VRMS”][i] and Constants.RAW_DATA_LIST[“IRMS”][i] are arrays with 100 elements.
    I’m going to send this JSON data in the format above five times in a row.
    Will it work properly?

Thanks in advance.

Anatoli.

Hi @Anatoli_Juny
1- yes, exactly.
2- you can just use print.
3- All of this data could be sent, but due to the nature of the Serial communication theis will be sent sequentially, so if you are developing time critical application using this data, this approach might not be the best.
Also why do you need to send strings like uuid, data_id? you can just send values without the field names.
for instance, you can send Constants.MAC_ADDR as string, then channel as int, i as Int… then the amount of data sent is much smaller, but the receiving side will have to parse this data.
also you need an error checking mechanism, so the receiving side can check if he recieved the data packet correctly.