家庭陪伴机器人 V3 – 雪品金管家 — ESP32-S3 simulation

基于 Velxio 示例 Desk robot in a 3D World:ESP32-S3、长方形 TFT 熊脸、SG90 耳朵×2、履带差速、PIR + HC-SR04、I2S 语音,状态机 SLEEP/AWAKE/LISTENING/SPEAKING/ALERT。

An interactive ESP32-S3 circuit simulation you can run free in your browser on Velxio, by zhouzhi0713.

Components

Sketch code

// 家庭陪伴机器人 V3 — 雪品金管家 (ESP32-S3 DevKit)
// 恢复版 2026-11-15:按 README §2-§5、§12-§14 重建(原 sketch.ino 丢失)
// 要点:
//  - 电机:STM32 单字节 I2C 协议 (ADDR 0x14),绝不用 Seeed 0x82/0xAA 旧协议
//  - 计时:10 秒判断用 millis()-seen>10000(比较时现取 millis),不用循环开头取的 n
//  - 屏幕:白底(SLEEP 全黑除外);钥匙绝不进代码,走本地中转接口
#include <Arduino.h>
#include <math.h>
#include <SPI.h>
#include <Wire.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <driver/i2s.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <ESP32Servo.h>

// ---- 引脚(ESP32-S3 DevKit)----
#define TFT_MOSI 11
#define TFT_MISO 13
#define TFT_SCK  12
#define TFT_CS   38
#define TFT_DC   39
#define TFT_RST  40
#define EAR_L_PIN 1
#define EAR_R_PIN 2
#define PIR_PIN   4
#define TRIG_PIN  5
#define ECHO_PIN  6
#define BUZZER_PIN 7
#define MIC_PIN   10
#define I2S_BCLK  14
#define I2S_WS    15
#define I2S_DIN   16
#define I2S_DOUT  17

#define MIC_THRESHOLD 1500
#define ALERT_DIST 20
#define ALERT_EXIT_DIST 25
#define AWAKE_TIMEOUT_MS 10000UL
#define LISTEN_QUIET_MS 1000UL
#define MOTOR_ADDR 0x14
#define MOTOR_SPEED 60

// 电机命令(STM32 单字节 I2C)
#define M_BRAKE 0x00
#define M_CW    0x02
#define M_CCW   0x03
#define M_STANDBY 0x04
#define M_NOT_STANDBY 0x05

const char* WIFI_SSID = "Velxio-GUEST";
const char* VOICE_URL = "http://host.velxio.internal:8765/voice?raw=1";

Adafruit_ILI9341 tft(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCK, TFT_RST, TFT_MISO);
Servo earL, earR;

enum State { SLEEP, AWAKE, LISTENING, SPEAKING, ALERT };
State st = SLEEP;
int dist = 200;
int ovr = -1;
unsigned long seen = 0;
unsigned long lastLoud = 0;
int motorV = 0;
int earLPos = 90, earRPos = 90;
int sayIdx = 0;
bool i2sOk = false;

const char* LINES[3] = {"你好呀,我在听。", "嗯,我在这儿呢。", "好呀,我们一起玩吧。"};

const char* stateName(State s) {
  switch (s) {
    case SLEEP: return "SLEEP";
    case AWAKE: return "AWAKE";
    case LISTENING: return "LISTENING";
    case SPEAKING: return "SPEAKING";
    case ALERT: return "ALERT";
  }
  return "?";
}

void sendMotor(uint8_t cmd, uint8_t ch, uint8_t spd, bool hasSpd) {
  Wire.beginTransmission(MOTOR_ADDR);
  Wire.write(cmd);
  Wire.write(ch);
  if (hasSpd) Wire.write(spd);
  Wire.endTransmission();
  delay(1);
}

void motor(int v) {
  motorV = v;
  if (v == 0) {
    sendMotor(M_BRAKE, 0, 0, false);
    sendMotor(M_BRAKE, 1, 0, false);
    return;
  }
  uint8_t s = (uint8_t)(abs(v) * 255 / 100);
  uint8_t d1 = v > 0 ? M_CW : M_CCW;
  uint8_t d2 = v > 0 ? M_CCW : M_CW;
  sendMotor(d1, 0, s, true);
  sendMotor(d2, 1, s, true);
}

void go(int v) { motor(v); }

void setEars(int l, int r) {
  earLPos = l; earRPos = r;
  earL.write(l);
  earR.write(r);
}

void drawFace(State s) {
  if (s == SLEEP) { tft.fillScreen(ILI9341_BLACK); return; }
  tft.fillScreen(ILI9341_WHITE);
  uint16_t blue = ILI9341_BLUE;
  uint16_t navy = tft.color565(0, 0, 139);
  uint16_t pink = tft.color565(255, 182, 193);
  if (s == ALERT) {
    tft.fillCircle(105, 90, 26, blue);
    tft.fillCircle(215, 90, 26, blue);
    tft.fillCircle(97, 82, 7, ILI9341_WHITE);
  

More projects by zhouzhi0713