PR

【M5stack unitV2】Read UART json with ESP32

wiring is “RX – TX” and “TX – RX”

void setup() {
  // Start serial communication
  Serial.begin(115200);
}

// Loop
void loop() {
  // Number of bytes available in the serial port
  if (Serial.available() > 0 ) {
    // Read serial data until newline
    String data = Serial.readStringUntil('\n');

    // Output the received data
    Serial.print("Receive:");
    Serial.println(data);
  }
}

Face Recognition,

const String face_name = "your face name here";

void setup() {
  // Start serial communication
  Serial.begin(115200);
}

// Loop
void loop() {
  // Number of bytes available in the serial port
  if (Serial.available() > 0) {
    // Receive serial data (until newline)
    String data = Serial.readStringUntil('\n');

    // Check if the data contains the word "name"
    if (data.indexOf("name") != -1) {
      // Check if the data also contains the word "face_name"
      if (data.indexOf(face_name) != -1) {
        Serial.println("1"); // Output 1 if "face_name" is found
      } else {
        Serial.println("0"); // Output 0 if "face_name" is not found
      }
    } else {
      Serial.println("0"); // Output 0 if "name" is not found
    }
  }
}

コメント

タイトルとURLをコピーしました