|
| 1 | +/* |
| 2 | + Sending live servo positions via web socket commands. |
| 3 | +
|
| 4 | + This example requires an ESP32 and a running Blender instance with the Blender |
| 5 | + Servo Animation Add-on. However, this example could be slightly adjusted to |
| 6 | + work with other WiFi-capable micro controllers as well. We create a web socket |
| 7 | + server to receive live position values via Blender and move a single servo |
| 8 | + which is controlled via the ESP32Servo library. |
| 9 | +*/ |
| 10 | + |
| 11 | +#include <AsyncWebSocket.h> |
| 12 | +#include <BlenderServoAnimation.h> |
| 13 | +#include <ESP32Servo.h> |
| 14 | +#include <WiFi.h> |
| 15 | + |
| 16 | +// Change to your SSID and password |
| 17 | +const char *ssid = "SSID"; |
| 18 | +const char *password = "PASSWORD"; |
| 19 | + |
| 20 | +// Create an asynchronous web socket server |
| 21 | +AsyncWebServer server(80); |
| 22 | +AsyncWebSocket ws("/"); |
| 23 | + |
| 24 | +// Servo object to send positions |
| 25 | +Servo myServo; |
| 26 | + |
| 27 | +// Callback function which is called whenever a servo needs to be moved |
| 28 | +void move(byte servoID, int angle) { |
| 29 | + // Ignore the servoID (there is only one servo) and write the current angle |
| 30 | + myServo.write(angle); |
| 31 | +} |
| 32 | + |
| 33 | +// Animation object to manage the servos |
| 34 | +// We skip providing fps or frames as we just want to use the live mode |
| 35 | +BlenderServoAnimation::Animation animation; |
| 36 | + |
| 37 | +// Servo object to manage the positions |
| 38 | +BlenderServoAnimation::Servo myBlenderServo(0, move); |
| 39 | + |
| 40 | +// Live mode stream instance acting as a middleware between web socket and |
| 41 | +// animation object |
| 42 | +BlenderServoAnimation::Live live; |
| 43 | + |
| 44 | +// Handler function writing data to the live stream instance when receiving data |
| 45 | +void onWebSocketEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, |
| 46 | + AwsEventType type, void *arg, uint8_t *data, size_t len) { |
| 47 | + if (type != WS_EVT_DATA) { |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + for (size_t i = 0; i < len; i++) { |
| 52 | + live.write(data[i]); |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +void setup() { |
| 57 | + Serial.begin(9600); |
| 58 | + |
| 59 | + WiFi.begin(ssid, password); |
| 60 | + while (WiFi.status() != WL_CONNECTED) { |
| 61 | + delay(1000); |
| 62 | + Serial.println("Connecting to WiFi..."); |
| 63 | + } |
| 64 | + Serial.println("Connected to WiFi"); |
| 65 | + Serial.println(WiFi.localIP()); |
| 66 | + |
| 67 | + ws.onEvent(onWebSocketEvent); |
| 68 | + |
| 69 | + server.addHandler(&ws); |
| 70 | + server.begin(); |
| 71 | + |
| 72 | + // Attach the servo to pin 12 |
| 73 | + myServo.attach(12); |
| 74 | + |
| 75 | + // Add the Blender servo object to the animation |
| 76 | + animation.addServo(myBlenderServo); |
| 77 | + |
| 78 | + // Trigger the animation live mode |
| 79 | + animation.live(live); |
| 80 | +} |
| 81 | + |
| 82 | +void loop() { |
| 83 | + // Update the animation state on each loop |
| 84 | + animation.run(); |
| 85 | +} |
0 commit comments