example — Xiao Esp32C6 simulation

An interactive Xiao Esp32C6 circuit simulation you can run free in your browser on Velxio, by raodheerajrao88.

Components

Sketch code

#include <Arduino.h>

String lineBuffer = "";

void sendAT(const char *cmd, unsigned long timeoutMs = 1200) {
  Serial.print(">> ");
  Serial.println(cmd);

  Serial1.print(cmd);
  Serial1.print("\r\n");

  unsigned long start = millis();

  while (millis() - start < timeoutMs) {
    while (Serial1.available()) {
      char c = (char)Serial1.read();
      Serial.write(c);
    }
    delay(1);
  }
}

bool configureRadio() {
  String response;

  // The simulator may reject the first mode command.
  for (int i = 0; i < 2; i++) {
    while (Serial1.available()) Serial1.read();

    Serial1.print("AT+MODE=TEST\r\n");
    response = "";
    unsigned long start = millis();

    while (millis() - start < 1000) {
      while (Serial1.available()) {
        response += (char)Serial1.read();
      }
      delay(1);
    }

    Serial.print(response);

    if (response.indexOf("+MODE: TEST") >= 0) break;
  }

  if (response.indexOf("+MODE: TEST") < 0) {
    Serial.println("ERROR: Could not enter TEST mode.");
    return false;
  }

  while (Serial1.available()) Serial1.read();
  Serial1.print(
    "AT+TEST=RFCFG,866,SF12,125,12,15,14,ON,OFF,OFF\r\n"
  );

  response = "";
  unsigned long start = millis();

  while (millis() - start < 1200) {
    while (Serial1.available()) {
      response += (char)Serial1.read();
    }
    delay(1);
  }

  Serial.print(response);
  return response.indexOf("+TEST: RFCFG") >= 0;
}

void processLine(String line) {
  line.trim();
  if (line.length() == 0) return;

  Serial.print("RX line: ");
  Serial.println(line);

  if (line.indexOf("49474E4954494F4E5F4F4E") >= 0) {
    Serial.println("*** EXCAVATOR IGNITION ON ***");
  } else if (line.indexOf("49474E4954494F4E5F4F4646") >= 0) {
    Serial.println("*** EXCAVATOR IGNITION OFF ***");
  }
}

void setup() {
  Serial.begin(115200);

  // Wio-E5 TX -> XIAO D7; Wio-E5 RX -> XIAO D6.
  Serial1.begin(9600, SERIAL_8N1, D7, D6);
  delay(1000);

  Serial.println("IGNITION RX STARTING");

  if (!configureRadio()) {
    Serial.println("Radio setup failed; stopping.");
    return;
  }

  Serial.println("Entering receive mode...");
  Serial1.print("AT+TEST=RXLRPKT\r\n");
}

void loop() {
  while (Serial1.available()) {
    char c = (char)Serial1.read();

    if (c == '\n') {
      processLine(lineBuffer);
      lineBuffer = "";
    } else if (c != '\r') {
      lineBuffer += c;

      if (lineBuffer.length() > 300) {
        processLine(lineBuffer);
        lineBuffer = "";
      }
    }
  }
}

More projects by raodheerajrao88