/* Puppet Voice Controller - FINAL XIAO ESP32-S3 + PCA9685 AP Mode - ESP32 creates its own WiFi network 5 Active Servos (elbows excluded) Channel Mapping: CH2 -> Right Wrist CH13 -> Left Wrist CH15 -> Left Knee CH0 -> Right Knee CH5 -> Head Wiring: XIAO SDA (D4) -> PCA9685 SDA XIAO SCL (D5) -> PCA9685 SCL XIAO 3.3V -> PCA9685 VCC XIAO 5V -> PCA9685 V+ GND -> GND (shared) */ #include #include #include #include #include // ─── AP Config ──────────────────────────────────────────────────────── const char* AP_SSID = "PuppetControl"; const char* AP_PASSWORD = "puppet123"; // ─── PCA9685 ────────────────────────────────────────────────────────── Adafruit_PWMServoDriver pca = Adafruit_PWMServoDriver(0x40); #define SERVO_FREQ 50 #define SERVO_MIN_PULSE 150 // 0° #define SERVO_MAX_PULSE 600 // 180° // ─── Channel Mapping ────────────────────────────────────────────────── #define CH_LEFT_WRIST 13 #define CH_RIGHT_WRIST 2 #define CH_LEFT_KNEE 15 #define CH_RIGHT_KNEE 0 #define CH_HEAD 5 // ─── Angle Constants ────────────────────────────────────────────────── #define LIMP 0 #define NEUTRAL 90 #define FULL 180 // Track current angles int currentAngles[16] = {0}; // ─── WebSocket ──────────────────────────────────────────────────────── WebSocketsServer webSocket = WebSocketsServer(81); // ─── Servo Helpers ──────────────────────────────────────────────────── int angleToPulse(int angle) { return map(angle, 0, 180, SERVO_MIN_PULSE, SERVO_MAX_PULSE); } void setServo(int ch, int angle) { angle = constrain(angle, 0, 180); pca.setPWM(ch, 0, angleToPulse(angle)); currentAngles[ch] = angle; Serial.printf("CH%d -> %d°\n", ch, angle); } void smoothMove(int ch, int target, int stepDelay = 10) { int current = currentAngles[ch]; int step = (target > current) ? 1 : -1; while (current != target) { current += step; setServo(ch, current); delay(stepDelay); } } // ─── Poses ──────────────────────────────────────────────────────────── void poseLimp() { smoothMove(CH_LEFT_WRIST, LIMP); smoothMove(CH_RIGHT_WRIST, LIMP); smoothMove(CH_LEFT_KNEE, LIMP); smoothMove(CH_RIGHT_KNEE, LIMP); smoothMove(CH_HEAD, LIMP); } void poseStand() { smoothMove(CH_LEFT_WRIST, NEUTRAL); smoothMove(CH_RIGHT_WRIST, NEUTRAL); smoothMove(CH_LEFT_KNEE, NEUTRAL); smoothMove(CH_RIGHT_KNEE, NEUTRAL); smoothMove(CH_HEAD, NEUTRAL); } void poseWave() { smoothMove(CH_RIGHT_WRIST, FULL, 8); delay(300); smoothMove(CH_RIGHT_WRIST, NEUTRAL, 8); delay(200); smoothMove(CH_RIGHT_WRIST, FULL, 8); delay(300); smoothMove(CH_RIGHT_WRIST, NEUTRAL, 8); } void poseBow() { smoothMove(CH_HEAD, FULL); delay(800); smoothMove(CH_HEAD, NEUTRAL); } void poseDance() { for (int i = 0; i < 3; i++) { smoothMove(CH_LEFT_WRIST, FULL, 8); smoothMove(CH_RIGHT_KNEE, FULL, 8); delay(200); smoothMove(CH_LEFT_WRIST, NEUTRAL, 8); smoothMove(CH_RIGHT_KNEE, NEUTRAL, 8); smoothMove(CH_RIGHT_WRIST, FULL, 8); smoothMove(CH_LEFT_KNEE, FULL, 8); delay(200); smoothMove(CH_RIGHT_WRIST, NEUTRAL, 8); smoothMove(CH_LEFT_KNEE, NEUTRAL, 8); } } void poseNod() { smoothMove(CH_HEAD, FULL); delay(400); smoothMove(CH_HEAD, NEUTRAL); delay(200); smoothMove(CH_HEAD, FULL); delay(400); smoothMove(CH_HEAD, NEUTRAL); } // ─── Command Parser ─────────────────────────────────────────────────── void handleCommand(String cmd) { cmd.toLowerCase(); cmd.trim(); Serial.println("CMD: " + cmd); // ── Wrist Commands ── if (cmd == "raise left wrist" || cmd == "left wrist up" || cmd == "lift left wrist") { smoothMove(CH_LEFT_WRIST, FULL); } else if (cmd == "lower left wrist" || cmd == "left wrist down") { smoothMove(CH_LEFT_WRIST, LIMP); } else if (cmd == "raise right wrist" || cmd == "right wrist up" || cmd == "lift right wrist") { smoothMove(CH_RIGHT_WRIST, FULL); } else if (cmd == "lower right wrist" || cmd == "right wrist down") { smoothMove(CH_RIGHT_WRIST, LIMP); } else if (cmd == "raise both wrists" || cmd == "both wrists up" || cmd == "hands up") { smoothMove(CH_LEFT_WRIST, FULL); smoothMove(CH_RIGHT_WRIST, FULL); } else if (cmd == "lower both wrists" || cmd == "both wrists down" || cmd == "hands down") { smoothMove(CH_LEFT_WRIST, LIMP); smoothMove(CH_RIGHT_WRIST, LIMP); } // ── Knee Commands ── else if (cmd == "raise left knee" || cmd == "left knee up" || cmd == "lift left leg") { smoothMove(CH_LEFT_KNEE, FULL); } else if (cmd == "lower left knee" || cmd == "left knee down" || cmd == "left leg down") { smoothMove(CH_LEFT_KNEE, LIMP); } else if (cmd == "raise right knee" || cmd == "right knee up" || cmd == "lift right leg") { smoothMove(CH_RIGHT_KNEE, FULL); } else if (cmd == "lower right knee" || cmd == "right knee down" || cmd == "right leg down") { smoothMove(CH_RIGHT_KNEE, LIMP); } else if (cmd == "raise both knees" || cmd == "both knees up") { smoothMove(CH_LEFT_KNEE, FULL); smoothMove(CH_RIGHT_KNEE, FULL); } else if (cmd == "lower both knees" || cmd == "both knees down") { smoothMove(CH_LEFT_KNEE, LIMP); smoothMove(CH_RIGHT_KNEE, LIMP); } // ── Head Commands ── else if (cmd == "nod" || cmd == "head up" || cmd == "raise head") { poseNod(); } else if (cmd == "head down" || cmd == "lower head") { smoothMove(CH_HEAD, FULL); } else if (cmd == "head neutral" || cmd == "center head") { smoothMove(CH_HEAD, NEUTRAL); } // ── Poses ── else if (cmd == "stand" || cmd == "ready" || cmd == "wake up") { poseStand(); } else if (cmd == "limp" || cmd == "sleep" || cmd == "rest") { poseLimp(); } else if (cmd == "wave") { poseWave(); } else if (cmd == "bow") { poseBow(); } else if (cmd == "dance") { poseDance(); } else if (cmd == "hands up" || cmd == "raise both wrists") { smoothMove(CH_LEFT_WRIST, FULL); smoothMove(CH_RIGHT_WRIST, FULL); } else { Serial.println("Unknown command: " + cmd); } } // ─── WebSocket Events ───────────────────────────────────────────────── void onWebSocketEvent(uint8_t clientId, WStype_t type, uint8_t* payload, size_t length) { switch (type) { case WStype_CONNECTED: Serial.printf("Client %d connected\n", clientId); webSocket.sendTXT(clientId, "{\"status\":\"connected\"}"); break; case WStype_DISCONNECTED: Serial.printf("Client %d disconnected\n", clientId); break; case WStype_TEXT: { String msg = String((char*)payload); StaticJsonDocument<256> doc; DeserializationError err = deserializeJson(doc, msg); if (!err && doc.containsKey("command")) { handleCommand(String((const char*)doc["command"])); webSocket.sendTXT(clientId, "{\"status\":\"ok\"}"); } break; } default: break; } } // ─── Setup ──────────────────────────────────────────────────────────── void setup() { Serial.begin(115200); delay(500); Serial.println("\n🎭 Puppet Controller Starting..."); Wire.begin(); pca.begin(); pca.setOscillatorFrequency(27000000); pca.setPWMFreq(SERVO_FREQ); delay(100); poseLimp(); Serial.println("Servos initialized — puppet is limp"); WiFi.mode(WIFI_AP); WiFi.softAP(AP_SSID, AP_PASSWORD); delay(500); Serial.println("\n✅ Access Point started!"); Serial.print("📶 WiFi: "); Serial.println(AP_SSID); Serial.print("🔑 Password: "); Serial.println(AP_PASSWORD); Serial.print("🌐 Connect then open browser to: ws://"); Serial.print(WiFi.softAPIP()); Serial.println(":81"); Serial.print("📱 ESP32 IP: "); Serial.println(WiFi.softAPIP()); webSocket.begin(); webSocket.onEvent(onWebSocketEvent); Serial.println("🔌 WebSocket server on port 81"); } // ─── Loop ───────────────────────────────────────────────────────────── void loop() { webSocket.loop(); }