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?


  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.


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.

Hi @karimhamdy1

Thanks for your help.
I sent JSON string data from ESP32 board to the Raspberry Pi using uart port.
But I have another problem.

I tried to send a simple string data from the raspberry pi to esp32 board using serial2(IO16 and IO17).
But it doesn’t work. The esp32 doesn’t receive anything from the serial2 port.
I sent the string data from the esp32 to the raspberry pi using serial2 port successfully.
I attached my code for raspberry pi and esp32.

  • Zerynth code for esp32 board

  • python code for raspberry pi

import serial
import time

if name == “main”:

ser = serial.Serial('/dev/ttyAMA0', 9600, timeout=1)

while True:   
    length = ser.write(b"Hello from Raspberry pi\n")

As I mentioned above, the sending from esp to raspberry pi is working well.
However, when I send the data from raspberry pi to esp32, the esp32 doesn’t receive anything.
The raspberry pi code runs without any error. In the code, length = ser.write(b"Hello from Raspberry pi\n") returns the number of bytes of data sent.
What’s the strange for me is that length = s2.available() is always 0 in the zerynth code.

I have been working all day on this issue. I’m very struggling with this issue.
I REALLY hope you get back to me with the best solution as soon as possible.

Thanks in advance.

Are you sure you connect the correct RX pin of SERIAL2 of ESP32 to TX of Rpi according to zerynth pinmap? are you sure of the wiring?
maybe it is string encoding on Rpi?

string1 = “Hello\n”
string1_encode = string1.encode()

in line 15 in the python script on ESP32, try to print the length like this:

Do you have a usb to ttl converter to test the output of raspberrypi ?
if not,on Rpi connect the TX pin to RX pin and see if you recieve the data
Also double check the configuration of Rpi for UART, you need to be sure that the Rpi send the data correctly then we can work on the ESP32 side

Hi @karimhamdy1

Thanks for your reply.

  1. The RX pin of SERIAL2 of ESP32 is connected to the TX pin of RPI correctly according to Zerynth pin map.

  2. As you can see, I’ve added a ‘b’ before the string to send. This will encode the string to bytes, because you can only send bytes through Serial.
    ser.write(b"Hello from Raspberry pi\n")
    I referenced the following tutorial to send the string data from the Pi to the server.
    Of course, I also tried the way you suggested.
    But it also doesn’t work.

  3. I tried print(length, stream=s0) as you suggested. However, the length is still 0.
    In fact, we don’t need to specify stream=s0 because s0 is the latest defined stream object.

4.There is no usb to ttl convert at TX/RX pin of the Raspberry Pi. The TX/RX pins of the Pi are connected to the serial2 pins(IO16 and IO17) of the esp32 directly.

  1. I’ll connect the TX pin and the RX pin of the Pi together. So I’ll see how it works.

  2. I disabled the BT on Pi to enable the uart on the Pi. The UART is enabled correctly on the Pi.
    Yes, it’s the key to make sure if the Pi really sends the data to the esp32. In order to check it, I’ll try the five item.

I’ll get back to you with the test result of 5.

By the way, I have a question. Is it possible to do uart communication between the esp32 and the pi via usb cable? In that case, uart communication will process on the serial port0. I already tried it, but even the communication from the esp32 to the Pi didn’t work.

Hi @karimhamdy1

I tested the uart communication on the Pi.
I connected the TX pin and RX pin together and sent a string data. The Pi receives the string data correctly.
That means the Pi TX pin is working properly.
It also means there is no any problem on the Pi.
What do you think?
In that point, what do I have to do?
Thanks in advance.


How are you? @karimhamdy1

I’m waiting for your solution.
It’s really hard to believe that only one way communication is possible.
I think I am missing something trivial.

Could you try with Serial1 instead of Serial2 on ESP32, just to be sure its not related to some pins.

Thanks for your help.

The uart works well on the serial1.
And I also tested uart on another board. The serial1 and serial2 work well on another board. I think the first board has been broken.

Thanks again.

Great, happy coding!