Rover
demo-kit/ é o que simula o dispositivo para a demo na página inicial. O próprio dispositivo não precisa dele.
demo-kit/ é o que simula o dispositivo para a demo na página inicial. O próprio dispositivo não precisa dele.
An ESP32, a dual H-bridge and two DC motors make a small tank-steered rover. It serves its own control pad, a device UI: the Connectivitizer app shows the pad full screen, and the stick on the phone drives the two tracks. Neither the rover nor the phone needs an open port or a public address.
Status: the sketch has run on a bare ESP32-S3 and through Connectivitizer, and has not turned a motor yet. What has been checked and how is under What has been checked. Treat the wiring and the constants as a starting point.
| Folder | What it is |
|---|---|
ui/rover.html | The control pad: one self-contained page. Telemetry on top, one joystick, ARM and STOP. |
ui/manifest.json | What the rover serves as /.cz/ui.json. |
ui/demo-data.js | A pretend rover, for showing the page without hardware (below). |
esp32/rover/ | The Arduino sketch. page.h is the page and the manifest, written by make-page.mjs. |
A rover that keeps driving when the connection drops is the thing to avoid. The sketch has three rules, and they are in the firmware, not in the page:
DEADMAN_MS), the motors stop. The page sends about 20 commands a second while the stick is held, so a lost phone, a closed app or a dropped session stops the rover within 300 ms of the last command that got through. The check runs on a timer of its own every 10 ms, not in loop(), so a slow client or a large answer cannot delay it. The time is counted from when a command arrives at the rover, not from when the phone sent it.POST /arm changes it, and arming always starts from standstill. Disarming also pulls the driver's standby pin low. STOP on the page sends a zero command and disarms.RAMP_PER_S); stopping and the stop before a reversal are immediate.For the first test, put the rover on a box with the wheels in the air. Check that both tracks run forward when the stick is pushed forward (LEFT_INVERTED, RIGHT_INVERTED turn one around), then pull the stick and close the app to see the dead-man stop.
The page has no say in any of this. It zeroes the stick and shows LINK LOST when it hears nothing for two seconds, but the rover has stopped by then.
| Part | Notes |
|---|---|
| ESP32 development board | A plain ESP32 DevKit (WROOM). On WROVER modules GPIO16 and GPIO17 are taken; move the distance sensor to other pins. |
| Dual H-bridge motor driver | A TB6612FNG board fits the pins below as they are. An L298N or DRV8833 board works with the same signals (PWM plus two direction pins per motor; for an L298N set PIN_STANDBY = -1 and PWM_HZ to 1000). |
| Two DC gear motors on a tracked or two-wheeled chassis | |
| Battery, 2S Li-ion (7.4 V) with protection | Other voltages: change the divider and BATTERY_EMPTY_V, BATTERY_FULL_V. |
| 5 V step-down converter | Feeds the ESP32 from the battery. |
| Resistors 100 kΩ and 33 kΩ | Battery voltage divider. |
| HC-SR04 ultrasonic sensor, resistors 1 kΩ and 2 kΩ (optional) | Without it set USE_SONAR to 0; distanceCm is then null and the radar on the page stays empty. |
ESP32 TB6612FNG
GPIO25 ─────── PWMA A01/A02 ── left motor
GPIO26 ─────── AIN1
GPIO27 ─────── AIN2
GPIO32 ─────── PWMB B01/B02 ── right motor
GPIO33 ─────── BIN1
GPIO18 ─────── BIN2
GPIO19 ─────── STBY (high while armed)
3V3 ─────── VCC
GND ─────── GND ───── battery −
VM ───── battery +
battery + ──[100 kΩ]──┬──[33 kΩ]── GND
└── GPIO34 8.4 V becomes 2.08 V
HC-SR04 (optional, 5 V): TRIG ── GPIO16
ECHO ──[1 kΩ]──┬──[2 kΩ]── GND
└── GPIO17Battery, driver and ESP32 share ground. The ESP32's pins take 3.3 V at most: both dividers are needed, and the battery pin has to be one of ADC1 (GPIO32 to 39), because ADC2 does not work while Wi-Fi is on. All pins are constants at the top of rover.ino.
The sketch needs the Connectivitizer library (../../connectivitizer-esp32) and nothing else.
cd src/examples/rover/esp32/rover
copy secrets.example.h secrets.h # Wi-Fi (2.4 GHz), device key, shared keyrover.ino, choose your ESP32 board, upload.arduino-cli lib install Connectivitizer, then arduino-cli compile --fqbn esp32:esp32:esp32 --upload -p COM3 .The keys come from your workspace in the web app: Credentials → Create credential → Device key and Credentials → Generate a shared key. The shared key also goes into the app. secrets.h is not committed.
After changing ui/rover.html or ui/manifest.json, write page.h again:
node make-page.mjsThe serial monitor (115200) prints the rover's address in the Wi-Fi and Reachable through Connectivitizer. once it is online. The device appears in the workspace as rover.
Open the Connectivitizer app (Android), pick rover, and the pad appears. Press ARM, then drag anywhere in the pad: the stick follows the finger from where it came down, and springs back when you let go. Forward and back are both tracks; left and right turn one against the other.
While you write the page, a browser in the same Wi-Fi works too: http://<address of the rover>/. A browser has no bridge to the app, so the page sends its commands with POST /drive there instead of the WebSocket, and shows LOCAL.
One app at a time: a new session replaces the old one (CZ_MAX_SESSIONS in the library). The sketch serves two WebSockets at most.
One port, the service api:
| Request | |
|---|---|
WebSocket /drive | In: { "l": -1..1, "r": -1..1 }, the power of the left and the right track, about 20 a second while the stick is held. Out: the telemetry object, 5 a second. Anything that is not such a command is ignored. |
POST /drive | The same command for a client without a WebSocket. Answers with the telemetry object; 400 when the body is not a command. The dead-man rule is the same. |
GET /telemetry | { "battery": 7.82, "batteryPercent": 71, "speed": 0.4, "heading": 212.5, "distanceCm": 64, "rssi": -58, "armed": true } |
POST /arm | { "armed": true } or { "armed": false }. Answers { "armed": … }; 400 for anything else. |
GET /.cz/ui.json, GET /.cz/ui.html | The manifest and the page. GET / is the page as well. |
What the telemetry means on this hardware:
battery is volts at the divider, smoothed; batteryPercent is that voltage between BATTERY_EMPTY_V and BATTERY_FULL_V, so it dips under load.speed is the power the motors get right now, 0 to 1, the mean of both tracks. It is not a measured speed: there are no wheel encoders.heading is an estimate from the motor powers (SPIN_DEG_PER_S), starting at 0 when the rover boots. It drifts. A compass or gyro would have to replace it for a heading you can rely on.distanceCm is the HC-SR04's reading, or null without a fresh echo within its 4 m.The WebSocket is served by the sketch itself: a small RFC 6455 server (the handshake with SHA-1 and base64 from the core's mbedtls, masked text frames in, unmasked text frames out, ping, close) on a plain WiFiServer, next to the HTTP requests on the same port. No WebSocket library is needed. It reads one small text frame per message; fragmented or binary messages close the connection.
Nothing here has been measured on a rover, so there are no numbers.
What decides it is how the session is routed, and the page shows which one it is (P2P or RELAY):
Either way a late command is only late, never dangerous by itself: the dead-man stop counts from the arrival of the last command, so a stalled connection stops the rover. The sketch switches Wi-Fi power saving off (WiFi.setSleep(false)), which otherwise delays packets.
The landing page of the web app shows this page without a rover (demo kit). ui/demo-data.js is the pretend rover: the same routes, the same WebSocket and the same JSON as the sketch, with the 300 ms dead-man rule, from a small model of a rover in an empty room (tracks with inertia, a heading, walls for the distance, a battery that sags under load). Two things differ on purpose: the pretend rover starts armed, and its values are invented.
In the demo only, the page has an attract mode: after four seconds without a touch the stick draws a slow figure-eight by itself, with a DEMO tag, and stops at the first touch. That code runs only when window.czDemo exists, which a page served by a real rover never has. A real rover does not drive itself.
cd src/connectivitizer
node scripts/build-demos.mjs --only rover --out <folder> # then open <folder>/rover/ui.html| How | |
|---|---|
| The sketch compiles | PlatformIO espressif32 6.12.0 (Arduino core 2.0.17), board esp32dev, with the library from this repository; no warnings with -Wall -Wextra. About 1.01 MB of flash, of which the page is 46 KB. |
| Arduino core 3.x | Not compiled. The sketch has the 3.x LEDC calls (ledcAttach) and the mbedtls 3 call behind #if; the library itself has not been compiled for core 3.x either. |
| The server and the motor logic | The sketch built for a PC against a stand-in for the Arduino core (sockets, a timer thread, mbedtls 3), then tested over real connections: the WebSocket handshake with Node's client, frames split and joined on the way, ping and close, refused frames, every route, arming, and the dead-man stop (motors at zero about 300 ms after the last command, over the WebSocket and over POST /drive). The page from that server in a browser drove it with POST /drive. This checks the code's logic, not an ESP32. |
| The page | In a browser with the pretend rover: stick, arming, stop, link loss, reduced motion, 360 × 600 up to 820 px wide. Not yet in the app with a real device. |
| On a board | Flashed to an ESP32-S3 DevKit board (N16R8) with nothing connected, and reached through a development platform with the TypeScript SDK, peer to peer: GET /telemetry, POST /arm, POST /drive and the WebSocket /drive (a command in, telemetry out) all answered with the keys of the pretend rover. On the S3 the pins had to change: GPIO 22 to 25 do not exist there, and on modules with octal PSRAM GPIO 26 to 37 belong to the flash and the PSRAM, so a sketch that drives one of them stops the chip. Here the motor pins moved to 15, 4, 5, 6, 7 and the battery to GPIO 1. Motors, PWM, the battery reading and the distance sensor are untested. It also compiles for the ESP32-S3 and the ESP32-C3. |