[Solved]Get info from string serial

Hello,
I have 1 arduino uno + 1 esp8266 talking by emulated serial pins.
Te arduino sends all info(all the time into loop function) using json method, so how can I receive that info by esp8266 side?
I tried to parse the serial info using the most common codes from web but at the hardware serial(esp) I am seeing:

{my correct info}{}{}{}{}.....
{}{}
.
.
.
{my correct info}{}{}{}{}.....

Should I keep the parse method directly reading from serial or should I extract only the correct data and than parse it?
Anybody can help me with some tip please?

Thanks in advance!

Is there a reason you’re using JSON? It might be easier if you stored all your info in a struct. You could then send the struct between Arduinos reliably using a packetizing/parsing library such as SerialTransfer.h. The library is installable through the Arduino IDE and includes many examples.

Here are the library’s features:

This library:

  • can be downloaded via the Arduino IDE’s Libraries Manager (search “SerialTransfer.h”)
  • works with “software-serial” libraries
  • is non blocking
  • uses packet delimiters
  • uses consistent overhead byte stuffing
  • uses CRC-8 (Polynomial 0x9B with lookup table)
  • allows the use of dynamically sized packets (packets can have payload lengths anywhere from 1 to 254 bytes)
  • can transfer bytes, ints, floats, and even structs!!

Example TX Arduino Sketch:

#include "SerialTransfer.h"

SerialTransfer myTransfer;

void setup()
{
  Serial.begin(115200);
  Serial1.begin(115200);
  myTransfer.begin(Serial1);
}

void loop()
{
  char buff[] = "hi";

  myTransfer.txObj(buff, sizeof(buff));
  myTransfer.sendData(sizeof(buff));
  delay(100);
}

Example RX Arduino Sketch:

#include "SerialTransfer.h"

SerialTransfer myTransfer;

void setup()
{
  Serial.begin(115200);
  Serial1.begin(115200);
  myTransfer.begin(Serial1);
}

void loop()
{
  if(myTransfer.available())
  {
    char buff[40];
    
    myTransfer.rxObj(buff, sizeof(buff));
    
    Serial.println("New Data: ");
    Serial.write(buff, sizeof(buff));
    Serial.println();
  }
  else if(myTransfer.status < 0)
  {
    Serial.print("ERROR: ");

    if(myTransfer.status == -1)
      Serial.println(F("CRC_ERROR"));
    else if(myTransfer.status == -2)
      Serial.println(F("PAYLOAD_ERROR"));
    else if(myTransfer.status == -3)
      Serial.println(F("STOP_BYTE_ERROR"));
  }
}

For theory behind robust serial communication, check out the tutorials Serial Input Basics and Serial Input Advanced.

If you really, really need to use JSON, you could probably parse it like this. I haven’t used ArduinoJson.h, so I can’t say for sure.

Hello, thanks for the info!
I confess that I'm not a good programmer, so sorry.
In my project I have a mainboard working with 2 embeded boards(arduino uno + esp8266), the ATmega gets all sensors data, serialize by json method and send by Software serial. This side(ATmega) always worked very well.
My problem is at ESP side because sometimes it doesn't get that data from emulated serial pins like showed above.
Do you know if this library works in ESP8266? I will read that links sent by you :wink:

Cheers

Hello Power_Broker,
Your info was very clean and direct to my learning, you solved my problem and I'm using a library recommended from you.
The problem was the wrong method that I was using in my function to receive data from serial.
Now I'm using your example labeled rx_multiple_objects.

Thanks a lot!
Cheers