ANROT Tutorial

ESP32 Serial Reading Tutorial

Use ESP32 and Arduino IDE to read ANROT serial packets for fast MCU-side evaluation and prototyping.

Updated Jan 1, 2025 Products 7
  • esp32
  • uart
  • tutorial

ESP32 Serial Reading Example

Protocol Support

Protocol / frameSupportHow this example handles it
0x91 IMUSOLSupportedDecodes a single-device float frame into receive_imusol; the sketch prints ID, roll, pitch, and yaw.
0x62 GWSOLSupportedDecodes gateway float frames and iterates receive_gwsol.receive_imusol[] for each node.
0x63 GWSOL CompactNot supportedThe ESP32 decoder has no compact gateway branch; use the Qt C++ or Ubuntu examples for 0x63.
0x90, 0xA0, 0xB0, 0xC0, 0xD0, 0xD1Partially supportedThe decoder can parse loose items; the sketch focuses on attitude output.
0xF0 PressureSkippedThe code advances over the payload length and does not store a pressure field.

Download and unzip: demo-esp32-en.zip

This example uses ESP32 Serial2 in Arduino IDE to read ANROT UART packets. It is intended for MCU-side connection validation before firmware integration.

Applicable products: Hi221 / Hi221 Dongle, Hi226, Hi229, CH100, CH104, CH108, CH110.

Wiring

IMU PINESP32 PIN
5V5V
GNDGND
TXDRXD
RXDTXD

Main sketch

/* Use UART pins 25 and 27.
   Tested on Wemos Lolin32.
*/
#define RXD2 25
#define TXD2 27
#include "imu_data_decode.h"
#include "packet.h"

uint32_t old_frame_ctr = 0;

void setup() {
  // Serial output for the Arduino monitor.
  Serial.begin(115200);
    
  // Note the format for setting a serial port is as follows: Serial2.begin(baud-rate, protocol, RX pin, TX pin);
  Serial2.begin(115200, SERIAL_8N1, RXD2, TXD2);

  imu_data_decode_init();
}

void loop() {
  while (Serial2.available()) {
    char c = Serial2.read();
    packet_decode(c);
  }

  // Print when a new frame is received.
  if (frame_count > old_frame_ctr) {
    old_frame_ctr = frame_count;

    //Hi221/226/229, ch100/110
    if (receive_gwsol.tag != KItemGWSOL) {
      Serial.println(String(receive_imusol.id) + ":"
                     + String(receive_imusol.eul[0]) + ","
                     + String(receive_imusol.eul[1]) + ","
                     + String(receive_imusol.eul[2])
                    );
    }
    //Hi221 dongle
    else {
      for (int i = 0; i < receive_gwsol.n; i++)
      {
        //show timestamp(ms) from startup
        uint32_t ts = esp_timer_get_time() / 1000;
        Serial.println(String(ts) + ":"
                       + String(receive_gwsol.receive_imusol[i].id) + ","
                       + String(receive_gwsol.receive_imusol[i].eul[0]) + ","
                       + String(receive_gwsol.receive_imusol[i].eul[1]) + ","
                       + String(receive_gwsol.receive_imusol[i].eul[2])
                      );

      }
    }
  }
}