#include #include #include "motor.h" #include "config.h" WebServer server(HTTP_PORT); // HTML page for motor control const char index_html[] PROGMEM = R"rawliteral( Motor Control

Motor Control

⚠️ STALL DETECTED!
CURRENT R
0.00A
CURRENT L
0.00A
Direction: STOPPED
20%

🏓 Pingpong Mode

INACTIVE
50%
2.0s
0%
0%
)rawliteral"; void handleRoot() { server.send(200, "text/html", index_html); } void handleSpeed() { if (server.hasArg("value")) { int speed = server.arg("value").toInt(); motor.setSpeed(speed); server.send(200, "text/plain", "OK"); } else { server.send(400, "text/plain", "Missing value"); } } void handleDirection() { if (server.hasArg("value")) { int dir = server.arg("value").toInt(); motor.setDirection(dir); server.send(200, "text/plain", "OK"); } else { server.send(400, "text/plain", "Missing value"); } } void handleStop() { motor.stop(); server.send(200, "text/plain", "OK"); } void handleStatus() { String json = "{\"speed\":" + String(motor.getSpeed()) + ",\"direction\":" + String(motor.getDirection()) + ",\"currentR\":" + String(motor.getCurrentRight(), 2) + ",\"currentL\":" + String(motor.getCurrentLeft(), 2) + ",\"stalled\":" + (motor.isStalled() ? "true" : "false") + ",\"pingpong\":" + (motor.isPingpongActive() ? "true" : "false") + ",\"ppSpeed\":" + String(motor.getPingpongSpeed()) + ",\"ppTime\":" + String(motor.getPingpongTime()) + ",\"ppSpeedRand\":" + String(motor.getPingpongSpeedRandom()) + ",\"ppTimeRand\":" + String(motor.getPingpongTimeRandom()) + "}"; server.send(200, "application/json", json); } void handlePingpongStart() { int speed = 50; int time = 2000; int speedRand = 0; int timeRand = 0; if (server.hasArg("speed")) { speed = server.arg("speed").toInt(); } if (server.hasArg("time")) { time = server.arg("time").toInt(); } if (server.hasArg("speedRand")) { speedRand = server.arg("speedRand").toInt(); } if (server.hasArg("timeRand")) { timeRand = server.arg("timeRand").toInt(); } motor.startPingpong(speed, time, speedRand, timeRand); server.send(200, "text/plain", "OK"); } void handlePingpongStop() { motor.stopPingpong(); server.send(200, "text/plain", "OK"); } void setupWebServer() { server.on("/", handleRoot); server.on("/speed", handleSpeed); server.on("/direction", handleDirection); server.on("/stop", handleStop); server.on("/status", handleStatus); server.on("/pingpong/start", handlePingpongStart); server.on("/pingpong/stop", handlePingpongStop); server.begin(); Serial.println("Web server started on port 80"); } void handleWebServer() { server.handleClient(); }