/* Puppet Voice Controller - ESP32-S3 XIAO PCA9685 PWM Driver + 7x SG90 Servos WebSocket server receives joint commands from browser app Wiring: XIAO ESP32-S3 SDA -> PCA9685 SDA XIAO ESP32-S3 SCL -> PCA9685 SCL PCA9685 VCC -> 3.3V PCA9685 V+ -> External 5V (for servos) PCA9685 GND -> GND Libraries needed (install via Arduino Library Manager): - Adafruit PWM Servo Driver Library - WebSockets by Markus Sattler - ArduinoJson */ #include #include #include #include #include // ─── Access Point Config ───────────────────────────────────────────── // ESP32 creates its OWN wifi network - connect your phone/laptop to this const char* AP_SSID = "PuppetControl"; const char* AP_PASSWORD = "puppet123"; // ─── PCA9685 Setup ─────────────────────────────────────────────────── Adafruit_PWMServoDriver pca = Adafruit_PWMServoDriver(0x40); // default I2C address #define SERVO_FREQ 50 // 50Hz for SG90 #define SERVO_MIN_PULSE 150 // ~0° pulse count #define SERVO_MAX_PULSE 600 // ~180° pulse count // ─── Servo Channel Mapping ─────────────────────────────────────────── #define CH_LEFT_ELBOW 0 #define CH_LEFT_WRIST 1 #define CH_RIGHT_ELBOW 2 #define CH_RIGHT_WRIST 3 #define CH_LEFT_KNEE 4 #define CH_RIGHT_KNEE 5 #define CH_HEAD 6 // ─── Angle Constants (tunable per joint) ──────────────────────────── // Adjust these after physical testing! struct JointConfig { int limp; // startup position (string slack) int neutral; // standing position int pulled; // full pull (max movement) }; JointConfig joints[7] = { {0, 20, 45}, // CH0 - Left Elbow {0, 20, 45}, // CH1 - Left Wrist {0, 20, 45}, // CH2 - Right Elbow {0, 20, 45}, // CH3 - Right Wrist {0, 20, 45}, // CH4 - Left Knee {0, 20, 45}, // CH5 - Right Knee {0, 15, 35}, // CH6 - Head (smaller range) }; // Track current angles int currentAngles[7] = {0, 0, 0, 0, 0, 0, 0}; // ─── WebSocket Server ───────────────────────────────────────────────── WebSocketsServer webSocket = WebSocketsServer(81); // ─── Servo Helpers ──────────────────────────────────────────────────── int angleToPulse(int angle) { return map(angle, 0, 180, SERVO_MIN_PULSE, SERVO_MAX_PULSE); } void setServo(int channel, int angle) { angle = constrain(angle, 0, 180); pca.setPWM(channel, 0, angleToPulse(angle)); currentAngles[channel] = angle; Serial.printf("CH%d -> %d°\n", channel, angle); } void smoothMove(int channel, int targetAngle, int stepDelay = 15) { int current = currentAngles[channel]; int step = (targetAngle > current) ? 1 : -1; while (current != targetAngle) { current += step; setServo(channel, current); delay(stepDelay); } } // ─── Puppet Poses ───────────────────────────────────────────────────── void poseLimp() { for (int i = 0; i < 7; i++) smoothMove(i, joints[i].limp); } void poseStand() { for (int i = 0; i < 7; i++) smoothMove(i, joints[i].neutral); } void poseWave() { smoothMove(CH_RIGHT_ELBOW, joints[CH_RIGHT_ELBOW].pulled); delay(300); smoothMove(CH_RIGHT_WRIST, joints[CH_RIGHT_WRIST].pulled); delay(300); smoothMove(CH_RIGHT_WRIST, joints[CH_RIGHT_WRIST].neutral); delay(300); smoothMove(CH_RIGHT_WRIST, joints[CH_RIGHT_WRIST].pulled); delay(300); smoothMove(CH_RIGHT_WRIST, joints[CH_RIGHT_WRIST].neutral); } void poseBow() { smoothMove(CH_HEAD, joints[CH_HEAD].pulled); smoothMove(CH_LEFT_ELBOW, joints[CH_LEFT_ELBOW].neutral); smoothMove(CH_RIGHT_ELBOW, joints[CH_RIGHT_ELBOW].neutral); delay(800); smoothMove(CH_HEAD, joints[CH_HEAD].neutral); } void poseDance() { smoothMove(CH_LEFT_ELBOW, joints[CH_LEFT_ELBOW].pulled, 10); smoothMove(CH_RIGHT_KNEE, joints[CH_RIGHT_KNEE].pulled, 10); delay(200); smoothMove(CH_LEFT_ELBOW, joints[CH_LEFT_ELBOW].neutral, 10); smoothMove(CH_RIGHT_KNEE, joints[CH_RIGHT_KNEE].neutral, 10); smoothMove(CH_RIGHT_ELBOW, joints[CH_RIGHT_ELBOW].pulled, 10); smoothMove(CH_LEFT_KNEE, joints[CH_LEFT_KNEE].pulled, 10); delay(200); smoothMove(CH_RIGHT_ELBOW, joints[CH_RIGHT_ELBOW].neutral, 10); smoothMove(CH_LEFT_KNEE, joints[CH_LEFT_KNEE].neutral, 10); } // ─── Command Parser ─────────────────────────────────────────────────── void handleCommand(String cmd) { cmd.toLowerCase(); cmd.trim(); cmd = cmd; // force reassignment after in-place ops Serial.println("CMD: " + cmd); // ── Single Joint Commands ── if (cmd == "raise left arm" || cmd == "lift left arm" || cmd == "left arm up") { smoothMove(CH_LEFT_ELBOW, joints[CH_LEFT_ELBOW].pulled); } else if (cmd == "lower left arm" || cmd == "left arm down") { smoothMove(CH_LEFT_ELBOW, joints[CH_LEFT_ELBOW].neutral); } else if (cmd == "raise right arm" || cmd == "lift right arm" || cmd == "right arm up") { smoothMove(CH_RIGHT_ELBOW, joints[CH_RIGHT_ELBOW].pulled); } else if (cmd == "lower right arm" || cmd == "right arm down") { smoothMove(CH_RIGHT_ELBOW, joints[CH_RIGHT_ELBOW].neutral); } else if (cmd == "raise left wrist" || cmd == "left wrist up") { smoothMove(CH_LEFT_WRIST, joints[CH_LEFT_WRIST].pulled); } else if (cmd == "lower left wrist" || cmd == "left wrist down") { smoothMove(CH_LEFT_WRIST, joints[CH_LEFT_WRIST].neutral); } else if (cmd == "raise right wrist" || cmd == "right wrist up") { smoothMove(CH_RIGHT_WRIST, joints[CH_RIGHT_WRIST].pulled); } else if (cmd == "lower right wrist" || cmd == "right wrist down") { smoothMove(CH_RIGHT_WRIST, joints[CH_RIGHT_WRIST].neutral); } else if (cmd == "raise left knee" || cmd == "lift left leg" || cmd == "left knee up") { smoothMove(CH_LEFT_KNEE, joints[CH_LEFT_KNEE].pulled); } else if (cmd == "lower left knee" || cmd == "left leg down") { smoothMove(CH_LEFT_KNEE, joints[CH_LEFT_KNEE].neutral); } else if (cmd == "raise right knee" || cmd == "lift right leg" || cmd == "right knee up") { smoothMove(CH_RIGHT_KNEE, joints[CH_RIGHT_KNEE].pulled); } else if (cmd == "lower right knee" || cmd == "right leg down") { smoothMove(CH_RIGHT_KNEE, joints[CH_RIGHT_KNEE].neutral); } else if (cmd == "nod" || cmd == "raise head" || cmd == "head up") { smoothMove(CH_HEAD, joints[CH_HEAD].pulled); } else if (cmd == "lower head" || cmd == "head down") { smoothMove(CH_HEAD, joints[CH_HEAD].neutral); } // ── Combo 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 == "raise both arms" || cmd == "hands up") { smoothMove(CH_LEFT_ELBOW, joints[CH_LEFT_ELBOW].pulled); smoothMove(CH_RIGHT_ELBOW, joints[CH_RIGHT_ELBOW].pulled); } else if (cmd == "lower both arms" || cmd == "arms down") { smoothMove(CH_LEFT_ELBOW, joints[CH_LEFT_ELBOW].neutral); smoothMove(CH_RIGHT_ELBOW, joints[CH_RIGHT_ELBOW].neutral); } else { Serial.println("Unknown command: " + cmd); } } // ─── WebSocket Event Handler ────────────────────────────────────────── 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); // Send current state back 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); Serial.println("Received: " + msg); // Parse JSON: {"command": "raise left arm"} 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..."); // Init PCA9685 Wire.begin(); pca.begin(); pca.setOscillatorFrequency(27000000); pca.setPWMFreq(SERVO_FREQ); delay(100); // Start limp poseLimp(); Serial.println("Servos initialized — puppet is limp"); // Start Access Point - ESP32 creates its own WiFi network WiFi.mode(WIFI_AP); WiFi.softAP(AP_SSID, AP_PASSWORD); delay(500); Serial.println("\n✅ Access Point started!"); Serial.print("📶 Connect your device to WiFi: "); Serial.println(AP_SSID); Serial.print("🔑 Password: "); Serial.println(AP_PASSWORD); Serial.print("🌐 Then open browser and go to: ws://"); Serial.print(WiFi.softAPIP()); Serial.println(":81"); Serial.print("📱 ESP32 IP is: "); Serial.println(WiFi.softAPIP()); // Start WebSocket server on port 81 webSocket.begin(); webSocket.onEvent(onWebSocketEvent); Serial.println("🔌 WebSocket server running on port 81"); } // ─── Loop ───────────────────────────────────────────────────────────── void loop() { webSocket.loop(); }