2024-02-181 min read•By Joshua Richards
Bridging Physical Hardware and WebXR with the ESP32
ESP32IoTWebSocketsHardwareThree.js
Bridging Physical Hardware and WebXR with the ESP32
As creative technologists, we often think of the browser as a digital sandbox isolated from the physical world. However, modern Web APIs combined with microcontrollers like the ESP32 completely bridge this gap.
Why the ESP32?
The ESP32 is a powerhouse:
- Dual-core Tensilica LX6 microprocessor running at 240 MHz.
- Integrated Wi-Fi and Bluetooth BLE.
- Ultra-low power sleep states.
- 36 GPIO pins for SPI, I2C, UART, and capacitive touch sensors.
When paired with an MPU-6050 6-axis gyroscope/accelerometer, an ESP32 can broadcast orientation telemetry over local WebSockets at 100 Hz with under 10ms of latency.
// ESP32 WebSocket orientation broadcast snippet
void sendOrientation(float pitch, float roll, float yaw) {
char buffer[128];
snprintf(buffer, sizeof(buffer), "{\"pitch\":%.2f,\"roll\":%.2f,\"yaw\":%.2f}", pitch, roll, yaw);
webSocket.broadcastTXT(buffer);
}
Receiving Telemetry in React Three Fiber
In the web application, a lightweight WebSocket client hook listens for telemetry and applies quaternions directly to the 3D model:
useFrame(() => {
if (telemetryRef.current) {
targetMesh.current.rotation.set(
telemetryRef.current.pitch,
telemetryRef.current.yaw,
telemetryRef.current.roll
);
}
});
Physical computing is not just about flashing LEDs—it's about making digital spaces tangible and responsive.