What are Heartbeats in ESP32 Wrover from Elegoo car kit v4?

Hi, I'm new to Arduino, and I'm programming with a couple of friends the Elegoo car kit v4. Right now I'm trying to understand how to use a websocket server on the ESP32 that comes with the kit, and to do so I'm studying the code that Elegoo gives, but there are tree things I cannot wrap my head around.
The first are heartbeat: what do I need the for and what are they? I tried looking around in the forum and on google but I found nothing.
The second is, what does the data_begin flag does?
The third and last is: what are WA_en and ED_client?

I hope I chose the right category, in case sorry, like I said I'm new, and I tried to choose the category that seemed the best fit for the question.

Here's the code

void SocketServer_Test(void)
{
  static bool ED_client = true;
  WiFiClient client = server.available(); //尝试建立客户对象
  if (client)                             //如果当前客户可用
  {
    WA_en = true;
    ED_client = true;
    Serial.println("[Client connected]");
    String readBuff;
    String sendBuff;
    uint8_t Heartbeat_count = 0;
    bool Heartbeat_status = false;
    bool data_begin = true;
    while (client.connected()) //如果客户端处于连接状态
    {
      if (client.available()) //如果有可读数据
      {
        char c = client.read();             //Read 1 byte
        Serial.print(c);                    //Print it
        if (true == data_begin && c == '{') // '{' is the initial char for the message
        {
          data_begin = false;
        }
        if (false == data_begin && c != ' ') //去掉空格
        {
          readBuff += c;
        }
        if (false == data_begin && c == '}') // '}' is the final char of the message
        {
          data_begin = true;
          if (true == readBuff.equals("{Heartbeat}"))
          {
            Heartbeat_status = true;
          }
          else
          {
            Serial2.print(readBuff);
          }
          //Serial2.print(readBuff);
          readBuff = "";
        }
      }
     // Serial2 seems to be the Arduino on the car: if it's available the data it's sending gets sent to the client (?)
      if (Serial2.available())
      {
        char c = Serial2.read();
        sendBuff += c;
        if (c == '}') //接收到结束字符
        {
          client.print(sendBuff);
          Serial.print(sendBuff); //从串口打印
          sendBuff = "";
        }
      }

      static unsigned long Heartbeat_time = 0;
      if (millis() - Heartbeat_time > 1000) //心跳频率
      {
        client.print("{Heartbeat}");
        if (true == Heartbeat_status)
        {
          Heartbeat_status = false;
          Heartbeat_count = 0;
        }
        else if (false == Heartbeat_status)
        {
          Heartbeat_count += 1;
        }
        if (Heartbeat_count > 3)
        {
          Heartbeat_count = 0;
          Heartbeat_status = false;
          break;
        }
        Heartbeat_time = millis();
      }
      static unsigned long Test_time = 0;
      if (millis() - Test_time > 1000) //定时检测连接设备
      {
        Test_time = millis();
        //Serial2.println(WiFi.softAPgetStationNum());
        if (0 == (WiFi.softAPgetStationNum())) // If there are no clients the car should stop.
        {
          Serial2.print("{\"N\":100}");
          break;
        }
      }
    }
    Serial2.print("{\"N\":100}");
    client.stop(); //结束当前连接:
    Serial.println("[Client disconnected]");
  }
  else
  {
    if (ED_client == true)
    {
      ED_client = false;
      Serial2.print("{\"N\":100}");
    }
  }
}

Wikipedia explains how heartbeat messages are used in the computing context. Heartbeat (computing) - Wikipedia

data_begin looks like it's used in the message parser.
In serial comms messages arrive one character at a time so you need to looks for start and end of message indicators.

I have no idea what 'WA_en' is used for. '_en' is sometimes used as an abbreviation to mean 'enabled'. Have a look in the libraries to see it appears anywhere.

No idea what ED_client refers to. Need to see the code from the other Arduino to see what the {"N":100} message indicates.

Example code that ships with libraries or products sometimes contains extra stuff that was added during development for debugging purposes and was never removed because it didn't cause any harm and the programmer was lazy or was under time pressure to finish.

Okk, thank you! I'll try removing some part and seeing if something changes!

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